api.c 2.4 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795687966879768798687996880068801688026880368804688056880668807688086880968810688116881268813688146881568816688176881868819688206882168822688236882468825688266882768828688296883068831688326883368834688356883668837688386883968840688416884268843688446884568846688476884868849688506885168852688536885468855688566885768858688596886068861688626886368864688656886668867688686886968870688716887268873688746887568876688776887868879688806888168882688836888468885688866888768888688896889068891688926889368894688956889668897688986889968900689016890268903689046890568906689076890868909689106891168912689136891468915689166891768918689196892068921689226892368924689256892668927689286892968930689316893268933689346893568936689376893868939689406894168942689436894468945689466894768948689496895068951689526895368954689556895668957689586895968960689616896268963689646896568966689676896868969689706897168972689736897468975689766897768978689796898068981689826898368984689856898668987689886898968990689916899268993689946899568996689976899868999690006900169002690036900469005690066900769008690096901069011690126901369014690156901669017690186901969020690216902269023690246902569026690276902869029690306903169032690336903469035690366903769038690396904069041690426904369044690456904669047690486904969050690516905269053690546905569056690576905869059690606906169062690636906469065690666906769068690696907069071690726907369074690756907669077690786907969080690816908269083690846908569086690876908869089690906909169092690936909469095690966909769098690996910069101691026910369104691056910669107691086910969110691116911269113691146911569116691176911869119691206912169122691236912469125691266912769128691296913069131691326913369134691356913669137691386913969140691416914269143691446914569146691476914869149691506915169152691536915469155691566915769158691596916069161691626916369164691656916669167691686916969170691716917269173691746917569176691776917869179691806918169182691836918469185691866918769188691896919069191691926919369194691956919669197691986919969200692016920269203692046920569206692076920869209692106921169212692136921469215692166921769218692196922069221692226922369224692256922669227692286922969230692316923269233692346923569236692376923869239692406924169242692436924469245692466924769248692496925069251692526925369254692556925669257692586925969260692616926269263692646926569266692676926869269692706927169272692736927469275692766927769278692796928069281692826928369284692856928669287692886928969290692916929269293692946929569296692976929869299693006930169302693036930469305693066930769308693096931069311693126931369314693156931669317693186931969320693216932269323693246932569326693276932869329693306933169332693336933469335693366933769338693396934069341693426934369344693456934669347693486934969350693516935269353693546935569356693576935869359693606936169362693636936469365693666936769368693696937069371693726937369374693756937669377693786937969380693816938269383693846938569386693876938869389693906939169392693936939469395693966939769398693996940069401694026940369404694056940669407694086940969410694116941269413694146941569416694176941869419694206942169422694236942469425694266942769428694296943069431694326943369434694356943669437694386943969440694416944269443694446944569446694476944869449694506945169452694536945469455694566945769458694596946069461694626946369464694656946669467694686946969470694716947269473694746947569476694776947869479694806948169482694836948469485694866948769488694896949069491694926949369494694956949669497694986949969500695016950269503695046950569506695076950869509695106951169512695136951469515695166951769518695196952069521695226952369524695256952669527695286952969530695316953269533695346953569536695376953869539695406954169542695436954469545695466954769548695496955069551695526955369554695556955669557695586955969560695616956269563695646956569566695676956869569695706957169572695736957469575695766957769578695796958069581695826958369584695856958669587695886958969590695916959269593695946959569596695976959869599696006960169602696036960469605696066960769608696096961069611696126961369614696156961669617696186961969620696216962269623696246962569626696276962869629696306963169632696336963469635696366963769638696396964069641696426964369644696456964669647696486964969650696516965269653696546965569656696576965869659696606966169662696636966469665696666966769668696696967069671696726967369674696756967669677696786967969680696816968269683696846968569686696876968869689696906969169692696936969469695696966969769698696996970069701697026970369704697056970669707697086970969710697116971269713697146971569716697176971869719697206972169722697236972469725697266972769728697296973069731697326973369734697356973669737697386973969740697416974269743697446974569746697476974869749697506975169752697536975469755697566975769758697596976069761697626976369764697656976669767697686976969770697716977269773697746977569776697776977869779697806978169782697836978469785697866978769788697896979069791697926979369794697956979669797697986979969800698016980269803698046980569806698076980869809698106981169812698136981469815698166981769818698196982069821698226982369824698256982669827698286982969830698316983269833698346983569836698376983869839698406984169842698436984469845698466984769848698496985069851698526985369854698556985669857698586985969860698616986269863698646986569866698676986869869698706987169872698736987469875698766987769878698796988069881698826988369884698856988669887698886988969890698916989269893698946989569896698976989869899699006990169902699036990469905699066990769908699096991069911699126991369914699156991669917699186991969920699216992269923699246992569926699276992869929699306993169932699336993469935699366993769938699396994069941699426994369944699456994669947699486994969950699516995269953699546995569956699576995869959699606996169962699636996469965699666996769968699696997069971699726997369974699756997669977699786997969980699816998269983699846998569986699876998869989699906999169992699936999469995699966999769998699997000070001700027000370004700057000670007700087000970010700117001270013700147001570016700177001870019700207002170022700237002470025700267002770028700297003070031700327003370034700357003670037700387003970040700417004270043700447004570046700477004870049700507005170052700537005470055700567005770058700597006070061700627006370064700657006670067700687006970070700717007270073700747007570076700777007870079700807008170082700837008470085700867008770088700897009070091700927009370094700957009670097700987009970100701017010270103701047010570106701077010870109701107011170112701137011470115701167011770118701197012070121701227012370124701257012670127701287012970130701317013270133701347013570136701377013870139701407014170142701437014470145701467014770148701497015070151701527015370154701557015670157701587015970160701617016270163701647016570166701677016870169701707017170172701737017470175701767017770178701797018070181701827018370184701857018670187701887018970190701917019270193701947019570196701977019870199702007020170202702037020470205702067020770208702097021070211702127021370214702157021670217702187021970220702217022270223702247022570226702277022870229702307023170232702337023470235702367023770238702397024070241702427024370244702457024670247702487024970250702517025270253702547025570256702577025870259702607026170262702637026470265702667026770268702697027070271702727027370274702757027670277702787027970280702817028270283702847028570286702877028870289702907029170292702937029470295702967029770298702997030070301703027030370304703057030670307703087030970310703117031270313703147031570316703177031870319703207032170322703237032470325703267032770328703297033070331703327033370334703357033670337703387033970340703417034270343703447034570346703477034870349703507035170352703537035470355703567035770358703597036070361703627036370364703657036670367703687036970370703717037270373703747037570376703777037870379703807038170382703837038470385703867038770388703897039070391703927039370394703957039670397703987039970400704017040270403704047040570406704077040870409704107041170412704137041470415704167041770418704197042070421704227042370424704257042670427704287042970430
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #ifndef USER_CRYPTO_ERROR
  215. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  216. #endif
  217. #endif
  218. #ifndef NO_SIG_WRAPPER
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #endif
  221. #ifdef HAVE_AESCCM
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #endif
  224. #ifdef HAVE_PKCS7
  225. #include <wolfssl/wolfcrypt/pkcs7.h>
  226. #include <wolfssl/wolfcrypt/asn.h>
  227. #ifdef HAVE_LIBZ
  228. #include <wolfssl/wolfcrypt/compress.h>
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  232. #include <wolfssl/wolfcrypt/asn.h>
  233. #endif
  234. #ifndef NO_DSA
  235. #include <wolfssl/wolfcrypt/dsa.h>
  236. #ifndef ONEK_BUF
  237. #define ONEK_BUF 1024
  238. #endif
  239. #ifndef TWOK_BUF
  240. #define TWOK_BUF 2048
  241. #endif
  242. #ifndef FOURK_BUF
  243. #define FOURK_BUF 4096
  244. #endif
  245. #ifndef DSA_SIG_SIZE
  246. #define DSA_SIG_SIZE 40
  247. #endif
  248. #ifndef MAX_DSA_PARAM_SIZE
  249. #define MAX_DSA_PARAM_SIZE 256
  250. #endif
  251. #endif
  252. #ifdef WOLFSSL_CMAC
  253. #include <wolfssl/wolfcrypt/cmac.h>
  254. #endif
  255. #ifdef HAVE_ED25519
  256. #include <wolfssl/wolfcrypt/ed25519.h>
  257. #endif
  258. #ifdef HAVE_CURVE25519
  259. #include <wolfssl/wolfcrypt/curve25519.h>
  260. #endif
  261. #ifdef HAVE_ED448
  262. #include <wolfssl/wolfcrypt/ed448.h>
  263. #endif
  264. #ifdef HAVE_CURVE448
  265. #include <wolfssl/wolfcrypt/curve448.h>
  266. #endif
  267. #ifdef HAVE_PKCS12
  268. #include <wolfssl/wolfcrypt/pkcs12.h>
  269. #endif
  270. #include <wolfssl/wolfcrypt/logging.h>
  271. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  272. #include <wolfssl/openssl/ssl.h>
  273. #ifndef NO_ASN
  274. /* for ASN_COMMON_NAME DN_tags enum */
  275. #include <wolfssl/wolfcrypt/asn.h>
  276. #endif
  277. #ifdef HAVE_OCSP
  278. #include <wolfssl/openssl/ocsp.h>
  279. #endif
  280. #endif
  281. #ifdef OPENSSL_EXTRA
  282. #include <wolfssl/openssl/cmac.h>
  283. #include <wolfssl/openssl/x509v3.h>
  284. #include <wolfssl/openssl/asn1.h>
  285. #include <wolfssl/openssl/crypto.h>
  286. #include <wolfssl/openssl/pkcs12.h>
  287. #include <wolfssl/openssl/evp.h>
  288. #include <wolfssl/openssl/dh.h>
  289. #include <wolfssl/openssl/bn.h>
  290. #include <wolfssl/openssl/buffer.h>
  291. #include <wolfssl/openssl/pem.h>
  292. #include <wolfssl/openssl/ec.h>
  293. #include <wolfssl/openssl/ecdh.h>
  294. #include <wolfssl/openssl/engine.h>
  295. #include <wolfssl/openssl/hmac.h>
  296. #include <wolfssl/openssl/objects.h>
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/modes.h>
  299. #include <wolfssl/openssl/fips_rand.h>
  300. #include <wolfssl/openssl/kdf.h>
  301. #ifdef OPENSSL_ALL
  302. #include <wolfssl/openssl/txt_db.h>
  303. #include <wolfssl/openssl/lhash.h>
  304. #endif
  305. #ifndef NO_AES
  306. #include <wolfssl/openssl/aes.h>
  307. #endif
  308. #ifndef NO_DES3
  309. #include <wolfssl/openssl/des.h>
  310. #endif
  311. #ifndef NO_RC4
  312. #include <wolfssl/openssl/rc4.h>
  313. #endif
  314. #ifdef HAVE_ECC
  315. #include <wolfssl/openssl/ecdsa.h>
  316. #endif
  317. #ifdef HAVE_PKCS7
  318. #include <wolfssl/openssl/pkcs7.h>
  319. #endif
  320. #ifdef HAVE_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_ED448
  324. #include <wolfssl/openssl/ed448.h>
  325. #endif
  326. #endif /* OPENSSL_EXTRA */
  327. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  328. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  329. #include <wolfssl/wolfcrypt/srp.h>
  330. #endif
  331. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  332. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  333. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  334. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  335. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  336. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  337. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  338. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  339. * number tracking */
  340. #include "wolfssl/internal.h"
  341. #endif
  342. /* force enable test buffers */
  343. #ifndef USE_CERT_BUFFERS_2048
  344. #define USE_CERT_BUFFERS_2048
  345. #endif
  346. #ifndef USE_CERT_BUFFERS_256
  347. #define USE_CERT_BUFFERS_256
  348. #endif
  349. #include <wolfssl/certs_test.h>
  350. #include "tests/utils.h"
  351. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  352. * have default (hidden) visibility, and in the absence of visibility, it's
  353. * benign to mask out the library implementation.
  354. */
  355. #define WOLFSSL_MISC_INCLUDED
  356. #include <wolfcrypt/src/misc.c>
  357. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  358. /* FIPS build has replaced ecc.h. */
  359. #define wc_ecc_key_get_priv(key) (&((key)->k))
  360. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. #endif
  362. typedef struct testVector {
  363. const char* input;
  364. const char* output;
  365. size_t inLen;
  366. size_t outLen;
  367. } testVector;
  368. #if defined(HAVE_PKCS7)
  369. typedef struct {
  370. const byte* content;
  371. word32 contentSz;
  372. int contentOID;
  373. int encryptOID;
  374. int keyWrapOID;
  375. int keyAgreeOID;
  376. byte* cert;
  377. size_t certSz;
  378. byte* privateKey;
  379. word32 privateKeySz;
  380. } pkcs7EnvelopedVector;
  381. #ifndef NO_PKCS7_ENCRYPTED_DATA
  382. typedef struct {
  383. const byte* content;
  384. word32 contentSz;
  385. int contentOID;
  386. int encryptOID;
  387. byte* encryptionKey;
  388. word32 encryptionKeySz;
  389. } pkcs7EncryptedVector;
  390. #endif
  391. #endif /* HAVE_PKCS7 */
  392. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  393. typedef int (*ssl_cb)(WOLFSSL* ssl);
  394. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  395. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  396. typedef struct test_ssl_cbf {
  397. method_provider method;
  398. ctx_cb ctx_ready;
  399. ssl_cb ssl_ready;
  400. ssl_cb on_result;
  401. ssl_cb on_cleanup;
  402. hs_cb on_handshake;
  403. WOLFSSL_CTX* ctx;
  404. const char* caPemFile;
  405. const char* certPemFile;
  406. const char* keyPemFile;
  407. const char* crlPemFile;
  408. #ifdef WOLFSSL_STATIC_MEMORY
  409. byte* mem;
  410. word32 memSz;
  411. wolfSSL_method_func method_ex;
  412. #endif
  413. int devId;
  414. int return_code;
  415. int last_err;
  416. unsigned char isSharedCtx:1;
  417. unsigned char loadToSSL:1;
  418. unsigned char ticNoInit:1;
  419. unsigned char doUdp:1;
  420. } test_ssl_cbf;
  421. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  422. typedef struct test_ssl_memio_ctx {
  423. WOLFSSL_CTX* s_ctx;
  424. WOLFSSL_CTX* c_ctx;
  425. WOLFSSL* s_ssl;
  426. WOLFSSL* c_ssl;
  427. const char* c_ciphers;
  428. const char* s_ciphers;
  429. char* c_msg;
  430. int c_msglen;
  431. char* s_msg;
  432. int s_msglen;
  433. test_ssl_cbf s_cb;
  434. test_ssl_cbf c_cb;
  435. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  436. int c_len;
  437. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  438. int s_len;
  439. } test_ssl_memio_ctx;
  440. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  441. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  442. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  443. const char* currentTestName;
  444. char tmpDirName[16];
  445. int tmpDirNameSet = 0;
  446. #endif
  447. /*----------------------------------------------------------------------------*
  448. | Constants
  449. *----------------------------------------------------------------------------*/
  450. /* Test result constants and macros. */
  451. /* Test succeeded. */
  452. #define TEST_SUCCESS (1)
  453. /* Test failed. */
  454. #define TEST_FAIL (0)
  455. /* Test skipped - not run. */
  456. #define TEST_SKIPPED (-7777)
  457. /* Returns the result based on whether check is true.
  458. *
  459. * @param [in] check Condition for success.
  460. * @return When condition is true: TEST_SUCCESS.
  461. * @return When condition is false: TEST_FAIL.
  462. */
  463. #ifdef DEBUG_WOLFSSL_VERBOSE
  464. #define XSTRINGIFY(s) STRINGIFY(s)
  465. #define STRINGIFY(s) #s
  466. #define TEST_RES_CHECK(check) ({ \
  467. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  468. if (_ret == TEST_FAIL) { \
  469. fprintf(stderr, " check \"%s\" at %d ", \
  470. XSTRINGIFY(check), __LINE__); \
  471. } \
  472. _ret; })
  473. #else
  474. #define TEST_RES_CHECK(check) \
  475. ((check) ? TEST_SUCCESS : TEST_FAIL)
  476. #endif /* DEBUG_WOLFSSL_VERBOSE */
  477. #define TEST_STRING "Everyone gets Friday off."
  478. #define TEST_STRING_SZ 25
  479. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  480. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  481. #define TEST_RSA_BITS 1024
  482. #else
  483. #define TEST_RSA_BITS 2048
  484. #endif
  485. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  486. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  487. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  488. static const char* bogusFile =
  489. #ifdef _WIN32
  490. "NUL"
  491. #else
  492. "/dev/null"
  493. #endif
  494. ;
  495. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  496. enum {
  497. TESTING_RSA = 1,
  498. TESTING_ECC = 2
  499. };
  500. #ifdef WOLFSSL_QNX_CAAM
  501. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  502. static int testDevId = WOLFSSL_CAAM_DEVID;
  503. #else
  504. static int testDevId = INVALID_DEVID;
  505. #endif
  506. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  507. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  508. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  509. #define HAVE_IO_TESTS_DEPENDENCIES
  510. #endif
  511. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  512. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  513. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  514. #endif
  515. /*----------------------------------------------------------------------------*
  516. | BIO with fixed read/write size
  517. *----------------------------------------------------------------------------*/
  518. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  519. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  520. int len)
  521. {
  522. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  523. len = 0;
  524. }
  525. else {
  526. if (bio->wrSz - bio->wrIdx < len) {
  527. len = bio->wrSz - bio->wrIdx;
  528. }
  529. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  530. bio->wrIdx += len;
  531. }
  532. return len;
  533. }
  534. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  535. {
  536. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  537. len = 0;
  538. }
  539. else {
  540. if (bio->wrSz - bio->rdIdx < len) {
  541. len = bio->wrSz - bio->rdIdx;
  542. }
  543. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  544. bio->rdIdx += len;
  545. }
  546. return len;
  547. }
  548. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  549. {
  550. static WOLFSSL_BIO_METHOD meth;
  551. meth.type = WOLFSSL_BIO_BIO;
  552. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  553. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  554. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  555. return &meth;
  556. }
  557. #endif
  558. /*----------------------------------------------------------------------------*
  559. | Setup
  560. *----------------------------------------------------------------------------*/
  561. static int test_wolfSSL_Init(void)
  562. {
  563. EXPECT_DECLS;
  564. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  565. return EXPECT_RESULT();
  566. }
  567. static int test_wolfSSL_Cleanup(void)
  568. {
  569. EXPECT_DECLS;
  570. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  571. return EXPECT_RESULT();
  572. }
  573. /* Initialize the wolfCrypt state.
  574. * POST: 0 success.
  575. */
  576. static int test_wolfCrypt_Init(void)
  577. {
  578. EXPECT_DECLS;
  579. ExpectIntEQ(wolfCrypt_Init(), 0);
  580. return EXPECT_RESULT();
  581. } /* END test_wolfCrypt_Init */
  582. static int test_wolfCrypt_Cleanup(void)
  583. {
  584. EXPECT_DECLS;
  585. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  586. return EXPECT_RESULT();
  587. }
  588. /*----------------------------------------------------------------------------*
  589. | Platform dependent function test
  590. *----------------------------------------------------------------------------*/
  591. static int test_fileAccess(void)
  592. {
  593. EXPECT_DECLS;
  594. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  595. const char *fname[] = {
  596. svrCertFile, svrKeyFile, caCertFile,
  597. eccCertFile, eccKeyFile, eccRsaCertFile,
  598. cliCertFile, cliCertDerFile, cliKeyFile,
  599. dhParamFile,
  600. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  601. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  602. NULL
  603. };
  604. const char derfile[] = "./certs/server-cert.der";
  605. XFILE f = XBADFILE;
  606. size_t sz;
  607. byte *buff = NULL;
  608. int i;
  609. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  610. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  611. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  612. XFCLOSE(f);
  613. }
  614. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  615. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  616. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  617. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  618. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  619. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  620. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  621. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  622. XFCLOSE(f);
  623. #endif
  624. return EXPECT_RESULT();
  625. }
  626. /*----------------------------------------------------------------------------*
  627. | Method Allocators
  628. *----------------------------------------------------------------------------*/
  629. static int test_wolfSSL_Method_Allocators(void)
  630. {
  631. EXPECT_DECLS;
  632. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  633. do { \
  634. WOLFSSL_METHOD *method = NULL; \
  635. condition(method = allocator()); \
  636. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  637. } while (0)
  638. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  639. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  640. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  641. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  642. #ifndef NO_OLD_TLS
  643. #ifdef WOLFSSL_ALLOW_SSLV3
  644. #ifndef NO_WOLFSSL_SERVER
  645. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  646. #endif
  647. #ifndef NO_WOLFSSL_CLIENT
  648. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  649. #endif
  650. #endif
  651. #ifdef WOLFSSL_ALLOW_TLSV10
  652. #ifndef NO_WOLFSSL_SERVER
  653. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  654. #endif
  655. #ifndef NO_WOLFSSL_CLIENT
  656. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  657. #endif
  658. #endif
  659. #ifndef NO_WOLFSSL_SERVER
  660. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  661. #endif
  662. #ifndef NO_WOLFSSL_CLIENT
  663. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  664. #endif
  665. #endif /* !NO_OLD_TLS */
  666. #ifndef WOLFSSL_NO_TLS12
  667. #ifndef NO_WOLFSSL_SERVER
  668. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  669. #endif
  670. #ifndef NO_WOLFSSL_CLIENT
  671. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  672. #endif
  673. #endif /* !WOLFSSL_NO_TLS12 */
  674. #ifdef WOLFSSL_TLS13
  675. #ifndef NO_WOLFSSL_SERVER
  676. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  677. #endif
  678. #ifndef NO_WOLFSSL_CLIENT
  679. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  680. #endif
  681. #endif /* WOLFSSL_TLS13 */
  682. #ifndef NO_WOLFSSL_SERVER
  683. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  684. #endif
  685. #ifndef NO_WOLFSSL_CLIENT
  686. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  687. #endif
  688. #ifdef WOLFSSL_DTLS
  689. #ifndef NO_OLD_TLS
  690. #ifndef NO_WOLFSSL_SERVER
  691. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  692. #endif
  693. #ifndef NO_WOLFSSL_CLIENT
  694. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  695. #endif
  696. #endif
  697. #ifndef WOLFSSL_NO_TLS12
  698. #ifndef NO_WOLFSSL_SERVER
  699. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  700. #endif
  701. #ifndef NO_WOLFSSL_CLIENT
  702. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  703. #endif
  704. #endif
  705. #endif /* WOLFSSL_DTLS */
  706. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  707. /* Stubs */
  708. #ifndef NO_WOLFSSL_SERVER
  709. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  710. #endif
  711. #ifndef NO_WOLFSSL_CLIENT
  712. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  713. #endif
  714. #endif
  715. /* Test Either Method (client or server) */
  716. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  717. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  718. #ifndef NO_OLD_TLS
  719. #ifdef WOLFSSL_ALLOW_TLSV10
  720. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  721. #endif
  722. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  723. #endif /* !NO_OLD_TLS */
  724. #ifndef WOLFSSL_NO_TLS12
  725. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  726. #endif /* !WOLFSSL_NO_TLS12 */
  727. #ifdef WOLFSSL_TLS13
  728. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  729. #endif /* WOLFSSL_TLS13 */
  730. #ifdef WOLFSSL_DTLS
  731. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  732. #ifndef NO_OLD_TLS
  733. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  734. #endif /* !NO_OLD_TLS */
  735. #ifndef WOLFSSL_NO_TLS12
  736. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  737. #endif /* !WOLFSSL_NO_TLS12 */
  738. #endif /* WOLFSSL_DTLS */
  739. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  740. return EXPECT_RESULT();
  741. }
  742. /*----------------------------------------------------------------------------*
  743. | Context
  744. *----------------------------------------------------------------------------*/
  745. #ifndef NO_WOLFSSL_SERVER
  746. static int test_wolfSSL_CTX_new(void)
  747. {
  748. EXPECT_DECLS;
  749. WOLFSSL_CTX *ctx;
  750. WOLFSSL_METHOD* method;
  751. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  752. ExpectNotNull(method = wolfSSLv23_server_method());
  753. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  754. wolfSSL_CTX_free(ctx);
  755. return EXPECT_RESULT();
  756. }
  757. #endif
  758. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  759. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  760. static int test_for_double_Free(void)
  761. {
  762. EXPECT_DECLS;
  763. WOLFSSL_CTX* ctx = NULL;
  764. WOLFSSL* ssl = NULL;
  765. int skipTest = 0;
  766. const char* testCertFile;
  767. const char* testKeyFile;
  768. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  769. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  770. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  771. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  772. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  773. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  774. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  775. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  776. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  777. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  778. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  779. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  780. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  781. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  782. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  783. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  784. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  785. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  786. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  787. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  788. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  789. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  790. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  791. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  792. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  793. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  794. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  795. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  796. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  797. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  798. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  799. #ifdef OPENSSL_EXTRA
  800. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  801. "!SRP:!kRSA:!aNULL:!eNULL";
  802. #endif
  803. #ifndef NO_RSA
  804. testCertFile = svrCertFile;
  805. testKeyFile = svrKeyFile;
  806. #elif defined(HAVE_ECC)
  807. testCertFile = eccCertFile;
  808. testKeyFile = eccKeyFile;
  809. #else
  810. skipTest = 1;
  811. #endif
  812. if (skipTest != 1) {
  813. #ifndef NO_WOLFSSL_SERVER
  814. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  815. #else
  816. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  817. #endif
  818. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  819. WOLFSSL_FILETYPE_PEM));
  820. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  821. WOLFSSL_FILETYPE_PEM));
  822. ExpectNotNull(ssl = wolfSSL_new(ctx));
  823. /* First test freeing SSL, then CTX */
  824. wolfSSL_free(ssl);
  825. ssl = NULL;
  826. wolfSSL_CTX_free(ctx);
  827. ctx = NULL;
  828. #ifndef NO_WOLFSSL_CLIENT
  829. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  830. #else
  831. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  832. #endif
  833. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  834. WOLFSSL_FILETYPE_PEM));
  835. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  836. WOLFSSL_FILETYPE_PEM));
  837. ExpectNotNull(ssl = wolfSSL_new(ctx));
  838. /* Next test freeing CTX then SSL */
  839. wolfSSL_CTX_free(ctx);
  840. ctx = NULL;
  841. wolfSSL_free(ssl);
  842. ssl = NULL;
  843. #ifndef NO_WOLFSSL_SERVER
  844. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  845. #else
  846. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  847. #endif
  848. /* Test setting ciphers at ctx level */
  849. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  850. WOLFSSL_FILETYPE_PEM));
  851. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  852. WOLFSSL_FILETYPE_PEM));
  853. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  854. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  855. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  856. /* only update TLSv13 suites */
  857. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  858. #endif
  859. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  860. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  861. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  862. /* only update pre-TLSv13 suites */
  863. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  864. "ECDHE-RSA-AES128-GCM-SHA256"));
  865. #endif
  866. #ifdef OPENSSL_EXTRA
  867. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  868. #endif
  869. ExpectNotNull(ssl = wolfSSL_new(ctx));
  870. wolfSSL_CTX_free(ctx);
  871. ctx = NULL;
  872. wolfSSL_free(ssl);
  873. ssl = NULL;
  874. #ifndef NO_WOLFSSL_CLIENT
  875. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  876. #else
  877. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  878. #endif
  879. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  880. WOLFSSL_FILETYPE_PEM));
  881. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  882. WOLFSSL_FILETYPE_PEM));
  883. ExpectNotNull(ssl = wolfSSL_new(ctx));
  884. /* test setting ciphers at SSL level */
  885. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  886. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  887. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  888. /* only update TLSv13 suites */
  889. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  890. #endif
  891. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  892. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  893. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  894. /* only update pre-TLSv13 suites */
  895. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  896. #endif
  897. wolfSSL_CTX_free(ctx);
  898. ctx = NULL;
  899. wolfSSL_free(ssl);
  900. ssl = NULL;
  901. }
  902. return EXPECT_RESULT();
  903. }
  904. #endif
  905. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  906. {
  907. EXPECT_DECLS;
  908. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  909. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  910. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  911. const char* testCertFile;
  912. const char* testKeyFile;
  913. WOLFSSL_CTX* ctx = NULL;
  914. WOLFSSL* ssl = NULL;
  915. const byte cipherList[] =
  916. {
  917. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  918. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  919. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  920. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  921. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  922. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  923. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  924. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  925. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  926. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  927. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  928. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  929. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  930. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  931. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  932. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  933. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  934. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  935. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  936. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  937. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  938. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  939. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  940. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  941. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  942. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  943. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  944. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  945. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  946. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  947. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  948. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  949. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  950. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  951. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  952. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  953. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  954. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  955. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  956. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  957. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  958. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  959. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  960. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  961. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  962. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  963. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  964. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  965. /* SHA256 */
  966. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  967. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  968. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  969. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  970. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  971. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  972. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  973. /* SHA384 */
  974. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  975. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  976. /* AES-GCM */
  977. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  978. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  979. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  980. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  981. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  982. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  983. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  984. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  985. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  986. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  987. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  988. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  989. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  990. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  991. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  992. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  993. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  994. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  995. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  996. * also, in some of the other AES-CCM suites
  997. * there will be second byte number conflicts
  998. * with non-ECC AES-GCM */
  999. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1000. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1001. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1002. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1003. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1004. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1005. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1006. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1007. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1008. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1009. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1010. /* Camellia */
  1011. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1012. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1013. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1014. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1015. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1016. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1017. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1018. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1019. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1020. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1021. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1022. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1023. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1024. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1025. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1026. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1027. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1028. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1029. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1030. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1031. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1032. /* TLS v1.3 cipher suites */
  1033. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1034. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1035. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1036. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1037. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1038. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1039. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1040. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1041. };
  1042. #ifndef NO_RSA
  1043. testCertFile = svrCertFile;
  1044. testKeyFile = svrKeyFile;
  1045. #elif defined(HAVE_ECC)
  1046. testCertFile = eccCertFile;
  1047. testKeyFile = eccKeyFile;
  1048. #endif
  1049. #ifndef NO_WOLFSSL_SERVER
  1050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1051. #else
  1052. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1053. #endif
  1054. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1055. sizeof(cipherList)));
  1056. wolfSSL_CTX_free(ctx);
  1057. ctx = NULL;
  1058. #ifndef NO_WOLFSSL_SERVER
  1059. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1060. #else
  1061. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1062. #endif
  1063. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1064. WOLFSSL_FILETYPE_PEM));
  1065. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1066. WOLFSSL_FILETYPE_PEM));
  1067. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1068. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1069. sizeof(cipherList)));
  1070. wolfSSL_free(ssl);
  1071. wolfSSL_CTX_free(ctx);
  1072. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1073. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1074. return EXPECT_RESULT();
  1075. }
  1076. static int test_wolfSSL_CTX_use_certificate_file(void)
  1077. {
  1078. EXPECT_DECLS;
  1079. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1080. WOLFSSL_CTX *ctx = NULL;
  1081. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1082. /* invalid context */
  1083. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1084. WOLFSSL_FILETYPE_PEM));
  1085. /* invalid cert file */
  1086. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1087. WOLFSSL_FILETYPE_PEM));
  1088. /* invalid cert type */
  1089. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1090. #ifdef NO_RSA
  1091. /* rsa needed */
  1092. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1093. WOLFSSL_FILETYPE_PEM));
  1094. #else
  1095. /* success */
  1096. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1097. WOLFSSL_FILETYPE_PEM));
  1098. #endif
  1099. wolfSSL_CTX_free(ctx);
  1100. #endif
  1101. return EXPECT_RESULT();
  1102. }
  1103. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1104. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1105. {
  1106. EXPECT_DECLS;
  1107. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1108. WOLFSSL_CTX* ctx = NULL;
  1109. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1110. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1111. server_cert_der_2048), WOLFSSL_SUCCESS);
  1112. wolfSSL_CTX_free(ctx);
  1113. #endif
  1114. return EXPECT_RESULT();
  1115. }
  1116. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1117. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1118. * context using buffer.
  1119. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1120. * --enable-testcert flag.
  1121. */
  1122. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1123. {
  1124. EXPECT_DECLS;
  1125. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1126. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1127. WOLFSSL_CTX* ctx = NULL;
  1128. int ret;
  1129. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1130. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1131. server_cert_der_2048, sizeof_server_cert_der_2048,
  1132. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1133. wolfSSL_CTX_free(ctx);
  1134. #endif
  1135. return EXPECT_RESULT();
  1136. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1137. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1138. {
  1139. EXPECT_DECLS;
  1140. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1141. WOLFSSL_CTX *ctx = NULL;
  1142. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1143. /* invalid context */
  1144. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1145. WOLFSSL_FILETYPE_PEM));
  1146. /* invalid key file */
  1147. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1148. WOLFSSL_FILETYPE_PEM));
  1149. /* invalid key type */
  1150. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1151. /* success */
  1152. #ifdef NO_RSA
  1153. /* rsa needed */
  1154. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1155. WOLFSSL_FILETYPE_PEM));
  1156. #else
  1157. /* success */
  1158. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1159. WOLFSSL_FILETYPE_PEM));
  1160. #endif
  1161. wolfSSL_CTX_free(ctx);
  1162. #endif
  1163. return EXPECT_RESULT();
  1164. }
  1165. /* test both file and buffer versions along with unloading trusted peer certs */
  1166. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1167. {
  1168. EXPECT_DECLS;
  1169. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1170. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1171. WOLFSSL_CTX *ctx = NULL;
  1172. WOLFSSL* ssl = NULL;
  1173. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1174. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1175. #if !defined(NO_FILESYSTEM)
  1176. /* invalid file */
  1177. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1178. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1179. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1180. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1181. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1182. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1183. /* success */
  1184. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1185. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1186. /* unload cert */
  1187. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1188. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1189. /* invalid file */
  1190. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1191. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1192. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1193. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1194. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1195. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1196. /* success */
  1197. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1198. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1199. #ifdef WOLFSSL_LOCAL_X509_STORE
  1200. /* unload cert */
  1201. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1202. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1203. #endif
  1204. #endif
  1205. /* Test of loading certs from buffers */
  1206. /* invalid buffer */
  1207. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1208. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1209. /* success */
  1210. #ifdef USE_CERT_BUFFERS_1024
  1211. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1212. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1213. #endif
  1214. #ifdef USE_CERT_BUFFERS_2048
  1215. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1216. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1217. #endif
  1218. /* unload cert */
  1219. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1220. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1221. wolfSSL_free(ssl);
  1222. wolfSSL_CTX_free(ctx);
  1223. #endif
  1224. return EXPECT_RESULT();
  1225. }
  1226. static int test_wolfSSL_CTX_load_verify_locations(void)
  1227. {
  1228. EXPECT_DECLS;
  1229. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1230. WOLFSSL_CTX *ctx = NULL;
  1231. #ifndef NO_RSA
  1232. WOLFSSL_CERT_MANAGER* cm = NULL;
  1233. #ifdef PERSIST_CERT_CACHE
  1234. int cacheSz = 0;
  1235. unsigned char* cache = NULL;
  1236. int used = 0;
  1237. #ifndef NO_FILESYSTEM
  1238. const char* cacheFile = "./tests/cert_cache.tmp";
  1239. #endif
  1240. int i;
  1241. int t;
  1242. int* p;
  1243. #endif
  1244. #endif
  1245. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1246. const char* load_certs_path = "./certs/external";
  1247. const char* load_no_certs_path = "./examples";
  1248. const char* load_expired_path = "./certs/test/expired";
  1249. #endif
  1250. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1251. /* invalid arguments */
  1252. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1253. WOLFSSL_FAILURE);
  1254. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1255. WOLFSSL_FAILURE);
  1256. /* invalid ca file */
  1257. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1258. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1259. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1260. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1261. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1262. /* invalid path */
  1263. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1264. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1265. #endif
  1266. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1267. /* test ignoring the invalid path */
  1268. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1269. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1270. #endif
  1271. /* load ca cert */
  1272. #ifdef NO_RSA
  1273. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1274. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1275. #else /* Skip the following test without RSA certs. */
  1276. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1277. WOLFSSL_SUCCESS);
  1278. #ifdef PERSIST_CERT_CACHE
  1279. /* Get cert cache size */
  1280. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1281. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1282. DYNAMIC_TYPE_TMP_BUFFER));
  1283. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1284. BAD_FUNC_ARG);
  1285. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1286. BAD_FUNC_ARG);
  1287. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1288. BAD_FUNC_ARG);
  1289. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1290. BAD_FUNC_ARG);
  1291. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1292. BAD_FUNC_ARG);
  1293. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1294. BAD_FUNC_ARG);
  1295. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1296. BAD_FUNC_ARG);
  1297. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1298. BAD_FUNC_ARG);
  1299. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1300. BAD_FUNC_ARG);
  1301. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1302. BUFFER_E);
  1303. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1304. ExpectIntEQ(cacheSz, used);
  1305. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1306. BAD_FUNC_ARG);
  1307. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1308. BAD_FUNC_ARG);
  1309. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1310. BAD_FUNC_ARG);
  1311. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1312. BAD_FUNC_ARG);
  1313. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1314. BAD_FUNC_ARG);
  1315. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1316. BAD_FUNC_ARG);
  1317. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1318. BAD_FUNC_ARG);
  1319. /* Smaller than header. */
  1320. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1321. for (i = 1; i < cacheSz; i++) {
  1322. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1323. BUFFER_E);
  1324. }
  1325. if (EXPECT_SUCCESS()) {
  1326. /* Modify header for bad results! */
  1327. p = (int*)cache;
  1328. /* version */
  1329. t = p[0]; p[0] = 0xff;
  1330. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1331. CACHE_MATCH_ERROR);
  1332. p[0] = t; p++;
  1333. /* rows */
  1334. t = p[0]; p[0] = 0xff;
  1335. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1336. CACHE_MATCH_ERROR);
  1337. p[0] = t; p++;
  1338. /* columns[0] */
  1339. t = p[0]; p[0] = -1;
  1340. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1341. PARSE_ERROR);
  1342. p[0] = t; p += CA_TABLE_SIZE;
  1343. /* signerSz*/
  1344. t = p[0]; p[0] = 0xff;
  1345. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1346. CACHE_MATCH_ERROR);
  1347. p[0] = t;
  1348. }
  1349. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1350. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1351. #ifndef NO_FILESYSTEM
  1352. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1353. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1354. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1355. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1356. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1357. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1358. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1359. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1360. WOLFSSL_BAD_FILE);
  1361. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1362. /* File contents is not a cache. */
  1363. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1364. CACHE_MATCH_ERROR);
  1365. #endif
  1366. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1367. #endif
  1368. /* Test unloading CA's */
  1369. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1370. #ifdef PERSIST_CERT_CACHE
  1371. /* Verify no certs (result is less than cacheSz) */
  1372. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1373. #endif
  1374. /* load ca cert again */
  1375. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1376. WOLFSSL_SUCCESS);
  1377. /* Test getting CERT_MANAGER */
  1378. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1379. /* Test unloading CA's using CM */
  1380. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1381. #ifdef PERSIST_CERT_CACHE
  1382. /* Verify no certs (result is less than cacheSz) */
  1383. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1384. #endif
  1385. #endif
  1386. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1387. /* Test loading CA certificates using a path */
  1388. #ifdef NO_RSA
  1389. /* failure here okay since certs in external directory are RSA */
  1390. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1391. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1392. #else
  1393. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1394. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1395. #endif
  1396. /* Test loading path with no files */
  1397. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1398. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1399. /* Test loading expired CA certificates */
  1400. #ifdef NO_RSA
  1401. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1402. load_expired_path,
  1403. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1404. WOLFSSL_SUCCESS);
  1405. #else
  1406. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1407. load_expired_path,
  1408. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1409. WOLFSSL_SUCCESS);
  1410. #endif
  1411. /* Test loading CA certificates and ignoring all errors */
  1412. #ifdef NO_RSA
  1413. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1414. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1415. #else
  1416. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1417. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1418. #endif
  1419. #endif
  1420. wolfSSL_CTX_free(ctx);
  1421. #endif
  1422. return EXPECT_RESULT();
  1423. }
  1424. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1425. {
  1426. int res = TEST_SKIPPED;
  1427. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1428. (!defined(NO_RSA) || defined(HAVE_ECC))
  1429. WOLFSSL_CTX* ctx;
  1430. byte dirValid = 0;
  1431. int ret = 0;
  1432. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1433. if (ctx == NULL) {
  1434. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1435. ret = -1;
  1436. }
  1437. if (ret == 0) {
  1438. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1439. dirValid = 1;
  1440. #else
  1441. word32 numDirs;
  1442. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1443. if (caDirs == NULL || numDirs == 0) {
  1444. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1445. ret = -1;
  1446. }
  1447. else {
  1448. ReadDirCtx dirCtx;
  1449. word32 i;
  1450. for (i = 0; i < numDirs; ++i) {
  1451. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1452. /* Directory isn't empty. */
  1453. dirValid = 1;
  1454. wc_ReadDirClose(&dirCtx);
  1455. break;
  1456. }
  1457. }
  1458. }
  1459. #endif
  1460. }
  1461. /*
  1462. * If the directory isn't empty, we should be able to load CA
  1463. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1464. * usable.
  1465. */
  1466. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1467. WOLFSSL_SUCCESS) {
  1468. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1469. ret = -1;
  1470. }
  1471. #ifdef OPENSSL_EXTRA
  1472. if (ret == 0 &&
  1473. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1474. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1475. ret = -1;
  1476. }
  1477. #endif /* OPENSSL_EXTRA */
  1478. wolfSSL_CTX_free(ctx);
  1479. res = TEST_RES_CHECK(ret == 0);
  1480. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1481. return res;
  1482. }
  1483. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1484. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1485. int file_type)
  1486. {
  1487. int ret;
  1488. WOLFSSL_CERT_MANAGER* cm;
  1489. cm = wolfSSL_CertManagerNew();
  1490. if (cm == NULL) {
  1491. fprintf(stderr, "test_cm_load_ca failed\n");
  1492. return -1;
  1493. }
  1494. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1495. wolfSSL_CertManagerFree(cm);
  1496. return ret;
  1497. }
  1498. static int test_cm_load_ca_file(const char* ca_cert_file)
  1499. {
  1500. int ret = 0;
  1501. byte* cert_buf = NULL;
  1502. size_t cert_sz = 0;
  1503. #if defined(WOLFSSL_PEM_TO_DER)
  1504. DerBuffer* pDer = NULL;
  1505. #endif
  1506. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1507. if (ret == 0) {
  1508. /* normal test */
  1509. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1510. if (ret == WOLFSSL_SUCCESS) {
  1511. /* test including null terminator in length */
  1512. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1513. if (tmp == NULL) {
  1514. ret = MEMORY_E;
  1515. }
  1516. else {
  1517. cert_buf = tmp;
  1518. cert_buf[cert_sz] = '\0';
  1519. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1520. WOLFSSL_FILETYPE_PEM);
  1521. }
  1522. }
  1523. #if defined(WOLFSSL_PEM_TO_DER)
  1524. if (ret == WOLFSSL_SUCCESS) {
  1525. /* test loading DER */
  1526. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1527. if (ret == 0 && pDer != NULL) {
  1528. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1529. WOLFSSL_FILETYPE_ASN1);
  1530. wc_FreeDer(&pDer);
  1531. }
  1532. }
  1533. #endif
  1534. }
  1535. free(cert_buf);
  1536. return ret;
  1537. }
  1538. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1539. int file_type, word32 flags)
  1540. {
  1541. int ret;
  1542. WOLFSSL_CERT_MANAGER* cm;
  1543. cm = wolfSSL_CertManagerNew();
  1544. if (cm == NULL) {
  1545. fprintf(stderr, "test_cm_load_ca failed\n");
  1546. return -1;
  1547. }
  1548. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1549. 0, flags);
  1550. wolfSSL_CertManagerFree(cm);
  1551. return ret;
  1552. }
  1553. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1554. {
  1555. int ret = 0;
  1556. byte* cert_buf = NULL;
  1557. size_t cert_sz = 0;
  1558. #if defined(WOLFSSL_PEM_TO_DER)
  1559. DerBuffer* pDer = NULL;
  1560. #endif
  1561. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1562. if (ret == 0) {
  1563. /* normal test */
  1564. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1565. WOLFSSL_FILETYPE_PEM, flags);
  1566. if (ret == WOLFSSL_SUCCESS) {
  1567. /* test including null terminator in length */
  1568. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1569. if (tmp == NULL) {
  1570. ret = MEMORY_E;
  1571. }
  1572. else {
  1573. cert_buf = tmp;
  1574. cert_buf[cert_sz] = '\0';
  1575. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1576. WOLFSSL_FILETYPE_PEM, flags);
  1577. }
  1578. }
  1579. #if defined(WOLFSSL_PEM_TO_DER)
  1580. if (ret == WOLFSSL_SUCCESS) {
  1581. /* test loading DER */
  1582. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1583. if (ret == 0 && pDer != NULL) {
  1584. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1585. WOLFSSL_FILETYPE_ASN1, flags);
  1586. wc_FreeDer(&pDer);
  1587. }
  1588. }
  1589. #endif
  1590. }
  1591. free(cert_buf);
  1592. return ret;
  1593. }
  1594. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1595. static int test_wolfSSL_CertManagerAPI(void)
  1596. {
  1597. EXPECT_DECLS;
  1598. #ifndef NO_CERTS
  1599. WOLFSSL_CERT_MANAGER* cm = NULL;
  1600. unsigned char c;
  1601. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1602. wolfSSL_CertManagerFree(NULL);
  1603. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1604. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1605. #ifdef WOLFSSL_TRUST_PEER_CERT
  1606. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1607. #endif
  1608. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1609. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1610. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1611. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1612. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1614. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1615. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1616. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1617. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1618. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1619. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1620. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1621. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1622. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1623. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1624. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1625. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1626. WOLFSSL_BAD_FILETYPE);
  1627. #endif
  1628. #if !defined(NO_FILESYSTEM)
  1629. {
  1630. const char* ca_cert = "./certs/ca-cert.pem";
  1631. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1632. const char* ca_cert_der = "./certs/ca-cert.der";
  1633. #endif
  1634. const char* ca_path = "./certs";
  1635. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1636. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1637. BAD_FUNC_ARG);
  1638. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1639. BAD_FUNC_ARG);
  1640. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1641. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1642. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1643. WOLFSSL_BAD_FILETYPE);
  1644. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1645. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1646. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1647. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1648. #endif
  1649. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1650. WOLFSSL_FATAL_ERROR);
  1651. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1652. WOLFSSL_FATAL_ERROR);
  1653. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  1654. WOLFSSL_FATAL_ERROR);
  1655. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  1656. WOLFSSL_FATAL_ERROR);
  1657. }
  1658. #endif
  1659. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  1660. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  1661. #elif !defined(HAVE_CRL)
  1662. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  1663. #endif
  1664. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  1665. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  1666. #ifdef HAVE_CRL
  1667. /* Test APIs when CRL is disabled. */
  1668. #ifdef HAVE_CRL_IO
  1669. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  1670. #endif
  1671. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  1672. sizeof_server_cert_der_2048), 1);
  1673. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  1674. #endif
  1675. /* OCSP */
  1676. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  1677. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  1678. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  1679. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  1680. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1681. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1682. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  1683. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1684. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  1685. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1686. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1687. #endif
  1688. #ifdef HAVE_OCSP
  1689. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  1690. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  1691. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  1692. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  1693. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  1694. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  1695. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  1696. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  1697. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1698. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  1699. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1700. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  1701. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1702. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  1703. BAD_FUNC_ARG);
  1704. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  1705. BAD_FUNC_ARG);
  1706. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  1707. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  1708. BAD_FUNC_ARG);
  1709. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  1710. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  1711. /* Test APIs when OCSP is disabled. */
  1712. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  1713. NULL, NULL, NULL, NULL), 1);
  1714. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  1715. #endif
  1716. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  1717. wolfSSL_CertManagerFree(cm);
  1718. wolfSSL_CertManagerFree(cm);
  1719. cm = NULL;
  1720. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1721. #ifdef HAVE_OCSP
  1722. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  1723. WOLFSSL_OCSP_CHECKALL), 1);
  1724. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1725. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1726. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1727. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1728. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  1729. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1730. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  1731. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  1732. #endif
  1733. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1734. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1735. #endif
  1736. #ifdef WOLFSSL_TRUST_PEER_CERT
  1737. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  1738. #endif
  1739. wolfSSL_CertManagerFree(cm);
  1740. #endif
  1741. return EXPECT_RESULT();
  1742. }
  1743. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1744. {
  1745. EXPECT_DECLS;
  1746. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1747. const char* ca_cert = "./certs/ca-cert.pem";
  1748. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1749. int ret;
  1750. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  1751. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1752. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1753. #elif defined(NO_RSA)
  1754. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1755. #else
  1756. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1757. #endif
  1758. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  1759. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1760. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1761. #elif defined(NO_RSA)
  1762. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1763. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1764. !defined(NO_ASN_TIME)
  1765. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1766. #else
  1767. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1768. #endif
  1769. #endif
  1770. return EXPECT_RESULT();
  1771. }
  1772. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1773. {
  1774. EXPECT_DECLS;
  1775. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1776. const char* ca_cert = "./certs/ca-cert.pem";
  1777. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1778. int ret;
  1779. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  1780. 1);
  1781. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1782. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1783. #elif defined(NO_RSA)
  1784. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1785. #else
  1786. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1787. #endif
  1788. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1789. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  1790. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1791. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1792. #elif defined(NO_RSA)
  1793. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1794. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1795. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1796. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1797. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1798. #else
  1799. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1800. #endif
  1801. #endif
  1802. return EXPECT_RESULT();
  1803. }
  1804. static int test_wolfSSL_CertManagerGetCerts(void)
  1805. {
  1806. EXPECT_DECLS;
  1807. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1808. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1809. defined(WOLFSSL_SIGNER_DER_CERT)
  1810. WOLFSSL_CERT_MANAGER* cm = NULL;
  1811. WOLFSSL_STACK* sk = NULL;
  1812. X509* x509 = NULL;
  1813. X509* cert1 = NULL;
  1814. FILE* file1 = NULL;
  1815. #ifdef DEBUG_WOLFSSL_VERBOSE
  1816. WOLFSSL_BIO* bio = NULL;
  1817. #endif
  1818. int i = 0;
  1819. int ret = 0;
  1820. const byte* der;
  1821. int derSz = 0;
  1822. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  1823. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1824. if (file1 != NULL) {
  1825. fclose(file1);
  1826. }
  1827. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  1828. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1829. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1830. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1831. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1832. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1833. * and full OpenSSL compatibility */
  1834. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1835. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  1836. #else
  1837. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1838. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  1839. #endif
  1840. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1841. "./certs/ca-cert.pem", NULL));
  1842. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1843. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  1844. ExpectNotNull(x509 = sk_X509_value(sk, i));
  1845. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1846. #ifdef DEBUG_WOLFSSL_VERBOSE
  1847. bio = BIO_new(wolfSSL_BIO_s_file());
  1848. if (bio != NULL) {
  1849. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1850. X509_print(bio, x509);
  1851. BIO_free(bio);
  1852. }
  1853. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1854. }
  1855. wolfSSL_X509_free(cert1);
  1856. sk_X509_pop_free(sk, NULL);
  1857. wolfSSL_CertManagerFree(cm);
  1858. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1859. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1860. defined(WOLFSSL_SIGNER_DER_CERT) */
  1861. return EXPECT_RESULT();
  1862. }
  1863. static int test_wolfSSL_CertManagerSetVerify(void)
  1864. {
  1865. EXPECT_DECLS;
  1866. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1867. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1868. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1869. WOLFSSL_CERT_MANAGER* cm = NULL;
  1870. int tmp = myVerifyAction;
  1871. const char* ca_cert = "./certs/ca-cert.pem";
  1872. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1873. wolfSSL_CertManagerSetVerify(NULL, NULL);
  1874. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  1875. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1876. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1877. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1878. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  1879. #else
  1880. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  1881. WOLFSSL_SUCCESS);
  1882. #endif
  1883. /* Use the test CB that always accepts certs */
  1884. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1885. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  1886. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1887. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1888. {
  1889. const char* verifyCert = "./certs/server-cert.der";
  1890. /* Use the test CB that always fails certs */
  1891. myVerifyAction = VERIFY_FORCE_FAIL;
  1892. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  1893. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  1894. }
  1895. #endif
  1896. wolfSSL_CertManagerFree(cm);
  1897. myVerifyAction = tmp;
  1898. #endif
  1899. return EXPECT_RESULT();
  1900. }
  1901. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1902. defined(DEBUG_UNIT_TEST_CERTS)
  1903. /* Used when debugging name constraint tests. Not static to allow use in
  1904. * multiple locations with complex define guards. */
  1905. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1906. {
  1907. BIO* out = BIO_new_file(fileName, "wb");
  1908. if (out != NULL) {
  1909. PEM_write_bio_X509(out, x509);
  1910. BIO_free(out);
  1911. }
  1912. }
  1913. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1914. {
  1915. BIO* out = BIO_new_file(fileName, "wb");
  1916. if (out != NULL) {
  1917. BIO_write(out, der, derSz);
  1918. BIO_free(out);
  1919. }
  1920. }
  1921. #else
  1922. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  1923. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  1924. #endif
  1925. static int test_wolfSSL_CertManagerNameConstraint(void)
  1926. {
  1927. EXPECT_DECLS;
  1928. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1929. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1930. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1931. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1932. !defined(NO_SHA256)
  1933. WOLFSSL_CERT_MANAGER* cm = NULL;
  1934. WOLFSSL_EVP_PKEY *priv = NULL;
  1935. WOLFSSL_X509_NAME* name = NULL;
  1936. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1937. const char* server_cert = "./certs/test/server-goodcn.pem";
  1938. int i = 0;
  1939. static const byte extNameConsOid[] = {85, 29, 30};
  1940. RsaKey key;
  1941. WC_RNG rng;
  1942. byte *der = NULL;
  1943. int derSz = 0;
  1944. word32 idx = 0;
  1945. byte *pt;
  1946. WOLFSSL_X509 *x509 = NULL;
  1947. WOLFSSL_X509 *ca = NULL;
  1948. wc_InitRng(&rng);
  1949. /* load in CA private key for signing */
  1950. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1951. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1952. sizeof_server_key_der_2048), 0);
  1953. /* get ca certificate then alter it */
  1954. ExpectNotNull(der =
  1955. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1956. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1957. WOLFSSL_FILETYPE_ASN1));
  1958. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1959. if (EXPECT_SUCCESS() && (der != NULL)) {
  1960. XMEMCPY(der, pt, derSz);
  1961. /* find the name constraint extension and alter it */
  1962. pt = der;
  1963. for (i = 0; i < derSz - 3; i++) {
  1964. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1965. pt += 3;
  1966. break;
  1967. }
  1968. pt++;
  1969. }
  1970. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1971. /* go to the length value and set it to 0 */
  1972. while (i < derSz && *pt != 0x81) {
  1973. pt++;
  1974. i++;
  1975. }
  1976. ExpectIntNE(i, derSz); /* did not place to alter */
  1977. pt++;
  1978. *pt = 0x00;
  1979. }
  1980. /* resign the altered certificate */
  1981. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1982. FOURK_BUF, &key, NULL, &rng)), 0);
  1983. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1984. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1985. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1986. wolfSSL_CertManagerFree(cm);
  1987. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1988. wolfSSL_X509_free(x509);
  1989. wc_FreeRsaKey(&key);
  1990. wc_FreeRng(&rng);
  1991. /* add email alt name to satisfy constraint */
  1992. pt = (byte*)server_key_der_2048;
  1993. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1994. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1995. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1996. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1997. WOLFSSL_FILETYPE_ASN1));
  1998. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1999. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2000. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2001. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2002. /* Good cert test with proper alt email name */
  2003. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2004. WOLFSSL_FILETYPE_PEM));
  2005. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2006. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2007. name = NULL;
  2008. ExpectNotNull(name = X509_NAME_new());
  2009. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2010. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2011. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2012. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2013. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2014. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2015. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2016. X509_NAME_free(name);
  2017. name = NULL;
  2018. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2019. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2020. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2021. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2022. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2023. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2024. wolfSSL_X509_free(x509);
  2025. x509 = NULL;
  2026. /* Cert with bad alt name list */
  2027. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2028. WOLFSSL_FILETYPE_PEM));
  2029. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2030. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2031. name = NULL;
  2032. ExpectNotNull(name = X509_NAME_new());
  2033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2034. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2035. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2036. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2037. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2038. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2039. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2040. X509_NAME_free(name);
  2041. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2042. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2043. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2044. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2045. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2046. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2047. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2048. wolfSSL_CertManagerFree(cm);
  2049. wolfSSL_X509_free(x509);
  2050. wolfSSL_X509_free(ca);
  2051. wolfSSL_EVP_PKEY_free(priv);
  2052. #endif
  2053. return EXPECT_RESULT();
  2054. }
  2055. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2056. {
  2057. EXPECT_DECLS;
  2058. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2059. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2060. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2061. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2062. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2063. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2064. const char* server_cert = "./certs/server-cert.pem";
  2065. WOLFSSL_CERT_MANAGER* cm = NULL;
  2066. WOLFSSL_X509 *x509 = NULL;
  2067. WOLFSSL_X509 *ca = NULL;
  2068. const unsigned char *der = NULL;
  2069. const unsigned char *pt;
  2070. WOLFSSL_EVP_PKEY *priv = NULL;
  2071. WOLFSSL_X509_NAME* name = NULL;
  2072. int derSz = 0;
  2073. /* C=US*/
  2074. char altName[] = {
  2075. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2076. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2077. };
  2078. /* C=ID */
  2079. char altNameFail[] = {
  2080. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2081. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2082. };
  2083. /* C=US ST=California*/
  2084. char altNameExc[] = {
  2085. 0x30, 0x22,
  2086. 0x31, 0x0B,
  2087. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2088. 0x31, 0x13,
  2089. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2090. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2091. };
  2092. /* load in CA private key for signing */
  2093. pt = ca_key_der_2048;
  2094. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2095. sizeof_ca_key_der_2048));
  2096. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2097. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2098. WOLFSSL_FILETYPE_ASN1));
  2099. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2100. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2101. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2102. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2103. WOLFSSL_FILETYPE_PEM));
  2104. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2105. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2106. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2107. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2108. #else
  2109. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2110. #endif
  2111. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2112. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2113. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2114. /* add in matching DIR alt name and resign */
  2115. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2116. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2117. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2118. #else
  2119. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2120. #endif
  2121. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2122. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2123. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2124. wolfSSL_X509_free(x509);
  2125. x509 = NULL;
  2126. /* check verify fail */
  2127. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2128. WOLFSSL_FILETYPE_PEM));
  2129. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2130. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2131. /* add in miss matching DIR alt name and resign */
  2132. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2133. ASN_DIR_TYPE);
  2134. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2135. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2136. #else
  2137. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2138. #endif
  2139. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2140. #ifndef WOLFSSL_NO_ASN_STRICT
  2141. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2142. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2143. #else
  2144. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2145. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2146. #endif
  2147. /* check that it still fails if one bad altname and one good altname is in
  2148. * the certificate */
  2149. wolfSSL_X509_free(x509);
  2150. x509 = NULL;
  2151. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2152. WOLFSSL_FILETYPE_PEM));
  2153. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2154. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2155. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2156. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2157. ASN_DIR_TYPE);
  2158. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2159. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2160. #else
  2161. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2162. #endif
  2163. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2164. #ifndef WOLFSSL_NO_ASN_STRICT
  2165. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2166. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2167. #else
  2168. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2169. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2170. #endif
  2171. /* check it fails with switching position of bad altname */
  2172. wolfSSL_X509_free(x509);
  2173. x509 = NULL;
  2174. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2175. WOLFSSL_FILETYPE_PEM));
  2176. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2177. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2178. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2179. ASN_DIR_TYPE);
  2180. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2181. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2182. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2183. #else
  2184. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2185. #endif
  2186. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2187. #ifndef WOLFSSL_NO_ASN_STRICT
  2188. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2189. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2190. #else
  2191. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2192. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2193. #endif
  2194. wolfSSL_CertManagerFree(cm);
  2195. wolfSSL_X509_free(x509);
  2196. x509 = NULL;
  2197. wolfSSL_X509_free(ca);
  2198. ca = NULL;
  2199. /* now test with excluded name constraint */
  2200. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2201. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2202. WOLFSSL_FILETYPE_ASN1));
  2203. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2204. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2205. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2206. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2207. WOLFSSL_FILETYPE_PEM));
  2208. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2209. ASN_DIR_TYPE);
  2210. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2211. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2212. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2213. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2214. #else
  2215. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2216. #endif
  2217. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2218. #ifndef WOLFSSL_NO_ASN_STRICT
  2219. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2220. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2221. #else
  2222. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2223. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2224. #endif
  2225. wolfSSL_CertManagerFree(cm);
  2226. wolfSSL_X509_free(x509);
  2227. wolfSSL_X509_free(ca);
  2228. wolfSSL_EVP_PKEY_free(priv);
  2229. #endif
  2230. return EXPECT_RESULT();
  2231. }
  2232. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2233. {
  2234. EXPECT_DECLS;
  2235. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2236. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2237. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2238. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2239. !defined(NO_SHA256)
  2240. WOLFSSL_CERT_MANAGER* cm = NULL;
  2241. WOLFSSL_EVP_PKEY *priv = NULL;
  2242. WOLFSSL_X509_NAME* name = NULL;
  2243. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2244. const char* server_cert = "./certs/test/server-goodcn.pem";
  2245. byte *der = NULL;
  2246. int derSz = 0;
  2247. byte *pt;
  2248. WOLFSSL_X509 *x509 = NULL;
  2249. WOLFSSL_X509 *ca = NULL;
  2250. pt = (byte*)server_key_der_2048;
  2251. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2252. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2253. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2254. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2255. WOLFSSL_FILETYPE_ASN1));
  2256. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2257. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2258. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2259. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2260. /* check satisfying .wolfssl.com constraint passes */
  2261. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2262. WOLFSSL_FILETYPE_PEM));
  2263. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2264. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2265. name = NULL;
  2266. ExpectNotNull(name = X509_NAME_new());
  2267. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2268. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2269. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2270. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2271. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2272. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2273. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2274. X509_NAME_free(name);
  2275. name = NULL;
  2276. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2277. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2278. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2279. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2280. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2281. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2282. wolfSSL_X509_free(x509);
  2283. x509 = NULL;
  2284. /* check satisfying .random.com constraint passes */
  2285. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2286. WOLFSSL_FILETYPE_PEM));
  2287. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2288. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2289. name = NULL;
  2290. ExpectNotNull(name = X509_NAME_new());
  2291. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2292. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2293. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2294. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2295. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2296. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2297. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2298. X509_NAME_free(name);
  2299. name = NULL;
  2300. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2301. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2302. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2303. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2304. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2305. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2306. wolfSSL_X509_free(x509);
  2307. x509 = NULL;
  2308. /* check fail case when neither constraint is matched */
  2309. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2310. WOLFSSL_FILETYPE_PEM));
  2311. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2312. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2313. name = NULL;
  2314. ExpectNotNull(name = X509_NAME_new());
  2315. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2316. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2317. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2318. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2319. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2320. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2321. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2322. X509_NAME_free(name);
  2323. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2324. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2325. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2326. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2327. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2328. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2329. wolfSSL_CertManagerFree(cm);
  2330. wolfSSL_X509_free(x509);
  2331. wolfSSL_X509_free(ca);
  2332. wolfSSL_EVP_PKEY_free(priv);
  2333. #endif
  2334. return EXPECT_RESULT();
  2335. }
  2336. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2337. {
  2338. EXPECT_DECLS;
  2339. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2340. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2341. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2342. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2343. !defined(NO_SHA256)
  2344. WOLFSSL_CERT_MANAGER* cm = NULL;
  2345. WOLFSSL_EVP_PKEY *priv = NULL;
  2346. WOLFSSL_X509_NAME* name = NULL;
  2347. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2348. const char* server_cert = "./certs/test/server-goodcn.pem";
  2349. byte *der = NULL;
  2350. int derSz;
  2351. byte *pt;
  2352. WOLFSSL_X509 *x509 = NULL;
  2353. WOLFSSL_X509 *ca = NULL;
  2354. pt = (byte*)server_key_der_2048;
  2355. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2356. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2357. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2358. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2359. WOLFSSL_FILETYPE_ASN1));
  2360. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2361. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2362. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2363. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2364. /* check satisfying wolfssl.com constraint passes */
  2365. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2366. WOLFSSL_FILETYPE_PEM));
  2367. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2368. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2369. name = NULL;
  2370. ExpectNotNull(name = X509_NAME_new());
  2371. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2372. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2373. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2374. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2375. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2376. X509_NAME_free(name);
  2377. name = NULL;
  2378. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2379. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2380. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2381. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2382. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2383. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2384. wolfSSL_X509_free(x509);
  2385. x509 = NULL;
  2386. /* check satisfying example.com constraint passes */
  2387. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2388. WOLFSSL_FILETYPE_PEM));
  2389. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2390. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2391. name = NULL;
  2392. ExpectNotNull(name = X509_NAME_new());
  2393. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2394. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2395. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2396. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2397. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2398. X509_NAME_free(name);
  2399. name = NULL;
  2400. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2401. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2402. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2403. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2404. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2405. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2406. wolfSSL_X509_free(x509);
  2407. x509 = NULL;
  2408. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2409. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2410. WOLFSSL_FILETYPE_PEM));
  2411. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2412. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2413. name = NULL;
  2414. ExpectNotNull(name = X509_NAME_new());
  2415. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2416. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2417. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2418. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2419. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2420. X509_NAME_free(name);
  2421. name = NULL;
  2422. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2423. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2424. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2425. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2426. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2427. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2428. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2429. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2430. wolfSSL_X509_free(x509);
  2431. x509 = NULL;
  2432. /* check fail when one DNS in the list is bad */
  2433. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2434. WOLFSSL_FILETYPE_PEM));
  2435. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2436. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2437. name = NULL;
  2438. ExpectNotNull(name = X509_NAME_new());
  2439. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2440. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2441. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2442. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2443. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2444. X509_NAME_free(name);
  2445. name = NULL;
  2446. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2447. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2448. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2449. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2450. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2451. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2452. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2453. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2454. wolfSSL_X509_free(x509);
  2455. x509 = NULL;
  2456. /* check fail case when neither constraint is matched */
  2457. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2458. WOLFSSL_FILETYPE_PEM));
  2459. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2460. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2461. name = NULL;
  2462. ExpectNotNull(name = X509_NAME_new());
  2463. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2464. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2465. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2466. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2467. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2468. X509_NAME_free(name);
  2469. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2470. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2471. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2472. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2473. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2474. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2475. wolfSSL_CertManagerFree(cm);
  2476. wolfSSL_X509_free(x509);
  2477. wolfSSL_X509_free(ca);
  2478. wolfSSL_EVP_PKEY_free(priv);
  2479. #endif
  2480. return EXPECT_RESULT();
  2481. }
  2482. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2483. {
  2484. EXPECT_DECLS;
  2485. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2486. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2487. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2488. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2489. !defined(NO_SHA256)
  2490. WOLFSSL_CERT_MANAGER* cm = NULL;
  2491. WOLFSSL_EVP_PKEY *priv = NULL;
  2492. WOLFSSL_X509_NAME* name = NULL;
  2493. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2494. const char* server_cert = "./certs/test/server-goodcn.pem";
  2495. byte *der = NULL;
  2496. int derSz;
  2497. byte *pt;
  2498. WOLFSSL_X509 *x509 = NULL;
  2499. WOLFSSL_X509 *ca = NULL;
  2500. pt = (byte*)server_key_der_2048;
  2501. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2502. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2503. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2504. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2505. WOLFSSL_FILETYPE_ASN1));
  2506. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2507. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2508. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2509. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2510. /* check satisfying wolfssl.com constraint passes */
  2511. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2512. WOLFSSL_FILETYPE_PEM));
  2513. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2514. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2515. name = NULL;
  2516. ExpectNotNull(name = X509_NAME_new());
  2517. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2518. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2519. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2520. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2521. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2522. X509_NAME_free(name);
  2523. name = NULL;
  2524. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2525. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2526. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2527. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2528. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2529. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2530. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2531. wolfSSL_X509_free(x509);
  2532. x509 = NULL;
  2533. /* fail with DNS check because of common name */
  2534. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2535. WOLFSSL_FILETYPE_PEM));
  2536. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2537. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2538. name = NULL;
  2539. ExpectNotNull(name = X509_NAME_new());
  2540. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2541. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2542. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2543. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2544. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2545. X509_NAME_free(name);
  2546. name = NULL;
  2547. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2548. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2549. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2550. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2551. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2552. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2553. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2554. wolfSSL_X509_free(x509);
  2555. x509 = NULL;
  2556. /* fail on permitted DNS name constraint */
  2557. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2558. WOLFSSL_FILETYPE_PEM));
  2559. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2560. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2561. name = NULL;
  2562. ExpectNotNull(name = X509_NAME_new());
  2563. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2564. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2565. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2566. X509_NAME_free(name);
  2567. name = NULL;
  2568. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2569. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2570. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2571. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2572. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2573. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2574. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2575. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2576. wolfSSL_X509_free(x509);
  2577. x509 = NULL;
  2578. /* fail on permitted email name constraint */
  2579. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2580. WOLFSSL_FILETYPE_PEM));
  2581. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2582. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2583. name = NULL;
  2584. ExpectNotNull(name = X509_NAME_new());
  2585. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2586. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2587. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2588. X509_NAME_free(name);
  2589. name = NULL;
  2590. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2591. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2592. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2593. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2594. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2595. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2596. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2597. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2598. wolfSSL_X509_free(x509);
  2599. x509 = NULL;
  2600. /* success with empty email name */
  2601. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2602. WOLFSSL_FILETYPE_PEM));
  2603. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2604. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2605. name = NULL;
  2606. ExpectNotNull(name = X509_NAME_new());
  2607. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2608. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2609. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2610. X509_NAME_free(name);
  2611. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2612. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2613. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2614. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2615. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2616. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2617. wolfSSL_X509_free(x509);
  2618. wolfSSL_CertManagerFree(cm);
  2619. wolfSSL_X509_free(ca);
  2620. wolfSSL_EVP_PKEY_free(priv);
  2621. #endif
  2622. return EXPECT_RESULT();
  2623. }
  2624. static int test_wolfSSL_CertManagerCRL(void)
  2625. {
  2626. EXPECT_DECLS;
  2627. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2628. !defined(NO_RSA)
  2629. const char* ca_cert = "./certs/ca-cert.pem";
  2630. const char* crl1 = "./certs/crl/crl.pem";
  2631. const char* crl2 = "./certs/crl/crl2.pem";
  2632. const unsigned char crl_buff[] = {
  2633. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2634. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2635. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2636. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2637. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2638. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2639. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2640. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2641. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2642. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2643. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2644. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2645. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2646. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2647. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2648. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  2649. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  2650. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  2651. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2652. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  2653. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2654. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  2655. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  2656. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  2657. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  2658. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  2659. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  2660. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  2661. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  2662. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  2663. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  2664. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  2665. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  2666. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  2667. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  2668. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  2669. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  2670. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  2671. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  2672. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  2673. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  2674. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  2675. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  2676. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  2677. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  2678. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  2679. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  2680. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  2681. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  2682. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  2683. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  2684. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  2685. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  2686. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  2687. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  2688. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  2689. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  2690. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  2691. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  2692. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  2693. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  2694. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  2695. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  2696. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  2697. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  2698. };
  2699. WOLFSSL_CERT_MANAGER* cm = NULL;
  2700. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2701. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  2702. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2703. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  2704. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  2705. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  2706. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  2707. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2708. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  2709. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  2710. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  2711. BAD_FUNC_ARG);
  2712. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  2713. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  2714. BAD_FUNC_ARG);
  2715. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  2716. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  2717. BAD_FUNC_ARG);
  2718. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2719. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  2720. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  2721. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  2722. #ifdef HAVE_CRL_IO
  2723. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  2724. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2725. #endif
  2726. #ifndef NO_FILESYSTEM
  2727. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  2728. 0), BAD_FUNC_ARG);
  2729. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  2730. 0), BAD_FUNC_ARG);
  2731. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2732. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  2733. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  2734. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2735. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2736. BAD_FUNC_ARG);
  2737. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2738. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  2739. ASN_PARSE_E);
  2740. #endif
  2741. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  2742. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2743. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  2744. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2745. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  2746. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2747. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  2748. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2749. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  2750. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2751. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  2752. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2753. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  2754. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2755. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  2756. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2757. ExpectIntEQ(WOLFSSL_SUCCESS,
  2758. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2759. ExpectIntEQ(WOLFSSL_SUCCESS,
  2760. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2761. ExpectIntEQ(WOLFSSL_SUCCESS,
  2762. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2763. wolfSSL_CertManagerFreeCRL(cm);
  2764. ExpectIntEQ(WOLFSSL_SUCCESS,
  2765. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2766. ExpectIntEQ(WOLFSSL_SUCCESS,
  2767. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2768. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2769. sizeof_server_cert_der_2048), CRL_MISSING);
  2770. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  2771. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  2772. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  2773. WOLFSSL_FILETYPE_ASN1), 1);
  2774. wolfSSL_CertManagerFree(cm);
  2775. #endif
  2776. return EXPECT_RESULT();
  2777. }
  2778. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  2779. {
  2780. EXPECT_DECLS;
  2781. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  2782. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  2783. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  2784. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  2785. defined(HAVE_LIGHTY)
  2786. WOLFSSL_CERT_MANAGER* cm = NULL;
  2787. /* Raw OCSP response bytes captured using the following setup:
  2788. * - Run responder with
  2789. * openssl ocsp -port 9999 -ndays 9999
  2790. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  2791. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  2792. * -rkey certs/ocsp/ocsp-responder-key.pem
  2793. * -CA certs/ocsp/intermediate1-ca-cert.pem
  2794. * - Run client with
  2795. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  2796. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  2797. * -cert certs/ocsp/server1-cert.pem
  2798. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  2799. * - Select the response packet in Wireshark, and export it using
  2800. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  2801. * packets only". After importing into the editor, remove the initial
  2802. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  2803. */
  2804. static const byte response[] = {
  2805. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  2806. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  2807. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  2808. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  2809. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  2810. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  2811. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2812. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2813. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2814. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2815. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2816. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2817. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2818. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2819. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2820. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2821. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2822. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  2823. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  2824. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  2825. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  2826. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  2827. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  2828. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  2829. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  2830. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  2831. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  2832. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  2833. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  2834. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  2835. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  2836. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  2837. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  2838. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  2839. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  2840. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  2841. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  2842. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  2843. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  2844. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  2845. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  2846. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  2847. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  2848. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  2849. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  2850. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  2851. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  2852. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  2853. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  2854. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  2855. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  2856. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  2857. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  2858. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  2859. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  2860. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  2861. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  2862. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  2863. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  2864. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  2865. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  2866. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  2867. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  2868. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  2869. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  2870. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  2871. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  2872. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  2873. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  2874. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  2875. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  2876. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  2877. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  2878. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  2879. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  2880. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  2881. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  2882. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  2883. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  2884. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  2885. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  2886. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  2887. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  2888. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  2889. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  2890. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2891. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2892. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2893. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2894. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2895. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2896. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2897. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2898. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2899. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2900. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2901. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  2902. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  2903. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  2904. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  2905. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  2906. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  2907. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  2908. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  2909. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  2910. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  2911. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  2912. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  2913. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  2914. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  2915. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  2916. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  2917. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  2918. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  2919. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  2920. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  2921. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2922. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2923. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  2924. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  2925. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  2926. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2927. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  2928. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  2929. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  2930. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  2931. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  2932. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  2933. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  2934. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  2935. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  2936. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  2937. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  2938. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  2939. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  2940. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  2941. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  2942. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  2943. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  2944. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  2945. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  2946. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  2947. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  2948. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  2949. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  2950. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  2951. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  2952. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  2953. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  2954. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  2955. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  2956. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  2957. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  2958. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  2959. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  2960. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  2961. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  2962. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  2963. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  2964. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  2965. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  2966. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  2967. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  2968. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  2969. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  2970. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  2971. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  2972. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  2973. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  2974. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  2975. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  2976. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  2977. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  2978. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  2979. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  2980. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  2981. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  2982. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  2983. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  2984. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  2985. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  2986. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  2987. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  2988. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  2989. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2990. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  2991. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  2992. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  2993. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  2994. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2995. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  2996. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  2997. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  2998. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  2999. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3000. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3001. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3002. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3003. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3004. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3005. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3006. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3007. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3008. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3009. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3010. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3011. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3012. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3013. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3014. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3015. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3016. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3017. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3018. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3019. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3020. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3021. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3022. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3023. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3024. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3025. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3026. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3027. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3028. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3029. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3030. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3031. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3032. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3033. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3034. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3035. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3036. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3037. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3038. };
  3039. OcspEntry entry[1];
  3040. CertStatus status[1];
  3041. OcspRequest* request = NULL;
  3042. #ifndef NO_FILESYSTEM
  3043. const char* ca_cert = "./certs/ca-cert.pem";
  3044. #endif
  3045. byte serial[] = {0x05};
  3046. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3047. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3048. XMEMSET(entry, 0, sizeof(OcspEntry));
  3049. XMEMSET(status, 0, sizeof(CertStatus));
  3050. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3051. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3052. DYNAMIC_TYPE_OCSP_REQUEST));
  3053. if ((request != NULL) && (request->serial != NULL)) {
  3054. request->serialSz = sizeof(serial);
  3055. XMEMCPY(request->serial, serial, sizeof(serial));
  3056. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3057. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3058. }
  3059. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3060. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3061. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3062. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3063. /* Response should be valid. */
  3064. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3065. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3066. /* Flip a byte in the request serial number, response should be invalid
  3067. * now. */
  3068. if ((request != NULL) && (request->serial != NULL))
  3069. request->serial[0] ^= request->serial[0];
  3070. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3071. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3072. #ifndef NO_FILESYSTEM
  3073. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3074. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3075. ExpectIntEQ(WOLFSSL_SUCCESS,
  3076. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3077. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3078. sizeof(server_cert_der_2048)), 1);
  3079. #endif
  3080. wolfSSL_OCSP_REQUEST_free(request);
  3081. wolfSSL_CertManagerFree(cm);
  3082. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3083. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3084. #endif /* HAVE_OCSP */
  3085. return EXPECT_RESULT();
  3086. }
  3087. static int test_wolfSSL_CheckOCSPResponse(void)
  3088. {
  3089. EXPECT_DECLS;
  3090. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3091. defined(OPENSSL_ALL)
  3092. const char* responseFile = "./certs/ocsp/test-response.der";
  3093. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3094. const char* responseNoInternFile =
  3095. "./certs/ocsp/test-response-nointern.der";
  3096. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3097. OcspResponse* res = NULL;
  3098. byte data[4096];
  3099. const unsigned char* pt;
  3100. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3101. * gcc sanitizer with --enable-heapmath.
  3102. */
  3103. XFILE f = XBADFILE;
  3104. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3105. WOLFSSL_X509_STORE* st = NULL;
  3106. WOLFSSL_X509* issuer = NULL;
  3107. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3108. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3109. if (f != XBADFILE) {
  3110. XFCLOSE(f);
  3111. f = XBADFILE;
  3112. }
  3113. pt = data;
  3114. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3115. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3116. SSL_FILETYPE_PEM));
  3117. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3118. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3119. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3120. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3121. wolfSSL_OCSP_BASICRESP_free(bs);
  3122. bs = NULL;
  3123. wolfSSL_OCSP_RESPONSE_free(res);
  3124. res = NULL;
  3125. wolfSSL_X509_STORE_free(st);
  3126. st = NULL;
  3127. wolfSSL_X509_free(issuer);
  3128. issuer = NULL;
  3129. /* check loading a response with optional certs */
  3130. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3131. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3132. if (f != XBADFILE)
  3133. XFCLOSE(f);
  3134. f = XBADFILE;
  3135. pt = data;
  3136. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3137. wolfSSL_OCSP_RESPONSE_free(res);
  3138. res = NULL;
  3139. /* check loading a response with multiple certs */
  3140. {
  3141. WOLFSSL_CERT_MANAGER* cm = NULL;
  3142. OcspEntry *entry = NULL;
  3143. CertStatus* status = NULL;
  3144. OcspRequest* request = NULL;
  3145. byte serial1[] = {0x01};
  3146. byte serial[] = {0x02};
  3147. byte issuerHash[] = {
  3148. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3149. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3150. 0x37, 0x15, 0x8A, 0x88
  3151. };
  3152. byte issuerKeyHash[] = {
  3153. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3154. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3155. 0x7E, 0x72, 0x15, 0x21
  3156. };
  3157. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3158. DYNAMIC_TYPE_OPENSSL));
  3159. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3160. DYNAMIC_TYPE_OPENSSL));
  3161. if (entry != NULL)
  3162. XMEMSET(entry, 0, sizeof(OcspEntry));
  3163. if (status != NULL)
  3164. XMEMSET(status, 0, sizeof(CertStatus));
  3165. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3166. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3167. DYNAMIC_TYPE_OCSP_REQUEST));
  3168. if (request != NULL && request->serial != NULL) {
  3169. request->serialSz = sizeof(serial);
  3170. XMEMCPY(request->serial, serial, sizeof(serial));
  3171. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3172. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3173. sizeof(issuerKeyHash));
  3174. }
  3175. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3176. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3177. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3178. WOLFSSL_SUCCESS);
  3179. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3180. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3181. if (f != XBADFILE)
  3182. XFCLOSE(f);
  3183. f = XBADFILE;
  3184. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3185. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3186. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3187. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3188. ExpectNotNull(entry->status);
  3189. if (request != NULL && request->serial != NULL)
  3190. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3191. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3192. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3193. /* store both status's in the entry to check that "next" is not
  3194. * overwritten */
  3195. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3196. status->next = entry->status;
  3197. entry->status = status;
  3198. }
  3199. if (request != NULL && request->serial != NULL)
  3200. XMEMCPY(request->serial, serial, sizeof(serial));
  3201. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3202. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3203. ExpectNotNull(entry->status->next);
  3204. /* compare the status found */
  3205. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3206. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3207. status->serialSz), 0);
  3208. if (status != NULL && entry != NULL && entry->status != status) {
  3209. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3210. }
  3211. wolfSSL_OCSP_CERTID_free(entry);
  3212. wolfSSL_OCSP_REQUEST_free(request);
  3213. wolfSSL_CertManagerFree(cm);
  3214. }
  3215. #if defined(WC_RSA_PSS)
  3216. {
  3217. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3218. /* check loading a response with RSA-PSS signature */
  3219. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3220. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3221. if (f != XBADFILE)
  3222. XFCLOSE(f);
  3223. pt = data;
  3224. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3225. /* try to verify the response */
  3226. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3227. SSL_FILETYPE_PEM));
  3228. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3229. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3230. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3231. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3232. WOLFSSL_SUCCESS);
  3233. wolfSSL_OCSP_BASICRESP_free(bs);
  3234. wolfSSL_OCSP_RESPONSE_free(res);
  3235. wolfSSL_X509_STORE_free(st);
  3236. wolfSSL_X509_free(issuer);
  3237. }
  3238. #endif
  3239. #endif /* HAVE_OCSP */
  3240. return EXPECT_RESULT();
  3241. }
  3242. static int test_wolfSSL_FPKI(void)
  3243. {
  3244. EXPECT_DECLS;
  3245. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3246. XFILE f = XBADFILE;
  3247. const char* fpkiCert = "./certs/fpki-cert.der";
  3248. DecodedCert cert;
  3249. byte buf[4096];
  3250. byte* uuid = NULL;
  3251. byte* fascn = NULL;
  3252. word32 fascnSz;
  3253. word32 uuidSz;
  3254. int bytes = 0;
  3255. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3256. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3257. if (f != XBADFILE)
  3258. XFCLOSE(f);
  3259. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3260. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3261. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3262. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3263. DYNAMIC_TYPE_TMP_BUFFER));
  3264. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3265. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3266. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3267. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3268. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3269. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3270. wc_FreeDecodedCert(&cert);
  3271. #endif
  3272. return EXPECT_RESULT();
  3273. }
  3274. /* use RID in confuncture with other names to test parsing of unknown other
  3275. * names */
  3276. static int test_wolfSSL_OtherName(void)
  3277. {
  3278. EXPECT_DECLS;
  3279. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3280. XFILE f = XBADFILE;
  3281. const char* ridCert = "./certs/rid-cert.der";
  3282. DecodedCert cert;
  3283. byte buf[4096];
  3284. int bytes = 0;
  3285. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3286. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3287. if (f != XBADFILE)
  3288. XFCLOSE(f);
  3289. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3290. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3291. wc_FreeDecodedCert(&cert);
  3292. #endif
  3293. return EXPECT_RESULT();
  3294. }
  3295. static int test_wolfSSL_CertRsaPss(void)
  3296. {
  3297. EXPECT_DECLS;
  3298. /* FIPS v2 and below don't support long salts. */
  3299. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3300. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3301. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3302. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3303. XFILE f = XBADFILE;
  3304. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3305. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3306. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3307. RSA_MAX_SIZE >= 3072
  3308. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3309. #endif
  3310. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3311. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3312. #endif
  3313. DecodedCert cert;
  3314. byte buf[4096];
  3315. int bytes = 0;
  3316. WOLFSSL_CERT_MANAGER* cm = NULL;
  3317. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3318. ExpectIntEQ(WOLFSSL_SUCCESS,
  3319. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3320. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3321. ExpectIntEQ(WOLFSSL_SUCCESS,
  3322. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3323. #endif
  3324. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3325. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3326. if (f != XBADFILE) {
  3327. XFCLOSE(f);
  3328. f = XBADFILE;
  3329. }
  3330. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3331. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3332. wc_FreeDecodedCert(&cert);
  3333. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3334. RSA_MAX_SIZE >= 3072
  3335. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3336. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3337. if (f != XBADFILE)
  3338. XFCLOSE(f);
  3339. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3340. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3341. wc_FreeDecodedCert(&cert);
  3342. #endif
  3343. wolfSSL_CertManagerFree(cm);
  3344. #endif
  3345. return EXPECT_RESULT();
  3346. }
  3347. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3348. {
  3349. EXPECT_DECLS;
  3350. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3351. !defined(NO_WOLFSSL_CLIENT)
  3352. WOLFSSL_CTX* ctx = NULL;
  3353. const char* ca_cert = "./certs/ca-cert.pem";
  3354. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3355. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3356. /* test good CA */
  3357. ExpectTrue(WOLFSSL_SUCCESS ==
  3358. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3359. WOLFSSL_LOAD_FLAG_NONE));
  3360. /* test expired CA */
  3361. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3362. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3363. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3364. #else
  3365. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3366. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3367. #endif
  3368. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3369. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3370. wolfSSL_CTX_free(ctx);
  3371. #endif
  3372. return EXPECT_RESULT();
  3373. }
  3374. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3375. {
  3376. EXPECT_DECLS;
  3377. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3378. defined(USE_CERT_BUFFERS_2048)
  3379. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3380. WOLFSSL_CTX* ctx;
  3381. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3382. byte ca_expired_cert[TWOK_BUF];
  3383. word32 sizeof_ca_expired_cert = 0;
  3384. XFILE fp = XBADFILE;
  3385. #ifndef NO_WOLFSSL_CLIENT
  3386. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3387. #else
  3388. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3389. #endif
  3390. ExpectNotNull(ctx);
  3391. /* test good CA */
  3392. ExpectTrue(WOLFSSL_SUCCESS ==
  3393. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3394. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3395. WOLFSSL_LOAD_FLAG_NONE));
  3396. /* load expired CA */
  3397. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3398. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3399. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3400. sizeof(ca_expired_cert), fp), 0);
  3401. if (fp != XBADFILE)
  3402. XFCLOSE(fp);
  3403. /* test expired CA failure */
  3404. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3405. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3406. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3407. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3408. #else
  3409. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3410. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3411. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3412. #endif
  3413. /* test expired CA success */
  3414. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3415. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3416. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3417. wolfSSL_CTX_free(ctx);
  3418. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3419. #endif
  3420. return EXPECT_RESULT();
  3421. }
  3422. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3423. {
  3424. EXPECT_DECLS;
  3425. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3426. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3427. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3428. WOLFSSL_CTX* ctx = NULL;
  3429. #ifndef NO_WOLFSSL_CLIENT
  3430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3431. #else
  3432. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3433. #endif
  3434. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3435. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3436. WOLFSSL_FILETYPE_ASN1));
  3437. wolfSSL_CTX_free(ctx);
  3438. #endif
  3439. return EXPECT_RESULT();
  3440. }
  3441. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3442. {
  3443. EXPECT_DECLS;
  3444. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3445. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3446. WOLFSSL_CTX* ctx;
  3447. WOLFSSL* ssl = NULL;
  3448. const char *certChain[] = {
  3449. "./certs/intermediate/client-int-cert.pem",
  3450. "./certs/intermediate/ca-int2-cert.pem",
  3451. "./certs/intermediate/ca-int-cert.pem",
  3452. "./certs/ca-cert.pem",
  3453. NULL
  3454. };
  3455. const char** cert;
  3456. WOLFSSL_X509* x509 = NULL;
  3457. WOLF_STACK_OF(X509)* chain = NULL;
  3458. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3459. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3460. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3461. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3462. WOLFSSL_FILETYPE_PEM));
  3463. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3464. X509_free(x509);
  3465. x509 = NULL;
  3466. }
  3467. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3468. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3469. WOLFSSL_FILETYPE_PEM));
  3470. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3471. X509_free(x509);
  3472. x509 = NULL;
  3473. }
  3474. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3475. ExpectIntEQ(sk_X509_num(chain), 3);
  3476. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3477. ExpectIntEQ(sk_X509_num(chain), 3);
  3478. SSL_free(ssl);
  3479. SSL_CTX_free(ctx);
  3480. #endif
  3481. return EXPECT_RESULT();
  3482. }
  3483. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3484. {
  3485. EXPECT_DECLS;
  3486. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3487. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3488. const char* server_chain_der = "./certs/server-cert-chain.der";
  3489. const char* client_single_pem = "./certs/client-cert.pem";
  3490. WOLFSSL_CTX* ctx;
  3491. (void)server_chain_der;
  3492. (void)client_single_pem;
  3493. (void)ctx;
  3494. #ifndef NO_WOLFSSL_CLIENT
  3495. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3496. #else
  3497. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3498. #endif
  3499. ExpectNotNull(ctx);
  3500. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3501. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3502. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3503. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3504. wolfSSL_CTX_free(ctx);
  3505. #endif
  3506. return EXPECT_RESULT();
  3507. }
  3508. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3509. {
  3510. EXPECT_DECLS;
  3511. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3512. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3513. WOLFSSL_CTX *ctx = NULL;
  3514. (void)ctx;
  3515. #ifndef NO_WOLFSSL_CLIENT
  3516. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3517. #else
  3518. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3519. #endif
  3520. /* invalid context */
  3521. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3522. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3523. /* invalid dhParamFile file */
  3524. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3525. NULL, WOLFSSL_FILETYPE_PEM));
  3526. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3527. bogusFile, WOLFSSL_FILETYPE_PEM));
  3528. /* success */
  3529. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3530. WOLFSSL_FILETYPE_PEM));
  3531. wolfSSL_CTX_free(ctx);
  3532. #endif
  3533. return EXPECT_RESULT();
  3534. }
  3535. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3536. {
  3537. EXPECT_DECLS;
  3538. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3539. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3540. WOLFSSL_CTX *ctx = NULL;
  3541. #ifndef NO_WOLFSSL_CLIENT
  3542. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3543. #else
  3544. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3545. #endif
  3546. /* invalid context */
  3547. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3548. dh_key_der_2048, sizeof_dh_key_der_2048,
  3549. WOLFSSL_FILETYPE_ASN1));
  3550. /* invalid dhParamFile file */
  3551. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3552. 0, WOLFSSL_FILETYPE_ASN1));
  3553. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3554. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3555. WOLFSSL_FILETYPE_ASN1));
  3556. /* success */
  3557. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3558. dh_key_der_2048, sizeof_dh_key_der_2048,
  3559. WOLFSSL_FILETYPE_ASN1));
  3560. wolfSSL_CTX_free(ctx);
  3561. #endif
  3562. return EXPECT_RESULT();
  3563. }
  3564. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3565. {
  3566. EXPECT_DECLS;
  3567. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3568. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3569. WOLFSSL_CTX *ctx;
  3570. (void)ctx;
  3571. #ifndef NO_WOLFSSL_CLIENT
  3572. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3573. #else
  3574. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3575. #endif
  3576. ExpectNotNull(ctx);
  3577. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3578. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3579. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3580. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3581. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3582. dh_key_der_2048, sizeof_dh_key_der_2048,
  3583. WOLFSSL_FILETYPE_ASN1));
  3584. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3585. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3586. dh_key_der_2048, sizeof_dh_key_der_2048,
  3587. WOLFSSL_FILETYPE_ASN1));
  3588. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3589. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3590. dh_key_der_2048, sizeof_dh_key_der_2048,
  3591. WOLFSSL_FILETYPE_ASN1));
  3592. wolfSSL_CTX_free(ctx);
  3593. #endif
  3594. return EXPECT_RESULT();
  3595. }
  3596. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3597. {
  3598. EXPECT_DECLS;
  3599. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3600. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3601. WOLFSSL_CTX* ctx = NULL;
  3602. const char* derCert = "./certs/server-cert.der";
  3603. const char* nullPath = NULL;
  3604. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3605. const char* emptyPath = "";
  3606. /* der load Case 1 ctx NULL */
  3607. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3608. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3609. #ifndef NO_WOLFSSL_CLIENT
  3610. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3611. #else
  3612. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3613. #endif
  3614. /* Case 2 filePath NULL */
  3615. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3616. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3617. /* Case 3 invalid format */
  3618. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3619. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3620. /* Case 4 filePath not valid */
  3621. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3622. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3623. /* Case 5 filePath empty */
  3624. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3625. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3626. #ifndef NO_RSA
  3627. /* Case 6 success case */
  3628. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3629. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3630. #endif
  3631. wolfSSL_CTX_free(ctx);
  3632. #endif
  3633. return EXPECT_RESULT();
  3634. }
  3635. static int test_wolfSSL_CTX_enable_disable(void)
  3636. {
  3637. EXPECT_DECLS;
  3638. #ifndef NO_CERTS
  3639. WOLFSSL_CTX* ctx = NULL;
  3640. #ifdef HAVE_CRL
  3641. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3642. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3643. #endif
  3644. #ifdef HAVE_OCSP
  3645. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3646. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3647. #endif
  3648. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3649. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3650. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3651. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3652. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3653. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3654. #endif
  3655. #ifndef NO_WOLFSSL_CLIENT
  3656. #ifdef HAVE_EXTENDED_MASTER
  3657. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3658. #endif
  3659. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3660. #ifdef HAVE_EXTENDED_MASTER
  3661. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3662. #endif
  3663. #elif !defined(NO_WOLFSSL_SERVER)
  3664. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3665. #endif
  3666. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3667. #ifdef HAVE_CRL
  3668. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3669. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3670. #endif
  3671. #ifdef HAVE_OCSP
  3672. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3673. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3674. WOLFSSL_SUCCESS);
  3675. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3676. WOLFSSL_SUCCESS);
  3677. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3678. WOLFSSL_SUCCESS);
  3679. #endif
  3680. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3681. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3682. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3683. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3684. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3685. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3686. #endif
  3687. wolfSSL_CTX_free(ctx);
  3688. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3689. #endif /* NO_CERTS */
  3690. return EXPECT_RESULT();
  3691. }
  3692. static int test_wolfSSL_CTX_ticket_API(void)
  3693. {
  3694. EXPECT_DECLS;
  3695. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3696. WOLFSSL_CTX* ctx = NULL;
  3697. void *userCtx = (void*)"this is my ctx";
  3698. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3699. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3700. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3701. wolfSSL_CTX_free(ctx);
  3702. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3703. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3704. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3705. return EXPECT_RESULT();
  3706. }
  3707. static int test_wolfSSL_set_minmax_proto_version(void)
  3708. {
  3709. EXPECT_DECLS;
  3710. #ifdef OPENSSL_EXTRA
  3711. WOLFSSL_CTX *ctx = NULL;
  3712. WOLFSSL *ssl = NULL;
  3713. (void)ssl;
  3714. #ifndef NO_WOLFSSL_CLIENT
  3715. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3716. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3717. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3718. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3719. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3720. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3721. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3722. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3723. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3724. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3725. wolfSSL_free(ssl);
  3726. wolfSSL_CTX_free(ctx);
  3727. ctx = NULL;
  3728. #endif
  3729. #ifndef NO_WOLFSSL_SERVER
  3730. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3731. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3732. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3733. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3734. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3735. wolfSSL_CTX_free(ctx);
  3736. #endif
  3737. #endif
  3738. return EXPECT_RESULT();
  3739. }
  3740. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3741. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  3742. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3743. {
  3744. EXPECT_DECLS;
  3745. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3746. return EXPECT_RESULT();
  3747. }
  3748. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3749. {
  3750. EXPECT_DECLS;
  3751. /* Set TLS 1.2 */
  3752. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3753. WOLFSSL_SUCCESS);
  3754. return EXPECT_RESULT();
  3755. }
  3756. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3757. * what was set at ctx creation. */
  3758. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3759. {
  3760. EXPECT_DECLS;
  3761. test_ssl_cbf client_cbs;
  3762. test_ssl_cbf server_cbs;
  3763. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3764. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3765. client_cbs.method = wolfTLS_client_method;
  3766. server_cbs.method = wolfTLS_server_method;
  3767. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3768. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3769. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3770. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  3771. &server_cbs, NULL), TEST_SUCCESS);
  3772. return EXPECT_RESULT();
  3773. }
  3774. #else
  3775. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3776. {
  3777. return TEST_SKIPPED;
  3778. }
  3779. #endif
  3780. /*----------------------------------------------------------------------------*
  3781. | SSL
  3782. *----------------------------------------------------------------------------*/
  3783. static int test_server_wolfSSL_new(void)
  3784. {
  3785. EXPECT_DECLS;
  3786. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3787. !defined(NO_WOLFSSL_SERVER)
  3788. WOLFSSL_CTX *ctx = NULL;
  3789. WOLFSSL_CTX *ctx_nocert = NULL;
  3790. WOLFSSL *ssl = NULL;
  3791. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3792. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3793. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3794. WOLFSSL_FILETYPE_PEM));
  3795. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3796. WOLFSSL_FILETYPE_PEM));
  3797. /* invalid context */
  3798. ExpectNull(ssl = wolfSSL_new(NULL));
  3799. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  3800. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  3801. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  3802. #endif
  3803. /* success */
  3804. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3805. wolfSSL_free(ssl);
  3806. wolfSSL_CTX_free(ctx);
  3807. wolfSSL_CTX_free(ctx_nocert);
  3808. #endif
  3809. return EXPECT_RESULT();
  3810. }
  3811. static int test_client_wolfSSL_new(void)
  3812. {
  3813. EXPECT_DECLS;
  3814. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3815. !defined(NO_WOLFSSL_CLIENT)
  3816. WOLFSSL_CTX *ctx = NULL;
  3817. WOLFSSL_CTX *ctx_nocert = NULL;
  3818. WOLFSSL *ssl = NULL;
  3819. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3821. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3822. /* invalid context */
  3823. ExpectNull(ssl = wolfSSL_new(NULL));
  3824. /* success */
  3825. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  3826. wolfSSL_free(ssl);
  3827. ssl = NULL;
  3828. /* success */
  3829. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3830. wolfSSL_free(ssl);
  3831. wolfSSL_CTX_free(ctx);
  3832. wolfSSL_CTX_free(ctx_nocert);
  3833. #endif
  3834. return EXPECT_RESULT();
  3835. }
  3836. static int test_wolfSSL_SetTmpDH_file(void)
  3837. {
  3838. EXPECT_DECLS;
  3839. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3840. !defined(NO_WOLFSSL_SERVER)
  3841. WOLFSSL_CTX *ctx = NULL;
  3842. WOLFSSL *ssl = NULL;
  3843. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3844. #ifndef NO_RSA
  3845. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3846. WOLFSSL_FILETYPE_PEM));
  3847. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3848. WOLFSSL_FILETYPE_PEM));
  3849. #elif defined(HAVE_ECC)
  3850. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3851. WOLFSSL_FILETYPE_PEM));
  3852. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3853. WOLFSSL_FILETYPE_PEM));
  3854. #elif defined(HAVE_ED25519)
  3855. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3856. WOLFSSL_FILETYPE_PEM));
  3857. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3858. WOLFSSL_FILETYPE_PEM));
  3859. #elif defined(HAVE_ED448)
  3860. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3861. WOLFSSL_FILETYPE_PEM));
  3862. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3863. WOLFSSL_FILETYPE_PEM));
  3864. #endif
  3865. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3866. /* invalid ssl */
  3867. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3868. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3869. /* invalid dhParamFile file */
  3870. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3871. NULL, WOLFSSL_FILETYPE_PEM));
  3872. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3873. bogusFile, WOLFSSL_FILETYPE_PEM));
  3874. /* success */
  3875. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3876. WOLFSSL_FILETYPE_PEM));
  3877. wolfSSL_free(ssl);
  3878. wolfSSL_CTX_free(ctx);
  3879. #endif
  3880. return EXPECT_RESULT();
  3881. }
  3882. static int test_wolfSSL_SetTmpDH_buffer(void)
  3883. {
  3884. EXPECT_DECLS;
  3885. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3886. WOLFSSL_CTX *ctx = NULL;
  3887. WOLFSSL *ssl = NULL;
  3888. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3889. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3890. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3891. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3892. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3893. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3894. /* invalid ssl */
  3895. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3896. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3897. /* invalid dhParamFile file */
  3898. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3899. 0, WOLFSSL_FILETYPE_ASN1));
  3900. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3901. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3902. /* success */
  3903. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3904. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3905. wolfSSL_free(ssl);
  3906. wolfSSL_CTX_free(ctx);
  3907. #endif
  3908. return EXPECT_RESULT();
  3909. }
  3910. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3911. {
  3912. EXPECT_DECLS;
  3913. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3914. WOLFSSL_CTX *ctx = NULL;
  3915. WOLFSSL_CTX *ctx2 = NULL;
  3916. WOLFSSL *ssl = NULL;
  3917. WOLFSSL *ssl2 = NULL;
  3918. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3919. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3920. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3921. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3922. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3923. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3924. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3925. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3926. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3927. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3928. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3929. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3930. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3931. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  3932. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3933. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3934. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3935. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3936. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3937. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3938. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3939. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3940. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3941. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3942. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3943. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3944. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3945. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3946. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3947. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3948. wolfSSL_free(ssl2);
  3949. wolfSSL_CTX_free(ctx2);
  3950. wolfSSL_free(ssl);
  3951. wolfSSL_CTX_free(ctx);
  3952. #endif
  3953. return EXPECT_RESULT();
  3954. }
  3955. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3956. * allowed.
  3957. * POST: return 1 on success.
  3958. */
  3959. static int test_wolfSSL_SetMinVersion(void)
  3960. {
  3961. int res = TEST_SKIPPED;
  3962. #ifndef NO_WOLFSSL_CLIENT
  3963. int failFlag = WOLFSSL_SUCCESS;
  3964. WOLFSSL_CTX* ctx = NULL;
  3965. WOLFSSL* ssl = NULL;
  3966. int itr;
  3967. #ifndef NO_OLD_TLS
  3968. const int versions[] = {
  3969. #ifdef WOLFSSL_ALLOW_TLSV10
  3970. WOLFSSL_TLSV1,
  3971. #endif
  3972. WOLFSSL_TLSV1_1,
  3973. WOLFSSL_TLSV1_2};
  3974. #elif !defined(WOLFSSL_NO_TLS12)
  3975. const int versions[] = { WOLFSSL_TLSV1_2 };
  3976. #else
  3977. const int versions[] = { WOLFSSL_TLSV1_3 };
  3978. #endif
  3979. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3980. ssl = wolfSSL_new(ctx);
  3981. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3982. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3983. failFlag = WOLFSSL_FAILURE;
  3984. }
  3985. }
  3986. wolfSSL_free(ssl);
  3987. wolfSSL_CTX_free(ctx);
  3988. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3989. #endif
  3990. return res;
  3991. } /* END test_wolfSSL_SetMinVersion */
  3992. #ifdef OPENSSL_EXTRA
  3993. static int test_ED25519(void)
  3994. {
  3995. EXPECT_DECLS;
  3996. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3997. defined(WOLFSSL_KEY_GEN)
  3998. byte priv[ED25519_PRV_KEY_SIZE];
  3999. unsigned int privSz = (unsigned int)sizeof(priv);
  4000. byte pub[ED25519_PUB_KEY_SIZE];
  4001. unsigned int pubSz = (unsigned int)sizeof(pub);
  4002. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4003. const char* msg = TEST_STRING;
  4004. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4005. byte sig[ED25519_SIG_SIZE];
  4006. unsigned int sigSz = (unsigned int)sizeof(sig);
  4007. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4008. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4009. WOLFSSL_SUCCESS);
  4010. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4011. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4012. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4013. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4014. &sigSz), WOLFSSL_SUCCESS);
  4015. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4016. #ifdef HAVE_ED25519_VERIFY
  4017. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4018. sigSz), WOLFSSL_SUCCESS);
  4019. #endif /* HAVE_ED25519_VERIFY */
  4020. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4021. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4022. return EXPECT_RESULT();
  4023. }
  4024. static int test_ED448(void)
  4025. {
  4026. EXPECT_DECLS;
  4027. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4028. defined(WOLFSSL_KEY_GEN)
  4029. byte priv[ED448_PRV_KEY_SIZE];
  4030. unsigned int privSz = (unsigned int)sizeof(priv);
  4031. byte pub[ED448_PUB_KEY_SIZE];
  4032. unsigned int pubSz = (unsigned int)sizeof(pub);
  4033. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4034. const char* msg = TEST_STRING;
  4035. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4036. byte sig[ED448_SIG_SIZE];
  4037. unsigned int sigSz = (unsigned int)sizeof(sig);
  4038. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4039. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  4040. WOLFSSL_SUCCESS);
  4041. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4042. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4043. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4044. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4045. &sigSz), WOLFSSL_SUCCESS);
  4046. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4047. #ifdef HAVE_ED448_VERIFY
  4048. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4049. sigSz), WOLFSSL_SUCCESS);
  4050. #endif /* HAVE_ED448_VERIFY */
  4051. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4052. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4053. return EXPECT_RESULT();
  4054. }
  4055. #endif /* OPENSSL_EXTRA */
  4056. #include <wolfssl/openssl/pem.h>
  4057. /*----------------------------------------------------------------------------*
  4058. | EVP
  4059. *----------------------------------------------------------------------------*/
  4060. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4061. {
  4062. EXPECT_DECLS;
  4063. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4064. WOLFSSL_BIO* rbio = NULL;
  4065. WOLFSSL_BIO* wbio = NULL;
  4066. WOLFSSL_EVP_PKEY* pkey = NULL;
  4067. char line[256] = { 0 };
  4068. char line1[256] = { 0 };
  4069. int i;
  4070. /* test error cases */
  4071. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4072. /*
  4073. * test RSA public key print
  4074. * in this test, pass '3' for indent
  4075. */
  4076. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4077. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4078. sizeof_client_keypub_der_1024));
  4079. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4080. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4081. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4082. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4083. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4084. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4085. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4086. strcpy(line1, " Modulus:\n");
  4087. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4088. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4089. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4090. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4091. /* skip to the end of modulus element*/
  4092. for (i = 0; i < 8 ;i++) {
  4093. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4094. }
  4095. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4096. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4097. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4098. /* should reach EOF */
  4099. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4100. EVP_PKEY_free(pkey);
  4101. pkey = NULL;
  4102. BIO_free(rbio);
  4103. BIO_free(wbio);
  4104. rbio = NULL;
  4105. wbio = NULL;
  4106. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4107. /*
  4108. * test DSA public key print
  4109. */
  4110. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4111. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4112. sizeof_dsa_pub_key_der_2048));
  4113. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4114. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4115. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4116. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4117. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4118. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4119. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4120. strcpy(line1, "pub:\n");
  4121. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4122. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4123. strcpy(line1,
  4124. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4125. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4126. /* skip to the end of pub element*/
  4127. for (i = 0; i < 17 ;i++) {
  4128. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4129. }
  4130. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4131. strcpy(line1, "P:\n");
  4132. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4133. /* skip to the end of P element*/
  4134. for (i = 0; i < 18 ;i++) {
  4135. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4136. }
  4137. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4138. strcpy(line1, "Q:\n");
  4139. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4140. /* skip to the end of Q element*/
  4141. for (i = 0; i < 3 ;i++) {
  4142. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4143. }
  4144. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4145. strcpy(line1, "G:\n");
  4146. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4147. /* skip to the end of G element*/
  4148. for (i = 0; i < 18 ;i++) {
  4149. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4150. }
  4151. /* should reach EOF */
  4152. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4153. EVP_PKEY_free(pkey);
  4154. pkey = NULL;
  4155. BIO_free(rbio);
  4156. BIO_free(wbio);
  4157. rbio = NULL;
  4158. wbio = NULL;
  4159. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4160. /*
  4161. * test ECC public key print
  4162. */
  4163. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4164. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4165. sizeof_ecc_clikeypub_der_256));
  4166. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4167. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4168. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4169. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4170. strcpy(line1, "Public-Key: (256 bit)\n");
  4171. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4172. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4173. strcpy(line1, "pub:\n");
  4174. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4175. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4176. strcpy(line1,
  4177. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4178. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4179. /* skip to the end of pub element*/
  4180. for (i = 0; i < 4 ;i++) {
  4181. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4182. }
  4183. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4184. strcpy(line1, "ASN1 OID: prime256v1\n");
  4185. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4186. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4187. strcpy(line1, "NIST CURVE: P-256\n");
  4188. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4189. /* should reach EOF */
  4190. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4191. EVP_PKEY_free(pkey);
  4192. pkey = NULL;
  4193. BIO_free(rbio);
  4194. BIO_free(wbio);
  4195. rbio = NULL;
  4196. wbio = NULL;
  4197. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4198. /*
  4199. * test DH public key print
  4200. */
  4201. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4202. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4203. sizeof_dh_pub_key_der_2048));
  4204. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4205. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4206. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4207. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4208. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4209. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4210. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4211. strcpy(line1, "public-key:\n");
  4212. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4213. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4214. strcpy(line1,
  4215. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4216. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4217. /* skip to the end of public-key element*/
  4218. for (i = 0; i < 17 ;i++) {
  4219. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4220. }
  4221. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4222. strcpy(line1, "prime:\n");
  4223. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4224. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4225. strcpy(line1,
  4226. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4227. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4228. /* skip to the end of prime element*/
  4229. for (i = 0; i < 17 ;i++) {
  4230. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4231. }
  4232. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4233. strcpy(line1, "generator: 2 (0x02)\n");
  4234. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4235. /* should reach EOF */
  4236. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4237. EVP_PKEY_free(pkey);
  4238. pkey = NULL;
  4239. BIO_free(rbio);
  4240. BIO_free(wbio);
  4241. rbio = NULL;
  4242. wbio = NULL;
  4243. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4244. /* to prevent "unused variable" warning */
  4245. (void)pkey;
  4246. (void)wbio;
  4247. (void)rbio;
  4248. (void)line;
  4249. (void)line1;
  4250. (void)i;
  4251. #endif /* OPENSSL_EXTRA */
  4252. return EXPECT_RESULT();
  4253. }
  4254. /* Test functions for base64 encode/decode */
  4255. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4256. {
  4257. EXPECT_DECLS;
  4258. #if defined(OPENSSL_EXTRA) && \
  4259. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4260. EVP_ENCODE_CTX* ctx = NULL;
  4261. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4262. ExpectIntEQ(ctx->remaining,0);
  4263. ExpectIntEQ(ctx->data[0],0);
  4264. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4265. EVP_ENCODE_CTX_free(ctx);
  4266. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4267. return EXPECT_RESULT();
  4268. }
  4269. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4270. {
  4271. EXPECT_DECLS;
  4272. #if defined(OPENSSL_EXTRA) && \
  4273. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4274. EVP_ENCODE_CTX* ctx = NULL;
  4275. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4276. EVP_ENCODE_CTX_free(ctx);
  4277. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4278. return EXPECT_RESULT();
  4279. }
  4280. static int test_wolfSSL_EVP_EncodeInit(void)
  4281. {
  4282. EXPECT_DECLS;
  4283. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4284. EVP_ENCODE_CTX* ctx = NULL;
  4285. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4286. ExpectIntEQ(ctx->remaining, 0);
  4287. ExpectIntEQ(ctx->data[0], 0);
  4288. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4289. if (ctx != NULL) {
  4290. /* make ctx dirty */
  4291. ctx->remaining = 10;
  4292. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4293. }
  4294. EVP_EncodeInit(ctx);
  4295. ExpectIntEQ(ctx->remaining, 0);
  4296. ExpectIntEQ(ctx->data[0], 0);
  4297. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4298. EVP_ENCODE_CTX_free(ctx);
  4299. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4300. return EXPECT_RESULT();
  4301. }
  4302. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4303. {
  4304. EXPECT_DECLS;
  4305. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4306. int outl;
  4307. int total;
  4308. const unsigned char plain0[] = {"Th"};
  4309. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4310. const unsigned char plain2[] = {"This is additional data."};
  4311. const unsigned char encBlock0[] = {"VGg="};
  4312. const unsigned char enc0[] = {"VGg=\n"};
  4313. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4314. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4315. const unsigned char enc2[] =
  4316. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4317. unsigned char encOutBuff[300];
  4318. EVP_ENCODE_CTX* ctx = NULL;
  4319. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4320. EVP_EncodeInit(ctx);
  4321. /* illegal parameter test */
  4322. ExpectIntEQ(
  4323. EVP_EncodeUpdate(
  4324. NULL, /* pass NULL as ctx */
  4325. encOutBuff,
  4326. &outl,
  4327. plain1,
  4328. sizeof(plain1)-1),
  4329. 0 /* expected result code 0: fail */
  4330. );
  4331. ExpectIntEQ(
  4332. EVP_EncodeUpdate(
  4333. ctx,
  4334. NULL, /* pass NULL as out buff */
  4335. &outl,
  4336. plain1,
  4337. sizeof(plain1)-1),
  4338. 0 /* expected result code 0: fail */
  4339. );
  4340. ExpectIntEQ(
  4341. EVP_EncodeUpdate(
  4342. ctx,
  4343. encOutBuff,
  4344. NULL, /* pass NULL as outl */
  4345. plain1,
  4346. sizeof(plain1)-1),
  4347. 0 /* expected result code 0: fail */
  4348. );
  4349. ExpectIntEQ(
  4350. EVP_EncodeUpdate(
  4351. ctx,
  4352. encOutBuff,
  4353. &outl,
  4354. NULL, /* pass NULL as in */
  4355. sizeof(plain1)-1),
  4356. 0 /* expected result code 0: fail */
  4357. );
  4358. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4359. /* meaningless parameter test */
  4360. ExpectIntEQ(
  4361. EVP_EncodeUpdate(
  4362. ctx,
  4363. encOutBuff,
  4364. &outl,
  4365. plain1,
  4366. 0), /* pass zero input */
  4367. 1 /* expected result code 1: success */
  4368. );
  4369. /* very small data encoding test */
  4370. EVP_EncodeInit(ctx);
  4371. ExpectIntEQ(
  4372. EVP_EncodeUpdate(
  4373. ctx,
  4374. encOutBuff,
  4375. &outl,
  4376. plain0,
  4377. sizeof(plain0)-1),
  4378. 1 /* expected result code 1: success */
  4379. );
  4380. ExpectIntEQ(outl,0);
  4381. if (EXPECT_SUCCESS()) {
  4382. EVP_EncodeFinal(
  4383. ctx,
  4384. encOutBuff + outl,
  4385. &outl);
  4386. }
  4387. ExpectIntEQ( outl, sizeof(enc0)-1);
  4388. ExpectIntEQ(
  4389. XSTRNCMP(
  4390. (const char*)encOutBuff,
  4391. (const char*)enc0,sizeof(enc0) ),
  4392. 0);
  4393. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4394. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4395. sizeof(encBlock0)-1);
  4396. ExpectStrEQ(encOutBuff, encBlock0);
  4397. /* pass small size( < 48bytes ) input, then make sure they are not
  4398. * encoded and just stored in ctx
  4399. */
  4400. EVP_EncodeInit(ctx);
  4401. total = 0;
  4402. outl = 0;
  4403. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4404. ExpectIntEQ(
  4405. EVP_EncodeUpdate(
  4406. ctx,
  4407. encOutBuff, /* buffer for output */
  4408. &outl, /* size of output */
  4409. plain1, /* input */
  4410. sizeof(plain1)-1), /* size of input */
  4411. 1); /* expected result code 1:success */
  4412. total += outl;
  4413. ExpectIntEQ(outl, 0); /* no output expected */
  4414. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4415. ExpectTrue(
  4416. XSTRNCMP((const char*)(ctx->data),
  4417. (const char*)plain1,
  4418. ctx->remaining) ==0 );
  4419. ExpectTrue(encOutBuff[0] == 0);
  4420. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4421. * the stored data and the new input together
  4422. */
  4423. ExpectIntEQ(
  4424. EVP_EncodeUpdate(
  4425. ctx,
  4426. encOutBuff + outl, /* buffer for output */
  4427. &outl, /* size of output */
  4428. plain2, /* additional input */
  4429. sizeof(plain2) -1), /* size of additional input */
  4430. 1); /* expected result code 1:success */
  4431. total += outl;
  4432. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4433. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4434. ExpectIntEQ(
  4435. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4436. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4437. EVP_EncodeFinal(
  4438. ctx,
  4439. encOutBuff + outl,
  4440. &outl);
  4441. total += outl;
  4442. ExpectIntNE(total,0);
  4443. ExpectIntNE(outl,0);
  4444. ExpectIntEQ(XSTRNCMP(
  4445. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4446. /* test with illeagal parameters */
  4447. outl = 1;
  4448. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4449. ExpectIntEQ(outl, 0);
  4450. outl = 1;
  4451. EVP_EncodeFinal(ctx, NULL, &outl);
  4452. ExpectIntEQ(outl, 0);
  4453. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4454. EVP_EncodeFinal(NULL, NULL, NULL);
  4455. EVP_ENCODE_CTX_free(ctx);
  4456. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4457. return EXPECT_RESULT();
  4458. }
  4459. static int test_wolfSSL_EVP_EncodeFinal(void)
  4460. {
  4461. int res = TEST_SKIPPED;
  4462. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4463. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4464. * test_wolfSSL_EVP_EncodeUpdate
  4465. */
  4466. res = TEST_SUCCESS;
  4467. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4468. return res;
  4469. }
  4470. static int test_wolfSSL_EVP_DecodeInit(void)
  4471. {
  4472. EXPECT_DECLS;
  4473. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4474. EVP_ENCODE_CTX* ctx = NULL;
  4475. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4476. ExpectIntEQ( ctx->remaining,0);
  4477. ExpectIntEQ( ctx->data[0],0);
  4478. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4479. if (ctx != NULL) {
  4480. /* make ctx dirty */
  4481. ctx->remaining = 10;
  4482. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4483. }
  4484. EVP_DecodeInit(ctx);
  4485. ExpectIntEQ( ctx->remaining,0);
  4486. ExpectIntEQ( ctx->data[0],0);
  4487. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4488. EVP_ENCODE_CTX_free(ctx);
  4489. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4490. return EXPECT_RESULT();
  4491. }
  4492. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4493. {
  4494. EXPECT_DECLS;
  4495. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4496. int outl;
  4497. unsigned char decOutBuff[300];
  4498. EVP_ENCODE_CTX* ctx = NULL;
  4499. static const unsigned char enc1[] =
  4500. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4501. /* const unsigned char plain1[] =
  4502. {"This is a base64 decoding test."} */
  4503. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4504. EVP_DecodeInit(ctx);
  4505. /* illegal parameter tests */
  4506. /* pass NULL as ctx */
  4507. ExpectIntEQ(
  4508. EVP_DecodeUpdate(
  4509. NULL, /* pass NULL as ctx */
  4510. decOutBuff,
  4511. &outl,
  4512. enc1,
  4513. sizeof(enc1)-1),
  4514. -1 /* expected result code -1: fail */
  4515. );
  4516. ExpectIntEQ( outl, 0);
  4517. /* pass NULL as output */
  4518. ExpectIntEQ(
  4519. EVP_DecodeUpdate(
  4520. ctx,
  4521. NULL, /* pass NULL as out buff */
  4522. &outl,
  4523. enc1,
  4524. sizeof(enc1)-1),
  4525. -1 /* expected result code -1: fail */
  4526. );
  4527. ExpectIntEQ( outl, 0);
  4528. /* pass NULL as outl */
  4529. ExpectIntEQ(
  4530. EVP_DecodeUpdate(
  4531. ctx,
  4532. decOutBuff,
  4533. NULL, /* pass NULL as outl */
  4534. enc1,
  4535. sizeof(enc1)-1),
  4536. -1 /* expected result code -1: fail */
  4537. );
  4538. /* pass NULL as input */
  4539. ExpectIntEQ(
  4540. EVP_DecodeUpdate(
  4541. ctx,
  4542. decOutBuff,
  4543. &outl,
  4544. NULL, /* pass NULL as in */
  4545. sizeof(enc1)-1),
  4546. -1 /* expected result code -1: fail */
  4547. );
  4548. ExpectIntEQ( outl, 0);
  4549. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4550. /* pass zero length input */
  4551. ExpectIntEQ(
  4552. EVP_DecodeUpdate(
  4553. ctx,
  4554. decOutBuff,
  4555. &outl,
  4556. enc1,
  4557. 0), /* pass zero as input len */
  4558. 1 /* expected result code 1: success */
  4559. );
  4560. /* decode correct base64 string */
  4561. {
  4562. static const unsigned char enc2[] =
  4563. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4564. static const unsigned char plain2[] =
  4565. {"This is a base64 decoding test."};
  4566. EVP_EncodeInit(ctx);
  4567. ExpectIntEQ(
  4568. EVP_DecodeUpdate(
  4569. ctx,
  4570. decOutBuff,
  4571. &outl,
  4572. enc2,
  4573. sizeof(enc2)-1),
  4574. 0 /* expected result code 0: success */
  4575. );
  4576. ExpectIntEQ(outl,sizeof(plain2) -1);
  4577. ExpectIntEQ(
  4578. EVP_DecodeFinal(
  4579. ctx,
  4580. decOutBuff + outl,
  4581. &outl),
  4582. 1 /* expected result code 1: success */
  4583. );
  4584. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4585. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4586. sizeof(plain2) -1 ),0);
  4587. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4588. sizeof(plain2)-1);
  4589. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4590. sizeof(plain2) -1 ),0);
  4591. }
  4592. /* decode correct base64 string which does not have '\n' in its last*/
  4593. {
  4594. static const unsigned char enc3[] =
  4595. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4596. static const unsigned char plain3[] =
  4597. {"This is a base64 decoding test."}; /* 31 chars */
  4598. EVP_EncodeInit(ctx);
  4599. ExpectIntEQ(
  4600. EVP_DecodeUpdate(
  4601. ctx,
  4602. decOutBuff,
  4603. &outl,
  4604. enc3,
  4605. sizeof(enc3)-1),
  4606. 0 /* expected result code 0: success */
  4607. );
  4608. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4609. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4610. sizeof(plain3) -1 ),0);
  4611. ExpectIntEQ(
  4612. EVP_DecodeFinal(
  4613. ctx,
  4614. decOutBuff + outl,
  4615. &outl),
  4616. 1 /* expected result code 1: success */
  4617. );
  4618. ExpectIntEQ(outl,0 );
  4619. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4620. sizeof(plain3)-1);
  4621. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4622. sizeof(plain3) -1 ),0);
  4623. }
  4624. /* decode string which has a padding char ('=') in the illegal position*/
  4625. {
  4626. static const unsigned char enc4[] =
  4627. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4628. EVP_EncodeInit(ctx);
  4629. ExpectIntEQ(
  4630. EVP_DecodeUpdate(
  4631. ctx,
  4632. decOutBuff,
  4633. &outl,
  4634. enc4,
  4635. sizeof(enc4)-1),
  4636. -1 /* expected result code -1: error */
  4637. );
  4638. ExpectIntEQ(outl,0);
  4639. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4640. }
  4641. /* small data decode test */
  4642. {
  4643. static const unsigned char enc00[] = {"VG"};
  4644. static const unsigned char enc01[] = {"g=\n"};
  4645. static const unsigned char plain4[] = {"Th"};
  4646. EVP_EncodeInit(ctx);
  4647. ExpectIntEQ(
  4648. EVP_DecodeUpdate(
  4649. ctx,
  4650. decOutBuff,
  4651. &outl,
  4652. enc00,
  4653. sizeof(enc00)-1),
  4654. 1 /* expected result code 1: success */
  4655. );
  4656. ExpectIntEQ(outl,0);
  4657. ExpectIntEQ(
  4658. EVP_DecodeUpdate(
  4659. ctx,
  4660. decOutBuff + outl,
  4661. &outl,
  4662. enc01,
  4663. sizeof(enc01)-1),
  4664. 0 /* expected result code 0: success */
  4665. );
  4666. ExpectIntEQ(outl,sizeof(plain4)-1);
  4667. /* test with illegal parameters */
  4668. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4669. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4670. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4671. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4672. if (EXPECT_SUCCESS()) {
  4673. EVP_DecodeFinal(
  4674. ctx,
  4675. decOutBuff + outl,
  4676. &outl);
  4677. }
  4678. ExpectIntEQ( outl, 0);
  4679. ExpectIntEQ(
  4680. XSTRNCMP(
  4681. (const char*)decOutBuff,
  4682. (const char*)plain4,sizeof(plain4)-1 ),
  4683. 0);
  4684. }
  4685. EVP_ENCODE_CTX_free(ctx);
  4686. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4687. return EXPECT_RESULT();
  4688. }
  4689. static int test_wolfSSL_EVP_DecodeFinal(void)
  4690. {
  4691. int res = TEST_SKIPPED;
  4692. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4693. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4694. * test_wolfSSL_EVP_DecodeUpdate
  4695. */
  4696. res = TEST_SUCCESS;
  4697. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4698. return res;
  4699. }
  4700. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4701. */
  4702. #ifdef OPENSSL_EXTRA
  4703. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4704. {
  4705. EXPECT_DECLS;
  4706. #ifndef NO_AES
  4707. const WOLFSSL_EVP_CIPHER* c;
  4708. c = wolfSSL_EVP_get_cipherbynid(419);
  4709. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4710. defined(WOLFSSL_AES_128)
  4711. ExpectNotNull(c);
  4712. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  4713. #else
  4714. ExpectNull(c);
  4715. #endif
  4716. c = wolfSSL_EVP_get_cipherbynid(423);
  4717. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4718. defined(WOLFSSL_AES_192)
  4719. ExpectNotNull(c);
  4720. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  4721. #else
  4722. ExpectNull(c);
  4723. #endif
  4724. c = wolfSSL_EVP_get_cipherbynid(427);
  4725. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4726. defined(WOLFSSL_AES_256)
  4727. ExpectNotNull(c);
  4728. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  4729. #else
  4730. ExpectNull(c);
  4731. #endif
  4732. c = wolfSSL_EVP_get_cipherbynid(904);
  4733. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4734. ExpectNotNull(c);
  4735. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  4736. #else
  4737. ExpectNull(c);
  4738. #endif
  4739. c = wolfSSL_EVP_get_cipherbynid(905);
  4740. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4741. ExpectNotNull(c);
  4742. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  4743. #else
  4744. ExpectNull(c);
  4745. #endif
  4746. c = wolfSSL_EVP_get_cipherbynid(906);
  4747. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4748. ExpectNotNull(c);
  4749. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  4750. #else
  4751. ExpectNull(c);
  4752. #endif
  4753. c = wolfSSL_EVP_get_cipherbynid(418);
  4754. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4755. ExpectNotNull(c);
  4756. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  4757. #else
  4758. ExpectNull(c);
  4759. #endif
  4760. c = wolfSSL_EVP_get_cipherbynid(422);
  4761. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4762. ExpectNotNull(c);
  4763. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  4764. #else
  4765. ExpectNull(c);
  4766. #endif
  4767. c = wolfSSL_EVP_get_cipherbynid(426);
  4768. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4769. ExpectNotNull(c);
  4770. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  4771. #else
  4772. ExpectNull(c);
  4773. #endif
  4774. #endif /* !NO_AES */
  4775. #ifndef NO_DES3
  4776. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4777. #ifdef WOLFSSL_DES_ECB
  4778. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4779. #endif
  4780. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4781. #ifdef WOLFSSL_DES_ECB
  4782. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4783. #endif
  4784. #endif /* !NO_DES3 */
  4785. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4786. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4787. #endif
  4788. /* test for nid is out of range */
  4789. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  4790. return EXPECT_RESULT();
  4791. }
  4792. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4793. {
  4794. EXPECT_DECLS;
  4795. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4796. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4797. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4798. const EVP_CIPHER *test;
  4799. byte key[AES_BLOCK_SIZE] = {0};
  4800. byte iv[AES_BLOCK_SIZE] = {0};
  4801. ExpectNotNull(ctx);
  4802. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4803. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4804. test = EVP_CIPHER_CTX_cipher(ctx);
  4805. ExpectTrue(init == test);
  4806. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4807. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4808. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4809. EVP_CIPHER_CTX_free(ctx);
  4810. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4811. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4812. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4813. return EXPECT_RESULT();
  4814. }
  4815. #endif /* OPENSSL_EXTRA */
  4816. /*----------------------------------------------------------------------------*
  4817. | IO
  4818. *----------------------------------------------------------------------------*/
  4819. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  4820. defined(HAVE_IO_TESTS_DEPENDENCIES)
  4821. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4822. #ifdef WC_SHA512_DIGEST_SIZE
  4823. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4824. #else
  4825. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4826. #endif
  4827. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4828. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4829. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4830. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4831. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4832. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4833. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4834. defined(HAVE_AES_CBC)
  4835. typedef struct openssl_key_ctx {
  4836. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4837. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4838. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4839. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4840. } openssl_key_ctx;
  4841. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4842. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4843. static WC_INLINE int OpenSSLTicketInit(void)
  4844. {
  4845. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4846. if (ret != 0) return ret;
  4847. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4848. sizeof(myOpenSSLKey_ctx.name));
  4849. if (ret != 0) return ret;
  4850. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4851. sizeof(myOpenSSLKey_ctx.key));
  4852. if (ret != 0) return ret;
  4853. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4854. sizeof(myOpenSSLKey_ctx.hmacKey));
  4855. if (ret != 0) return ret;
  4856. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4857. sizeof(myOpenSSLKey_ctx.iv));
  4858. if (ret != 0) return ret;
  4859. return 0;
  4860. }
  4861. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4862. byte name[WOLFSSL_TICKET_NAME_SZ],
  4863. byte iv[WOLFSSL_TICKET_IV_SZ],
  4864. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4865. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4866. (void)ssl;
  4867. if (enc) {
  4868. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4869. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4870. }
  4871. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4872. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4873. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4874. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4875. return 0;
  4876. }
  4877. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4878. if (enc)
  4879. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4880. else
  4881. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4882. return 1;
  4883. }
  4884. static WC_INLINE void OpenSSLTicketCleanup(void)
  4885. {
  4886. wc_FreeRng(&myOpenSSLKey_rng);
  4887. }
  4888. #endif
  4889. #endif
  4890. /* helper functions */
  4891. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  4892. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4893. void *ctx)
  4894. {
  4895. struct test_ssl_memio_ctx *test_ctx;
  4896. byte *buf;
  4897. int *len;
  4898. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4899. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4900. buf = test_ctx->c_buff;
  4901. len = &test_ctx->c_len;
  4902. }
  4903. else {
  4904. buf = test_ctx->s_buff;
  4905. len = &test_ctx->s_len;
  4906. }
  4907. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  4908. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4909. XMEMCPY(buf + *len, data, sz);
  4910. *len += sz;
  4911. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  4912. {
  4913. /* This can be imported into Wireshark by transforming the file with
  4914. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  4915. * And then loading test_output.dump.hex into Wireshark using the
  4916. * "Import from Hex Dump..." option ion and selecting the TCP
  4917. * encapsulation option. */
  4918. char dump_file_name[64];
  4919. WOLFSSL_BIO *dump_file;
  4920. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  4921. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  4922. if (dump_file != NULL) {
  4923. (void)wolfSSL_BIO_write(dump_file, data, sz);
  4924. wolfSSL_BIO_free(dump_file);
  4925. }
  4926. }
  4927. #endif
  4928. return sz;
  4929. }
  4930. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4931. void *ctx)
  4932. {
  4933. struct test_ssl_memio_ctx *test_ctx;
  4934. int read_sz;
  4935. byte *buf;
  4936. int *len;
  4937. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4938. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4939. buf = test_ctx->s_buff;
  4940. len = &test_ctx->s_len;
  4941. }
  4942. else {
  4943. buf = test_ctx->c_buff;
  4944. len = &test_ctx->c_len;
  4945. }
  4946. if (*len == 0)
  4947. return WOLFSSL_CBIO_ERR_WANT_READ;
  4948. read_sz = sz < *len ? sz : *len;
  4949. XMEMCPY(data, buf, read_sz);
  4950. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4951. *len -= read_sz;
  4952. return read_sz;
  4953. }
  4954. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  4955. {
  4956. EXPECT_DECLS;
  4957. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4958. int c_sharedCtx = 0;
  4959. int s_sharedCtx = 0;
  4960. #endif
  4961. const char* certFile = svrCertFile;
  4962. const char* keyFile = svrKeyFile;
  4963. /********************************
  4964. * Create WOLFSSL_CTX for client.
  4965. ********************************/
  4966. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4967. if (ctx->c_ctx != NULL) {
  4968. c_sharedCtx = ctx->c_cb.isSharedCtx;
  4969. }
  4970. else
  4971. #endif
  4972. {
  4973. WOLFSSL_METHOD* method = NULL;
  4974. if (ctx->c_cb.method != NULL) {
  4975. method = ctx->c_cb.method();
  4976. }
  4977. else {
  4978. method = wolfSSLv23_client_method();
  4979. }
  4980. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  4981. }
  4982. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  4983. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  4984. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4985. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  4986. #endif
  4987. if (ctx->c_cb.caPemFile != NULL)
  4988. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  4989. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  4990. else
  4991. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  4992. caCertFile, 0), WOLFSSL_SUCCESS);
  4993. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4994. if (!c_sharedCtx)
  4995. #endif
  4996. {
  4997. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  4998. cliCertFile), WOLFSSL_SUCCESS);
  4999. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, cliKeyFile,
  5000. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5001. }
  5002. #ifdef HAVE_CRL
  5003. if (ctx->c_cb.crlPemFile != NULL) {
  5004. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5005. WOLFSSL_SUCCESS);
  5006. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5007. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5008. }
  5009. #endif
  5010. if (ctx->c_ciphers != NULL) {
  5011. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5012. WOLFSSL_SUCCESS);
  5013. }
  5014. if (ctx->c_cb.ctx_ready != NULL) {
  5015. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5016. }
  5017. /********************************
  5018. * Create WOLFSSL_CTX for server.
  5019. ********************************/
  5020. if (ctx->s_ctx != NULL) {
  5021. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5022. s_sharedCtx = 1;
  5023. #endif
  5024. ctx->s_cb.isSharedCtx = 1;
  5025. }
  5026. else
  5027. {
  5028. WOLFSSL_METHOD* method = NULL;
  5029. if (ctx->s_cb.method != NULL) {
  5030. method = ctx->s_cb.method();
  5031. }
  5032. else {
  5033. method = wolfSSLv23_server_method();
  5034. }
  5035. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5036. ctx->s_cb.isSharedCtx = 0;
  5037. }
  5038. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5039. #if defined(HAVE_SESSION_TICKET) && \
  5040. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5041. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5042. OpenSSLTicketInit();
  5043. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5044. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5045. TicketInit();
  5046. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5047. #endif
  5048. #endif
  5049. }
  5050. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5051. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5052. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5053. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5054. if (ctx->s_cb.caPemFile != NULL)
  5055. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5056. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5057. else
  5058. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5059. cliCertFile, 0), WOLFSSL_SUCCESS);
  5060. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5061. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5062. #endif
  5063. if (ctx->s_cb.certPemFile != NULL) {
  5064. certFile = ctx->s_cb.certPemFile;
  5065. }
  5066. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5067. if (!s_sharedCtx)
  5068. #endif
  5069. {
  5070. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5071. certFile), WOLFSSL_SUCCESS);
  5072. }
  5073. if (ctx->s_cb.keyPemFile != NULL) {
  5074. keyFile = ctx->s_cb.keyPemFile;
  5075. }
  5076. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5077. if (!s_sharedCtx)
  5078. #endif
  5079. {
  5080. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, keyFile,
  5081. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5082. }
  5083. if (ctx->s_ciphers != NULL) {
  5084. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5085. WOLFSSL_SUCCESS);
  5086. }
  5087. if (ctx->s_cb.ctx_ready != NULL) {
  5088. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5089. }
  5090. /****************************
  5091. * Create WOLFSSL for client.
  5092. ****************************/
  5093. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5094. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5095. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5096. if (0
  5097. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5098. || c_sharedCtx
  5099. #endif
  5100. )
  5101. {
  5102. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl, cliCertFile),
  5103. WOLFSSL_SUCCESS);
  5104. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, cliKeyFile,
  5105. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5106. }
  5107. if (ctx->c_cb.ssl_ready != NULL) {
  5108. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5109. }
  5110. /****************************
  5111. * Create WOLFSSL for server.
  5112. ****************************/
  5113. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5114. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5115. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5116. if (0
  5117. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5118. || s_sharedCtx
  5119. #endif
  5120. )
  5121. {
  5122. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl, certFile),
  5123. WOLFSSL_SUCCESS);
  5124. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, keyFile,
  5125. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5126. }
  5127. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5128. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5129. #elif !defined(NO_DH)
  5130. /* will repick suites with DHE, higher priority than PSK */
  5131. SetDH(ctx->s_ssl);
  5132. #endif
  5133. if (ctx->s_cb.ssl_ready != NULL) {
  5134. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5135. }
  5136. return EXPECT_RESULT();
  5137. }
  5138. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5139. int* rounds)
  5140. {
  5141. int handshake_complete = 0;
  5142. int hs_c = 0;
  5143. int hs_s = 0;
  5144. int failing_s = 0;
  5145. int failing_c = 0;
  5146. int ret;
  5147. int err;
  5148. if (rounds != NULL) {
  5149. *rounds = 0;
  5150. }
  5151. while ((!handshake_complete) && (max_rounds > 0)) {
  5152. if (!hs_c) {
  5153. wolfSSL_SetLoggingPrefix("client");
  5154. ret = wolfSSL_connect(ctx->c_ssl);
  5155. wolfSSL_SetLoggingPrefix(NULL);
  5156. if (ret == WOLFSSL_SUCCESS) {
  5157. hs_c = 1;
  5158. }
  5159. else {
  5160. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5161. if (err != WOLFSSL_ERROR_WANT_READ &&
  5162. err != WOLFSSL_ERROR_WANT_WRITE) {
  5163. failing_c = 1;
  5164. hs_c = 1;
  5165. if (failing_c && failing_s) {
  5166. break;
  5167. }
  5168. }
  5169. }
  5170. }
  5171. if (!hs_s) {
  5172. wolfSSL_SetLoggingPrefix("server");
  5173. ret = wolfSSL_accept(ctx->s_ssl);
  5174. wolfSSL_SetLoggingPrefix(NULL);
  5175. if (ret == WOLFSSL_SUCCESS) {
  5176. hs_s = 1;
  5177. }
  5178. else {
  5179. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5180. if (err != WOLFSSL_ERROR_WANT_READ &&
  5181. err != WOLFSSL_ERROR_WANT_WRITE) {
  5182. failing_s = 1;
  5183. hs_s = 1;
  5184. if (failing_c && failing_s) {
  5185. break;
  5186. }
  5187. }
  5188. }
  5189. }
  5190. handshake_complete = hs_c && hs_s;
  5191. max_rounds--;
  5192. if (rounds != NULL) {
  5193. *rounds += 1;
  5194. }
  5195. }
  5196. if (!handshake_complete) {
  5197. return TEST_FAIL;
  5198. }
  5199. return TEST_SUCCESS;
  5200. }
  5201. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5202. {
  5203. EXPECT_DECLS;
  5204. char input[1024];
  5205. int idx = 0;
  5206. const char* msg_c = "hello wolfssl!";
  5207. int msglen_c = (int)XSTRLEN(msg_c);
  5208. const char* msg_s = "I hear you fa shizzle!";
  5209. int msglen_s = (int)XSTRLEN(msg_s);
  5210. if (ctx->c_msg != NULL) {
  5211. msg_c = ctx->c_msg;
  5212. msglen_c = ctx->c_msglen;
  5213. }
  5214. if (ctx->s_msg != NULL) {
  5215. msg_s = ctx->s_msg;
  5216. msglen_s = ctx->s_msglen;
  5217. }
  5218. wolfSSL_SetLoggingPrefix("client");
  5219. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5220. wolfSSL_SetLoggingPrefix("server");
  5221. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5222. if (idx >= 0) {
  5223. input[idx] = '\0';
  5224. }
  5225. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5226. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5227. ctx->s_cb.return_code = EXPECT_RESULT();
  5228. wolfSSL_SetLoggingPrefix("client");
  5229. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5230. wolfSSL_SetLoggingPrefix(NULL);
  5231. if (idx >= 0) {
  5232. input[idx] = '\0';
  5233. }
  5234. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5235. ctx->c_cb.return_code = EXPECT_RESULT();
  5236. if (ctx->c_cb.on_result != NULL) {
  5237. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5238. }
  5239. if (ctx->s_cb.on_result != NULL) {
  5240. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5241. }
  5242. return EXPECT_RESULT();
  5243. }
  5244. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5245. {
  5246. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5247. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5248. if (ctx->c_cb.on_cleanup != NULL) {
  5249. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5250. }
  5251. if (ctx->s_cb.on_cleanup != NULL) {
  5252. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5253. }
  5254. wolfSSL_shutdown(ctx->s_ssl);
  5255. wolfSSL_shutdown(ctx->c_ssl);
  5256. wolfSSL_free(ctx->s_ssl);
  5257. wolfSSL_free(ctx->c_ssl);
  5258. if (!ctx->s_cb.isSharedCtx) {
  5259. wolfSSL_CTX_free(ctx->s_ctx);
  5260. ctx->s_ctx = NULL;
  5261. }
  5262. if (!ctx->c_cb.isSharedCtx) {
  5263. wolfSSL_CTX_free(ctx->c_ctx);
  5264. ctx->c_ctx = NULL;
  5265. }
  5266. if (!ctx->s_cb.ticNoInit) {
  5267. #if defined(HAVE_SESSION_TICKET) && \
  5268. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5269. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5270. OpenSSLTicketCleanup();
  5271. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5272. TicketCleanup();
  5273. #endif
  5274. #endif
  5275. }
  5276. }
  5277. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5278. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5279. {
  5280. EXPECT_DECLS;
  5281. struct test_ssl_memio_ctx test_ctx;
  5282. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5283. size_t msg_len;
  5284. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5285. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5286. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5287. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5288. test_ctx.c_ctx = client_cb->ctx;
  5289. test_ctx.s_ctx = server_cb->ctx;
  5290. test_ctx.c_cb.return_code = TEST_FAIL;
  5291. test_ctx.s_cb.return_code = TEST_FAIL;
  5292. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5293. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5294. if (client_on_handshake != NULL) {
  5295. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5296. TEST_SUCCESS);
  5297. }
  5298. if (client_cb->on_handshake != NULL) {
  5299. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  5300. TEST_SUCCESS);
  5301. }
  5302. if (server_cb->on_handshake != NULL) {
  5303. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  5304. TEST_SUCCESS);
  5305. }
  5306. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5307. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5308. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5309. MD_MAX_SIZE);
  5310. ExpectIntGE(msg_len, 0);
  5311. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5312. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5313. MD_MAX_SIZE);
  5314. ExpectIntGE(msg_len, 0);
  5315. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5316. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5317. test_ssl_memio_cleanup(&test_ctx);
  5318. client_cb->return_code = test_ctx.c_cb.return_code;
  5319. client_cb->last_err = test_ctx.c_cb.last_err;
  5320. server_cb->return_code = test_ctx.s_cb.return_code;
  5321. server_cb->last_err = test_ctx.s_cb.last_err;
  5322. return EXPECT_RESULT();
  5323. }
  5324. #endif
  5325. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5326. #ifdef WOLFSSL_SESSION_EXPORT
  5327. #ifdef WOLFSSL_DTLS
  5328. /* set up function for sending session information */
  5329. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5330. {
  5331. WOLFSSL_CTX* ctx = NULL;
  5332. WOLFSSL* ssl = NULL;
  5333. AssertNotNull(inSsl);
  5334. AssertNotNull(buf);
  5335. AssertIntNE(0, sz);
  5336. /* Set ctx to DTLS 1.2 */
  5337. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5338. AssertNotNull(ctx);
  5339. ssl = wolfSSL_new(ctx);
  5340. AssertNotNull(ssl);
  5341. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5342. wolfSSL_free(ssl);
  5343. wolfSSL_CTX_free(ctx);
  5344. (void)userCtx;
  5345. return 0;
  5346. }
  5347. #endif
  5348. /* returns negative value on fail and positive (including 0) on success */
  5349. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5350. {
  5351. int ret, err, loop_count, count, timeout = 10;
  5352. char msg[] = "I hear you fa shizzle!";
  5353. char input[1024];
  5354. loop_count = ((func_args*)args)->argc;
  5355. #ifdef WOLFSSL_ASYNC_CRYPT
  5356. err = 0; /* Reset error */
  5357. #endif
  5358. do {
  5359. #ifdef WOLFSSL_ASYNC_CRYPT
  5360. if (err == WC_PENDING_E) {
  5361. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5362. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5363. }
  5364. #endif
  5365. ret = wolfSSL_accept(ssl);
  5366. err = wolfSSL_get_error(ssl, 0);
  5367. if (err == WOLFSSL_ERROR_WANT_READ ||
  5368. err == WOLFSSL_ERROR_WANT_WRITE) {
  5369. int select_ret;
  5370. err = WC_PENDING_E;
  5371. select_ret = tcp_select(*sockfd, timeout);
  5372. if (select_ret == TEST_TIMEOUT) {
  5373. return WOLFSSL_FATAL_ERROR;
  5374. }
  5375. }
  5376. } while (err == WC_PENDING_E);
  5377. if (ret != WOLFSSL_SUCCESS) {
  5378. char buff[WOLFSSL_MAX_ERROR_SZ];
  5379. fprintf(stderr, "error = %d, %s\n", err,
  5380. wolfSSL_ERR_error_string(err, buff));
  5381. return ret;
  5382. }
  5383. for (count = 0; count < loop_count; count++) {
  5384. int select_ret;
  5385. select_ret = tcp_select(*sockfd, timeout);
  5386. if (select_ret == TEST_TIMEOUT) {
  5387. ret = WOLFSSL_FATAL_ERROR;
  5388. break;
  5389. }
  5390. do {
  5391. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5392. if (ret > 0) {
  5393. input[ret] = '\0';
  5394. fprintf(stderr, "Client message: %s\n", input);
  5395. }
  5396. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5397. do {
  5398. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5399. return WOLFSSL_FATAL_ERROR;
  5400. }
  5401. err = wolfSSL_get_error(ssl, ret);
  5402. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5403. }
  5404. return ret;
  5405. }
  5406. #endif /* WOLFSSL_SESSION_EXPORT */
  5407. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5408. {
  5409. SOCKET_T sockfd = 0;
  5410. SOCKET_T clientfd = 0;
  5411. word16 port;
  5412. callback_functions* cbf;
  5413. WOLFSSL_CTX* ctx = NULL;
  5414. WOLFSSL* ssl = NULL;
  5415. func_args* opts = (func_args*)args;
  5416. char msg[] = "I hear you fa shizzle!";
  5417. char input[1024];
  5418. int idx;
  5419. int ret, err = 0;
  5420. int sharedCtx = 0;
  5421. int doUdp = 0;
  5422. SOCKADDR_IN_T cliAddr;
  5423. socklen_t cliLen;
  5424. const char* certFile = svrCertFile;
  5425. const char* keyFile = svrKeyFile;
  5426. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5427. size_t msg_len = 0;
  5428. #endif
  5429. wolfSSL_SetLoggingPrefix("server");
  5430. #ifdef WOLFSSL_TIRTOS
  5431. fdOpenSession(Task_self());
  5432. #endif
  5433. opts->return_code = TEST_FAIL;
  5434. cbf = opts->callbacks;
  5435. if (cbf != NULL && cbf->ctx) {
  5436. ctx = cbf->ctx;
  5437. sharedCtx = 1;
  5438. }
  5439. else
  5440. {
  5441. WOLFSSL_METHOD* method = NULL;
  5442. if (cbf != NULL && cbf->method != NULL) {
  5443. method = cbf->method();
  5444. }
  5445. else {
  5446. method = wolfSSLv23_server_method();
  5447. }
  5448. ctx = wolfSSL_CTX_new(method);
  5449. }
  5450. if (ctx == NULL) {
  5451. /* Release the wait for TCP ready. */
  5452. signal_ready(opts->signal);
  5453. goto done;
  5454. }
  5455. if (cbf == NULL || !cbf->ticNoInit) {
  5456. #if defined(HAVE_SESSION_TICKET) && \
  5457. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5458. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5459. OpenSSLTicketInit();
  5460. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5461. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5462. TicketInit();
  5463. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5464. #endif
  5465. #endif
  5466. }
  5467. #if defined(USE_WINDOWS_API)
  5468. port = opts->signal->port;
  5469. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5470. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5471. /* Let tcp_listen assign port */
  5472. port = 0;
  5473. #else
  5474. /* Use default port */
  5475. port = wolfSSLPort;
  5476. #endif
  5477. if (cbf != NULL)
  5478. doUdp = cbf->doUdp;
  5479. /* do it here to detect failure */
  5480. tcp_accept(
  5481. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5482. if (doUdp) {
  5483. cliLen = sizeof(cliAddr);
  5484. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5485. (struct sockaddr*)&cliAddr, &cliLen);
  5486. AssertIntGT(idx, 0);
  5487. }
  5488. else {
  5489. CloseSocket(sockfd);
  5490. }
  5491. wolfSSL_CTX_set_verify(ctx,
  5492. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5493. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5494. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5495. #endif
  5496. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5497. != WOLFSSL_SUCCESS) {
  5498. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5499. goto done;
  5500. }
  5501. if (cbf != NULL && cbf->certPemFile != NULL)
  5502. certFile = cbf->certPemFile;
  5503. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5504. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5505. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5506. #else
  5507. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5508. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5509. #endif
  5510. /*err_sys("can't load server cert chain file, "
  5511. "Please run from wolfSSL home dir");*/
  5512. goto done;
  5513. }
  5514. if (cbf != NULL && cbf->keyPemFile != NULL)
  5515. keyFile = cbf->keyPemFile;
  5516. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5517. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5518. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5519. #else
  5520. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5521. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5522. #endif
  5523. /*err_sys("can't load server key file, "
  5524. "Please run from wolfSSL home dir");*/
  5525. goto done;
  5526. }
  5527. #ifdef HAVE_CRL
  5528. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5529. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5530. goto done;
  5531. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5532. != WOLFSSL_SUCCESS)
  5533. goto done;
  5534. }
  5535. #endif
  5536. /* call ctx setup callback */
  5537. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5538. cbf->ctx_ready(ctx);
  5539. }
  5540. ssl = wolfSSL_new(ctx);
  5541. if (ssl == NULL) {
  5542. goto done;
  5543. }
  5544. if (doUdp) {
  5545. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5546. if (err != WOLFSSL_SUCCESS)
  5547. goto done;
  5548. }
  5549. #ifdef WOLFSSL_SESSION_EXPORT
  5550. /* only add in more complex nonblocking case with session export tests */
  5551. if (args && opts->argc > 0) {
  5552. /* set as nonblock and time out for waiting on read/write */
  5553. tcp_set_nonblocking(&clientfd);
  5554. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5555. }
  5556. #endif
  5557. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5558. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5559. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5560. #else
  5561. if (wolfSSL_use_certificate_file(ssl, certFile,
  5562. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5563. #endif
  5564. /*err_sys("can't load server cert chain file, "
  5565. "Please run from wolfSSL home dir");*/
  5566. goto done;
  5567. }
  5568. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5569. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5570. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5571. #else
  5572. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5573. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5574. #endif
  5575. /*err_sys("can't load server key file, "
  5576. "Please run from wolfSSL home dir");*/
  5577. goto done;
  5578. }
  5579. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5580. /*err_sys("SSL_set_fd failed");*/
  5581. goto done;
  5582. }
  5583. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5584. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5585. #elif !defined(NO_DH)
  5586. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5587. #endif
  5588. /* call ssl setup callback */
  5589. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5590. cbf->ssl_ready(ssl);
  5591. }
  5592. #ifdef WOLFSSL_SESSION_EXPORT
  5593. /* only add in more complex nonblocking case with session export tests */
  5594. if (opts->argc > 0) {
  5595. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5596. if (ret >= 0) {
  5597. opts->return_code = TEST_SUCCESS;
  5598. }
  5599. #ifdef WOLFSSL_TIRTOS
  5600. Task_yield();
  5601. #endif
  5602. goto done;
  5603. }
  5604. #endif
  5605. #ifdef WOLFSSL_ASYNC_CRYPT
  5606. err = 0; /* Reset error */
  5607. #endif
  5608. do {
  5609. #ifdef WOLFSSL_ASYNC_CRYPT
  5610. if (err == WC_PENDING_E) {
  5611. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5612. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5613. }
  5614. #endif
  5615. ret = wolfSSL_negotiate(ssl);
  5616. err = wolfSSL_get_error(ssl, 0);
  5617. } while (err == WC_PENDING_E);
  5618. if (ret != WOLFSSL_SUCCESS) {
  5619. char buff[WOLFSSL_MAX_ERROR_SZ];
  5620. fprintf(stderr, "error = %d, %s\n", err,
  5621. wolfSSL_ERR_error_string(err, buff));
  5622. /*err_sys("SSL_accept failed");*/
  5623. goto done;
  5624. }
  5625. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5626. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5627. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5628. AssertIntGE(msg_len, 0);
  5629. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5630. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5631. AssertIntGE(msg_len, 0);
  5632. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5633. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5634. if (idx > 0) {
  5635. input[idx] = '\0';
  5636. fprintf(stderr, "Client message: %s\n", input);
  5637. }
  5638. else if (idx < 0) {
  5639. goto done;
  5640. }
  5641. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5642. /*err_sys("SSL_write failed");*/
  5643. goto done;
  5644. }
  5645. if (cbf != NULL && cbf->on_result != NULL)
  5646. cbf->on_result(ssl);
  5647. #ifdef WOLFSSL_TIRTOS
  5648. Task_yield();
  5649. #endif
  5650. opts->return_code = TEST_SUCCESS;
  5651. done:
  5652. if (cbf != NULL)
  5653. cbf->last_err = err;
  5654. if (cbf != NULL && cbf->on_cleanup != NULL)
  5655. cbf->on_cleanup(ssl);
  5656. wolfSSL_shutdown(ssl);
  5657. wolfSSL_free(ssl);
  5658. if (!sharedCtx)
  5659. wolfSSL_CTX_free(ctx);
  5660. CloseSocket(clientfd);
  5661. #ifdef WOLFSSL_TIRTOS
  5662. fdCloseSession(Task_self());
  5663. #endif
  5664. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5665. && defined(HAVE_THREAD_LS)
  5666. wc_ecc_fp_free(); /* free per thread cache */
  5667. #endif
  5668. if (cbf == NULL || !cbf->ticNoInit) {
  5669. #if defined(HAVE_SESSION_TICKET) && \
  5670. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5671. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5672. OpenSSLTicketCleanup();
  5673. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5674. TicketCleanup();
  5675. #endif
  5676. #endif
  5677. }
  5678. wolfSSL_SetLoggingPrefix(NULL);
  5679. WOLFSSL_RETURN_FROM_THREAD(0);
  5680. }
  5681. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5682. !defined(WOLFSSL_NO_TLS12)
  5683. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5684. {
  5685. SOCKET_T sockfd = 0;
  5686. SOCKET_T clientfd = 0;
  5687. word16 port;
  5688. callback_functions* cbf;
  5689. WOLFSSL_CTX* ctx = 0;
  5690. WOLFSSL* ssl = 0;
  5691. char msg[] = "I hear you fa shizzle!";
  5692. char input[1024];
  5693. int idx;
  5694. int ret, err = 0;
  5695. int sharedCtx = 0;
  5696. func_args* opts = (func_args*)args;
  5697. int loop_count = opts->argc;
  5698. int count = 0;
  5699. #ifdef WOLFSSL_TIRTOS
  5700. fdOpenSession(Task_self());
  5701. #endif
  5702. opts->return_code = TEST_FAIL;
  5703. cbf = opts->callbacks;
  5704. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5705. if (cbf != NULL && cbf->ctx) {
  5706. ctx = cbf->ctx;
  5707. sharedCtx = 1;
  5708. }
  5709. else
  5710. #endif
  5711. {
  5712. WOLFSSL_METHOD* method = NULL;
  5713. if (cbf != NULL && cbf->method != NULL) {
  5714. method = cbf->method();
  5715. }
  5716. else {
  5717. method = wolfSSLv23_server_method();
  5718. }
  5719. ctx = wolfSSL_CTX_new(method);
  5720. }
  5721. #if defined(USE_WINDOWS_API)
  5722. port = opts->signal->port;
  5723. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5724. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5725. /* Let tcp_listen assign port */
  5726. port = 0;
  5727. #else
  5728. /* Use default port */
  5729. port = wolfSSLPort;
  5730. #endif
  5731. wolfSSL_CTX_set_verify(ctx,
  5732. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5733. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5734. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5735. #endif
  5736. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5737. != WOLFSSL_SUCCESS) {
  5738. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5739. /* Release the wait for TCP ready. */
  5740. signal_ready(opts->signal);
  5741. goto done;
  5742. }
  5743. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  5744. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5745. /*err_sys("can't load server cert chain file, "
  5746. "Please run from wolfSSL home dir");*/
  5747. /* Release the wait for TCP ready. */
  5748. signal_ready(opts->signal);
  5749. goto done;
  5750. }
  5751. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5752. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5753. /*err_sys("can't load server key file, "
  5754. "Please run from wolfSSL home dir");*/
  5755. /* Release the wait for TCP ready. */
  5756. signal_ready(opts->signal);
  5757. goto done;
  5758. }
  5759. /* call ctx setup callback */
  5760. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5761. cbf->ctx_ready(ctx);
  5762. }
  5763. while (count != loop_count) {
  5764. ssl = wolfSSL_new(ctx);
  5765. if (ssl == NULL) {
  5766. signal_ready(opts->signal);
  5767. goto done;
  5768. }
  5769. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5770. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5771. /*err_sys("can't load server cert chain file, "
  5772. "Please run from wolfSSL home dir");*/
  5773. /* Release the wait for TCP ready. */
  5774. signal_ready(opts->signal);
  5775. goto done;
  5776. }
  5777. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5778. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5779. /*err_sys("can't load server key file, "
  5780. "Please run from wolfSSL home dir");*/
  5781. /* Release the wait for TCP ready. */
  5782. signal_ready(opts->signal);
  5783. goto done;
  5784. }
  5785. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5786. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5787. #elif !defined(NO_DH)
  5788. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5789. #endif
  5790. /* call ssl setup callback */
  5791. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5792. cbf->ssl_ready(ssl);
  5793. }
  5794. /* do it here to detect failure */
  5795. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  5796. 0);
  5797. CloseSocket(sockfd);
  5798. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5799. /*err_sys("SSL_set_fd failed");*/
  5800. goto done;
  5801. }
  5802. #ifdef WOLFSSL_ASYNC_CRYPT
  5803. err = 0; /* Reset error */
  5804. #endif
  5805. do {
  5806. #ifdef WOLFSSL_ASYNC_CRYPT
  5807. if (err == WC_PENDING_E) {
  5808. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5809. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5810. }
  5811. #endif
  5812. ret = wolfSSL_accept(ssl);
  5813. err = wolfSSL_get_error(ssl, 0);
  5814. } while (err == WC_PENDING_E);
  5815. if (ret != WOLFSSL_SUCCESS) {
  5816. char buff[WOLFSSL_MAX_ERROR_SZ];
  5817. fprintf(stderr, "error = %d, %s\n", err,
  5818. wolfSSL_ERR_error_string(err, buff));
  5819. /*err_sys("SSL_accept failed");*/
  5820. goto done;
  5821. }
  5822. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5823. if (idx > 0) {
  5824. input[idx] = '\0';
  5825. fprintf(stderr, "Client message: %s\n", input);
  5826. }
  5827. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5828. /*err_sys("SSL_write failed");*/
  5829. goto done;
  5830. }
  5831. /* free ssl for this connection */
  5832. wolfSSL_shutdown(ssl);
  5833. wolfSSL_free(ssl); ssl = NULL;
  5834. CloseSocket(clientfd);
  5835. count++;
  5836. }
  5837. #ifdef WOLFSSL_TIRTOS
  5838. Task_yield();
  5839. #endif
  5840. opts->return_code = TEST_SUCCESS;
  5841. done:
  5842. if (ssl != NULL) {
  5843. wolfSSL_shutdown(ssl);
  5844. wolfSSL_free(ssl);
  5845. }
  5846. if (!sharedCtx)
  5847. wolfSSL_CTX_free(ctx);
  5848. CloseSocket(clientfd);
  5849. #ifdef WOLFSSL_TIRTOS
  5850. fdCloseSession(Task_self());
  5851. #endif
  5852. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5853. && defined(HAVE_THREAD_LS)
  5854. wc_ecc_fp_free(); /* free per thread cache */
  5855. #endif
  5856. WOLFSSL_RETURN_FROM_THREAD(0);
  5857. }
  5858. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5859. static int test_client_nofail(void* args, cbType cb)
  5860. {
  5861. #if !defined(NO_WOLFSSL_CLIENT)
  5862. SOCKET_T sockfd = 0;
  5863. callback_functions* cbf;
  5864. WOLFSSL_CTX* ctx = 0;
  5865. WOLFSSL* ssl = 0;
  5866. WOLFSSL_CIPHER* cipher;
  5867. char msg[64] = "hello wolfssl!";
  5868. char reply[1024];
  5869. int input;
  5870. int msgSz = (int)XSTRLEN(msg);
  5871. int ret, err = 0;
  5872. int cipherSuite;
  5873. int sharedCtx = 0;
  5874. int doUdp = 0;
  5875. const char* cipherName1, *cipherName2;
  5876. wolfSSL_SetLoggingPrefix("client");
  5877. #ifdef WOLFSSL_TIRTOS
  5878. fdOpenSession(Task_self());
  5879. #endif
  5880. ((func_args*)args)->return_code = TEST_FAIL;
  5881. cbf = ((func_args*)args)->callbacks;
  5882. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5883. if (cbf != NULL && cbf->ctx) {
  5884. ctx = cbf->ctx;
  5885. sharedCtx = cbf->isSharedCtx;
  5886. }
  5887. else
  5888. #endif
  5889. {
  5890. WOLFSSL_METHOD* method = NULL;
  5891. if (cbf != NULL && cbf->method != NULL) {
  5892. method = cbf->method();
  5893. }
  5894. else {
  5895. method = wolfSSLv23_client_method();
  5896. }
  5897. ctx = wolfSSL_CTX_new(method);
  5898. }
  5899. if (cbf != NULL)
  5900. doUdp = cbf->doUdp;
  5901. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5902. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5903. #endif
  5904. /* Do connect here so server detects failures */
  5905. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5906. doUdp, 0, NULL);
  5907. /* Connect the socket so that we don't have to set the peer later on */
  5908. if (doUdp)
  5909. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5910. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5911. {
  5912. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5913. goto done;
  5914. }
  5915. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5916. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5917. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5918. #else
  5919. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5920. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5921. #endif
  5922. /*err_sys("can't load client cert file, "
  5923. "Please run from wolfSSL home dir");*/
  5924. goto done;
  5925. }
  5926. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5927. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5928. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5929. #else
  5930. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5931. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5932. #endif
  5933. /*err_sys("can't load client key file, "
  5934. "Please run from wolfSSL home dir");*/
  5935. goto done;
  5936. }
  5937. #ifdef HAVE_CRL
  5938. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5939. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5940. goto done;
  5941. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5942. != WOLFSSL_SUCCESS)
  5943. goto done;
  5944. }
  5945. #endif
  5946. /* call ctx setup callback */
  5947. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5948. cbf->ctx_ready(ctx);
  5949. }
  5950. ssl = wolfSSL_new(ctx);
  5951. if (ssl == NULL) {
  5952. goto done;
  5953. }
  5954. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5955. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5956. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5957. #else
  5958. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5959. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5960. #endif
  5961. /*err_sys("can't load client cert file, "
  5962. "Please run from wolfSSL home dir");*/
  5963. goto done;
  5964. }
  5965. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5966. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5967. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5968. #else
  5969. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5970. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5971. #endif
  5972. /*err_sys("can't load client key file, "
  5973. "Please run from wolfSSL home dir");*/
  5974. goto done;
  5975. }
  5976. if (!doUdp) {
  5977. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5978. /*err_sys("SSL_set_fd failed");*/
  5979. goto done;
  5980. }
  5981. }
  5982. else {
  5983. #ifdef WOLFSSL_DTLS
  5984. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5985. /*err_sys("SSL_set_fd failed");*/
  5986. goto done;
  5987. }
  5988. #else
  5989. goto done;
  5990. #endif
  5991. }
  5992. /* call ssl setup callback */
  5993. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5994. cbf->ssl_ready(ssl);
  5995. }
  5996. #ifdef WOLFSSL_ASYNC_CRYPT
  5997. err = 0; /* Reset error */
  5998. #endif
  5999. do {
  6000. #ifdef WOLFSSL_ASYNC_CRYPT
  6001. if (err == WC_PENDING_E) {
  6002. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6003. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6004. }
  6005. #endif
  6006. ret = wolfSSL_negotiate(ssl);
  6007. err = wolfSSL_get_error(ssl, 0);
  6008. } while (err == WC_PENDING_E);
  6009. if (ret != WOLFSSL_SUCCESS) {
  6010. char buff[WOLFSSL_MAX_ERROR_SZ];
  6011. fprintf(stderr, "error = %d, %s\n", err,
  6012. wolfSSL_ERR_error_string(err, buff));
  6013. /*err_sys("SSL_connect failed");*/
  6014. goto done;
  6015. }
  6016. /* test the various get cipher methods */
  6017. /* Internal cipher suite names */
  6018. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6019. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6020. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6021. (cipherSuite >> 8), cipherSuite & 0xFF);
  6022. AssertStrEQ(cipherName1, cipherName2);
  6023. /* IANA Cipher Suites Names */
  6024. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6025. then it's the internal cipher suite name */
  6026. cipher = wolfSSL_get_current_cipher(ssl);
  6027. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6028. cipherName2 = wolfSSL_get_cipher(ssl);
  6029. AssertStrEQ(cipherName1, cipherName2);
  6030. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6031. !defined(WOLFSSL_QT)
  6032. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6033. (cipherSuite >> 8), cipherSuite & 0xFF);
  6034. AssertStrEQ(cipherName1, cipherName2);
  6035. #endif
  6036. if (cb != NULL)
  6037. (cb)(ctx, ssl);
  6038. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6039. /*err_sys("SSL_write failed");*/
  6040. goto done;
  6041. }
  6042. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6043. if (input > 0) {
  6044. reply[input] = '\0';
  6045. fprintf(stderr, "Server response: %s\n", reply);
  6046. }
  6047. if (cbf != NULL && cbf->on_result != NULL)
  6048. cbf->on_result(ssl);
  6049. ((func_args*)args)->return_code = TEST_SUCCESS;
  6050. done:
  6051. if (cbf != NULL)
  6052. cbf->last_err = err;
  6053. if (cbf != NULL && cbf->on_cleanup != NULL)
  6054. cbf->on_cleanup(ssl);
  6055. wolfSSL_free(ssl);
  6056. if (!sharedCtx)
  6057. wolfSSL_CTX_free(ctx);
  6058. CloseSocket(sockfd);
  6059. #ifdef WOLFSSL_TIRTOS
  6060. fdCloseSession(Task_self());
  6061. #endif
  6062. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6063. && defined(HAVE_THREAD_LS)
  6064. wc_ecc_fp_free(); /* free per thread cache */
  6065. #endif
  6066. #else
  6067. (void)args;
  6068. (void)cb;
  6069. #endif /* !NO_WOLFSSL_CLIENT */
  6070. wolfSSL_SetLoggingPrefix(NULL);
  6071. return 0;
  6072. }
  6073. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6074. callback_functions* server_cb, cbType client_on_handshake)
  6075. {
  6076. func_args client_args;
  6077. func_args server_args;
  6078. tcp_ready ready;
  6079. THREAD_TYPE serverThread;
  6080. XMEMSET(&client_args, 0, sizeof(func_args));
  6081. XMEMSET(&server_args, 0, sizeof(func_args));
  6082. #ifdef WOLFSSL_TIRTOS
  6083. fdOpenSession(Task_self());
  6084. #endif
  6085. StartTCP();
  6086. InitTcpReady(&ready);
  6087. #if defined(USE_WINDOWS_API)
  6088. /* use RNG to get random port if using windows */
  6089. ready.port = GetRandomPort();
  6090. #endif
  6091. server_args.signal = &ready;
  6092. server_args.callbacks = server_cb;
  6093. client_args.signal = &ready;
  6094. client_args.callbacks = client_cb;
  6095. start_thread(test_server_nofail, &server_args, &serverThread);
  6096. wait_tcp_ready(&server_args);
  6097. test_client_nofail(&client_args, client_on_handshake);
  6098. join_thread(serverThread);
  6099. client_cb->return_code = client_args.return_code;
  6100. server_cb->return_code = server_args.return_code;
  6101. FreeTcpReady(&ready);
  6102. #ifdef WOLFSSL_TIRTOS
  6103. fdOpenSession(Task_self());
  6104. #endif
  6105. }
  6106. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6107. callback_functions* server_cb)
  6108. {
  6109. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6110. }
  6111. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6112. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6113. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6114. void* server_args)
  6115. {
  6116. SOCKET_T sockfd = 0;
  6117. callback_functions* cbf;
  6118. WOLFSSL_CTX* ctx = 0;
  6119. WOLFSSL* ssl = 0;
  6120. WOLFSSL_SESSION* session = NULL;
  6121. char msg[64] = "hello wolfssl!";
  6122. char reply[1024];
  6123. int input;
  6124. int msgSz = (int)XSTRLEN(msg);
  6125. int ret, err = 0;
  6126. int sharedCtx = 0;
  6127. #ifdef WOLFSSL_TIRTOS
  6128. fdOpenSession(Task_self());
  6129. #endif
  6130. ((func_args*)args)->return_code = TEST_FAIL;
  6131. cbf = ((func_args*)args)->callbacks;
  6132. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6133. if (cbf != NULL && cbf->ctx) {
  6134. ctx = cbf->ctx;
  6135. sharedCtx = 1;
  6136. }
  6137. else
  6138. #endif
  6139. {
  6140. WOLFSSL_METHOD* method = NULL;
  6141. if (cbf != NULL && cbf->method != NULL) {
  6142. method = cbf->method();
  6143. }
  6144. else {
  6145. method = wolfSSLv23_client_method();
  6146. }
  6147. ctx = wolfSSL_CTX_new(method);
  6148. }
  6149. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6150. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6151. #endif
  6152. /* Do connect here so server detects failures */
  6153. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6154. 0, 0, NULL);
  6155. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6156. WOLFSSL_SUCCESS) {
  6157. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6158. goto done;
  6159. }
  6160. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6161. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6162. /*err_sys("can't load client cert file, "
  6163. "Please run from wolfSSL home dir");*/
  6164. goto done;
  6165. }
  6166. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6167. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6168. /*err_sys("can't load client key file, "
  6169. "Please run from wolfSSL home dir");*/
  6170. goto done;
  6171. }
  6172. /* call ctx setup callback */
  6173. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6174. cbf->ctx_ready(ctx);
  6175. }
  6176. ssl = wolfSSL_new(ctx);
  6177. if (ssl == NULL) {
  6178. goto done;
  6179. }
  6180. /* keep handshake resources for re-using WOLFSSL obj */
  6181. wolfSSL_KeepArrays(ssl);
  6182. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6183. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6184. goto done;
  6185. }
  6186. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6187. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6188. /*err_sys("can't load client cert file, "
  6189. "Please run from wolfSSL home dir");*/
  6190. goto done;
  6191. }
  6192. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6193. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6194. /*err_sys("can't load client key file, "
  6195. "Please run from wolfSSL home dir");*/
  6196. goto done;
  6197. }
  6198. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6199. /*err_sys("SSL_set_fd failed");*/
  6200. goto done;
  6201. }
  6202. /* call ssl setup callback */
  6203. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6204. cbf->ssl_ready(ssl);
  6205. }
  6206. #ifdef WOLFSSL_ASYNC_CRYPT
  6207. err = 0; /* Reset error */
  6208. #endif
  6209. do {
  6210. #ifdef WOLFSSL_ASYNC_CRYPT
  6211. if (err == WC_PENDING_E) {
  6212. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6213. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6214. }
  6215. #endif
  6216. ret = wolfSSL_connect(ssl);
  6217. err = wolfSSL_get_error(ssl, 0);
  6218. } while (err == WC_PENDING_E);
  6219. if (ret != WOLFSSL_SUCCESS) {
  6220. char buff[WOLFSSL_MAX_ERROR_SZ];
  6221. fprintf(stderr, "error = %d, %s\n", err,
  6222. wolfSSL_ERR_error_string(err, buff));
  6223. /*err_sys("SSL_connect failed");*/
  6224. goto done;
  6225. }
  6226. /* Build first session */
  6227. if (cb != NULL)
  6228. cb(ctx, ssl);
  6229. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6230. /*err_sys("SSL_write failed");*/
  6231. goto done;
  6232. }
  6233. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6234. if (input > 0) {
  6235. reply[input] = '\0';
  6236. fprintf(stderr, "Server response: %s\n", reply);
  6237. }
  6238. /* Session Resumption by re-using WOLFSSL object */
  6239. wolfSSL_set_quiet_shutdown(ssl, 1);
  6240. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6241. /* err_sys ("SSL shutdown failed"); */
  6242. goto done;
  6243. }
  6244. session = wolfSSL_get1_session(ssl);
  6245. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6246. wolfSSL_SESSION_free(session);
  6247. /* err_sys ("SSL_clear failed"); */
  6248. goto done;
  6249. }
  6250. wolfSSL_set_session(ssl, session);
  6251. wolfSSL_SESSION_free(session);
  6252. session = NULL;
  6253. /* close socket once */
  6254. CloseSocket(sockfd);
  6255. sockfd = 0;
  6256. /* wait until server ready */
  6257. wait_tcp_ready((func_args*)server_args);
  6258. fprintf(stderr, "session resumption\n");
  6259. /* Do re-connect */
  6260. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6261. 0, 0, NULL);
  6262. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6263. /*err_sys("SSL_set_fd failed");*/
  6264. goto done;
  6265. }
  6266. #ifdef WOLFSSL_ASYNC_CRYPT
  6267. err = 0; /* Reset error */
  6268. #endif
  6269. do {
  6270. #ifdef WOLFSSL_ASYNC_CRYPT
  6271. if (err == WC_PENDING_E) {
  6272. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6273. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6274. }
  6275. #endif
  6276. ret = wolfSSL_connect(ssl);
  6277. err = wolfSSL_get_error(ssl, 0);
  6278. } while (err == WC_PENDING_E);
  6279. if (ret != WOLFSSL_SUCCESS) {
  6280. char buff[WOLFSSL_MAX_ERROR_SZ];
  6281. fprintf(stderr, "error = %d, %s\n", err,
  6282. wolfSSL_ERR_error_string(err, buff));
  6283. /*err_sys("SSL_connect failed");*/
  6284. goto done;
  6285. }
  6286. /* Build first session */
  6287. if (cb != NULL)
  6288. cb(ctx, ssl);
  6289. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6290. /*err_sys("SSL_write failed");*/
  6291. goto done;
  6292. }
  6293. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6294. if (input > 0) {
  6295. reply[input] = '\0';
  6296. fprintf(stderr, "Server response: %s\n", reply);
  6297. }
  6298. ((func_args*)args)->return_code = TEST_SUCCESS;
  6299. done:
  6300. wolfSSL_free(ssl);
  6301. if (!sharedCtx)
  6302. wolfSSL_CTX_free(ctx);
  6303. CloseSocket(sockfd);
  6304. #ifdef WOLFSSL_TIRTOS
  6305. fdCloseSession(Task_self());
  6306. #endif
  6307. return;
  6308. }
  6309. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6310. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6311. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6312. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6313. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6314. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6315. #define HAVE_ALPN_PROTOS_SUPPORT
  6316. #endif
  6317. /* Generic TLS client / server with callbacks for API unit tests
  6318. * Used by SNI / ALPN / crypto callback helper functions */
  6319. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6320. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6321. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6322. #define ENABLE_TLS_CALLBACK_TEST
  6323. #endif
  6324. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6325. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6326. /* TLS server for API unit testing - generic */
  6327. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6328. {
  6329. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6330. WOLFSSL_CTX* ctx = NULL;
  6331. WOLFSSL* ssl = NULL;
  6332. SOCKET_T sfd = 0;
  6333. SOCKET_T cfd = 0;
  6334. word16 port;
  6335. char msg[] = "I hear you fa shizzle!";
  6336. int len = (int) XSTRLEN(msg);
  6337. char input[1024];
  6338. int idx;
  6339. int ret, err = 0;
  6340. ((func_args*)args)->return_code = TEST_FAIL;
  6341. #if defined(USE_WINDOWS_API)
  6342. port = ((func_args*)args)->signal->port;
  6343. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6344. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6345. /* Let tcp_listen assign port */
  6346. port = 0;
  6347. #else
  6348. /* Use default port */
  6349. port = wolfSSLPort;
  6350. #endif
  6351. #ifdef WOLFSSL_DTLS
  6352. if (callbacks->method == wolfDTLS_server_method
  6353. #ifdef WOLFSSL_STATIC_MEMORY
  6354. || callbacks->method_ex == wolfDTLS_server_method_ex
  6355. #endif
  6356. #ifndef NO_OLD_TLS
  6357. || callbacks->method == wolfDTLSv1_server_method
  6358. #ifdef WOLFSSL_STATIC_MEMORY
  6359. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6360. #endif
  6361. #endif
  6362. #ifndef WOLFSSL_NO_TLS12
  6363. || callbacks->method == wolfDTLSv1_2_server_method
  6364. #ifdef WOLFSSL_STATIC_MEMORY
  6365. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6366. #endif
  6367. #endif
  6368. #ifdef WOLFSSL_DTLS13
  6369. || callbacks->method == wolfDTLSv1_3_server_method
  6370. #ifdef WOLFSSL_STATIC_MEMORY
  6371. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6372. #endif
  6373. #endif
  6374. ) {
  6375. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6376. }
  6377. else
  6378. #endif
  6379. {
  6380. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6381. }
  6382. #ifdef WOLFSSL_STATIC_MEMORY
  6383. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6384. callbacks->memSz > 0) {
  6385. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6386. callbacks->mem, callbacks->memSz, 0, 1);
  6387. if (ret != WOLFSSL_SUCCESS) {
  6388. fprintf(stderr, "CTX static new failed %d\n", ret);
  6389. goto cleanup;
  6390. }
  6391. }
  6392. #else
  6393. ctx = wolfSSL_CTX_new(callbacks->method());
  6394. #endif
  6395. if (ctx == NULL) {
  6396. fprintf(stderr, "CTX new failed\n");
  6397. goto cleanup;
  6398. }
  6399. /* set defaults */
  6400. if (callbacks->caPemFile == NULL)
  6401. callbacks->caPemFile = cliCertFile;
  6402. if (callbacks->certPemFile == NULL)
  6403. callbacks->certPemFile = svrCertFile;
  6404. if (callbacks->keyPemFile == NULL)
  6405. callbacks->keyPemFile = svrKeyFile;
  6406. #ifdef WOLFSSL_TIRTOS
  6407. fdOpenSession(Task_self());
  6408. #endif
  6409. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6410. wolfSSL_CTX_set_verify(ctx,
  6411. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6412. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6413. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6414. #endif
  6415. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6416. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6417. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6418. goto cleanup;
  6419. }
  6420. #endif
  6421. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6422. WOLFSSL_SUCCESS) {
  6423. goto cleanup;
  6424. }
  6425. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6426. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6427. goto cleanup;
  6428. }
  6429. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6430. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6431. goto cleanup;
  6432. }
  6433. #ifdef HAVE_CRL
  6434. if (callbacks->crlPemFile != NULL) {
  6435. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6436. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6437. goto cleanup;
  6438. }
  6439. }
  6440. #endif
  6441. if (callbacks->ctx_ready)
  6442. callbacks->ctx_ready(ctx);
  6443. ssl = wolfSSL_new(ctx);
  6444. if (ssl == NULL) {
  6445. fprintf(stderr, "SSL new failed\n");
  6446. goto cleanup;
  6447. }
  6448. if (wolfSSL_dtls(ssl)) {
  6449. SOCKADDR_IN_T cliAddr;
  6450. socklen_t cliLen;
  6451. cliLen = sizeof(cliAddr);
  6452. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6453. (struct sockaddr*)&cliAddr, &cliLen);
  6454. if (idx <= 0) {
  6455. goto cleanup;
  6456. }
  6457. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6458. }
  6459. else {
  6460. CloseSocket(sfd);
  6461. }
  6462. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6463. goto cleanup;
  6464. }
  6465. if (callbacks->loadToSSL) {
  6466. wolfSSL_SetDevId(ssl, callbacks->devId);
  6467. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6468. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6469. goto cleanup;
  6470. }
  6471. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6472. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6473. goto cleanup;
  6474. }
  6475. }
  6476. #ifdef NO_PSK
  6477. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6478. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6479. #elif !defined(NO_DH)
  6480. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6481. #endif
  6482. #endif
  6483. if (callbacks->ssl_ready)
  6484. callbacks->ssl_ready(ssl);
  6485. #ifdef WOLFSSL_ASYNC_CRYPT
  6486. err = 0; /* Reset error */
  6487. #endif
  6488. do {
  6489. #ifdef WOLFSSL_ASYNC_CRYPT
  6490. if (err == WC_PENDING_E) {
  6491. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6492. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6493. }
  6494. #endif
  6495. ret = wolfSSL_accept(ssl);
  6496. err = wolfSSL_get_error(ssl, ret);
  6497. } while (err == WC_PENDING_E);
  6498. if (ret != WOLFSSL_SUCCESS) {
  6499. char buff[WOLFSSL_MAX_ERROR_SZ];
  6500. fprintf(stderr, "accept error = %d, %s\n", err,
  6501. wolfSSL_ERR_error_string(err, buff));
  6502. /*err_sys("SSL_accept failed");*/
  6503. }
  6504. else {
  6505. #ifdef WOLFSSL_ASYNC_CRYPT
  6506. err = 0; /* Reset error */
  6507. #endif
  6508. do {
  6509. #ifdef WOLFSSL_ASYNC_CRYPT
  6510. if (err == WC_PENDING_E) {
  6511. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6512. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6513. }
  6514. #endif
  6515. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6516. err = wolfSSL_get_error(ssl, idx);
  6517. } while (err == WC_PENDING_E);
  6518. if (idx > 0) {
  6519. input[idx] = 0;
  6520. fprintf(stderr, "Client message: %s\n", input);
  6521. }
  6522. #ifdef WOLFSSL_ASYNC_CRYPT
  6523. err = 0; /* Reset error */
  6524. #endif
  6525. do {
  6526. #ifdef WOLFSSL_ASYNC_CRYPT
  6527. if (err == WC_PENDING_E) {
  6528. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6529. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6530. }
  6531. #endif
  6532. ret = wolfSSL_write(ssl, msg, len);
  6533. err = wolfSSL_get_error(ssl, ret);
  6534. } while (err == WC_PENDING_E);
  6535. if (len != ret) {
  6536. goto cleanup;
  6537. }
  6538. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6539. defined(WOLFSSL_DTLS)
  6540. if (wolfSSL_dtls(ssl)) {
  6541. byte* import;
  6542. word32 sz;
  6543. wolfSSL_dtls_export(ssl, NULL, &sz);
  6544. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6545. if (import == NULL) {
  6546. goto cleanup;
  6547. }
  6548. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6549. if (idx < 0) {
  6550. goto cleanup;
  6551. }
  6552. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6553. goto cleanup;
  6554. }
  6555. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6556. }
  6557. #endif
  6558. #ifdef WOLFSSL_TIRTOS
  6559. Task_yield();
  6560. #endif
  6561. ((func_args*)args)->return_code = TEST_SUCCESS;
  6562. }
  6563. if (callbacks->on_result)
  6564. callbacks->on_result(ssl);
  6565. wolfSSL_shutdown(ssl);
  6566. cleanup:
  6567. wolfSSL_free(ssl);
  6568. wolfSSL_CTX_free(ctx);
  6569. CloseSocket(cfd);
  6570. #ifdef WOLFSSL_TIRTOS
  6571. fdCloseSession(Task_self());
  6572. #endif
  6573. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6574. && defined(HAVE_THREAD_LS)
  6575. wc_ecc_fp_free(); /* free per thread cache */
  6576. #endif
  6577. WOLFSSL_RETURN_FROM_THREAD(0);
  6578. }
  6579. /* TLS Client for API unit testing - generic */
  6580. static void run_wolfssl_client(void* args)
  6581. {
  6582. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6583. WOLFSSL_CTX* ctx = NULL;
  6584. WOLFSSL* ssl = NULL;
  6585. SOCKET_T sfd = 0;
  6586. char msg[] = "hello wolfssl server!";
  6587. int len = (int) XSTRLEN(msg);
  6588. char input[1024];
  6589. int ret, err = 0;
  6590. ((func_args*)args)->return_code = TEST_FAIL;
  6591. /* set defaults */
  6592. if (callbacks->caPemFile == NULL)
  6593. callbacks->caPemFile = caCertFile;
  6594. if (callbacks->certPemFile == NULL)
  6595. callbacks->certPemFile = cliCertFile;
  6596. if (callbacks->keyPemFile == NULL)
  6597. callbacks->keyPemFile = cliKeyFile;
  6598. #ifdef WOLFSSL_STATIC_MEMORY
  6599. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6600. callbacks->memSz > 0) {
  6601. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6602. callbacks->mem, callbacks->memSz, 0, 1);
  6603. if (ret != WOLFSSL_SUCCESS) {
  6604. fprintf(stderr, "CTX static new failed %d\n", ret);
  6605. goto cleanup;
  6606. }
  6607. }
  6608. #else
  6609. ctx = wolfSSL_CTX_new(callbacks->method());
  6610. #endif
  6611. if (ctx == NULL) {
  6612. fprintf(stderr, "CTX new failed\n");
  6613. goto cleanup;
  6614. }
  6615. #ifdef WOLFSSL_TIRTOS
  6616. fdOpenSession(Task_self());
  6617. #endif
  6618. if (!callbacks->loadToSSL) {
  6619. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6620. }
  6621. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6622. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6623. #endif
  6624. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6625. WOLFSSL_SUCCESS) {
  6626. goto cleanup;
  6627. }
  6628. if (!callbacks->loadToSSL) {
  6629. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6630. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6631. goto cleanup;
  6632. }
  6633. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6634. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6635. goto cleanup;
  6636. }
  6637. }
  6638. #ifdef HAVE_CRL
  6639. if (callbacks->crlPemFile != NULL) {
  6640. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6641. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6642. goto cleanup;
  6643. }
  6644. }
  6645. #endif
  6646. if (callbacks->ctx_ready)
  6647. callbacks->ctx_ready(ctx);
  6648. ssl = wolfSSL_new(ctx);
  6649. if (wolfSSL_dtls(ssl)) {
  6650. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6651. 1, 0, ssl);
  6652. }
  6653. else {
  6654. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6655. 0, 0, ssl);
  6656. }
  6657. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6658. goto cleanup;
  6659. }
  6660. if (callbacks->loadToSSL) {
  6661. wolfSSL_SetDevId(ssl, callbacks->devId);
  6662. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6663. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6664. goto cleanup;
  6665. }
  6666. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6667. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6668. goto cleanup;
  6669. }
  6670. }
  6671. if (callbacks->ssl_ready)
  6672. callbacks->ssl_ready(ssl);
  6673. #ifdef WOLFSSL_ASYNC_CRYPT
  6674. err = 0; /* Reset error */
  6675. #endif
  6676. do {
  6677. #ifdef WOLFSSL_ASYNC_CRYPT
  6678. if (err == WC_PENDING_E) {
  6679. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6680. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6681. }
  6682. #endif
  6683. ret = wolfSSL_connect(ssl);
  6684. err = wolfSSL_get_error(ssl, ret);
  6685. } while (err == WC_PENDING_E);
  6686. if (ret != WOLFSSL_SUCCESS) {
  6687. char buff[WOLFSSL_MAX_ERROR_SZ];
  6688. fprintf(stderr, "error = %d, %s\n", err,
  6689. wolfSSL_ERR_error_string(err, buff));
  6690. /*err_sys("SSL_connect failed");*/
  6691. }
  6692. else {
  6693. #ifdef WOLFSSL_ASYNC_CRYPT
  6694. err = 0; /* Reset error */
  6695. #endif
  6696. do {
  6697. #ifdef WOLFSSL_ASYNC_CRYPT
  6698. if (err == WC_PENDING_E) {
  6699. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6700. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6701. }
  6702. #endif
  6703. ret = wolfSSL_write(ssl, msg, len);
  6704. err = wolfSSL_get_error(ssl, ret);
  6705. } while (err == WC_PENDING_E);
  6706. if (len != ret)
  6707. goto cleanup;
  6708. #ifdef WOLFSSL_ASYNC_CRYPT
  6709. err = 0; /* Reset error */
  6710. #endif
  6711. do {
  6712. #ifdef WOLFSSL_ASYNC_CRYPT
  6713. if (err == WC_PENDING_E) {
  6714. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6715. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6716. }
  6717. #endif
  6718. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6719. err = wolfSSL_get_error(ssl, ret);
  6720. } while (err == WC_PENDING_E);
  6721. if (ret > 0) {
  6722. input[ret] = '\0'; /* null term */
  6723. fprintf(stderr, "Server response: %s\n", input);
  6724. }
  6725. ((func_args*)args)->return_code = TEST_SUCCESS;
  6726. }
  6727. if (callbacks->on_result)
  6728. callbacks->on_result(ssl);
  6729. cleanup:
  6730. wolfSSL_free(ssl);
  6731. wolfSSL_CTX_free(ctx);
  6732. CloseSocket(sfd);
  6733. #ifdef WOLFSSL_TIRTOS
  6734. fdCloseSession(Task_self());
  6735. #endif
  6736. }
  6737. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6738. static int test_wolfSSL_read_write(void)
  6739. {
  6740. /* The unit testing for read and write shall happen simultaneously, since
  6741. * one can't do anything with one without the other. (Except for a failure
  6742. * test case.) This function will call all the others that will set up,
  6743. * execute, and report their test findings.
  6744. *
  6745. * Set up the success case first. This function will become the template
  6746. * for the other tests. This should eventually be renamed
  6747. *
  6748. * The success case isn't interesting, how can this fail?
  6749. * - Do not give the client context a CA certificate. The connect should
  6750. * fail. Do not need server for this?
  6751. * - Using NULL for the ssl object on server. Do not need client for this.
  6752. * - Using NULL for the ssl object on client. Do not need server for this.
  6753. * - Good ssl objects for client and server. Client write() without server
  6754. * read().
  6755. * - Good ssl objects for client and server. Server write() without client
  6756. * read().
  6757. * - Forgetting the password callback?
  6758. */
  6759. tcp_ready ready;
  6760. func_args client_args;
  6761. func_args server_args;
  6762. THREAD_TYPE serverThread;
  6763. EXPECT_DECLS;
  6764. XMEMSET(&client_args, 0, sizeof(func_args));
  6765. XMEMSET(&server_args, 0, sizeof(func_args));
  6766. #ifdef WOLFSSL_TIRTOS
  6767. fdOpenSession(Task_self());
  6768. #endif
  6769. StartTCP();
  6770. InitTcpReady(&ready);
  6771. #if defined(USE_WINDOWS_API)
  6772. /* use RNG to get random port if using windows */
  6773. ready.port = GetRandomPort();
  6774. #endif
  6775. server_args.signal = &ready;
  6776. client_args.signal = &ready;
  6777. start_thread(test_server_nofail, &server_args, &serverThread);
  6778. wait_tcp_ready(&server_args);
  6779. test_client_nofail(&client_args, NULL);
  6780. join_thread(serverThread);
  6781. ExpectTrue(client_args.return_code);
  6782. ExpectTrue(server_args.return_code);
  6783. FreeTcpReady(&ready);
  6784. #ifdef WOLFSSL_TIRTOS
  6785. fdOpenSession(Task_self());
  6786. #endif
  6787. return EXPECT_RESULT();
  6788. }
  6789. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6790. {
  6791. EXPECT_DECLS;
  6792. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6793. !defined(WOLFSSL_NO_TLS12)
  6794. /* The unit test for session resumption by re-using WOLFSSL object.
  6795. * WOLFSSL object is not cleared after first session. It reuse the object
  6796. * for second connection.
  6797. */
  6798. tcp_ready ready;
  6799. func_args client_args;
  6800. func_args server_args;
  6801. THREAD_TYPE serverThread;
  6802. callback_functions client_cbf;
  6803. callback_functions server_cbf;
  6804. XMEMSET(&client_args, 0, sizeof(func_args));
  6805. XMEMSET(&server_args, 0, sizeof(func_args));
  6806. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6807. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6808. #ifdef WOLFSSL_TIRTOS
  6809. fdOpenSession(Task_self());
  6810. #endif
  6811. StartTCP();
  6812. InitTcpReady(&ready);
  6813. #if defined(USE_WINDOWS_API)
  6814. /* use RNG to get random port if using windows */
  6815. ready.port = GetRandomPort();
  6816. #endif
  6817. client_cbf.method = wolfTLSv1_2_client_method;
  6818. server_cbf.method = wolfTLSv1_2_server_method;
  6819. client_args.callbacks = &client_cbf;
  6820. server_args.callbacks = &server_cbf;
  6821. server_args.signal = &ready;
  6822. client_args.signal = &ready;
  6823. /* the var is used for loop number */
  6824. server_args.argc = 2;
  6825. start_thread(test_server_loop, &server_args, &serverThread);
  6826. wait_tcp_ready(&server_args);
  6827. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6828. join_thread(serverThread);
  6829. ExpectTrue(client_args.return_code);
  6830. ExpectTrue(server_args.return_code);
  6831. FreeTcpReady(&ready);
  6832. #ifdef WOLFSSL_TIRTOS
  6833. fdOpenSession(Task_self());
  6834. #endif
  6835. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6836. * !defined(WOLFSSL_TLS13) */
  6837. return EXPECT_RESULT();
  6838. }
  6839. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6840. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6841. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6842. WOLFSSL_CTX* ctx)
  6843. {
  6844. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6845. myVerifyAction = VERIFY_USE_PREVERFIY;
  6846. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6847. return TEST_SUCCESS;
  6848. }
  6849. #endif
  6850. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6851. {
  6852. EXPECT_DECLS;
  6853. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6854. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6855. test_ssl_cbf client_cbf;
  6856. test_ssl_cbf server_cbf;
  6857. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6858. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6859. #ifdef WOLFSSL_TLS13
  6860. client_cbf.method = wolfTLSv1_3_client_method;
  6861. #endif /* WOLFSSL_TLS13 */
  6862. client_cbf.ctx_ready =
  6863. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6864. /* test case 1 verify depth is equal to peer chain */
  6865. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6866. &server_cbf, NULL), TEST_SUCCESS);
  6867. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6868. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6869. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6870. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6871. return EXPECT_RESULT();
  6872. }
  6873. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6874. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6875. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6876. WOLFSSL_CTX* ctx)
  6877. {
  6878. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6879. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  6880. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6881. return TEST_SUCCESS;
  6882. }
  6883. #endif
  6884. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6885. {
  6886. EXPECT_DECLS;
  6887. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6888. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6889. test_ssl_cbf client_cbf;
  6890. test_ssl_cbf server_cbf;
  6891. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6892. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6893. #ifdef WOLFSSL_TLS13
  6894. client_cbf.method = wolfTLSv1_3_client_method;
  6895. #endif /* WOLFSSL_TLS13 */
  6896. client_cbf.ctx_ready =
  6897. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6898. /* test case 2
  6899. * verify depth is zero, number of peer's chain is 2.
  6900. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6901. * callback.
  6902. */
  6903. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6904. &server_cbf, NULL), TEST_SUCCESS);
  6905. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6906. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6907. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6908. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6909. return EXPECT_RESULT();
  6910. }
  6911. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6912. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6913. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6914. WOLFSSL_CTX* ctx)
  6915. {
  6916. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6917. myVerifyAction = VERIFY_USE_PREVERFIY;
  6918. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6919. return TEST_SUCCESS;
  6920. }
  6921. #endif
  6922. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6923. {
  6924. EXPECT_DECLS;
  6925. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6926. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6927. test_ssl_cbf client_cbf;
  6928. test_ssl_cbf server_cbf;
  6929. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6930. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6931. #ifdef WOLFSSL_TLS13
  6932. client_cbf.method = wolfTLSv1_3_client_method;
  6933. #endif /* WOLFSSL_TLS13 */
  6934. client_cbf.ctx_ready =
  6935. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6936. /* test case 3
  6937. * verify depth is zero, number of peer's chain is 2
  6938. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6939. * therefore, handshake becomes failure.
  6940. */
  6941. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6942. &server_cbf, NULL), TEST_FAIL);
  6943. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  6944. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  6945. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  6946. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  6947. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6948. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6949. return EXPECT_RESULT();
  6950. }
  6951. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6952. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6953. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6954. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6955. {
  6956. EXPECT_DECLS;
  6957. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6958. return EXPECT_RESULT();
  6959. }
  6960. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6961. {
  6962. EXPECT_DECLS;
  6963. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6964. return EXPECT_RESULT();
  6965. }
  6966. #endif
  6967. static int test_wolfSSL_CTX_set_cipher_list(void)
  6968. {
  6969. EXPECT_DECLS;
  6970. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6971. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6972. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6973. WOLFSSL_CTX* ctxClient = NULL;
  6974. WOLFSSL* sslClient = NULL;
  6975. test_ssl_cbf client_cbf;
  6976. test_ssl_cbf server_cbf;
  6977. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6978. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6979. server_cbf.method = wolfTLSv1_2_server_method;
  6980. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6981. client_cbf.method = wolfTLSv1_2_client_method;
  6982. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6983. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6984. &server_cbf, NULL), TEST_SUCCESS);
  6985. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6986. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6987. /* check with cipher string that has '+' */
  6988. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6989. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6990. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6991. /* check for the existence of an ECDHE ECDSA cipher suite */
  6992. if (EXPECT_SUCCESS()) {
  6993. int i = 0;
  6994. int found = 0;
  6995. const char* suite;
  6996. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6997. WOLFSSL_CIPHER* current;
  6998. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6999. do {
  7000. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7001. if (current) {
  7002. suite = wolfSSL_CIPHER_get_name(current);
  7003. if (suite && XSTRSTR(suite, "ECDSA")) {
  7004. found = 1;
  7005. break;
  7006. }
  7007. }
  7008. } while (current);
  7009. ExpectIntEQ(found, 1);
  7010. }
  7011. wolfSSL_free(sslClient);
  7012. wolfSSL_CTX_free(ctxClient);
  7013. #endif
  7014. return EXPECT_RESULT();
  7015. }
  7016. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7017. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7018. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7019. WOLFSSL* ssl)
  7020. {
  7021. EXPECT_DECLS;
  7022. size_t msg_len;
  7023. (void)ctx;
  7024. /* get_finished test */
  7025. /* 1. get own sent message */
  7026. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7027. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7028. ExpectIntGE(msg_len, 0);
  7029. /* 2. get peer message */
  7030. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7031. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7032. ExpectIntGE(msg_len, 0);
  7033. return EXPECT_RESULT();
  7034. }
  7035. #endif
  7036. static int test_wolfSSL_get_finished(void)
  7037. {
  7038. EXPECT_DECLS;
  7039. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7040. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7041. test_ssl_cbf client_cbf;
  7042. test_ssl_cbf server_cbf;
  7043. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7044. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7045. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7046. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7047. TEST_SUCCESS);
  7048. /* test received msg vs sent msg */
  7049. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7050. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7051. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7052. return EXPECT_RESULT();
  7053. }
  7054. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7055. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7056. !defined(NO_SESSION_CACHE)
  7057. /* Sessions to restore/store */
  7058. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7059. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7060. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7061. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7062. {
  7063. /* Don't store sessions. Lookup is still enabled. */
  7064. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7065. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7066. #ifdef OPENSSL_EXTRA
  7067. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7068. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7069. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7070. #endif
  7071. /* Require both peers to provide certs */
  7072. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7073. }
  7074. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7075. {
  7076. WOLFSSL_SESSION** sess;
  7077. if (wolfSSL_is_server(ssl))
  7078. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7079. else
  7080. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7081. if (*sess == NULL) {
  7082. #ifdef NO_SESSION_CACHE_REF
  7083. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7084. #else
  7085. /* Test for backwards compatibility */
  7086. if (wolfSSL_is_server(ssl)) {
  7087. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7088. }
  7089. else {
  7090. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7091. }
  7092. #endif
  7093. /* Now save the session in the internal store to make it available
  7094. * for lookup. For TLS 1.3, we can't save the session without
  7095. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7096. * session from cache. */
  7097. if (wolfSSL_is_server(ssl)
  7098. #ifndef WOLFSSL_TICKET_HAVE_ID
  7099. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7100. #endif
  7101. )
  7102. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7103. *sess), WOLFSSL_SUCCESS);
  7104. }
  7105. else {
  7106. /* If we have a session retrieved then remaining connections should be
  7107. * resuming on that session */
  7108. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7109. }
  7110. /* Save CTX to be able to decrypt tickets */
  7111. if (wolfSSL_is_server(ssl) &&
  7112. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7113. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7114. = wolfSSL_get_SSL_CTX(ssl));
  7115. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7116. WOLFSSL_SUCCESS);
  7117. }
  7118. #ifdef SESSION_CERTS
  7119. #ifndef WOLFSSL_TICKET_HAVE_ID
  7120. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7121. wolfSSL_session_reused(ssl))
  7122. #endif
  7123. {
  7124. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7125. * for all connections. TLS 1.3 only has tickets so if we don't
  7126. * include the session id in the ticket then the certificates
  7127. * will not be available on resumption. */
  7128. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7129. AssertNotNull(peer);
  7130. wolfSSL_X509_free(peer);
  7131. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7132. #ifdef OPENSSL_EXTRA
  7133. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7134. #endif
  7135. }
  7136. #endif /* SESSION_CERTS */
  7137. }
  7138. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7139. {
  7140. /* Set the session to reuse for the client */
  7141. AssertIntEQ(wolfSSL_set_session(ssl,
  7142. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7143. }
  7144. #endif
  7145. static int test_wolfSSL_CTX_add_session(void)
  7146. {
  7147. EXPECT_DECLS;
  7148. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7149. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7150. !defined(NO_SESSION_CACHE)
  7151. tcp_ready ready;
  7152. func_args client_args;
  7153. func_args server_args;
  7154. THREAD_TYPE serverThread;
  7155. callback_functions client_cb;
  7156. callback_functions server_cb;
  7157. method_provider methods[][2] = {
  7158. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7159. !defined(NO_DES3))
  7160. /* Without AES there are almost no ciphersuites available. This leads
  7161. * to no ciphersuites being available and an error. */
  7162. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7163. #endif
  7164. #ifndef WOLFSSL_NO_TLS12
  7165. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7166. #endif
  7167. /* Needs the default ticket callback since it is tied to the
  7168. * connection context and this makes it easy to carry over the ticket
  7169. * crypto context between connections */
  7170. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7171. defined(HAVE_SESSION_TICKET)
  7172. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7173. #endif
  7174. };
  7175. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7176. size_t i, j;
  7177. for (i = 0; i < methodsLen; i++) {
  7178. /* First run creates a connection while the second+ run will attempt
  7179. * to resume the connection. The trick is that the internal cache
  7180. * is turned off. wolfSSL_CTX_add_session should put the session in
  7181. * the cache anyway. */
  7182. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7183. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7184. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7185. #ifdef NO_SESSION_CACHE_REF
  7186. for (j = 0; j < 4; j++) {
  7187. #else
  7188. /* The session may be overwritten in this case. Do only one resumption
  7189. * to stop this test from failing intermittently. */
  7190. for (j = 0; j < 2; j++) {
  7191. #endif
  7192. #ifdef WOLFSSL_TIRTOS
  7193. fdOpenSession(Task_self());
  7194. #endif
  7195. StartTCP();
  7196. InitTcpReady(&ready);
  7197. XMEMSET(&client_args, 0, sizeof(func_args));
  7198. XMEMSET(&server_args, 0, sizeof(func_args));
  7199. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7200. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7201. client_cb.method = methods[i][0];
  7202. server_cb.method = methods[i][1];
  7203. server_args.signal = &ready;
  7204. server_args.callbacks = &server_cb;
  7205. client_args.signal = &ready;
  7206. client_args.callbacks = &client_cb;
  7207. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7208. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7209. server_cb.isSharedCtx = 1;
  7210. }
  7211. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7212. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7213. if (j != 0)
  7214. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7215. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7216. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7217. server_cb.ticNoInit = 1; /* Use default builtin */
  7218. start_thread(test_server_nofail, &server_args, &serverThread);
  7219. wait_tcp_ready(&server_args);
  7220. test_client_nofail(&client_args, NULL);
  7221. join_thread(serverThread);
  7222. ExpectTrue(client_args.return_code);
  7223. ExpectTrue(server_args.return_code);
  7224. FreeTcpReady(&ready);
  7225. if (EXPECT_FAIL())
  7226. break;
  7227. }
  7228. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7229. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7230. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7231. if (EXPECT_FAIL())
  7232. break;
  7233. }
  7234. #endif
  7235. return EXPECT_RESULT();
  7236. }
  7237. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7238. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7239. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7240. defined(HAVE_SESSION_TICKET) && \
  7241. !defined(TITAN_SESSION_CACHE) && \
  7242. !defined(HUGE_SESSION_CACHE) && \
  7243. !defined(BIG_SESSION_CACHE) && \
  7244. !defined(MEDIUM_SESSION_CACHE)
  7245. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7246. /* Sessions to restore/store */
  7247. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7248. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7249. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7250. static int twcase_new_session_called = 0;
  7251. static int twcase_remove_session_called = 0;
  7252. static int twcase_get_session_called = 0;
  7253. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7254. #define SESSION_CACHE_SIZE 33
  7255. typedef struct {
  7256. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7257. WOLFSSL_SESSION* value;
  7258. } hashTable_entry;
  7259. typedef struct {
  7260. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7261. size_t capacity; /* size of entries */
  7262. size_t length; /* number of items in the hash table */
  7263. wolfSSL_Mutex htLock; /* lock */
  7264. }hashTable;
  7265. static hashTable server_sessionCache;
  7266. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7267. {
  7268. int i;
  7269. unsigned int len;
  7270. (void)ssl;
  7271. /*
  7272. * This example uses a hash table.
  7273. * Steps you should take for a non-demo code:
  7274. * - acquire a lock for the file named according to the session id
  7275. * - open the file
  7276. * - encrypt and write the SSL_SESSION object to the file
  7277. * - release the lock
  7278. *
  7279. * Return:
  7280. * 0: The callback does not wish to hold a reference of the sess
  7281. * 1: The callback wants to hold a reference of the sess. The callback is
  7282. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7283. */
  7284. if (sess == NULL)
  7285. return 0;
  7286. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7287. return 0;
  7288. }
  7289. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7290. if (server_sessionCache.entries[i].value == NULL) {
  7291. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7292. server_sessionCache.entries[i].value = sess;
  7293. server_sessionCache.length++;
  7294. break;
  7295. }
  7296. }
  7297. ++twcase_new_session_called;
  7298. wc_UnLockMutex(&server_sessionCache.htLock);
  7299. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7300. twcase_new_session_called);
  7301. return 1;
  7302. }
  7303. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7304. {
  7305. int i;
  7306. (void)ctx;
  7307. (void)sess;
  7308. if (sess == NULL)
  7309. return;
  7310. /*
  7311. * This example uses a hash table.
  7312. * Steps you should take for a non-demo code:
  7313. * - acquire a lock for the file named according to the session id
  7314. * - remove the file
  7315. * - release the lock
  7316. */
  7317. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7318. return;
  7319. }
  7320. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7321. if (server_sessionCache.entries[i].key != NULL &&
  7322. XMEMCMP(server_sessionCache.entries[i].key,
  7323. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7324. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7325. server_sessionCache.entries[i].value = NULL;
  7326. server_sessionCache.entries[i].key = NULL;
  7327. server_sessionCache.length--;
  7328. break;
  7329. }
  7330. }
  7331. ++twcase_remove_session_called;
  7332. wc_UnLockMutex(&server_sessionCache.htLock);
  7333. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7334. twcase_remove_session_called);
  7335. }
  7336. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7337. const unsigned char *id, int len, int *ref)
  7338. {
  7339. int i;
  7340. (void)ssl;
  7341. (void)id;
  7342. (void)len;
  7343. /*
  7344. * This example uses a hash table.
  7345. * Steps you should take for a non-demo code:
  7346. * - acquire a lock for the file named according to the session id in the
  7347. * 2nd arg
  7348. * - read and decrypt contents of file and create a new SSL_SESSION
  7349. * - object release the lock
  7350. * - return the new session object
  7351. */
  7352. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7353. ++twcase_get_session_called);
  7354. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7355. * be responsible for the pointer then set to 0. */
  7356. *ref = 1;
  7357. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7358. if (server_sessionCache.entries[i].key != NULL &&
  7359. XMEMCMP(server_sessionCache.entries[i].key, id,
  7360. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7361. return server_sessionCache.entries[i].value;
  7362. }
  7363. }
  7364. return NULL;
  7365. }
  7366. static int twcase_get_sessionCb_cleanup(void)
  7367. {
  7368. int i;
  7369. int cnt = 0;
  7370. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7371. * for freeing sessions */
  7372. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7373. if (server_sessionCache.entries[i].value != NULL) {
  7374. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7375. cnt++;
  7376. }
  7377. }
  7378. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7379. cnt);
  7380. return TEST_SUCCESS;
  7381. }
  7382. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7383. {
  7384. EXPECT_DECLS;
  7385. /* off - Disable internal cache */
  7386. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7387. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7388. #ifdef OPENSSL_EXTRA
  7389. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7390. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7391. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7392. #endif
  7393. /* off - Do not setup external cache */
  7394. /* Require both peers to provide certs */
  7395. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7396. return EXPECT_RESULT();
  7397. }
  7398. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7399. {
  7400. /* on - internal cache is on by default */
  7401. /* off - Do not setup external cache */
  7402. /* Require both peers to provide certs */
  7403. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7404. return TEST_SUCCESS;
  7405. }
  7406. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7407. {
  7408. EXPECT_DECLS;
  7409. /* off - Disable internal cache */
  7410. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7411. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7412. #ifdef OPENSSL_EXTRA
  7413. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7414. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7415. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7416. #endif
  7417. /* on - Enable external cache */
  7418. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7419. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7420. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7421. /* Require both peers to provide certs */
  7422. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7423. return EXPECT_RESULT();
  7424. }
  7425. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7426. {
  7427. /* on - internal cache is on by default */
  7428. /* on - Enable external cache */
  7429. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7430. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7431. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7432. /* Require both peers to provide certs */
  7433. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7434. return TEST_SUCCESS;
  7435. }
  7436. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7437. {
  7438. /* on - internal cache is on by default */
  7439. /* on - Enable external cache */
  7440. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7441. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7442. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7443. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7444. /* Require both peers to provide certs */
  7445. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7446. return TEST_SUCCESS;
  7447. }
  7448. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7449. {
  7450. EXPECT_DECLS;
  7451. WOLFSSL_SESSION** sess;
  7452. if (wolfSSL_is_server(ssl))
  7453. sess = &twcase_server_first_session_ptr;
  7454. else
  7455. return TEST_SUCCESS;
  7456. if (*sess == NULL) {
  7457. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7458. /* Now save the session in the internal store to make it available
  7459. * for lookup. For TLS 1.3, we can't save the session without
  7460. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7461. * session from cache. */
  7462. if (wolfSSL_is_server(ssl)
  7463. #ifndef WOLFSSL_TICKET_HAVE_ID
  7464. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7465. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7466. #endif
  7467. ) {
  7468. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7469. *sess), WOLFSSL_SUCCESS);
  7470. }
  7471. }
  7472. /* Save CTX to be able to decrypt tickets */
  7473. if (twcase_server_current_ctx_ptr == NULL) {
  7474. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7475. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7476. WOLFSSL_SUCCESS);
  7477. }
  7478. #ifdef SESSION_CERTS
  7479. #ifndef WOLFSSL_TICKET_HAVE_ID
  7480. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7481. wolfSSL_session_reused(ssl))
  7482. #endif
  7483. {
  7484. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7485. * for all connections. TLS 1.3 only has tickets so if we don't
  7486. * include the session id in the ticket then the certificates
  7487. * will not be available on resumption. */
  7488. WOLFSSL_X509* peer = NULL;
  7489. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7490. wolfSSL_X509_free(peer);
  7491. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7492. }
  7493. #endif
  7494. return EXPECT_RESULT();
  7495. }
  7496. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7497. {
  7498. EXPECT_DECLS;
  7499. WOLFSSL_SESSION** sess;
  7500. sess = &twcase_client_first_session_ptr;
  7501. if (*sess == NULL) {
  7502. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7503. }
  7504. else {
  7505. /* If we have a session retrieved then remaining connections should be
  7506. * resuming on that session */
  7507. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7508. }
  7509. #ifdef SESSION_CERTS
  7510. #ifndef WOLFSSL_TICKET_HAVE_ID
  7511. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7512. wolfSSL_session_reused(ssl))
  7513. #endif
  7514. {
  7515. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7516. ExpectNotNull(peer);
  7517. wolfSSL_X509_free(peer);
  7518. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7519. #ifdef OPENSSL_EXTRA
  7520. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7521. #endif
  7522. }
  7523. #endif
  7524. return EXPECT_RESULT();
  7525. }
  7526. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7527. {
  7528. EXPECT_DECLS;
  7529. /* Set the session to reuse for the client */
  7530. ExpectNotNull(ssl);
  7531. ExpectNotNull(twcase_client_first_session_ptr);
  7532. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7533. WOLFSSL_SUCCESS);
  7534. return EXPECT_RESULT();
  7535. }
  7536. struct test_add_session_ext_params {
  7537. method_provider client_meth;
  7538. method_provider server_meth;
  7539. const char* tls_version;
  7540. };
  7541. static int test_wolfSSL_CTX_add_session_ext(
  7542. struct test_add_session_ext_params* param)
  7543. {
  7544. EXPECT_DECLS;
  7545. /* Test the default 33 sessions */
  7546. int j;
  7547. /* Clear cache before starting */
  7548. wolfSSL_CTX_flush_sessions(NULL, -1);
  7549. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7550. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7551. return BAD_MUTEX_E;
  7552. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7553. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7554. for (j = 0; j < 5; j++) {
  7555. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7556. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7557. test_ssl_cbf client_cb;
  7558. test_ssl_cbf server_cb;
  7559. (void)dtls;
  7560. /* Test five cache configurations */
  7561. twcase_client_first_session_ptr = NULL;
  7562. twcase_server_first_session_ptr = NULL;
  7563. twcase_server_current_ctx_ptr = NULL;
  7564. twcase_new_session_called = 0;
  7565. twcase_remove_session_called = 0;
  7566. twcase_get_session_called = 0;
  7567. /* connection 1 - first connection */
  7568. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7569. XMEMSET(&client_cb, 0, sizeof(client_cb));
  7570. XMEMSET(&server_cb, 0, sizeof(server_cb));
  7571. client_cb.method = param->client_meth;
  7572. server_cb.method = param->server_meth;
  7573. if (dtls)
  7574. client_cb.doUdp = server_cb.doUdp = 1;
  7575. /* Setup internal and external cache */
  7576. switch (j) {
  7577. case 0:
  7578. /* SSL_OP_NO_TICKET stateful ticket case */
  7579. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7580. break;
  7581. case 1:
  7582. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7583. break;
  7584. case 2:
  7585. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7586. break;
  7587. case 3:
  7588. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7589. break;
  7590. case 4:
  7591. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7592. break;
  7593. }
  7594. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7595. /* Add session to internal cache and save SSL session for testing */
  7596. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7597. /* Save client SSL session for testing */
  7598. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7599. server_cb.ticNoInit = 1; /* Use default builtin */
  7600. /* Don't free/release ctx */
  7601. server_cb.ctx = twcase_server_current_ctx_ptr;
  7602. server_cb.isSharedCtx = 1;
  7603. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7604. &server_cb, NULL), TEST_SUCCESS);
  7605. ExpectIntEQ(twcase_get_session_called, 0);
  7606. if (EXPECT_FAIL()) {
  7607. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7608. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7609. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7610. break;
  7611. }
  7612. switch (j) {
  7613. case 0:
  7614. case 1:
  7615. case 2:
  7616. /* cache cannot be searched with out a connection */
  7617. /* Add a new session */
  7618. ExpectIntEQ(twcase_new_session_called, 1);
  7619. /* In twcase_server_sess_ctx_pre_shutdown
  7620. * wolfSSL_CTX_add_session which evicts the existing session
  7621. * in cache and adds it back in */
  7622. ExpectIntLE(twcase_remove_session_called, 1);
  7623. break;
  7624. case 3:
  7625. case 4:
  7626. /* no external cache */
  7627. ExpectIntEQ(twcase_new_session_called, 0);
  7628. ExpectIntEQ(twcase_remove_session_called, 0);
  7629. break;
  7630. }
  7631. /* connection 2 - session resume */
  7632. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7633. twcase_new_session_called = 0;
  7634. twcase_remove_session_called = 0;
  7635. twcase_get_session_called = 0;
  7636. server_cb.on_result = 0;
  7637. client_cb.on_result = 0;
  7638. server_cb.ticNoInit = 1; /* Use default builtin */
  7639. server_cb.ctx = twcase_server_current_ctx_ptr;
  7640. /* try session resumption */
  7641. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7642. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7643. &server_cb, NULL), TEST_SUCCESS);
  7644. /* Clear cache before checking */
  7645. wolfSSL_CTX_flush_sessions(NULL, -1);
  7646. switch (j) {
  7647. case 0:
  7648. if (tls13) {
  7649. /* (D)TLSv1.3 stateful case */
  7650. /* cache hit */
  7651. /* DTLS accesses cache once for stateless parsing and
  7652. * once for stateful parsing */
  7653. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7654. /* (D)TLSv1.3 creates a new ticket,
  7655. * updates both internal and external cache */
  7656. ExpectIntEQ(twcase_new_session_called, 1);
  7657. /* A new session ID is created for a new ticket */
  7658. ExpectIntEQ(twcase_remove_session_called, 2);
  7659. }
  7660. else {
  7661. /* non (D)TLSv1.3 case, no update */
  7662. /* DTLS accesses cache once for stateless parsing and
  7663. * once for stateful parsing */
  7664. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7665. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7666. #else
  7667. ExpectIntEQ(twcase_get_session_called, 1);
  7668. #endif
  7669. ExpectIntEQ(twcase_new_session_called, 0);
  7670. /* Called on session added in
  7671. * twcase_server_sess_ctx_pre_shutdown */
  7672. ExpectIntEQ(twcase_remove_session_called, 1);
  7673. }
  7674. break;
  7675. case 1:
  7676. if (tls13) {
  7677. /* (D)TLSv1.3 case */
  7678. /* cache hit */
  7679. ExpectIntEQ(twcase_get_session_called, 1);
  7680. /* (D)TLSv1.3 creates a new ticket,
  7681. * updates both internal and external cache */
  7682. ExpectIntEQ(twcase_new_session_called, 1);
  7683. /* Called on session added in
  7684. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7685. ExpectIntEQ(twcase_remove_session_called, 1);
  7686. }
  7687. else {
  7688. /* non (D)TLSv1.3 case */
  7689. /* cache hit */
  7690. /* DTLS accesses cache once for stateless parsing and
  7691. * once for stateful parsing */
  7692. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7693. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7694. #else
  7695. ExpectIntEQ(twcase_get_session_called, 1);
  7696. #endif
  7697. ExpectIntEQ(twcase_new_session_called, 0);
  7698. /* Called on session added in
  7699. * twcase_server_sess_ctx_pre_shutdown */
  7700. ExpectIntEQ(twcase_remove_session_called, 1);
  7701. }
  7702. break;
  7703. case 2:
  7704. if (tls13) {
  7705. /* (D)TLSv1.3 case */
  7706. /* cache hit */
  7707. ExpectIntEQ(twcase_get_session_called, 1);
  7708. /* (D)TLSv1.3 creates a new ticket,
  7709. * updates both internal and external cache */
  7710. ExpectIntEQ(twcase_new_session_called, 1);
  7711. /* Called on session added in
  7712. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7713. ExpectIntEQ(twcase_remove_session_called, 1);
  7714. }
  7715. else {
  7716. /* non (D)TLSv1.3 case */
  7717. /* cache hit */
  7718. /* DTLS accesses cache once for stateless parsing and
  7719. * once for stateful parsing */
  7720. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7721. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7722. #else
  7723. ExpectIntEQ(twcase_get_session_called, 1);
  7724. #endif
  7725. ExpectIntEQ(twcase_new_session_called, 0);
  7726. /* Called on session added in
  7727. * twcase_server_sess_ctx_pre_shutdown */
  7728. ExpectIntEQ(twcase_remove_session_called, 1);
  7729. }
  7730. break;
  7731. case 3:
  7732. case 4:
  7733. /* no external cache */
  7734. ExpectIntEQ(twcase_get_session_called, 0);
  7735. ExpectIntEQ(twcase_new_session_called, 0);
  7736. ExpectIntEQ(twcase_remove_session_called, 0);
  7737. break;
  7738. }
  7739. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7740. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7741. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7742. if (EXPECT_FAIL())
  7743. break;
  7744. }
  7745. twcase_get_sessionCb_cleanup();
  7746. XMEMSET(&server_sessionCache.entries, 0,
  7747. sizeof(server_sessionCache.entries));
  7748. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  7749. wc_FreeMutex(&server_sessionCache.htLock);
  7750. return EXPECT_RESULT();
  7751. }
  7752. #endif
  7753. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  7754. {
  7755. EXPECT_DECLS;
  7756. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7757. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7758. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7759. defined(HAVE_SESSION_TICKET) && \
  7760. !defined(TITAN_SESSION_CACHE) && \
  7761. !defined(HUGE_SESSION_CACHE) && \
  7762. !defined(BIG_SESSION_CACHE) && \
  7763. !defined(MEDIUM_SESSION_CACHE)
  7764. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7765. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7766. struct test_add_session_ext_params param[1] = {
  7767. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  7768. };
  7769. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7770. #endif
  7771. #endif
  7772. return EXPECT_RESULT();
  7773. }
  7774. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  7775. {
  7776. EXPECT_DECLS;
  7777. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7778. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7779. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7780. defined(HAVE_SESSION_TICKET) && \
  7781. !defined(TITAN_SESSION_CACHE) && \
  7782. !defined(HUGE_SESSION_CACHE) && \
  7783. !defined(BIG_SESSION_CACHE) && \
  7784. !defined(MEDIUM_SESSION_CACHE)
  7785. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7786. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7787. #ifdef WOLFSSL_DTLS13
  7788. struct test_add_session_ext_params param[1] = {
  7789. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  7790. };
  7791. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7792. #endif
  7793. #endif
  7794. #endif
  7795. return EXPECT_RESULT();
  7796. }
  7797. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  7798. {
  7799. EXPECT_DECLS;
  7800. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7801. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7802. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7803. defined(HAVE_SESSION_TICKET) && \
  7804. !defined(TITAN_SESSION_CACHE) && \
  7805. !defined(HUGE_SESSION_CACHE) && \
  7806. !defined(BIG_SESSION_CACHE) && \
  7807. !defined(MEDIUM_SESSION_CACHE)
  7808. #ifndef WOLFSSL_NO_TLS12
  7809. struct test_add_session_ext_params param[1] = {
  7810. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  7811. };
  7812. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7813. #endif
  7814. #endif
  7815. return EXPECT_RESULT();
  7816. }
  7817. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  7818. {
  7819. EXPECT_DECLS;
  7820. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7821. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7822. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7823. defined(HAVE_SESSION_TICKET) && \
  7824. !defined(TITAN_SESSION_CACHE) && \
  7825. !defined(HUGE_SESSION_CACHE) && \
  7826. !defined(BIG_SESSION_CACHE) && \
  7827. !defined(MEDIUM_SESSION_CACHE)
  7828. #ifndef WOLFSSL_NO_TLS12
  7829. #ifdef WOLFSSL_DTLS
  7830. struct test_add_session_ext_params param[1] = {
  7831. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  7832. };
  7833. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7834. #endif
  7835. #endif
  7836. #endif
  7837. return EXPECT_RESULT();
  7838. }
  7839. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  7840. {
  7841. EXPECT_DECLS;
  7842. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7843. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7844. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7845. defined(HAVE_SESSION_TICKET) && \
  7846. !defined(TITAN_SESSION_CACHE) && \
  7847. !defined(HUGE_SESSION_CACHE) && \
  7848. !defined(BIG_SESSION_CACHE) && \
  7849. !defined(MEDIUM_SESSION_CACHE)
  7850. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7851. !defined(NO_DES3))
  7852. struct test_add_session_ext_params param[1] = {
  7853. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  7854. };
  7855. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7856. #endif
  7857. #endif
  7858. return EXPECT_RESULT();
  7859. }
  7860. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  7861. {
  7862. EXPECT_DECLS;
  7863. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7864. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7865. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7866. defined(HAVE_SESSION_TICKET) && \
  7867. !defined(TITAN_SESSION_CACHE) && \
  7868. !defined(HUGE_SESSION_CACHE) && \
  7869. !defined(BIG_SESSION_CACHE) && \
  7870. !defined(MEDIUM_SESSION_CACHE)
  7871. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7872. !defined(NO_DES3))
  7873. #ifdef WOLFSSL_DTLS
  7874. struct test_add_session_ext_params param[1] = {
  7875. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  7876. };
  7877. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7878. #endif
  7879. #endif
  7880. #endif
  7881. return EXPECT_RESULT();
  7882. }
  7883. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7884. /* canned export of a session using older version 3 */
  7885. static unsigned char version_3[] = {
  7886. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7887. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7888. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7889. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7890. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7891. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7892. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7893. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7894. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7895. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7896. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7897. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7899. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7900. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7901. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7902. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7903. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7904. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7905. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7906. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7907. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7908. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7909. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7910. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7911. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7912. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7913. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7914. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7915. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7916. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7917. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7918. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7919. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7920. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7921. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7922. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7923. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7924. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7925. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7926. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7928. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7929. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7930. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7931. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7932. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7933. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7934. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7935. 0x2E, 0x31, 0xED, 0x4F
  7936. };
  7937. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7938. static int test_wolfSSL_dtls_export(void)
  7939. {
  7940. EXPECT_DECLS;
  7941. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7942. tcp_ready ready;
  7943. func_args client_args;
  7944. func_args server_args;
  7945. THREAD_TYPE serverThread;
  7946. callback_functions server_cbf;
  7947. callback_functions client_cbf;
  7948. #ifdef WOLFSSL_TIRTOS
  7949. fdOpenSession(Task_self());
  7950. #endif
  7951. InitTcpReady(&ready);
  7952. #if defined(USE_WINDOWS_API)
  7953. /* use RNG to get random port if using windows */
  7954. ready.port = GetRandomPort();
  7955. #endif
  7956. /* set using dtls */
  7957. XMEMSET(&client_args, 0, sizeof(func_args));
  7958. XMEMSET(&server_args, 0, sizeof(func_args));
  7959. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7960. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7961. server_cbf.method = wolfDTLSv1_2_server_method;
  7962. client_cbf.method = wolfDTLSv1_2_client_method;
  7963. server_args.callbacks = &server_cbf;
  7964. client_args.callbacks = &client_cbf;
  7965. server_args.signal = &ready;
  7966. client_args.signal = &ready;
  7967. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7968. wait_tcp_ready(&server_args);
  7969. run_wolfssl_client(&client_args);
  7970. join_thread(serverThread);
  7971. ExpectTrue(client_args.return_code);
  7972. ExpectTrue(server_args.return_code);
  7973. FreeTcpReady(&ready);
  7974. #ifdef WOLFSSL_TIRTOS
  7975. fdOpenSession(Task_self());
  7976. #endif
  7977. if (EXPECT_SUCCESS()) {
  7978. SOCKET_T sockfd = 0;
  7979. WOLFSSL_CTX* ctx = NULL;
  7980. WOLFSSL* ssl = NULL;
  7981. char msg[64] = "hello wolfssl!";
  7982. char reply[1024];
  7983. int msgSz = (int)XSTRLEN(msg);
  7984. byte *session, *window;
  7985. unsigned int sessionSz = 0;
  7986. unsigned int windowSz = 0;
  7987. #ifndef TEST_IPV6
  7988. struct sockaddr_in peerAddr;
  7989. #else
  7990. struct sockaddr_in6 peerAddr;
  7991. #endif /* TEST_IPV6 */
  7992. int i;
  7993. /* Set ctx to DTLS 1.2 */
  7994. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7995. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7996. /* test importing version 3 */
  7997. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7998. /* test importing bad length and bad version */
  7999. version_3[2] += 1;
  8000. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8001. version_3[2] -= 1; version_3[1] = 0XA0;
  8002. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8003. wolfSSL_free(ssl);
  8004. wolfSSL_CTX_free(ctx);
  8005. /* check storing client state after connection and storing window only */
  8006. #ifdef WOLFSSL_TIRTOS
  8007. fdOpenSession(Task_self());
  8008. #endif
  8009. InitTcpReady(&ready);
  8010. #if defined(USE_WINDOWS_API)
  8011. /* use RNG to get random port if using windows */
  8012. ready.port = GetRandomPort();
  8013. #endif
  8014. /* set using dtls */
  8015. XMEMSET(&server_args, 0, sizeof(func_args));
  8016. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8017. server_cbf.method = wolfDTLSv1_2_server_method;
  8018. server_cbf.doUdp = 1;
  8019. server_args.callbacks = &server_cbf;
  8020. server_args.argc = 3; /* set loop_count to 3 */
  8021. server_args.signal = &ready;
  8022. start_thread(test_server_nofail, &server_args, &serverThread);
  8023. wait_tcp_ready(&server_args);
  8024. /* create and connect with client */
  8025. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8026. ExpectIntEQ(WOLFSSL_SUCCESS,
  8027. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8028. ExpectIntEQ(WOLFSSL_SUCCESS,
  8029. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8030. ExpectIntEQ(WOLFSSL_SUCCESS,
  8031. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8032. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8033. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8034. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8035. /* store server information connected too */
  8036. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8037. #ifndef TEST_IPV6
  8038. peerAddr.sin_family = AF_INET;
  8039. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8040. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8041. #else
  8042. peerAddr.sin6_family = AF_INET6;
  8043. ExpectIntEQ(
  8044. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8045. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8046. #endif
  8047. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8048. WOLFSSL_SUCCESS);
  8049. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8050. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8051. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8052. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8053. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8054. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8055. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8056. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8057. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8058. wolfSSL_free(ssl);
  8059. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8060. /* restore state */
  8061. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8062. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8063. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8064. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8065. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8066. WOLFSSL_SUCCESS);
  8067. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8068. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8069. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8070. wolfSSL_free(ssl);
  8071. }
  8072. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8073. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8074. wolfSSL_CTX_free(ctx);
  8075. fprintf(stderr, "done and waiting for server\n");
  8076. join_thread(serverThread);
  8077. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8078. FreeTcpReady(&ready);
  8079. #ifdef WOLFSSL_TIRTOS
  8080. fdOpenSession(Task_self());
  8081. #endif
  8082. }
  8083. #endif
  8084. return EXPECT_RESULT();
  8085. }
  8086. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8087. #ifdef WOLFSSL_TLS13
  8088. static const byte canned_client_tls13_session[] = {
  8089. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8090. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8091. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8092. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8093. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8094. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8095. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8096. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8097. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8098. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8099. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8100. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8101. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8102. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8103. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8104. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8105. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8106. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8107. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8108. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8109. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8110. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8111. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8112. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8113. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8114. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8115. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8116. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8118. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8119. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8120. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8121. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8122. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8123. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8124. 0x00, 0x03
  8125. };
  8126. static const byte canned_server_tls13_session[] = {
  8127. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8128. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8129. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8130. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8133. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8134. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8135. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8136. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8137. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8138. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8139. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8140. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8141. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8142. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8143. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8144. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8145. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8146. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8147. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8148. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8149. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8150. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8151. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8152. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8153. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8154. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8156. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8157. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8158. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8159. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8160. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8161. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8162. 0x00, 0x04
  8163. };
  8164. #endif /* WOLFSSL_TLS13 */
  8165. static const byte canned_client_session[] = {
  8166. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8167. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8168. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8169. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8170. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8171. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8173. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8174. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8175. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8176. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8177. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8178. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8179. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8180. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8181. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8182. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8183. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8184. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8185. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8186. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8187. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8188. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8189. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8190. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8191. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8192. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8193. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8194. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8195. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8197. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8198. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8199. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8200. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8201. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8202. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8203. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8204. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8205. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8206. 0x00, 0x03
  8207. };
  8208. static const byte canned_server_session[] = {
  8209. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8210. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8211. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8212. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8213. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8216. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8217. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8218. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8219. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8220. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8221. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8222. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8223. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8224. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8225. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8226. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8227. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8228. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8229. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8230. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8231. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8232. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8233. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8234. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8235. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8236. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8237. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8240. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8241. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8242. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8243. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8244. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8245. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8246. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8247. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8248. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8249. 0x00, 0x04
  8250. };
  8251. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8252. {
  8253. SOCKET_T sockfd = 0;
  8254. SOCKET_T clientfd = 0;
  8255. word16 port;
  8256. callback_functions* cbf;
  8257. WOLFSSL_CTX* ctx = 0;
  8258. WOLFSSL* ssl = 0;
  8259. char msg[] = "I hear you fa shizzle!";
  8260. char input[1024];
  8261. int idx;
  8262. #ifdef WOLFSSL_TIRTOS
  8263. fdOpenSession(Task_self());
  8264. #endif
  8265. ((func_args*)args)->return_code = TEST_FAIL;
  8266. cbf = ((func_args*)args)->callbacks;
  8267. #if defined(USE_WINDOWS_API)
  8268. port = ((func_args*)args)->signal->port;
  8269. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8270. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8271. /* Let tcp_listen assign port */
  8272. port = 0;
  8273. #else
  8274. /* Use default port */
  8275. port = wolfSSLPort;
  8276. #endif
  8277. /* do it here to detect failure */
  8278. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8279. CloseSocket(sockfd);
  8280. {
  8281. WOLFSSL_METHOD* method = NULL;
  8282. if (cbf != NULL && cbf->method != NULL) {
  8283. method = cbf->method();
  8284. }
  8285. else {
  8286. method = wolfTLSv1_2_server_method();
  8287. }
  8288. ctx = wolfSSL_CTX_new(method);
  8289. }
  8290. if (ctx == NULL) {
  8291. goto done;
  8292. }
  8293. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8294. /* call ctx setup callback */
  8295. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8296. cbf->ctx_ready(ctx);
  8297. }
  8298. ssl = wolfSSL_new(ctx);
  8299. if (ssl == NULL) {
  8300. goto done;
  8301. }
  8302. wolfSSL_set_fd(ssl, clientfd);
  8303. /* call ssl setup callback */
  8304. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8305. cbf->ssl_ready(ssl);
  8306. }
  8307. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8308. if (idx > 0) {
  8309. input[idx] = '\0';
  8310. fprintf(stderr, "Client message export/import: %s\n", input);
  8311. }
  8312. else {
  8313. fprintf(stderr, "ret = %d error = %d\n", idx,
  8314. wolfSSL_get_error(ssl, idx));
  8315. goto done;
  8316. }
  8317. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8318. /*err_sys("SSL_write failed");*/
  8319. WOLFSSL_RETURN_FROM_THREAD(0);
  8320. }
  8321. #ifdef WOLFSSL_TIRTOS
  8322. Task_yield();
  8323. #endif
  8324. ((func_args*)args)->return_code = TEST_SUCCESS;
  8325. done:
  8326. wolfSSL_shutdown(ssl);
  8327. wolfSSL_free(ssl);
  8328. wolfSSL_CTX_free(ctx);
  8329. CloseSocket(clientfd);
  8330. #ifdef WOLFSSL_TIRTOS
  8331. fdCloseSession(Task_self());
  8332. #endif
  8333. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8334. && defined(HAVE_THREAD_LS)
  8335. wc_ecc_fp_free(); /* free per thread cache */
  8336. #endif
  8337. #if defined(HAVE_SESSION_TICKET) && \
  8338. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8339. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8340. OpenSSLTicketCleanup();
  8341. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8342. TicketCleanup();
  8343. #endif
  8344. #endif
  8345. WOLFSSL_RETURN_FROM_THREAD(0);
  8346. }
  8347. static void load_tls12_canned_server(WOLFSSL* ssl)
  8348. {
  8349. int clientfd = wolfSSL_get_fd(ssl);
  8350. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8351. sizeof(canned_server_session)), sizeof(canned_server_session));
  8352. wolfSSL_set_fd(ssl, clientfd);
  8353. }
  8354. #ifdef WOLFSSL_TLS13
  8355. static void load_tls13_canned_server(WOLFSSL* ssl)
  8356. {
  8357. int clientfd = wolfSSL_get_fd(ssl);
  8358. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8359. sizeof(canned_server_tls13_session)),
  8360. sizeof(canned_server_tls13_session));
  8361. wolfSSL_set_fd(ssl, clientfd);
  8362. }
  8363. #endif
  8364. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8365. static int test_wolfSSL_tls_export_run(int v)
  8366. {
  8367. EXPECT_DECLS;
  8368. SOCKET_T sockfd = 0;
  8369. WOLFSSL_CTX* ctx = 0;
  8370. WOLFSSL* ssl = 0;
  8371. char msg[64] = "hello wolfssl!";
  8372. char reply[1024];
  8373. word32 replySz;
  8374. int msgSz = (int)XSTRLEN(msg);
  8375. const byte* clientSession = NULL;
  8376. int clientSessionSz = 0;
  8377. tcp_ready ready;
  8378. func_args server_args;
  8379. THREAD_TYPE serverThread;
  8380. callback_functions server_cbf;
  8381. #ifdef WOLFSSL_TIRTOS
  8382. fdOpenSession(Task_self());
  8383. #endif
  8384. InitTcpReady(&ready);
  8385. #if defined(USE_WINDOWS_API)
  8386. /* use RNG to get random port if using windows */
  8387. ready.port = GetRandomPort();
  8388. #endif
  8389. XMEMSET(&server_args, 0, sizeof(func_args));
  8390. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8391. switch (v) {
  8392. case WOLFSSL_TLSV1_2:
  8393. server_cbf.method = wolfTLSv1_2_server_method;
  8394. server_cbf.ssl_ready = load_tls12_canned_server;
  8395. /* setup the client side */
  8396. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8397. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8398. clientSession = canned_client_session;
  8399. clientSessionSz = sizeof(canned_client_session);
  8400. break;
  8401. #ifdef WOLFSSL_TLS13
  8402. case WOLFSSL_TLSV1_3:
  8403. server_cbf.method = wolfTLSv1_3_server_method;
  8404. server_cbf.ssl_ready = load_tls13_canned_server;
  8405. /* setup the client side */
  8406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8407. clientSession = canned_client_tls13_session;
  8408. clientSessionSz = sizeof(canned_client_tls13_session);
  8409. break;
  8410. #endif
  8411. }
  8412. server_args.callbacks = &server_cbf;
  8413. server_args.signal = &ready;
  8414. start_thread(tls_export_server, &server_args, &serverThread);
  8415. wait_tcp_ready(&server_args);
  8416. #ifdef WOLFSSL_TIRTOS
  8417. fdOpenSession(Task_self());
  8418. #endif
  8419. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8420. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8421. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8422. clientSessionSz);
  8423. replySz = sizeof(reply);
  8424. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8425. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8426. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8427. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8428. #endif
  8429. wolfSSL_set_fd(ssl, sockfd);
  8430. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8431. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8432. wolfSSL_free(ssl);
  8433. wolfSSL_CTX_free(ctx);
  8434. CloseSocket(sockfd);
  8435. #ifdef WOLFSSL_TIRTOS
  8436. fdCloseSession(Task_self());
  8437. #endif
  8438. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8439. && defined(HAVE_THREAD_LS)
  8440. wc_ecc_fp_free(); /* free per thread cache */
  8441. #endif
  8442. join_thread(serverThread);
  8443. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8444. FreeTcpReady(&ready);
  8445. #ifdef WOLFSSL_TIRTOS
  8446. fdOpenSession(Task_self());
  8447. #endif
  8448. return EXPECT_RESULT();
  8449. }
  8450. #endif
  8451. static int test_wolfSSL_tls_export(void)
  8452. {
  8453. int res = TEST_SKIPPED;
  8454. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8455. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8456. #ifdef WOLFSSL_TLS13
  8457. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8458. #endif
  8459. res = TEST_RES_CHECK(1);
  8460. #endif
  8461. return res;
  8462. }
  8463. /*----------------------------------------------------------------------------*
  8464. | TLS extensions tests
  8465. *----------------------------------------------------------------------------*/
  8466. #ifdef ENABLE_TLS_CALLBACK_TEST
  8467. /* Connection test runner - generic */
  8468. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8469. callback_functions* server_callbacks)
  8470. {
  8471. tcp_ready ready;
  8472. func_args client_args;
  8473. func_args server_args;
  8474. THREAD_TYPE serverThread;
  8475. XMEMSET(&client_args, 0, sizeof(func_args));
  8476. XMEMSET(&server_args, 0, sizeof(func_args));
  8477. StartTCP();
  8478. client_args.callbacks = client_callbacks;
  8479. server_args.callbacks = server_callbacks;
  8480. #ifdef WOLFSSL_TIRTOS
  8481. fdOpenSession(Task_self());
  8482. #endif
  8483. /* RUN Server side */
  8484. InitTcpReady(&ready);
  8485. #if defined(USE_WINDOWS_API)
  8486. /* use RNG to get random port if using windows */
  8487. ready.port = GetRandomPort();
  8488. #endif
  8489. server_args.signal = &ready;
  8490. client_args.signal = &ready;
  8491. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8492. wait_tcp_ready(&server_args);
  8493. /* RUN Client side */
  8494. run_wolfssl_client(&client_args);
  8495. join_thread(serverThread);
  8496. FreeTcpReady(&ready);
  8497. #ifdef WOLFSSL_TIRTOS
  8498. fdCloseSession(Task_self());
  8499. #endif
  8500. client_callbacks->return_code = client_args.return_code;
  8501. server_callbacks->return_code = server_args.return_code;
  8502. }
  8503. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8504. #ifdef HAVE_SNI
  8505. static int test_wolfSSL_UseSNI_params(void)
  8506. {
  8507. EXPECT_DECLS;
  8508. #if !defined(NO_WOLFSSL_CLIENT)
  8509. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8510. WOLFSSL *ssl = wolfSSL_new(ctx);
  8511. ExpectNotNull(ctx);
  8512. ExpectNotNull(ssl);
  8513. /* invalid [ctx|ssl] */
  8514. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8515. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8516. /* invalid type */
  8517. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8518. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8519. /* invalid data */
  8520. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8521. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8522. /* success case */
  8523. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8524. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8525. wolfSSL_free(ssl);
  8526. wolfSSL_CTX_free(ctx);
  8527. #endif /* !NO_WOLFSSL_CLIENT */
  8528. return EXPECT_RESULT();
  8529. }
  8530. /* BEGIN of connection tests callbacks */
  8531. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8532. {
  8533. AssertIntEQ(WOLFSSL_SUCCESS,
  8534. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8535. }
  8536. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8537. {
  8538. AssertIntEQ(WOLFSSL_SUCCESS,
  8539. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8540. }
  8541. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8542. {
  8543. AssertIntEQ(WOLFSSL_SUCCESS,
  8544. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8545. }
  8546. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8547. {
  8548. use_SNI_at_ssl(ssl);
  8549. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8550. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8551. }
  8552. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8553. {
  8554. use_SNI_at_ssl(ssl);
  8555. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8556. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8557. }
  8558. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8559. {
  8560. use_SNI_at_ctx(ctx);
  8561. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8562. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8563. }
  8564. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8565. {
  8566. use_SNI_at_ssl(ssl);
  8567. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8568. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8569. }
  8570. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8571. {
  8572. use_SNI_at_ctx(ctx);
  8573. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8574. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8575. }
  8576. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8577. {
  8578. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8579. }
  8580. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8581. {
  8582. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8583. }
  8584. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8585. {
  8586. byte type = WOLFSSL_SNI_HOST_NAME;
  8587. void* request = (void*) &type; /* to be overwritten */
  8588. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8589. AssertNotNull(request);
  8590. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8591. AssertNull(request);
  8592. }
  8593. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8594. {
  8595. byte type = WOLFSSL_SNI_HOST_NAME;
  8596. void* request = NULL;
  8597. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8598. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8599. AssertNotNull(request);
  8600. AssertStrEQ("www.wolfssl.com", (char*)request);
  8601. }
  8602. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8603. {
  8604. byte type = WOLFSSL_SNI_HOST_NAME;
  8605. void* request = NULL;
  8606. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8607. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8608. AssertNotNull(request);
  8609. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8610. }
  8611. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8612. {
  8613. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8614. }
  8615. /* END of connection tests callbacks */
  8616. static int test_wolfSSL_UseSNI_connection(void)
  8617. {
  8618. int res = TEST_SKIPPED;
  8619. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8620. callback_functions client_cb;
  8621. callback_functions server_cb;
  8622. size_t i;
  8623. #ifdef WOLFSSL_STATIC_MEMORY
  8624. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8625. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8626. #endif
  8627. struct {
  8628. method_provider client_meth;
  8629. method_provider server_meth;
  8630. #ifdef WOLFSSL_STATIC_MEMORY
  8631. wolfSSL_method_func client_meth_ex;
  8632. wolfSSL_method_func server_meth_ex;
  8633. #endif
  8634. } methods[] = {
  8635. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8636. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8637. #ifdef WOLFSSL_STATIC_MEMORY
  8638. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8639. #endif
  8640. },
  8641. #endif
  8642. #ifndef WOLFSSL_NO_TLS12
  8643. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8644. #ifdef WOLFSSL_STATIC_MEMORY
  8645. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8646. #endif
  8647. },
  8648. #endif
  8649. #ifdef WOLFSSL_TLS13
  8650. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  8651. #ifdef WOLFSSL_STATIC_MEMORY
  8652. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  8653. #endif
  8654. },
  8655. #endif
  8656. };
  8657. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  8658. for (i = 0; i < methodsSz; i++) {
  8659. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8660. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8661. client_cb.method = methods[i].client_meth;
  8662. server_cb.method = methods[i].server_meth;
  8663. client_cb.devId = testDevId;
  8664. server_cb.devId = testDevId;
  8665. #ifdef WOLFSSL_STATIC_MEMORY
  8666. client_cb.method_ex = methods[i].client_meth_ex;
  8667. server_cb.method_ex = methods[i].server_meth_ex;
  8668. client_cb.mem = cliMem;
  8669. client_cb.memSz = (word32)sizeof(cliMem);
  8670. server_cb.mem = svrMem;
  8671. server_cb.memSz = (word32)sizeof(svrMem);;
  8672. #endif
  8673. /* success case at ctx */
  8674. fprintf(stderr, "\n\tsuccess case at ctx\n");
  8675. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8676. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8677. test_wolfSSL_client_server(&client_cb, &server_cb);
  8678. /* success case at ssl */
  8679. fprintf(stderr, "\tsuccess case at ssl\n");
  8680. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8681. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8682. test_wolfSSL_client_server(&client_cb, &server_cb);
  8683. /* default mismatch behavior */
  8684. fprintf(stderr, "\tdefault mismatch behavior\n");
  8685. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8686. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8687. test_wolfSSL_client_server(&client_cb, &server_cb);
  8688. /* continue on mismatch */
  8689. fprintf(stderr, "\tcontinue on mismatch\n");
  8690. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8691. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8692. test_wolfSSL_client_server(&client_cb, &server_cb);
  8693. /* fake answer on mismatch */
  8694. fprintf(stderr, "\tfake answer on mismatch\n");
  8695. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8696. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8697. test_wolfSSL_client_server(&client_cb, &server_cb);
  8698. /* sni abort - success */
  8699. fprintf(stderr, "\tsni abort - success\n");
  8700. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8701. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8702. test_wolfSSL_client_server(&client_cb, &server_cb);
  8703. /* sni abort - abort when absent (ctx) */
  8704. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  8705. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8706. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8707. test_wolfSSL_client_server(&client_cb, &server_cb);
  8708. /* sni abort - abort when absent (ssl) */
  8709. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  8710. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8711. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8712. test_wolfSSL_client_server(&client_cb, &server_cb);
  8713. /* sni abort - success when overwritten */
  8714. fprintf(stderr, "\tsni abort - success when overwritten\n");
  8715. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8716. 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;
  8717. test_wolfSSL_client_server(&client_cb, &server_cb);
  8718. /* sni abort - success when allowing mismatches */
  8719. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  8720. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8721. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8722. test_wolfSSL_client_server(&client_cb, &server_cb);
  8723. }
  8724. res = TEST_RES_CHECK(1);
  8725. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8726. return res;
  8727. }
  8728. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8729. {
  8730. EXPECT_DECLS;
  8731. byte buff[] = { /* www.paypal.com */
  8732. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8733. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8734. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8735. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8736. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8737. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8738. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8739. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8740. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8741. };
  8742. byte buff2[] = { /* api.textmate.org */
  8743. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8744. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8745. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8746. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8747. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8748. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8749. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8750. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8751. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8752. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8753. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8754. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8755. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8756. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8757. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8758. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8759. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8760. };
  8761. byte buff3[] = { /* no sni extension */
  8762. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8763. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8764. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8765. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8766. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8767. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8768. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8769. };
  8770. byte buff4[] = { /* last extension has zero size */
  8771. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8772. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8773. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8774. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8775. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8776. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8777. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8778. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8779. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8780. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8781. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8782. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8783. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8784. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8785. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8786. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8787. 0x12, 0x00, 0x00
  8788. };
  8789. byte buff5[] = { /* SSL v2.0 client hello */
  8790. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8791. /* dummy bytes below, just to pass size check */
  8792. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8793. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8794. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8795. };
  8796. byte result[32] = {0};
  8797. word32 length = 32;
  8798. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8799. 0, result, &length));
  8800. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8801. 0, result, &length));
  8802. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8803. 1, result, &length));
  8804. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8805. 0, result, &length));
  8806. buff[0] = 0x16;
  8807. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8808. 0, result, &length));
  8809. buff[1] = 0x03;
  8810. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8811. sizeof(buff), 0, result, &length));
  8812. buff[2] = 0x03;
  8813. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8814. sizeof(buff), 0, result, &length));
  8815. buff[4] = 0x64;
  8816. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8817. 0, result, &length));
  8818. if (EXPECT_SUCCESS())
  8819. result[length] = 0;
  8820. ExpectStrEQ("www.paypal.com", (const char*) result);
  8821. length = 32;
  8822. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8823. 0, result, &length));
  8824. if (EXPECT_SUCCESS())
  8825. result[length] = 0;
  8826. ExpectStrEQ("api.textmate.org", (const char*) result);
  8827. /* SSL v2.0 tests */
  8828. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8829. sizeof(buff5), 0, result, &length));
  8830. buff5[2] = 0x02;
  8831. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8832. sizeof(buff5), 0, result, &length));
  8833. buff5[2] = 0x01; buff5[6] = 0x08;
  8834. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8835. sizeof(buff5), 0, result, &length));
  8836. buff5[6] = 0x09; buff5[8] = 0x01;
  8837. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8838. sizeof(buff5), 0, result, &length));
  8839. return EXPECT_RESULT();
  8840. }
  8841. #endif /* HAVE_SNI */
  8842. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8843. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  8844. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8845. /* Dummy peer functions to satisfy the exporter/importer */
  8846. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  8847. int* ipSz, unsigned short* port, int* fam)
  8848. {
  8849. (void)ssl;
  8850. ip[0] = -1;
  8851. *ipSz = 1;
  8852. *port = 1;
  8853. *fam = 2;
  8854. return 1;
  8855. }
  8856. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  8857. int ipSz, unsigned short port, int fam)
  8858. {
  8859. (void)ssl;
  8860. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  8861. return 0;
  8862. return 1;
  8863. }
  8864. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  8865. WOLFSSL **ssl)
  8866. {
  8867. EXPECT_DECLS;
  8868. unsigned char* sessionBuf = NULL;
  8869. unsigned int sessionSz = 0;
  8870. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  8871. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  8872. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  8873. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  8874. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  8875. ExpectNotNull(sessionBuf =
  8876. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  8877. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  8878. wolfSSL_free(*ssl);
  8879. *ssl = NULL;
  8880. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  8881. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  8882. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  8883. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  8884. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8885. return EXPECT_RESULT();
  8886. }
  8887. #endif
  8888. static int test_wolfSSL_dtls_export_peers(void)
  8889. {
  8890. EXPECT_DECLS;
  8891. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  8892. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  8893. test_ssl_cbf client_cbf;
  8894. test_ssl_cbf server_cbf;
  8895. size_t i, j;
  8896. struct test_params {
  8897. method_provider client_meth;
  8898. method_provider server_meth;
  8899. const char* dtls_version;
  8900. } params[] = {
  8901. #ifndef NO_OLD_TLS
  8902. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  8903. #endif
  8904. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  8905. /* TODO DTLS 1.3 exporting not supported
  8906. #ifdef WOLFSSL_DTLS13
  8907. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  8908. #endif
  8909. */
  8910. };
  8911. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  8912. for (j = 0; j <= 0b11; j++) {
  8913. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  8914. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  8915. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  8916. client_cbf.method = params[i].client_meth;
  8917. server_cbf.method = params[i].server_meth;
  8918. if (j & 0b01) {
  8919. client_cbf.on_handshake =
  8920. test_wolfSSL_dtls_export_peers_on_handshake;
  8921. printf(" With client export;");
  8922. }
  8923. if (j & 0b10) {
  8924. server_cbf.on_handshake =
  8925. test_wolfSSL_dtls_export_peers_on_handshake;
  8926. printf(" With server export;");
  8927. }
  8928. printf("\n");
  8929. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  8930. &server_cbf, NULL), TEST_SUCCESS);
  8931. if (!EXPECT_SUCCESS())
  8932. break;
  8933. }
  8934. }
  8935. #endif
  8936. return EXPECT_RESULT();
  8937. }
  8938. static int test_wolfSSL_UseTrustedCA(void)
  8939. {
  8940. EXPECT_DECLS;
  8941. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8942. && !defined(NO_RSA)
  8943. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8944. WOLFSSL_CTX *ctx = NULL;
  8945. WOLFSSL *ssl = NULL;
  8946. byte id[20];
  8947. #ifndef NO_WOLFSSL_SERVER
  8948. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8949. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8950. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8951. #else
  8952. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8953. #endif
  8954. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8955. XMEMSET(id, 0, sizeof(id));
  8956. /* error cases */
  8957. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8958. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8959. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8960. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8961. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8962. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8963. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8964. #ifdef NO_SHA
  8965. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8966. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8967. #endif
  8968. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8969. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8970. /* success cases */
  8971. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8972. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8973. #ifndef NO_SHA
  8974. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8975. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8976. #endif
  8977. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8978. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8979. wolfSSL_free(ssl);
  8980. wolfSSL_CTX_free(ctx);
  8981. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8982. #endif /* HAVE_TRUSTED_CA */
  8983. return EXPECT_RESULT();
  8984. }
  8985. static int test_wolfSSL_UseMaxFragment(void)
  8986. {
  8987. EXPECT_DECLS;
  8988. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8989. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8990. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8991. #ifndef NO_WOLFSSL_SERVER
  8992. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8993. #else
  8994. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8995. #endif
  8996. WOLFSSL *ssl = NULL;
  8997. #ifdef OPENSSL_EXTRA
  8998. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8999. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9000. #else
  9001. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9002. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9003. #endif
  9004. #ifndef NO_WOLFSSL_SERVER
  9005. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9006. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9007. #endif
  9008. ExpectNotNull(ctx);
  9009. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9010. #ifdef OPENSSL_EXTRA
  9011. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9012. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9013. #else
  9014. UseMaxFragment = wolfSSL_UseMaxFragment;
  9015. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9016. #endif
  9017. /* error cases */
  9018. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9019. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9020. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9021. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9022. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9023. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9024. /* success case */
  9025. #ifdef OPENSSL_EXTRA
  9026. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9027. #else
  9028. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9029. #endif
  9030. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9031. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9032. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9033. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9034. #ifdef OPENSSL_EXTRA
  9035. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9036. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9037. #else
  9038. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9039. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9040. #endif
  9041. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9042. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9043. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9044. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9045. #ifdef OPENSSL_EXTRA
  9046. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9047. #else
  9048. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9049. #endif
  9050. wolfSSL_free(ssl);
  9051. wolfSSL_CTX_free(ctx);
  9052. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9053. #endif
  9054. return EXPECT_RESULT();
  9055. }
  9056. static int test_wolfSSL_UseTruncatedHMAC(void)
  9057. {
  9058. EXPECT_DECLS;
  9059. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9060. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9061. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9062. #ifndef NO_WOLFSSL_SERVER
  9063. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9064. #else
  9065. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9066. #endif
  9067. WOLFSSL *ssl = NULL;
  9068. ExpectNotNull(ctx);
  9069. #ifndef NO_WOLFSSL_SERVER
  9070. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9071. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9072. #endif
  9073. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9074. /* error cases */
  9075. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9076. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9077. /* success case */
  9078. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9079. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9080. wolfSSL_free(ssl);
  9081. wolfSSL_CTX_free(ctx);
  9082. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9083. #endif
  9084. return EXPECT_RESULT();
  9085. }
  9086. static int test_wolfSSL_UseSupportedCurve(void)
  9087. {
  9088. EXPECT_DECLS;
  9089. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9090. !defined(NO_TLS)
  9091. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9092. WOLFSSL *ssl = wolfSSL_new(ctx);
  9093. ExpectNotNull(ctx);
  9094. ExpectNotNull(ssl);
  9095. /* error cases */
  9096. ExpectIntNE(WOLFSSL_SUCCESS,
  9097. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9098. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9099. ExpectIntNE(WOLFSSL_SUCCESS,
  9100. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9101. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9102. /* success case */
  9103. ExpectIntEQ(WOLFSSL_SUCCESS,
  9104. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9105. ExpectIntEQ(WOLFSSL_SUCCESS,
  9106. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9107. wolfSSL_free(ssl);
  9108. wolfSSL_CTX_free(ctx);
  9109. #endif
  9110. return EXPECT_RESULT();
  9111. }
  9112. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9113. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9114. {
  9115. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9116. }
  9117. static void use_ALPN_all(WOLFSSL* ssl)
  9118. {
  9119. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9120. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9121. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9122. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9123. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9124. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9125. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9126. }
  9127. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9128. {
  9129. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9130. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9131. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9132. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9133. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9134. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9135. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9136. }
  9137. static void use_ALPN_one(WOLFSSL* ssl)
  9138. {
  9139. /* spdy/2 */
  9140. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9141. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9142. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9143. }
  9144. static void use_ALPN_unknown(WOLFSSL* ssl)
  9145. {
  9146. /* http/2.0 */
  9147. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9148. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9149. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9150. }
  9151. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9152. {
  9153. /* http/2.0 */
  9154. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9155. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9156. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9157. }
  9158. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9159. {
  9160. /* spdy/3 */
  9161. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9162. char *proto = NULL;
  9163. word16 protoSz = 0;
  9164. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9165. /* check value */
  9166. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9167. if (proto) {
  9168. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9169. }
  9170. }
  9171. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9172. {
  9173. char *proto = NULL;
  9174. word16 protoSz = 0;
  9175. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9176. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9177. /* check value */
  9178. AssertIntEQ(1, (0 == protoSz));
  9179. AssertIntEQ(1, (NULL == proto));
  9180. }
  9181. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9182. {
  9183. /* http/1.1 */
  9184. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9185. char *proto;
  9186. word16 protoSz = 0;
  9187. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9188. /* check value */
  9189. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9190. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9191. }
  9192. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9193. {
  9194. /* spdy/2 */
  9195. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9196. char *proto;
  9197. word16 protoSz = 0;
  9198. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9199. /* check value */
  9200. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9201. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9202. }
  9203. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9204. {
  9205. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9206. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9207. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9208. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9209. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9210. char *clist = NULL;
  9211. word16 clistSz = 0;
  9212. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9213. &clistSz));
  9214. /* check value */
  9215. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9216. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9217. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9218. }
  9219. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9220. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9221. /* ALPN select callback, success with spdy/2 */
  9222. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  9223. unsigned char *outlen, const unsigned char *in,
  9224. unsigned int inlen, void *arg)
  9225. {
  9226. /* spdy/2 */
  9227. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9228. (void)ssl;
  9229. (void)arg;
  9230. /* adding +1 since LEN byte comes first */
  9231. if (inlen < sizeof(proto) + 1) {
  9232. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9233. }
  9234. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  9235. *out = in + 1;
  9236. *outlen = (unsigned char)sizeof(proto);
  9237. return SSL_TLSEXT_ERR_OK;
  9238. }
  9239. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9240. }
  9241. /* ALPN select callback, force failure */
  9242. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  9243. unsigned char *outlen, const unsigned char *in,
  9244. unsigned int inlen, void *arg)
  9245. {
  9246. (void)ssl;
  9247. (void)out;
  9248. (void)outlen;
  9249. (void)in;
  9250. (void)inlen;
  9251. (void)arg;
  9252. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9253. }
  9254. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  9255. {
  9256. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  9257. }
  9258. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  9259. {
  9260. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  9261. }
  9262. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  9263. static int test_wolfSSL_UseALPN_connection(void)
  9264. {
  9265. int res = TEST_SKIPPED;
  9266. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9267. callback_functions client_cb;
  9268. callback_functions server_cb;
  9269. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9270. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9271. client_cb.method = wolfSSLv23_client_method;
  9272. server_cb.method = wolfSSLv23_server_method;
  9273. client_cb.devId = testDevId;
  9274. server_cb.devId = testDevId;
  9275. /* success case same list */
  9276. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9277. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9278. test_wolfSSL_client_server(&client_cb, &server_cb);
  9279. /* success case only one for server */
  9280. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9281. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9282. test_wolfSSL_client_server(&client_cb, &server_cb);
  9283. /* success case only one for client */
  9284. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9285. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9286. test_wolfSSL_client_server(&client_cb, &server_cb);
  9287. /* success case none for client */
  9288. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9289. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9290. test_wolfSSL_client_server(&client_cb, &server_cb);
  9291. /* success case mismatch behavior but option 'continue' set */
  9292. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9293. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9294. test_wolfSSL_client_server(&client_cb, &server_cb);
  9295. /* success case read protocol send by client */
  9296. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9297. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9298. test_wolfSSL_client_server(&client_cb, &server_cb);
  9299. /* mismatch behavior with same list
  9300. * the first and only this one must be taken */
  9301. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9302. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9303. test_wolfSSL_client_server(&client_cb, &server_cb);
  9304. /* default mismatch behavior */
  9305. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9306. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9307. test_wolfSSL_client_server(&client_cb, &server_cb);
  9308. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9309. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9310. /* WOLFSSL-level ALPN select callback tests */
  9311. /* Callback: success (one protocol, spdy/2) */
  9312. client_cb.ctx_ready = NULL;
  9313. client_cb.ssl_ready = use_ALPN_one;
  9314. client_cb.on_result = verify_ALPN_matching_spdy2;
  9315. server_cb.ctx_ready = NULL;
  9316. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  9317. server_cb.on_result = verify_ALPN_matching_spdy2;
  9318. test_wolfSSL_client_server(&client_cb, &server_cb);
  9319. /* Callback: failure (one client protocol, spdy/2) */
  9320. client_cb.ctx_ready = NULL;
  9321. client_cb.ssl_ready = use_ALPN_one;
  9322. client_cb.on_result = NULL;
  9323. server_cb.ctx_ready = NULL;
  9324. server_cb.ssl_ready = use_ALPN_failure_callback;
  9325. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9326. test_wolfSSL_client_server(&client_cb, &server_cb);
  9327. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  9328. res = TEST_RES_CHECK(1);
  9329. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9330. return res;
  9331. }
  9332. static int test_wolfSSL_UseALPN_params(void)
  9333. {
  9334. EXPECT_DECLS;
  9335. #ifndef NO_WOLFSSL_CLIENT
  9336. /* "http/1.1" */
  9337. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9338. /* "spdy/1" */
  9339. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9340. /* "spdy/2" */
  9341. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9342. /* "spdy/3" */
  9343. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9344. char buff[256];
  9345. word32 idx;
  9346. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9347. WOLFSSL *ssl = wolfSSL_new(ctx);
  9348. ExpectNotNull(ctx);
  9349. ExpectNotNull(ssl);
  9350. /* error cases */
  9351. ExpectIntNE(WOLFSSL_SUCCESS,
  9352. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9353. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9354. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9355. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9356. /* success case */
  9357. /* http1 only */
  9358. ExpectIntEQ(WOLFSSL_SUCCESS,
  9359. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9360. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9361. /* http1, spdy1 */
  9362. XMEMCPY(buff, http1, sizeof(http1));
  9363. idx = sizeof(http1);
  9364. buff[idx++] = ',';
  9365. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9366. idx += sizeof(spdy1);
  9367. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9368. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9369. /* http1, spdy2, spdy1 */
  9370. XMEMCPY(buff, http1, sizeof(http1));
  9371. idx = sizeof(http1);
  9372. buff[idx++] = ',';
  9373. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9374. idx += sizeof(spdy2);
  9375. buff[idx++] = ',';
  9376. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9377. idx += sizeof(spdy1);
  9378. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9379. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9380. /* spdy3, http1, spdy2, spdy1 */
  9381. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9382. idx = sizeof(spdy3);
  9383. buff[idx++] = ',';
  9384. XMEMCPY(buff+idx, http1, sizeof(http1));
  9385. idx += sizeof(http1);
  9386. buff[idx++] = ',';
  9387. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9388. idx += sizeof(spdy2);
  9389. buff[idx++] = ',';
  9390. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9391. idx += sizeof(spdy1);
  9392. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9393. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9394. wolfSSL_free(ssl);
  9395. wolfSSL_CTX_free(ctx);
  9396. #endif
  9397. return EXPECT_RESULT();
  9398. }
  9399. #endif /* HAVE_ALPN */
  9400. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9401. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9402. {
  9403. unsigned char p[] = {
  9404. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9405. 6, 's', 'p', 'd', 'y', '/', '2',
  9406. 6, 's', 'p', 'd', 'y', '/', '1',
  9407. };
  9408. unsigned char p_len = sizeof(p);
  9409. int ret;
  9410. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9411. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9412. AssertIntEQ(ret, 0);
  9413. #else
  9414. AssertIntEQ(ret, SSL_SUCCESS);
  9415. #endif
  9416. }
  9417. static void set_alpn_protos(SSL* ssl)
  9418. {
  9419. unsigned char p[] = {
  9420. 6, 's', 'p', 'd', 'y', '/', '3',
  9421. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9422. 6, 's', 'p', 'd', 'y', '/', '2',
  9423. 6, 's', 'p', 'd', 'y', '/', '1',
  9424. };
  9425. unsigned char p_len = sizeof(p);
  9426. int ret;
  9427. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9428. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9429. AssertIntEQ(ret, 0);
  9430. #else
  9431. AssertIntEQ(ret, SSL_SUCCESS);
  9432. #endif
  9433. }
  9434. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9435. {
  9436. /* "spdy/3" */
  9437. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9438. const unsigned char *proto;
  9439. unsigned int protoSz = 0;
  9440. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9441. /* check value */
  9442. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9443. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9444. }
  9445. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9446. {
  9447. /* "http/1.1" */
  9448. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9449. const unsigned char *proto;
  9450. unsigned int protoSz = 0;
  9451. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9452. /* check value */
  9453. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9454. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9455. }
  9456. static int test_wolfSSL_set_alpn_protos(void)
  9457. {
  9458. int res = TEST_SKIPPED;
  9459. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9460. callback_functions client_cb;
  9461. callback_functions server_cb;
  9462. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9463. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9464. client_cb.method = wolfSSLv23_client_method;
  9465. server_cb.method = wolfSSLv23_server_method;
  9466. client_cb.devId = testDevId;
  9467. server_cb.devId = testDevId;
  9468. /* use CTX_alpn_protos */
  9469. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9470. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9471. test_wolfSSL_client_server(&client_cb, &server_cb);
  9472. /* use set_alpn_protos */
  9473. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9474. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9475. test_wolfSSL_client_server(&client_cb, &server_cb);
  9476. res = TEST_SUCCESS;
  9477. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9478. return res;
  9479. }
  9480. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9481. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9482. {
  9483. EXPECT_DECLS;
  9484. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9485. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9486. WOLFSSL *ssl = wolfSSL_new(ctx);
  9487. ExpectNotNull(ctx);
  9488. ExpectNotNull(ssl);
  9489. /* error cases */
  9490. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9491. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9492. /* success cases */
  9493. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9494. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9495. wolfSSL_free(ssl);
  9496. wolfSSL_CTX_free(ctx);
  9497. #endif
  9498. return EXPECT_RESULT();
  9499. }
  9500. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9501. {
  9502. EXPECT_DECLS;
  9503. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9504. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9505. WOLFSSL *ssl = wolfSSL_new(ctx);
  9506. ExpectNotNull(ctx);
  9507. ExpectNotNull(ssl);
  9508. /* error cases */
  9509. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9510. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9511. /* success cases */
  9512. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9513. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9514. wolfSSL_free(ssl);
  9515. wolfSSL_CTX_free(ctx);
  9516. #endif
  9517. return EXPECT_RESULT();
  9518. }
  9519. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9520. static int test_wolfSSL_SCR_Reconnect(void)
  9521. {
  9522. EXPECT_DECLS;
  9523. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9524. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9525. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9526. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9527. struct test_memio_ctx test_ctx;
  9528. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9529. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9530. byte data;
  9531. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9532. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9533. test_ctx.s_ciphers =
  9534. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9535. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9536. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9537. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9538. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9539. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9540. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9541. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9542. /* WOLFSSL_FATAL_ERROR since it will block */
  9543. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9544. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9545. WOLFSSL_ERROR_WANT_READ);
  9546. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9547. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9548. WOLFSSL_ERROR_WANT_READ);
  9549. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9550. wolfSSL_free(ssl_c);
  9551. ssl_c = NULL;
  9552. wolfSSL_free(ssl_s);
  9553. ssl_s = NULL;
  9554. wolfSSL_CTX_free(ctx_c);
  9555. ctx_c = NULL;
  9556. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9557. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9558. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9559. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9560. wolfSSL_free(ssl_s);
  9561. wolfSSL_free(ssl_c);
  9562. wolfSSL_CTX_free(ctx_s);
  9563. wolfSSL_CTX_free(ctx_c);
  9564. #endif
  9565. return EXPECT_RESULT();
  9566. }
  9567. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9568. (!defined(NO_RSA) || defined(HAVE_ECC))
  9569. /* Called when writing. */
  9570. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9571. {
  9572. (void)ssl;
  9573. (void)buf;
  9574. (void)sz;
  9575. (void)ctx;
  9576. /* Force error return from wolfSSL_accept_TLSv13(). */
  9577. return WANT_WRITE;
  9578. }
  9579. /* Called when reading. */
  9580. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9581. {
  9582. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9583. int len = (int)msg->length;
  9584. (void)ssl;
  9585. (void)sz;
  9586. /* Pass back as much of message as will fit in buffer. */
  9587. if (len > sz)
  9588. len = sz;
  9589. XMEMCPY(buf, msg->buffer, len);
  9590. /* Move over returned data. */
  9591. msg->buffer += len;
  9592. msg->length -= len;
  9593. /* Amount actually copied. */
  9594. return len;
  9595. }
  9596. #endif
  9597. /* Test the detection of duplicate known TLS extensions.
  9598. * Specifically in a ClientHello.
  9599. */
  9600. static int test_tls_ext_duplicate(void)
  9601. {
  9602. EXPECT_DECLS;
  9603. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9604. !defined(NO_FILESYSTEM)
  9605. const unsigned char clientHelloDupTlsExt[] = {
  9606. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9607. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9608. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9609. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9610. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9611. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9612. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9613. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9614. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9615. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9616. 0x00, 0x9e, 0x01, 0x00,
  9617. /* Extensions - duplicate signature algorithms. */
  9618. 0x00, 0x19, 0x00, 0x0d,
  9619. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9620. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9621. /* Supported Versions extension for TLS 1.3. */
  9622. 0x00, 0x2b,
  9623. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9624. };
  9625. WOLFSSL_BUFFER_INFO msg;
  9626. const char* testCertFile;
  9627. const char* testKeyFile;
  9628. WOLFSSL_CTX *ctx = NULL;
  9629. WOLFSSL *ssl = NULL;
  9630. #ifndef NO_RSA
  9631. testCertFile = svrCertFile;
  9632. testKeyFile = svrKeyFile;
  9633. #elif defined(HAVE_ECC)
  9634. testCertFile = eccCertFile;
  9635. testKeyFile = eccKeyFile;
  9636. #endif
  9637. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9638. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9639. WOLFSSL_FILETYPE_PEM));
  9640. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9641. WOLFSSL_FILETYPE_PEM));
  9642. /* Read from 'msg'. */
  9643. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9644. /* No where to send to - dummy sender. */
  9645. wolfSSL_SetIOSend(ctx, DummySend);
  9646. ssl = wolfSSL_new(ctx);
  9647. ExpectNotNull(ssl);
  9648. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9649. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  9650. wolfSSL_SetIOReadCtx(ssl, &msg);
  9651. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  9652. /* can return duplicate ext error or socket error if the peer closed down
  9653. * while sending alert */
  9654. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  9655. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  9656. }
  9657. wolfSSL_free(ssl);
  9658. wolfSSL_CTX_free(ctx);
  9659. #endif
  9660. return EXPECT_RESULT();
  9661. }
  9662. /*----------------------------------------------------------------------------*
  9663. | X509 Tests
  9664. *----------------------------------------------------------------------------*/
  9665. static int test_wolfSSL_X509_NAME_get_entry(void)
  9666. {
  9667. EXPECT_DECLS;
  9668. #if !defined(NO_CERTS) && !defined(NO_RSA)
  9669. #if defined(OPENSSL_ALL) || \
  9670. (defined(OPENSSL_EXTRA) && \
  9671. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  9672. /* use openssl like name to test mapping */
  9673. X509_NAME_ENTRY* ne;
  9674. X509_NAME* name;
  9675. X509* x509 = NULL;
  9676. #ifndef NO_FILESYSTEM
  9677. ASN1_STRING* asn;
  9678. char* subCN = NULL;
  9679. #endif
  9680. int idx;
  9681. ASN1_OBJECT *object = NULL;
  9682. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9683. defined(WOLFSSL_NGINX)
  9684. #ifndef NO_BIO
  9685. BIO* bio = NULL;
  9686. #endif
  9687. #endif
  9688. #ifndef NO_FILESYSTEM
  9689. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9690. WOLFSSL_FILETYPE_PEM));
  9691. ExpectNotNull(name = X509_get_subject_name(x509));
  9692. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9693. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9694. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  9695. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  9696. wolfSSL_FreeX509(x509);
  9697. x509 = NULL;
  9698. #endif
  9699. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9700. WOLFSSL_FILETYPE_PEM));
  9701. ExpectNotNull(name = X509_get_subject_name(x509));
  9702. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9703. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9704. defined(WOLFSSL_NGINX)
  9705. #ifndef NO_BIO
  9706. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9707. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  9708. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9709. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9710. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9711. BIO_free(bio);
  9712. #endif
  9713. #endif
  9714. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9715. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9716. wolfSSL_FreeX509(x509);
  9717. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9718. #endif /* !NO_CERTS && !NO_RSA */
  9719. return EXPECT_RESULT();
  9720. }
  9721. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9722. static int test_wolfSSL_PKCS12(void)
  9723. {
  9724. EXPECT_DECLS;
  9725. /* .p12 file is encrypted with DES3 */
  9726. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9727. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9728. * Password Key
  9729. */
  9730. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9731. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9732. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9733. byte buf[6000];
  9734. char file[] = "./certs/test-servercert.p12";
  9735. char order[] = "./certs/ecc-rsa-server.p12";
  9736. #ifdef WC_RC2
  9737. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9738. #endif
  9739. char pass[] = "a password";
  9740. const char goodPsw[] = "wolfSSL test";
  9741. const char badPsw[] = "bad";
  9742. #ifdef HAVE_ECC
  9743. WOLFSSL_X509_NAME *subject = NULL;
  9744. WOLFSSL_X509 *x509 = NULL;
  9745. #endif
  9746. XFILE f = XBADFILE;
  9747. int bytes, ret, goodPswLen, badPswLen;
  9748. WOLFSSL_BIO *bio = NULL;
  9749. WOLFSSL_EVP_PKEY *pkey = NULL;
  9750. WC_PKCS12 *pkcs12 = NULL;
  9751. WC_PKCS12 *pkcs12_2 = NULL;
  9752. WOLFSSL_X509 *cert = NULL;
  9753. WOLFSSL_X509 *tmp = NULL;
  9754. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9755. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9756. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9757. WOLFSSL_CTX *ctx = NULL;
  9758. WOLFSSL *ssl = NULL;
  9759. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9760. #endif
  9761. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9762. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9763. if (f != XBADFILE) {
  9764. XFCLOSE(f);
  9765. f = XBADFILE;
  9766. }
  9767. goodPswLen = (int)XSTRLEN(goodPsw);
  9768. badPswLen = (int)XSTRLEN(badPsw);
  9769. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9770. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9771. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9772. ExpectNotNull(pkcs12);
  9773. BIO_free(bio);
  9774. bio = NULL;
  9775. /* check verify MAC directly */
  9776. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9777. /* check verify MAC fail case directly */
  9778. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9779. /* check verify MAC fail case */
  9780. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9781. ExpectNull(pkey);
  9782. ExpectNull(cert);
  9783. /* check parse with no extra certs kept */
  9784. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9785. 1);
  9786. ExpectNotNull(pkey);
  9787. ExpectNotNull(cert);
  9788. wolfSSL_EVP_PKEY_free(pkey);
  9789. pkey = NULL;
  9790. wolfSSL_X509_free(cert);
  9791. cert = NULL;
  9792. /* check parse with extra certs kept */
  9793. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9794. 1);
  9795. ExpectNotNull(pkey);
  9796. ExpectNotNull(cert);
  9797. ExpectNotNull(ca);
  9798. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9799. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9800. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9801. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9802. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9803. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9804. #else
  9805. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9806. #endif
  9807. /* Copy stack structure */
  9808. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9809. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9810. /* CTX now owns the tmp_ca stack structure */
  9811. tmp_ca = NULL;
  9812. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9813. ExpectNotNull(tmp_ca);
  9814. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9815. /* Check that the main cert is also set */
  9816. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9817. ExpectNotNull(ssl = SSL_new(ctx));
  9818. ExpectNotNull(SSL_get_certificate(ssl));
  9819. SSL_free(ssl);
  9820. SSL_CTX_free(ctx);
  9821. ctx = NULL;
  9822. #endif
  9823. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9824. /* should be 2 other certs on stack */
  9825. ExpectNotNull(tmp = sk_X509_pop(ca));
  9826. X509_free(tmp);
  9827. ExpectNotNull(tmp = sk_X509_pop(ca));
  9828. X509_free(tmp);
  9829. ExpectNull(sk_X509_pop(ca));
  9830. EVP_PKEY_free(pkey);
  9831. pkey = NULL;
  9832. X509_free(cert);
  9833. cert = NULL;
  9834. sk_X509_pop_free(ca, X509_free);
  9835. ca = NULL;
  9836. /* check PKCS12_create */
  9837. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9838. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9839. SSL_SUCCESS);
  9840. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9841. -1, -1, 100, -1, 0)));
  9842. EVP_PKEY_free(pkey);
  9843. pkey = NULL;
  9844. X509_free(cert);
  9845. cert = NULL;
  9846. sk_X509_pop_free(ca, NULL);
  9847. ca = NULL;
  9848. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9849. SSL_SUCCESS);
  9850. PKCS12_free(pkcs12_2);
  9851. pkcs12_2 = NULL;
  9852. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9853. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9854. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9855. 2000, 1, 0)));
  9856. EVP_PKEY_free(pkey);
  9857. pkey = NULL;
  9858. X509_free(cert);
  9859. cert = NULL;
  9860. sk_X509_pop_free(ca, NULL);
  9861. ca = NULL;
  9862. /* convert to DER then back and parse */
  9863. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9864. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9865. PKCS12_free(pkcs12_2);
  9866. pkcs12_2 = NULL;
  9867. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9868. BIO_free(bio);
  9869. bio = NULL;
  9870. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9871. SSL_SUCCESS);
  9872. /* should be 2 other certs on stack */
  9873. ExpectNotNull(tmp = sk_X509_pop(ca));
  9874. X509_free(tmp);
  9875. ExpectNotNull(tmp = sk_X509_pop(ca));
  9876. X509_free(tmp);
  9877. ExpectNull(sk_X509_pop(ca));
  9878. #ifndef NO_RC4
  9879. PKCS12_free(pkcs12_2);
  9880. pkcs12_2 = NULL;
  9881. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9882. NID_pbe_WithSHA1And128BitRC4,
  9883. NID_pbe_WithSHA1And128BitRC4,
  9884. 2000, 1, 0)));
  9885. EVP_PKEY_free(pkey);
  9886. pkey = NULL;
  9887. X509_free(cert);
  9888. cert = NULL;
  9889. sk_X509_pop_free(ca, NULL);
  9890. ca = NULL;
  9891. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9892. SSL_SUCCESS);
  9893. #endif /* NO_RC4 */
  9894. EVP_PKEY_free(pkey);
  9895. pkey = NULL;
  9896. X509_free(cert);
  9897. cert = NULL;
  9898. PKCS12_free(pkcs12);
  9899. pkcs12 = NULL;
  9900. PKCS12_free(pkcs12_2);
  9901. pkcs12_2 = NULL;
  9902. sk_X509_pop_free(ca, NULL);
  9903. ca = NULL;
  9904. #ifdef HAVE_ECC
  9905. /* test order of parsing */
  9906. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9907. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9908. if (f != XBADFILE) {
  9909. XFCLOSE(f);
  9910. f = XBADFILE;
  9911. }
  9912. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9913. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9914. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9915. WOLFSSL_SUCCESS);
  9916. /* check use of pkey after parse */
  9917. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9918. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9919. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9920. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9921. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9922. #else
  9923. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9924. #endif
  9925. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9926. SSL_CTX_free(ctx);
  9927. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9928. #endif
  9929. ExpectNotNull(pkey);
  9930. ExpectNotNull(cert);
  9931. ExpectNotNull(ca);
  9932. /* compare subject lines of certificates */
  9933. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9934. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9935. SSL_FILETYPE_PEM));
  9936. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9937. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9938. X509_free(x509);
  9939. x509 = NULL;
  9940. /* test expected fail case */
  9941. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9942. SSL_FILETYPE_PEM));
  9943. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9944. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9945. X509_free(x509);
  9946. x509 = NULL;
  9947. X509_free(cert);
  9948. cert = NULL;
  9949. /* get subject line from ca stack */
  9950. ExpectNotNull(cert = sk_X509_pop(ca));
  9951. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9952. /* compare subject from certificate in ca to expected */
  9953. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9954. SSL_FILETYPE_PEM));
  9955. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9956. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9957. EVP_PKEY_free(pkey);
  9958. pkey = NULL;
  9959. X509_free(x509);
  9960. x509 = NULL;
  9961. X509_free(cert);
  9962. cert = NULL;
  9963. BIO_free(bio);
  9964. bio = NULL;
  9965. PKCS12_free(pkcs12);
  9966. pkcs12 = NULL;
  9967. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9968. ca = NULL;
  9969. /* test order of parsing */
  9970. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9971. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9972. if (f != XBADFILE) {
  9973. XFCLOSE(f);
  9974. f = XBADFILE;
  9975. }
  9976. /* check verify MAC fail case */
  9977. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9978. ExpectNull(pkey);
  9979. ExpectNull(cert);
  9980. /* check parse with no extra certs kept */
  9981. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9982. 1);
  9983. ExpectNotNull(pkey);
  9984. ExpectNotNull(cert);
  9985. wolfSSL_EVP_PKEY_free(pkey);
  9986. pkey = NULL;
  9987. wolfSSL_X509_free(cert);
  9988. cert = NULL;
  9989. /* check parse with extra certs kept */
  9990. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9991. 1);
  9992. ExpectNotNull(pkey);
  9993. ExpectNotNull(cert);
  9994. ExpectNotNull(ca);
  9995. wolfSSL_EVP_PKEY_free(pkey);
  9996. pkey = NULL;
  9997. wolfSSL_X509_free(cert);
  9998. cert = NULL;
  9999. sk_X509_pop_free(ca, NULL);
  10000. ca = NULL;
  10001. PKCS12_free(pkcs12);
  10002. pkcs12 = NULL;
  10003. #endif /* HAVE_ECC */
  10004. #ifdef WC_RC2
  10005. /* test PKCS#12 with RC2 encryption */
  10006. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10007. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10008. if (f != XBADFILE) {
  10009. XFCLOSE(f);
  10010. f = XBADFILE;
  10011. }
  10012. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10013. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10014. /* check verify MAC fail case */
  10015. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10016. ExpectNull(pkey);
  10017. ExpectNull(cert);
  10018. /* check parse with not extra certs kept */
  10019. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10020. WOLFSSL_SUCCESS);
  10021. ExpectNotNull(pkey);
  10022. ExpectNotNull(cert);
  10023. wolfSSL_EVP_PKEY_free(pkey);
  10024. pkey = NULL;
  10025. wolfSSL_X509_free(cert);
  10026. cert = NULL;
  10027. /* check parse with extra certs kept */
  10028. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10029. WOLFSSL_SUCCESS);
  10030. ExpectNotNull(pkey);
  10031. ExpectNotNull(cert);
  10032. ExpectNotNull(ca);
  10033. wolfSSL_EVP_PKEY_free(pkey);
  10034. wolfSSL_X509_free(cert);
  10035. sk_X509_pop_free(ca, NULL);
  10036. BIO_free(bio);
  10037. bio = NULL;
  10038. PKCS12_free(pkcs12);
  10039. pkcs12 = NULL;
  10040. #endif /* WC_RC2 */
  10041. /* Test i2d_PKCS12_bio */
  10042. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10043. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10044. if (f != XBADFILE)
  10045. XFCLOSE(f);
  10046. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10047. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10048. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10049. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10050. PKCS12_free(pkcs12);
  10051. BIO_free(bio);
  10052. (void)order;
  10053. #endif /* OPENSSL_EXTRA */
  10054. #endif /* HAVE_FIPS */
  10055. return EXPECT_RESULT();
  10056. }
  10057. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10058. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10059. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10060. #define TEST_PKCS8_ENC
  10061. #endif
  10062. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10063. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10064. /* used to keep track if FailTestCallback was called */
  10065. static int failTestCallbackCalled = 0;
  10066. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10067. {
  10068. (void)passwd;
  10069. (void)sz;
  10070. (void)rw;
  10071. (void)userdata;
  10072. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10073. failTestCallbackCalled = 1;
  10074. return -1;
  10075. }
  10076. #endif
  10077. static int test_wolfSSL_no_password_cb(void)
  10078. {
  10079. EXPECT_DECLS;
  10080. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10081. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10082. WOLFSSL_CTX* ctx = NULL;
  10083. byte buff[FOURK_BUF];
  10084. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10085. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10086. XFILE f = XBADFILE;
  10087. int bytes = 0;
  10088. #ifndef NO_WOLFSSL_CLIENT
  10089. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10090. #else
  10091. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10092. #endif
  10093. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10094. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10095. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10096. if (f != XBADFILE) {
  10097. XFCLOSE(f);
  10098. f = XBADFILE;
  10099. }
  10100. ExpectIntLE(bytes, sizeof(buff));
  10101. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10102. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10103. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10104. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10105. if (f != XBADFILE)
  10106. XFCLOSE(f);
  10107. ExpectIntLE(bytes, sizeof(buff));
  10108. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10109. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10110. wolfSSL_CTX_free(ctx);
  10111. /* Password callback should not be called by default */
  10112. ExpectIntEQ(failTestCallbackCalled, 0);
  10113. #endif
  10114. return EXPECT_RESULT();
  10115. }
  10116. #ifdef TEST_PKCS8_ENC
  10117. /* for PKCS8 test case */
  10118. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10119. {
  10120. int flag = 0;
  10121. (void)rw;
  10122. if (userdata != NULL) {
  10123. flag = *((int*)userdata); /* user set data */
  10124. }
  10125. switch (flag) {
  10126. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10127. * can be anything the user wishes to be passed to the callback
  10128. * associated with the WOLFSSL_CTX */
  10129. XSTRNCPY(passwd, "yassl123", sz);
  10130. return 8;
  10131. default:
  10132. return BAD_FUNC_ARG;
  10133. }
  10134. }
  10135. #endif /* TEST_PKCS8_ENC */
  10136. /* Testing functions dealing with PKCS8 */
  10137. static int test_wolfSSL_PKCS8(void)
  10138. {
  10139. EXPECT_DECLS;
  10140. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10141. !defined(WOLFCRYPT_ONLY)
  10142. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10143. byte buff[FOURK_BUF];
  10144. byte der[FOURK_BUF];
  10145. #ifndef NO_RSA
  10146. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10147. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10148. #endif
  10149. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10150. #ifdef HAVE_ECC
  10151. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10152. #endif
  10153. XFILE f = XBADFILE;
  10154. int bytes = 0;
  10155. WOLFSSL_CTX* ctx = NULL;
  10156. #if defined(HAVE_ECC) && !defined(NO_CODING)
  10157. int ret;
  10158. ecc_key key;
  10159. word32 x = 0;
  10160. #endif
  10161. #ifdef TEST_PKCS8_ENC
  10162. #if !defined(NO_RSA) && !defined(NO_SHA)
  10163. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10164. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10165. #endif
  10166. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10167. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10168. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10169. #endif
  10170. int flag;
  10171. #endif
  10172. (void)der;
  10173. #ifndef NO_WOLFSSL_CLIENT
  10174. #ifndef WOLFSSL_NO_TLS12
  10175. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10176. #else
  10177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10178. #endif
  10179. #else
  10180. #ifndef WOLFSSL_NO_TLS12
  10181. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10182. #else
  10183. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10184. #endif
  10185. #endif
  10186. #ifdef TEST_PKCS8_ENC
  10187. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10188. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10189. flag = 1; /* used by password callback as return code */
  10190. #if !defined(NO_RSA) && !defined(NO_SHA)
  10191. /* test loading PEM PKCS8 encrypted file */
  10192. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10193. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10194. if (f != XBADFILE) {
  10195. XFCLOSE(f);
  10196. f = XBADFILE;
  10197. }
  10198. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10199. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10200. /* this next case should fail because of password callback return code */
  10201. flag = 0; /* used by password callback as return code */
  10202. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10203. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10204. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10205. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10206. "yassl123"), 0);
  10207. /* test that error value is returned with a bad password */
  10208. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10209. "bad"), 0);
  10210. /* test loading PEM PKCS8 encrypted file */
  10211. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10212. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10213. if (f != XBADFILE) {
  10214. XFCLOSE(f);
  10215. f = XBADFILE;
  10216. }
  10217. flag = 1; /* used by password callback as return code */
  10218. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10219. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10220. /* this next case should fail because of password callback return code */
  10221. flag = 0; /* used by password callback as return code */
  10222. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10223. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10224. #endif /* !NO_RSA && !NO_SHA */
  10225. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10226. /* test loading PEM PKCS8 encrypted ECC Key file */
  10227. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10228. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10229. if (f != XBADFILE) {
  10230. XFCLOSE(f);
  10231. f = XBADFILE;
  10232. }
  10233. flag = 1; /* used by password callback as return code */
  10234. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10235. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10236. /* this next case should fail because of password callback return code */
  10237. flag = 0; /* used by password callback as return code */
  10238. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10239. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10240. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10241. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10242. "yassl123"), 0);
  10243. /* test that error value is returned with a bad password */
  10244. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10245. "bad"), 0);
  10246. /* test loading DER PKCS8 encrypted ECC Key file */
  10247. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10248. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10249. if (f != XBADFILE) {
  10250. XFCLOSE(f);
  10251. f = XBADFILE;
  10252. }
  10253. flag = 1; /* used by password callback as return code */
  10254. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10255. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10256. /* this next case should fail because of password callback return code */
  10257. flag = 0; /* used by password callback as return code */
  10258. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10259. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10260. /* leave flag as "okay" */
  10261. flag = 1;
  10262. #endif /* HAVE_ECC && !NO_SHA */
  10263. #endif /* TEST_PKCS8_ENC */
  10264. #ifndef NO_RSA
  10265. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10266. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10267. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10268. if (f != XBADFILE) {
  10269. XFCLOSE(f);
  10270. f = XBADFILE;
  10271. }
  10272. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10273. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10274. /* test loading PEM PKCS8 private key file (not encrypted) */
  10275. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10276. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10277. if (f != XBADFILE) {
  10278. XFCLOSE(f);
  10279. f = XBADFILE;
  10280. }
  10281. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10282. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10283. #endif /* !NO_RSA */
  10284. /* Test PKCS8 PEM ECC key no crypt */
  10285. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10286. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10287. if (f != XBADFILE) {
  10288. XFCLOSE(f);
  10289. f = XBADFILE;
  10290. }
  10291. #ifdef HAVE_ECC
  10292. /* Test PKCS8 PEM ECC key no crypt */
  10293. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10294. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10295. #ifndef NO_CODING
  10296. /* decrypt PKCS8 PEM to key in DER format */
  10297. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10298. (word32)sizeof(der), NULL)), 0);
  10299. ret = wc_ecc_init(&key);
  10300. if (ret == 0) {
  10301. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10302. wc_ecc_free(&key);
  10303. }
  10304. ExpectIntEQ(ret, 0);
  10305. #endif
  10306. /* Test PKCS8 DER ECC key no crypt */
  10307. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10308. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10309. if (f != XBADFILE)
  10310. XFCLOSE(f);
  10311. /* Test using a PKCS8 ECC PEM */
  10312. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10313. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10314. #else
  10315. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10316. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10317. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10318. #endif /* HAVE_ECC */
  10319. wolfSSL_CTX_free(ctx);
  10320. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10321. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10322. return EXPECT_RESULT();
  10323. }
  10324. static int test_wolfSSL_PKCS8_ED25519(void)
  10325. {
  10326. EXPECT_DECLS;
  10327. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10328. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10329. defined(HAVE_ED25519_KEY_IMPORT)
  10330. const byte encPrivKey[] = \
  10331. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10332. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10333. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10334. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10335. "2L9W4v7swXkV+X+a1ww=\n"
  10336. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10337. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10338. byte der[FOURK_BUF];
  10339. WOLFSSL_CTX* ctx = NULL;
  10340. int bytes;
  10341. XMEMSET(der, 0, sizeof(der));
  10342. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10343. (word32)sizeof(der), password)), 0);
  10344. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10345. #ifndef NO_WOLFSSL_SERVER
  10346. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10347. #else
  10348. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10349. #endif
  10350. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10351. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10352. wolfSSL_CTX_free(ctx);
  10353. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10354. #endif
  10355. return EXPECT_RESULT();
  10356. }
  10357. static int test_wolfSSL_PKCS8_ED448(void)
  10358. {
  10359. EXPECT_DECLS;
  10360. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10361. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10362. defined(HAVE_ED448_KEY_IMPORT)
  10363. const byte encPrivKey[] = \
  10364. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10365. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10366. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10367. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10368. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10369. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10370. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10371. byte der[FOURK_BUF];
  10372. WOLFSSL_CTX* ctx = NULL;
  10373. int bytes;
  10374. XMEMSET(der, 0, sizeof(der));
  10375. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10376. (word32)sizeof(der), password)), 0);
  10377. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10378. #ifndef NO_WOLFSSL_SERVER
  10379. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10380. #else
  10381. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10382. #endif
  10383. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10384. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10385. wolfSSL_CTX_free(ctx);
  10386. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10387. #endif
  10388. return EXPECT_RESULT();
  10389. }
  10390. /* Testing functions dealing with PKCS5 */
  10391. static int test_wolfSSL_PKCS5(void)
  10392. {
  10393. EXPECT_DECLS;
  10394. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10395. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10396. const char* passwd = "myfipsPa$$W0rd";
  10397. #else
  10398. const char *passwd = "pass1234";
  10399. #endif
  10400. const unsigned char *salt = (unsigned char *)"salt1234";
  10401. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10402. DYNAMIC_TYPE_TMP_BUFFER);
  10403. int ret = 0;
  10404. ExpectNotNull(out);
  10405. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10406. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10407. WOLFSSL_SUCCESS);
  10408. #ifdef WOLFSSL_SHA512
  10409. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10410. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10411. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10412. #endif
  10413. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10414. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10415. return EXPECT_RESULT();
  10416. }
  10417. /* test parsing URI from certificate */
  10418. static int test_wolfSSL_URI(void)
  10419. {
  10420. EXPECT_DECLS;
  10421. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10422. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10423. defined(OPENSSL_EXTRA))
  10424. WOLFSSL_X509* x509 = NULL;
  10425. const char uri[] = "./certs/client-uri-cert.pem";
  10426. const char urn[] = "./certs/client-absolute-urn.pem";
  10427. const char badUri[] = "./certs/client-relative-uri.pem";
  10428. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10429. WOLFSSL_FILETYPE_PEM));
  10430. wolfSSL_FreeX509(x509);
  10431. x509 = NULL;
  10432. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10433. WOLFSSL_FILETYPE_PEM));
  10434. wolfSSL_FreeX509(x509);
  10435. x509 = NULL;
  10436. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10437. && !defined(WOLFSSL_FPKI)
  10438. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10439. WOLFSSL_FILETYPE_PEM));
  10440. #else
  10441. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10442. WOLFSSL_FILETYPE_PEM));
  10443. #endif
  10444. wolfSSL_FreeX509(x509);
  10445. #endif
  10446. return EXPECT_RESULT();
  10447. }
  10448. static int test_wolfSSL_TBS(void)
  10449. {
  10450. EXPECT_DECLS;
  10451. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10452. && defined(OPENSSL_EXTRA)
  10453. WOLFSSL_X509* x509 = NULL;
  10454. const unsigned char* tbs;
  10455. int tbsSz;
  10456. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10457. WOLFSSL_FILETYPE_PEM));
  10458. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10459. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10460. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10461. ExpectIntEQ(tbsSz, 1003);
  10462. wolfSSL_FreeX509(x509);
  10463. #endif
  10464. return EXPECT_RESULT();
  10465. }
  10466. static int test_wolfSSL_X509_verify(void)
  10467. {
  10468. EXPECT_DECLS;
  10469. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10470. defined(OPENSSL_EXTRA)
  10471. WOLFSSL_X509* ca = NULL;
  10472. WOLFSSL_X509* serv = NULL;
  10473. WOLFSSL_EVP_PKEY* pkey = NULL;
  10474. unsigned char buf[2048];
  10475. const unsigned char* pt = NULL;
  10476. int bufSz;
  10477. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10478. WOLFSSL_FILETYPE_PEM));
  10479. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10480. WOLFSSL_SUCCESS);
  10481. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10482. WOLFSSL_SUCCESS);
  10483. ExpectIntEQ(bufSz, 294);
  10484. bufSz = 2048;
  10485. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10486. WOLFSSL_SUCCESS);
  10487. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10488. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10489. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10490. WOLFSSL_FILETYPE_PEM));
  10491. /* success case */
  10492. pt = buf;
  10493. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10494. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10495. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10496. wolfSSL_EVP_PKEY_free(pkey);
  10497. pkey = NULL;
  10498. /* fail case */
  10499. bufSz = 2048;
  10500. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10501. WOLFSSL_SUCCESS);
  10502. pt = buf;
  10503. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10504. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10505. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10506. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10507. wolfSSL_EVP_PKEY_free(pkey);
  10508. wolfSSL_FreeX509(ca);
  10509. wolfSSL_FreeX509(serv);
  10510. #endif
  10511. return EXPECT_RESULT();
  10512. }
  10513. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10514. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10515. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10516. /* create certificate with version 2 */
  10517. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10518. {
  10519. EXPECT_DECLS;
  10520. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10521. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10522. unsigned char *der = NULL, *key = NULL, *pt;
  10523. char *header = NULL, *name = NULL;
  10524. int derSz;
  10525. long keySz;
  10526. XFILE fp = XBADFILE;
  10527. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10528. time_t t;
  10529. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10530. WOLFSSL_FILETYPE_PEM));
  10531. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10532. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10533. WOLFSSL_SUCCESS);
  10534. if (fp != XBADFILE)
  10535. XFCLOSE(fp);
  10536. pt = key;
  10537. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10538. (const unsigned char**)&pt, keySz));
  10539. /* create the version 2 certificate */
  10540. ExpectNotNull(x509v2 = X509_new());
  10541. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10542. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10543. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10544. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10545. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10546. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10547. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10548. t = time(NULL);
  10549. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10550. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10551. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10552. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10553. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10554. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10555. ExpectIntGT(derSz, 0);
  10556. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10557. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10558. /* TODO: Replace with API call */
  10559. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10560. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10561. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10562. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10563. wolfSSL_X509_free(x509);
  10564. wolfSSL_X509_free(x509v2);
  10565. wolfSSL_EVP_PKEY_free(priv);
  10566. wolfSSL_EVP_PKEY_free(pub);
  10567. wolfSSL_ASN1_TIME_free(notBefore);
  10568. wolfSSL_ASN1_TIME_free(notAfter);
  10569. return EXPECT_RESULT();
  10570. }
  10571. /* override certificate version error */
  10572. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10573. {
  10574. EXPECT_DECLS;
  10575. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10576. ExpectIntEQ(store->error, ASN_VERSION_E);
  10577. #else
  10578. ExpectIntEQ(store->error, 0);
  10579. #endif
  10580. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10581. (void)preverify;
  10582. return EXPECT_RESULT() == TEST_SUCCESS;
  10583. }
  10584. /* set verify callback that will override bad certificate version */
  10585. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10586. {
  10587. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10588. return TEST_SUCCESS;
  10589. }
  10590. #endif
  10591. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10592. {
  10593. EXPECT_DECLS;
  10594. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10595. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10596. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10597. test_ssl_cbf func_cb_client;
  10598. test_ssl_cbf func_cb_server;
  10599. /* test server rejects a client certificate that is not version 3 */
  10600. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10601. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10602. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10603. #ifndef WOLFSSL_NO_TLS12
  10604. func_cb_client.method = wolfTLSv1_2_client_method;
  10605. #else
  10606. func_cb_client.method = wolfTLSv1_3_client_method;
  10607. #endif
  10608. #ifndef WOLFSSL_NO_TLS12
  10609. func_cb_server.method = wolfTLSv1_2_server_method;
  10610. #else
  10611. func_cb_server.method = wolfTLSv1_3_server_method;
  10612. #endif
  10613. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10614. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10615. &func_cb_server, NULL), TEST_FAIL);
  10616. #else
  10617. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10618. &func_cb_server, NULL), TEST_SUCCESS);
  10619. #endif
  10620. #endif
  10621. return EXPECT_RESULT();
  10622. }
  10623. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10624. {
  10625. EXPECT_DECLS;
  10626. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10627. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10628. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10629. test_ssl_cbf func_cb_client;
  10630. test_ssl_cbf func_cb_server;
  10631. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10632. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10633. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10634. func_cb_server.ctx_ready = &test_set_override_x509;
  10635. #ifndef WOLFSSL_NO_TLS12
  10636. func_cb_client.method = wolfTLSv1_2_client_method;
  10637. #else
  10638. func_cb_client.method = wolfTLSv1_3_client_method;
  10639. #endif
  10640. #ifndef WOLFSSL_NO_TLS12
  10641. func_cb_server.method = wolfTLSv1_2_server_method;
  10642. #else
  10643. func_cb_server.method = wolfTLSv1_3_server_method;
  10644. #endif
  10645. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10646. &func_cb_server, NULL), TEST_SUCCESS);
  10647. #endif
  10648. return EXPECT_RESULT();
  10649. }
  10650. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  10651. * version allowed.
  10652. * POST: 1 on success.
  10653. */
  10654. static int test_wolfSSL_CTX_SetMinVersion(void)
  10655. {
  10656. int res = TEST_SKIPPED;
  10657. #ifndef NO_WOLFSSL_CLIENT
  10658. int failFlag = WOLFSSL_SUCCESS;
  10659. WOLFSSL_CTX* ctx;
  10660. int itr;
  10661. #ifndef NO_OLD_TLS
  10662. const int versions[] = {
  10663. #ifdef WOLFSSL_ALLOW_TLSV10
  10664. WOLFSSL_TLSV1,
  10665. #endif
  10666. WOLFSSL_TLSV1_1,
  10667. WOLFSSL_TLSV1_2 };
  10668. #elif !defined(WOLFSSL_NO_TLS12)
  10669. const int versions[] = { WOLFSSL_TLSV1_2 };
  10670. #elif defined(WOLFSSL_TLS13)
  10671. const int versions[] = { WOLFSSL_TLSV1_3 };
  10672. #else
  10673. const int versions[0];
  10674. #endif
  10675. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10676. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  10677. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  10678. != WOLFSSL_SUCCESS) {
  10679. failFlag = WOLFSSL_FAILURE;
  10680. }
  10681. }
  10682. wolfSSL_CTX_free(ctx);
  10683. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  10684. #endif
  10685. return res;
  10686. } /* END test_wolfSSL_CTX_SetMinVersion */
  10687. /*----------------------------------------------------------------------------*
  10688. | OCSP Stapling
  10689. *----------------------------------------------------------------------------*/
  10690. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10691. * need to contact the CA, lowering the cost of cert revocation checking.
  10692. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10693. * POST: 1 returned for success.
  10694. */
  10695. static int test_wolfSSL_UseOCSPStapling(void)
  10696. {
  10697. EXPECT_DECLS;
  10698. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10699. !defined(NO_WOLFSSL_CLIENT)
  10700. WOLFSSL_CTX* ctx = NULL;
  10701. WOLFSSL* ssl = NULL;
  10702. #ifndef NO_WOLFSSL_CLIENT
  10703. #ifndef WOLFSSL_NO_TLS12
  10704. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10705. #else
  10706. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10707. #endif
  10708. #else
  10709. #ifndef WOLFSSL_NO_TLS12
  10710. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10711. #else
  10712. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10713. #endif
  10714. #endif
  10715. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10716. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10717. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10718. #ifndef NO_WOLFSSL_CLIENT
  10719. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10720. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10721. #else
  10722. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10723. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10724. #endif
  10725. wolfSSL_free(ssl);
  10726. wolfSSL_CTX_free(ctx);
  10727. #endif
  10728. return EXPECT_RESULT();
  10729. } /* END test_wolfSSL_UseOCSPStapling */
  10730. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10731. * stapling eliminates the need to contact the CA and lowers cert revocation
  10732. * check.
  10733. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10734. */
  10735. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10736. {
  10737. EXPECT_DECLS;
  10738. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10739. !defined(NO_WOLFSSL_CLIENT)
  10740. WOLFSSL_CTX* ctx = NULL;
  10741. WOLFSSL* ssl = NULL;
  10742. #ifndef NO_WOLFSSL_CLIENT
  10743. #ifndef WOLFSSL_NO_TLS12
  10744. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10745. #else
  10746. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10747. #endif
  10748. #else
  10749. #ifndef WOLFSSL_NO_TLS12
  10750. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10751. #else
  10752. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10753. #endif
  10754. #endif
  10755. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10756. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10757. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10758. #ifndef NO_WOLFSSL_CLIENT
  10759. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10760. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10761. #else
  10762. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10763. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10764. #endif
  10765. wolfSSL_free(ssl);
  10766. wolfSSL_CTX_free(ctx);
  10767. #endif
  10768. return EXPECT_RESULT();
  10769. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10770. /*----------------------------------------------------------------------------*
  10771. | Multicast Tests
  10772. *----------------------------------------------------------------------------*/
  10773. static int test_wolfSSL_mcast(void)
  10774. {
  10775. EXPECT_DECLS;
  10776. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10777. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10778. WOLFSSL_CTX* ctx = NULL;
  10779. WOLFSSL* ssl = NULL;
  10780. byte preMasterSecret[512];
  10781. byte clientRandom[32];
  10782. byte serverRandom[32];
  10783. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10784. byte buf[256];
  10785. word16 newId;
  10786. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10787. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10788. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10789. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10790. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10791. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10792. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10793. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10794. WOLFSSL_SUCCESS);
  10795. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10796. ExpectIntLE(newId, 100);
  10797. wolfSSL_free(ssl);
  10798. wolfSSL_CTX_free(ctx);
  10799. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10800. * WOLFSSL_SNIFFER) */
  10801. return EXPECT_RESULT();
  10802. }
  10803. /*----------------------------------------------------------------------------*
  10804. | Wolfcrypt
  10805. *----------------------------------------------------------------------------*/
  10806. /*
  10807. * Unit test for the wc_InitBlake2b()
  10808. */
  10809. static int test_wc_InitBlake2b(void)
  10810. {
  10811. EXPECT_DECLS;
  10812. #ifdef HAVE_BLAKE2
  10813. Blake2b blake;
  10814. /* Test good arg. */
  10815. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  10816. /* Test bad arg. */
  10817. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  10818. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  10819. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  10820. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  10821. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  10822. #endif
  10823. return EXPECT_RESULT();
  10824. } /* END test_wc_InitBlake2b*/
  10825. /*
  10826. * Unit test for the wc_InitBlake2b_WithKey()
  10827. */
  10828. static int test_wc_InitBlake2b_WithKey(void)
  10829. {
  10830. EXPECT_DECLS;
  10831. #ifdef HAVE_BLAKE2
  10832. Blake2b blake;
  10833. word32 digestSz = BLAKE2B_KEYBYTES;
  10834. byte key[BLAKE2B_KEYBYTES];
  10835. word32 keylen = BLAKE2B_KEYBYTES;
  10836. XMEMSET(key, 0, sizeof(key));
  10837. /* Test good arg. */
  10838. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  10839. /* Test bad args. */
  10840. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  10841. BAD_FUNC_ARG);
  10842. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  10843. BAD_FUNC_ARG);
  10844. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  10845. #endif
  10846. return EXPECT_RESULT();
  10847. } /* END wc_InitBlake2b_WithKey*/
  10848. /*
  10849. * Unit test for the wc_InitBlake2s_WithKey()
  10850. */
  10851. static int test_wc_InitBlake2s_WithKey(void)
  10852. {
  10853. EXPECT_DECLS;
  10854. #ifdef HAVE_BLAKE2S
  10855. Blake2s blake;
  10856. word32 digestSz = BLAKE2S_KEYBYTES;
  10857. byte *key = (byte*)"01234567890123456789012345678901";
  10858. word32 keylen = BLAKE2S_KEYBYTES;
  10859. /* Test good arg. */
  10860. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  10861. /* Test bad args. */
  10862. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  10863. BAD_FUNC_ARG);
  10864. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  10865. BAD_FUNC_ARG);
  10866. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  10867. #endif
  10868. return EXPECT_RESULT();
  10869. } /* END wc_InitBlake2s_WithKey*/
  10870. /*
  10871. * Unit test for the wc_InitMd5()
  10872. */
  10873. static int test_wc_InitMd5(void)
  10874. {
  10875. EXPECT_DECLS;
  10876. #ifndef NO_MD5
  10877. wc_Md5 md5;
  10878. /* Test good arg. */
  10879. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10880. /* Test bad arg. */
  10881. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  10882. wc_Md5Free(&md5);
  10883. #endif
  10884. return EXPECT_RESULT();
  10885. } /* END test_wc_InitMd5 */
  10886. /*
  10887. * Testing wc_UpdateMd5()
  10888. */
  10889. static int test_wc_Md5Update(void)
  10890. {
  10891. EXPECT_DECLS;
  10892. #ifndef NO_MD5
  10893. wc_Md5 md5;
  10894. byte hash[WC_MD5_DIGEST_SIZE];
  10895. testVector a, b, c;
  10896. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10897. /* Input */
  10898. a.input = "a";
  10899. a.inLen = XSTRLEN(a.input);
  10900. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  10901. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10902. /* Update input. */
  10903. a.input = "abc";
  10904. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10905. "\x72";
  10906. a.inLen = XSTRLEN(a.input);
  10907. a.outLen = XSTRLEN(a.output);
  10908. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  10909. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10910. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  10911. /* Pass in bad values. */
  10912. b.input = NULL;
  10913. b.inLen = 0;
  10914. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  10915. c.input = NULL;
  10916. c.inLen = WC_MD5_DIGEST_SIZE;
  10917. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  10918. BAD_FUNC_ARG);
  10919. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  10920. BAD_FUNC_ARG);
  10921. wc_Md5Free(&md5);
  10922. #endif
  10923. return EXPECT_RESULT();
  10924. } /* END test_wc_Md5Update() */
  10925. /*
  10926. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10927. */
  10928. static int test_wc_Md5Final(void)
  10929. {
  10930. EXPECT_DECLS;
  10931. #ifndef NO_MD5
  10932. /* Instantiate */
  10933. wc_Md5 md5;
  10934. byte* hash_test[3];
  10935. byte hash1[WC_MD5_DIGEST_SIZE];
  10936. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10937. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10938. int times, i;
  10939. /* Initialize */
  10940. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10941. hash_test[0] = hash1;
  10942. hash_test[1] = hash2;
  10943. hash_test[2] = hash3;
  10944. times = sizeof(hash_test)/sizeof(byte*);
  10945. for (i = 0; i < times; i++) {
  10946. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  10947. }
  10948. /* Test bad args. */
  10949. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  10950. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  10951. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  10952. wc_Md5Free(&md5);
  10953. #endif
  10954. return EXPECT_RESULT();
  10955. }
  10956. /*
  10957. * Unit test for the wc_InitSha()
  10958. */
  10959. static int test_wc_InitSha(void)
  10960. {
  10961. EXPECT_DECLS;
  10962. #ifndef NO_SHA
  10963. wc_Sha sha;
  10964. /* Test good arg. */
  10965. ExpectIntEQ(wc_InitSha(&sha), 0);
  10966. /* Test bad arg. */
  10967. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  10968. wc_ShaFree(&sha);
  10969. #endif
  10970. return EXPECT_RESULT();
  10971. } /* END test_wc_InitSha */
  10972. /*
  10973. * Tesing wc_ShaUpdate()
  10974. */
  10975. static int test_wc_ShaUpdate(void)
  10976. {
  10977. EXPECT_DECLS;
  10978. #ifndef NO_SHA
  10979. wc_Sha sha;
  10980. byte hash[WC_SHA_DIGEST_SIZE];
  10981. testVector a, b, c;
  10982. ExpectIntEQ(wc_InitSha(&sha), 0);
  10983. /* Input. */
  10984. a.input = "a";
  10985. a.inLen = XSTRLEN(a.input);
  10986. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  10987. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  10988. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10989. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10990. /* Update input. */
  10991. a.input = "abc";
  10992. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10993. "\x6C\x9C\xD0\xD8\x9D";
  10994. a.inLen = XSTRLEN(a.input);
  10995. a.outLen = XSTRLEN(a.output);
  10996. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10997. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10998. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  10999. /* Try passing in bad values. */
  11000. b.input = NULL;
  11001. b.inLen = 0;
  11002. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11003. c.input = NULL;
  11004. c.inLen = WC_SHA_DIGEST_SIZE;
  11005. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11006. BAD_FUNC_ARG);
  11007. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11008. BAD_FUNC_ARG);
  11009. wc_ShaFree(&sha);
  11010. #endif
  11011. return EXPECT_RESULT();
  11012. } /* END test_wc_ShaUpdate() */
  11013. /*
  11014. * Unit test on wc_ShaFinal
  11015. */
  11016. static int test_wc_ShaFinal(void)
  11017. {
  11018. EXPECT_DECLS;
  11019. #ifndef NO_SHA
  11020. wc_Sha sha;
  11021. byte* hash_test[3];
  11022. byte hash1[WC_SHA_DIGEST_SIZE];
  11023. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11024. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11025. int times, i;
  11026. /* Initialize*/
  11027. ExpectIntEQ(wc_InitSha(&sha), 0);
  11028. hash_test[0] = hash1;
  11029. hash_test[1] = hash2;
  11030. hash_test[2] = hash3;
  11031. times = sizeof(hash_test)/sizeof(byte*);
  11032. for (i = 0; i < times; i++) {
  11033. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11034. }
  11035. /* Test bad args. */
  11036. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11037. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11038. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11039. wc_ShaFree(&sha);
  11040. #endif
  11041. return EXPECT_RESULT();
  11042. } /* END test_wc_ShaFinal */
  11043. /*
  11044. * Unit test for wc_InitSha256()
  11045. */
  11046. static int test_wc_InitSha256(void)
  11047. {
  11048. EXPECT_DECLS;
  11049. #ifndef NO_SHA256
  11050. wc_Sha256 sha256;
  11051. /* Test good arg. */
  11052. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11053. /* Test bad arg. */
  11054. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11055. wc_Sha256Free(&sha256);
  11056. #endif
  11057. return EXPECT_RESULT();
  11058. } /* END test_wc_InitSha256 */
  11059. /*
  11060. * Unit test for wc_Sha256Update()
  11061. */
  11062. static int test_wc_Sha256Update(void)
  11063. {
  11064. EXPECT_DECLS;
  11065. #ifndef NO_SHA256
  11066. wc_Sha256 sha256;
  11067. byte hash[WC_SHA256_DIGEST_SIZE];
  11068. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11069. testVector a, b, c;
  11070. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11071. /* Input. */
  11072. a.input = "a";
  11073. a.inLen = XSTRLEN(a.input);
  11074. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11075. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11076. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11077. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11078. /* Update input. */
  11079. a.input = "abc";
  11080. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11081. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11082. "\x15\xAD";
  11083. a.inLen = XSTRLEN(a.input);
  11084. a.outLen = XSTRLEN(a.output);
  11085. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11086. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11087. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11088. /* Unaligned check. */
  11089. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11090. 0);
  11091. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11092. /* Try passing in bad values */
  11093. b.input = NULL;
  11094. b.inLen = 0;
  11095. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11096. c.input = NULL;
  11097. c.inLen = WC_SHA256_DIGEST_SIZE;
  11098. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11099. BAD_FUNC_ARG);
  11100. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11101. BAD_FUNC_ARG);
  11102. wc_Sha256Free(&sha256);
  11103. #endif
  11104. return EXPECT_RESULT();
  11105. } /* END test_wc_Sha256Update */
  11106. /*
  11107. * Unit test function for wc_Sha256Final()
  11108. */
  11109. static int test_wc_Sha256Final(void)
  11110. {
  11111. EXPECT_DECLS;
  11112. #ifndef NO_SHA256
  11113. wc_Sha256 sha256;
  11114. byte* hash_test[3];
  11115. byte hash1[WC_SHA256_DIGEST_SIZE];
  11116. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11117. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11118. int times, i;
  11119. /* Initialize */
  11120. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11121. hash_test[0] = hash1;
  11122. hash_test[1] = hash2;
  11123. hash_test[2] = hash3;
  11124. times = sizeof(hash_test) / sizeof(byte*);
  11125. for (i = 0; i < times; i++) {
  11126. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11127. }
  11128. /* Test bad args. */
  11129. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11130. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11131. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11132. wc_Sha256Free(&sha256);
  11133. #endif
  11134. return EXPECT_RESULT();
  11135. } /* END test_wc_Sha256Final */
  11136. /*
  11137. * Unit test function for wc_Sha256FinalRaw()
  11138. */
  11139. static int test_wc_Sha256FinalRaw(void)
  11140. {
  11141. EXPECT_DECLS;
  11142. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11143. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11144. !defined(WOLFSSL_NO_HASH_RAW)
  11145. wc_Sha256 sha256;
  11146. byte* hash_test[3];
  11147. byte hash1[WC_SHA256_DIGEST_SIZE];
  11148. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11149. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11150. int times, i;
  11151. /* Initialize */
  11152. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11153. hash_test[0] = hash1;
  11154. hash_test[1] = hash2;
  11155. hash_test[2] = hash3;
  11156. times = sizeof(hash_test) / sizeof(byte*);
  11157. for (i = 0; i < times; i++) {
  11158. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11159. }
  11160. /* Test bad args. */
  11161. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11162. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11163. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11164. wc_Sha256Free(&sha256);
  11165. #endif
  11166. return EXPECT_RESULT();
  11167. } /* END test_wc_Sha256FinalRaw */
  11168. /*
  11169. * Unit test function for wc_Sha256GetFlags()
  11170. */
  11171. static int test_wc_Sha256GetFlags(void)
  11172. {
  11173. EXPECT_DECLS;
  11174. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11175. wc_Sha256 sha256;
  11176. word32 flags = 0;
  11177. /* Initialize */
  11178. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11179. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11180. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11181. wc_Sha256Free(&sha256);
  11182. #endif
  11183. return EXPECT_RESULT();
  11184. } /* END test_wc_Sha256GetFlags */
  11185. /*
  11186. * Unit test function for wc_Sha256Free()
  11187. */
  11188. static int test_wc_Sha256Free(void)
  11189. {
  11190. EXPECT_DECLS;
  11191. #ifndef NO_SHA256
  11192. wc_Sha256Free(NULL);
  11193. /* Set result to SUCCESS. */
  11194. ExpectTrue(1);
  11195. #endif
  11196. return EXPECT_RESULT();
  11197. } /* END test_wc_Sha256Free */
  11198. /*
  11199. * Unit test function for wc_Sha256GetHash()
  11200. */
  11201. static int test_wc_Sha256GetHash(void)
  11202. {
  11203. EXPECT_DECLS;
  11204. #ifndef NO_SHA256
  11205. wc_Sha256 sha256;
  11206. byte hash1[WC_SHA256_DIGEST_SIZE];
  11207. /* Initialize */
  11208. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11209. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11210. /* test bad arguments*/
  11211. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11212. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11213. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11214. wc_Sha256Free(&sha256);
  11215. #endif
  11216. return EXPECT_RESULT();
  11217. } /* END test_wc_Sha256GetHash */
  11218. /*
  11219. * Unit test function for wc_Sha256Copy()
  11220. */
  11221. static int test_wc_Sha256Copy(void)
  11222. {
  11223. EXPECT_DECLS;
  11224. #ifndef NO_SHA256
  11225. wc_Sha256 sha256;
  11226. wc_Sha256 temp;
  11227. XMEMSET(&sha256, 0, sizeof(sha256));
  11228. XMEMSET(&temp, 0, sizeof(temp));
  11229. /* Initialize */
  11230. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11231. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11232. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11233. /* test bad arguments*/
  11234. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11235. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11236. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11237. wc_Sha256Free(&sha256);
  11238. wc_Sha256Free(&temp);
  11239. #endif
  11240. return EXPECT_RESULT();
  11241. } /* END test_wc_Sha256Copy */
  11242. /*
  11243. * Testing wc_InitSha512()
  11244. */
  11245. static int test_wc_InitSha512(void)
  11246. {
  11247. EXPECT_DECLS;
  11248. #ifdef WOLFSSL_SHA512
  11249. wc_Sha512 sha512;
  11250. /* Test good arg. */
  11251. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11252. /* Test bad arg. */
  11253. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11254. wc_Sha512Free(&sha512);
  11255. #endif
  11256. return EXPECT_RESULT();
  11257. } /* END test_wc_InitSha512 */
  11258. /*
  11259. * wc_Sha512Update() test.
  11260. */
  11261. static int test_wc_Sha512Update(void)
  11262. {
  11263. EXPECT_DECLS;
  11264. #ifdef WOLFSSL_SHA512
  11265. wc_Sha512 sha512;
  11266. byte hash[WC_SHA512_DIGEST_SIZE];
  11267. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11268. testVector a, b, c;
  11269. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11270. /* Input. */
  11271. a.input = "a";
  11272. a.inLen = XSTRLEN(a.input);
  11273. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11274. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11275. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11276. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11277. /* Update input. */
  11278. a.input = "abc";
  11279. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11280. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11281. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11282. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11283. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11284. a.inLen = XSTRLEN(a.input);
  11285. a.outLen = XSTRLEN(a.output);
  11286. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11287. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11288. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11289. /* Unaligned check. */
  11290. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11291. 0);
  11292. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11293. /* Try passing in bad values */
  11294. b.input = NULL;
  11295. b.inLen = 0;
  11296. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11297. c.input = NULL;
  11298. c.inLen = WC_SHA512_DIGEST_SIZE;
  11299. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11300. BAD_FUNC_ARG);
  11301. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11302. BAD_FUNC_ARG);
  11303. wc_Sha512Free(&sha512);
  11304. #endif
  11305. return EXPECT_RESULT();
  11306. } /* END test_wc_Sha512Update */
  11307. #ifdef WOLFSSL_SHA512
  11308. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11309. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11310. /* Performs test for
  11311. * - wc_Sha512Final/wc_Sha512FinalRaw
  11312. * - wc_Sha512_224Final/wc_Sha512_224Final
  11313. * - wc_Sha512_256Final/wc_Sha512_256Final
  11314. * parameter:
  11315. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11316. * WC_HASH_TYPE_SHA512_256
  11317. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11318. *return 0 on success
  11319. */
  11320. static int test_Sha512_Family_Final(int type, int isRaw)
  11321. {
  11322. EXPECT_DECLS;
  11323. wc_Sha512 sha512;
  11324. byte* hash_test[3];
  11325. byte hash1[WC_SHA512_DIGEST_SIZE];
  11326. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11327. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11328. int times, i;
  11329. int(*initFp)(wc_Sha512*);
  11330. int(*finalFp)(wc_Sha512*, byte*);
  11331. void(*freeFp)(wc_Sha512*);
  11332. if (type == WC_HASH_TYPE_SHA512) {
  11333. initFp = wc_InitSha512;
  11334. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11335. !defined(WOLFSSL_NO_HASH_RAW)
  11336. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11337. #else
  11338. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11339. #endif
  11340. freeFp = wc_Sha512Free;
  11341. }
  11342. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11343. #if !defined(WOLFSSL_NOSHA512_224)
  11344. else if (type == WC_HASH_TYPE_SHA512_224) {
  11345. initFp = wc_InitSha512_224;
  11346. #if !defined(WOLFSSL_NO_HASH_RAW)
  11347. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11348. #else
  11349. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11350. #endif
  11351. freeFp = wc_Sha512_224Free;
  11352. }
  11353. #endif
  11354. #if !defined(WOLFSSL_NOSHA512_256)
  11355. else if (type == WC_HASH_TYPE_SHA512_256) {
  11356. initFp = wc_InitSha512_256;
  11357. #if !defined(WOLFSSL_NO_HASH_RAW)
  11358. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11359. #else
  11360. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11361. #endif
  11362. freeFp = wc_Sha512_256Free;
  11363. }
  11364. #endif
  11365. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11366. else
  11367. return TEST_FAIL;
  11368. /* Initialize */
  11369. ExpectIntEQ(initFp(&sha512), 0);
  11370. hash_test[0] = hash1;
  11371. hash_test[1] = hash2;
  11372. hash_test[2] = hash3;
  11373. times = sizeof(hash_test) / sizeof(byte *);
  11374. /* Good test args. */
  11375. for (i = 0; i < times; i++) {
  11376. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11377. }
  11378. /* Test bad args. */
  11379. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11380. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11381. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11382. freeFp(&sha512);
  11383. return EXPECT_RESULT();
  11384. }
  11385. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11386. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11387. #endif /* WOLFSSL_SHA512 */
  11388. /*
  11389. * Unit test function for wc_Sha512Final()
  11390. */
  11391. static int test_wc_Sha512Final(void)
  11392. {
  11393. EXPECT_DECLS;
  11394. #ifdef WOLFSSL_SHA512
  11395. wc_Sha512 sha512;
  11396. byte* hash_test[3];
  11397. byte hash1[WC_SHA512_DIGEST_SIZE];
  11398. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11399. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11400. int times, i;
  11401. /* Initialize */
  11402. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11403. hash_test[0] = hash1;
  11404. hash_test[1] = hash2;
  11405. hash_test[2] = hash3;
  11406. times = sizeof(hash_test) / sizeof(byte *);
  11407. for (i = 0; i < times; i++) {
  11408. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11409. }
  11410. /* Test bad args. */
  11411. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11412. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11413. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11414. wc_Sha512Free(&sha512);
  11415. #endif
  11416. return EXPECT_RESULT();
  11417. } /* END test_wc_Sha512Final */
  11418. /*
  11419. * Unit test function for wc_Sha512GetFlags()
  11420. */
  11421. static int test_wc_Sha512GetFlags(void)
  11422. {
  11423. EXPECT_DECLS;
  11424. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11425. wc_Sha512 sha512;
  11426. word32 flags = 0;
  11427. /* Initialize */
  11428. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11429. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11430. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11431. wc_Sha512Free(&sha512);
  11432. #endif
  11433. return EXPECT_RESULT();
  11434. } /* END test_wc_Sha512GetFlags */
  11435. /*
  11436. * Unit test function for wc_Sha512FinalRaw()
  11437. */
  11438. static int test_wc_Sha512FinalRaw(void)
  11439. {
  11440. EXPECT_DECLS;
  11441. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11442. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11443. !defined(WOLFSSL_NO_HASH_RAW)
  11444. wc_Sha512 sha512;
  11445. byte* hash_test[3];
  11446. byte hash1[WC_SHA512_DIGEST_SIZE];
  11447. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11448. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11449. int times, i;
  11450. /* Initialize */
  11451. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11452. hash_test[0] = hash1;
  11453. hash_test[1] = hash2;
  11454. hash_test[2] = hash3;
  11455. times = sizeof(hash_test) / sizeof(byte*);
  11456. /* Good test args. */
  11457. for (i = 0; i < times; i++) {
  11458. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11459. }
  11460. /* Test bad args. */
  11461. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11462. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11463. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11464. wc_Sha512Free(&sha512);
  11465. #endif
  11466. return EXPECT_RESULT();
  11467. } /* END test_wc_Sha512FinalRaw */
  11468. /*
  11469. * Unit test function for wc_Sha512Free()
  11470. */
  11471. static int test_wc_Sha512Free(void)
  11472. {
  11473. EXPECT_DECLS;
  11474. #ifdef WOLFSSL_SHA512
  11475. wc_Sha512Free(NULL);
  11476. /* Set result to SUCCESS. */
  11477. ExpectTrue(1);
  11478. #endif
  11479. return EXPECT_RESULT();
  11480. } /* END test_wc_Sha512Free */
  11481. #ifdef WOLFSSL_SHA512
  11482. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11483. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11484. static int test_Sha512_Family_GetHash(int type )
  11485. {
  11486. EXPECT_DECLS;
  11487. int(*initFp)(wc_Sha512*);
  11488. int(*ghashFp)(wc_Sha512*, byte*);
  11489. wc_Sha512 sha512;
  11490. byte hash1[WC_SHA512_DIGEST_SIZE];
  11491. if (type == WC_HASH_TYPE_SHA512) {
  11492. initFp = wc_InitSha512;
  11493. ghashFp = wc_Sha512GetHash;
  11494. }
  11495. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11496. #if !defined(WOLFSSL_NOSHA512_224)
  11497. else if (type == WC_HASH_TYPE_SHA512_224) {
  11498. initFp = wc_InitSha512_224;
  11499. ghashFp = wc_Sha512_224GetHash;
  11500. }
  11501. #endif
  11502. #if !defined(WOLFSSL_NOSHA512_256)
  11503. else if (type == WC_HASH_TYPE_SHA512_256) {
  11504. initFp = wc_InitSha512_256;
  11505. ghashFp = wc_Sha512_256GetHash;
  11506. }
  11507. #endif
  11508. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11509. else {
  11510. initFp = NULL;
  11511. ghashFp = NULL;
  11512. }
  11513. if (initFp == NULL || ghashFp == NULL)
  11514. return TEST_FAIL;
  11515. ExpectIntEQ(initFp(&sha512), 0);
  11516. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11517. /* test bad arguments*/
  11518. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11519. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11520. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11521. wc_Sha512Free(&sha512);
  11522. return EXPECT_RESULT();
  11523. }
  11524. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11525. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11526. #endif /* WOLFSSL_SHA512 */
  11527. /*
  11528. * Unit test function for wc_Sha512GetHash()
  11529. */
  11530. static int test_wc_Sha512GetHash(void)
  11531. {
  11532. EXPECT_DECLS;
  11533. #ifdef WOLFSSL_SHA512
  11534. wc_Sha512 sha512;
  11535. byte hash1[WC_SHA512_DIGEST_SIZE];
  11536. /* Initialize */
  11537. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11538. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11539. /* test bad arguments*/
  11540. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11541. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11542. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11543. wc_Sha512Free(&sha512);
  11544. #endif
  11545. return EXPECT_RESULT();
  11546. } /* END test_wc_Sha512GetHash */
  11547. /*
  11548. * Unit test function for wc_Sha512Copy()
  11549. */
  11550. static int test_wc_Sha512Copy(void)
  11551. {
  11552. EXPECT_DECLS;
  11553. #ifdef WOLFSSL_SHA512
  11554. wc_Sha512 sha512;
  11555. wc_Sha512 temp;
  11556. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11557. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11558. /* Initialize */
  11559. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11560. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11561. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11562. /* test bad arguments*/
  11563. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11564. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11565. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11566. wc_Sha512Free(&sha512);
  11567. wc_Sha512Free(&temp);
  11568. #endif
  11569. return EXPECT_RESULT();
  11570. } /* END test_wc_Sha512Copy */
  11571. static int test_wc_InitSha512_224(void)
  11572. {
  11573. EXPECT_DECLS;
  11574. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11575. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11576. wc_Sha512 sha512;
  11577. /* Test good arg. */
  11578. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11579. /* Test bad arg. */
  11580. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11581. wc_Sha512_224Free(&sha512);
  11582. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11583. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11584. return EXPECT_RESULT();
  11585. }
  11586. static int test_wc_Sha512_224Update(void)
  11587. {
  11588. EXPECT_DECLS;
  11589. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11590. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11591. wc_Sha512 sha512;
  11592. byte hash[WC_SHA512_DIGEST_SIZE];
  11593. testVector a, c;
  11594. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11595. /* Input. */
  11596. a.input = "a";
  11597. a.inLen = XSTRLEN(a.input);
  11598. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11599. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11600. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11601. 0);
  11602. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11603. /* Update input. */
  11604. a.input = "abc";
  11605. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11606. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11607. a.inLen = XSTRLEN(a.input);
  11608. a.outLen = XSTRLEN(a.output);
  11609. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11610. 0);
  11611. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11612. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11613. c.input = NULL;
  11614. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11615. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11616. BAD_FUNC_ARG);
  11617. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11618. BAD_FUNC_ARG);
  11619. wc_Sha512_224Free(&sha512);
  11620. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11621. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11622. return EXPECT_RESULT();
  11623. }
  11624. static int test_wc_Sha512_224Final(void)
  11625. {
  11626. EXPECT_DECLS;
  11627. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11628. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11629. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11630. TEST_SUCCESS);
  11631. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11632. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11633. return EXPECT_RESULT();
  11634. }
  11635. static int test_wc_Sha512_224GetFlags(void)
  11636. {
  11637. EXPECT_DECLS;
  11638. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11639. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11640. wc_Sha512 sha512;
  11641. wc_Sha512 copy;
  11642. word32 flags = 0;
  11643. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11644. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11645. /* Initialize */
  11646. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11647. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11648. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  11649. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11650. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  11651. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  11652. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11653. wc_Sha512_224Free(&copy);
  11654. wc_Sha512_224Free(&sha512);
  11655. #endif
  11656. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11657. return EXPECT_RESULT();
  11658. }
  11659. static int test_wc_Sha512_224FinalRaw(void)
  11660. {
  11661. EXPECT_DECLS;
  11662. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11663. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11664. !defined(WOLFSSL_NO_HASH_RAW)
  11665. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  11666. TEST_SUCCESS);
  11667. #endif
  11668. return EXPECT_RESULT();
  11669. }
  11670. static int test_wc_Sha512_224Free(void)
  11671. {
  11672. EXPECT_DECLS;
  11673. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11674. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11675. wc_Sha512_224Free(NULL);
  11676. /* Set result to SUCCESS. */
  11677. ExpectTrue(1);
  11678. #endif
  11679. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11680. return EXPECT_RESULT();
  11681. }
  11682. static int test_wc_Sha512_224GetHash(void)
  11683. {
  11684. EXPECT_DECLS;
  11685. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11686. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11687. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  11688. TEST_SUCCESS);
  11689. #endif
  11690. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11691. return EXPECT_RESULT();
  11692. }
  11693. static int test_wc_Sha512_224Copy(void)
  11694. {
  11695. EXPECT_DECLS;
  11696. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11697. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11698. wc_Sha512 sha512;
  11699. wc_Sha512 temp;
  11700. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11701. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11702. /* Initialize */
  11703. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11704. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  11705. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  11706. /* test bad arguments*/
  11707. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  11708. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  11709. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  11710. wc_Sha512_224Free(&sha512);
  11711. wc_Sha512_224Free(&temp);
  11712. #endif
  11713. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11714. return EXPECT_RESULT();
  11715. }
  11716. static int test_wc_InitSha512_256(void)
  11717. {
  11718. EXPECT_DECLS;
  11719. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11720. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11721. wc_Sha512 sha512;
  11722. /* Test good arg. */
  11723. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11724. /* Test bad arg. */
  11725. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  11726. wc_Sha512_256Free(&sha512);
  11727. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11728. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11729. return EXPECT_RESULT();
  11730. }
  11731. static int test_wc_Sha512_256Update(void)
  11732. {
  11733. EXPECT_DECLS;
  11734. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11735. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11736. wc_Sha512 sha512;
  11737. byte hash[WC_SHA512_DIGEST_SIZE];
  11738. testVector a, c;
  11739. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11740. /* Input. */
  11741. a.input = "a";
  11742. a.inLen = XSTRLEN(a.input);
  11743. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  11744. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  11745. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11746. 0);
  11747. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11748. /* Update input. */
  11749. a.input = "abc";
  11750. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11751. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11752. "\x07\xe7\xaf\x23";
  11753. a.inLen = XSTRLEN(a.input);
  11754. a.outLen = XSTRLEN(a.output);
  11755. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11756. 0);
  11757. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11758. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  11759. c.input = NULL;
  11760. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11761. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11762. BAD_FUNC_ARG);
  11763. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11764. BAD_FUNC_ARG);
  11765. wc_Sha512_256Free(&sha512);
  11766. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11767. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11768. return EXPECT_RESULT();
  11769. }
  11770. static int test_wc_Sha512_256Final(void)
  11771. {
  11772. EXPECT_DECLS;
  11773. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11774. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11775. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  11776. TEST_SUCCESS);
  11777. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11778. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11779. return EXPECT_RESULT();
  11780. }
  11781. static int test_wc_Sha512_256GetFlags(void)
  11782. {
  11783. EXPECT_DECLS;
  11784. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11785. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11786. wc_Sha512 sha512, copy;
  11787. word32 flags = 0;
  11788. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11789. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11790. /* Initialize */
  11791. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11792. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  11793. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  11794. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11795. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  11796. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  11797. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11798. wc_Sha512_256Free(&sha512);
  11799. #endif
  11800. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11801. return EXPECT_RESULT();
  11802. }
  11803. static int test_wc_Sha512_256FinalRaw(void)
  11804. {
  11805. EXPECT_DECLS;
  11806. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11807. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11808. !defined(WOLFSSL_NO_HASH_RAW)
  11809. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  11810. TEST_SUCCESS);
  11811. #endif
  11812. return EXPECT_RESULT();
  11813. }
  11814. static int test_wc_Sha512_256Free(void)
  11815. {
  11816. EXPECT_DECLS;
  11817. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11818. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11819. wc_Sha512_256Free(NULL);
  11820. /* Set result to SUCCESS. */
  11821. ExpectTrue(1);
  11822. #endif
  11823. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11824. return EXPECT_RESULT();
  11825. }
  11826. static int test_wc_Sha512_256GetHash(void)
  11827. {
  11828. EXPECT_DECLS;
  11829. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11830. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11831. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  11832. TEST_SUCCESS);
  11833. #endif
  11834. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11835. return EXPECT_RESULT();
  11836. }
  11837. static int test_wc_Sha512_256Copy(void)
  11838. {
  11839. EXPECT_DECLS;
  11840. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11841. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11842. wc_Sha512 sha512;
  11843. wc_Sha512 temp;
  11844. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11845. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11846. /* Initialize */
  11847. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11848. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  11849. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  11850. /* test bad arguments*/
  11851. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  11852. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  11853. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  11854. wc_Sha512_256Free(&sha512);
  11855. wc_Sha512_256Free(&temp);
  11856. #endif
  11857. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11858. return EXPECT_RESULT();
  11859. }
  11860. /*
  11861. * Testing wc_InitSha384()
  11862. */
  11863. static int test_wc_InitSha384(void)
  11864. {
  11865. EXPECT_DECLS;
  11866. #ifdef WOLFSSL_SHA384
  11867. wc_Sha384 sha384;
  11868. /* Test good arg. */
  11869. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11870. /* Test bad arg. */
  11871. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  11872. wc_Sha384Free(&sha384);
  11873. #endif
  11874. return EXPECT_RESULT();
  11875. } /* END test_wc_InitSha384 */
  11876. /*
  11877. * test wc_Sha384Update()
  11878. */
  11879. static int test_wc_Sha384Update(void)
  11880. {
  11881. EXPECT_DECLS;
  11882. #ifdef WOLFSSL_SHA384
  11883. wc_Sha384 sha384;
  11884. byte hash[WC_SHA384_DIGEST_SIZE];
  11885. testVector a, b, c;
  11886. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11887. /* Input */
  11888. a.input = "a";
  11889. a.inLen = XSTRLEN(a.input);
  11890. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  11891. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  11892. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11893. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11894. /* Update input. */
  11895. a.input = "abc";
  11896. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11897. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11898. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11899. "\xc8\x25\xa7";
  11900. a.inLen = XSTRLEN(a.input);
  11901. a.outLen = XSTRLEN(a.output);
  11902. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11903. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11904. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  11905. /* Pass in bad values. */
  11906. b.input = NULL;
  11907. b.inLen = 0;
  11908. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  11909. c.input = NULL;
  11910. c.inLen = WC_SHA384_DIGEST_SIZE;
  11911. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  11912. BAD_FUNC_ARG);
  11913. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  11914. BAD_FUNC_ARG);
  11915. wc_Sha384Free(&sha384);
  11916. #endif
  11917. return EXPECT_RESULT();
  11918. } /* END test_wc_Sha384Update */
  11919. /*
  11920. * Unit test function for wc_Sha384Final();
  11921. */
  11922. static int test_wc_Sha384Final(void)
  11923. {
  11924. EXPECT_DECLS;
  11925. #ifdef WOLFSSL_SHA384
  11926. wc_Sha384 sha384;
  11927. byte* hash_test[3];
  11928. byte hash1[WC_SHA384_DIGEST_SIZE];
  11929. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11930. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11931. int times, i;
  11932. /* Initialize */
  11933. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11934. hash_test[0] = hash1;
  11935. hash_test[1] = hash2;
  11936. hash_test[2] = hash3;
  11937. times = sizeof(hash_test) / sizeof(byte*);
  11938. /* Good test args. */
  11939. for (i = 0; i < times; i++) {
  11940. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  11941. }
  11942. /* Test bad args. */
  11943. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  11944. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  11945. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  11946. wc_Sha384Free(&sha384);
  11947. #endif
  11948. return EXPECT_RESULT();
  11949. } /* END test_wc_Sha384Final */
  11950. /*
  11951. * Unit test function for wc_Sha384GetFlags()
  11952. */
  11953. static int test_wc_Sha384GetFlags(void)
  11954. {
  11955. EXPECT_DECLS;
  11956. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11957. wc_Sha384 sha384;
  11958. word32 flags = 0;
  11959. /* Initialize */
  11960. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11961. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  11962. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11963. wc_Sha384Free(&sha384);
  11964. #endif
  11965. return EXPECT_RESULT();
  11966. } /* END test_wc_Sha384GetFlags */
  11967. /*
  11968. * Unit test function for wc_Sha384FinalRaw()
  11969. */
  11970. static int test_wc_Sha384FinalRaw(void)
  11971. {
  11972. EXPECT_DECLS;
  11973. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11974. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11975. !defined(WOLFSSL_NO_HASH_RAW)
  11976. wc_Sha384 sha384;
  11977. byte* hash_test[3];
  11978. byte hash1[WC_SHA384_DIGEST_SIZE];
  11979. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11980. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11981. int times, i;
  11982. /* Initialize */
  11983. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11984. hash_test[0] = hash1;
  11985. hash_test[1] = hash2;
  11986. hash_test[2] = hash3;
  11987. times = sizeof(hash_test) / sizeof(byte*);
  11988. /* Good test args. */
  11989. for (i = 0; i < times; i++) {
  11990. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  11991. }
  11992. /* Test bad args. */
  11993. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11994. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11995. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  11996. wc_Sha384Free(&sha384);
  11997. #endif
  11998. return EXPECT_RESULT();
  11999. } /* END test_wc_Sha384FinalRaw */
  12000. /*
  12001. * Unit test function for wc_Sha384Free()
  12002. */
  12003. static int test_wc_Sha384Free(void)
  12004. {
  12005. EXPECT_DECLS;
  12006. #ifdef WOLFSSL_SHA384
  12007. wc_Sha384Free(NULL);
  12008. /* Set result to SUCCESS. */
  12009. ExpectTrue(1);
  12010. #endif
  12011. return EXPECT_RESULT();
  12012. } /* END test_wc_Sha384Free */
  12013. /*
  12014. * Unit test function for wc_Sha384GetHash()
  12015. */
  12016. static int test_wc_Sha384GetHash(void)
  12017. {
  12018. EXPECT_DECLS;
  12019. #ifdef WOLFSSL_SHA384
  12020. wc_Sha384 sha384;
  12021. byte hash1[WC_SHA384_DIGEST_SIZE];
  12022. /* Initialize */
  12023. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12024. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12025. /* test bad arguments*/
  12026. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12027. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12028. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12029. wc_Sha384Free(&sha384);
  12030. #endif
  12031. return EXPECT_RESULT();
  12032. } /* END test_wc_Sha384GetHash */
  12033. /*
  12034. * Unit test function for wc_Sha384Copy()
  12035. */
  12036. static int test_wc_Sha384Copy(void)
  12037. {
  12038. EXPECT_DECLS;
  12039. #ifdef WOLFSSL_SHA384
  12040. wc_Sha384 sha384;
  12041. wc_Sha384 temp;
  12042. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12043. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12044. /* Initialize */
  12045. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12046. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12047. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12048. /* test bad arguments*/
  12049. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12050. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12051. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12052. wc_Sha384Free(&sha384);
  12053. wc_Sha384Free(&temp);
  12054. #endif
  12055. return EXPECT_RESULT();
  12056. } /* END test_wc_Sha384Copy */
  12057. /*
  12058. * Testing wc_InitSha224();
  12059. */
  12060. static int test_wc_InitSha224(void)
  12061. {
  12062. EXPECT_DECLS;
  12063. #ifdef WOLFSSL_SHA224
  12064. wc_Sha224 sha224;
  12065. /* Test good arg. */
  12066. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12067. /* Test bad arg. */
  12068. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12069. wc_Sha224Free(&sha224);
  12070. #endif
  12071. return EXPECT_RESULT();
  12072. } /* END test_wc_InitSha224 */
  12073. /*
  12074. * Unit test on wc_Sha224Update
  12075. */
  12076. static int test_wc_Sha224Update(void)
  12077. {
  12078. EXPECT_DECLS;
  12079. #ifdef WOLFSSL_SHA224
  12080. wc_Sha224 sha224;
  12081. byte hash[WC_SHA224_DIGEST_SIZE];
  12082. testVector a, b, c;
  12083. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12084. /* Input. */
  12085. a.input = "a";
  12086. a.inLen = XSTRLEN(a.input);
  12087. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12088. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12089. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12090. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12091. /* Update input. */
  12092. a.input = "abc";
  12093. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12094. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12095. a.inLen = XSTRLEN(a.input);
  12096. a.outLen = XSTRLEN(a.output);
  12097. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12098. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12099. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12100. /* Pass in bad values. */
  12101. b.input = NULL;
  12102. b.inLen = 0;
  12103. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12104. c.input = NULL;
  12105. c.inLen = WC_SHA224_DIGEST_SIZE;
  12106. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12107. BAD_FUNC_ARG);
  12108. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12109. BAD_FUNC_ARG);
  12110. wc_Sha224Free(&sha224);
  12111. #endif
  12112. return EXPECT_RESULT();
  12113. } /* END test_wc_Sha224Update */
  12114. /*
  12115. * Unit test for wc_Sha224Final();
  12116. */
  12117. static int test_wc_Sha224Final(void)
  12118. {
  12119. EXPECT_DECLS;
  12120. #ifdef WOLFSSL_SHA224
  12121. wc_Sha224 sha224;
  12122. byte* hash_test[3];
  12123. byte hash1[WC_SHA224_DIGEST_SIZE];
  12124. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12125. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12126. int times, i;
  12127. /* Initialize */
  12128. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12129. hash_test[0] = hash1;
  12130. hash_test[1] = hash2;
  12131. hash_test[2] = hash3;
  12132. times = sizeof(hash_test) / sizeof(byte*);
  12133. /* Good test args. */
  12134. /* Testing oversized buffers. */
  12135. for (i = 0; i < times; i++) {
  12136. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12137. }
  12138. /* Test bad args. */
  12139. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12140. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12141. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12142. wc_Sha224Free(&sha224);
  12143. #endif
  12144. return EXPECT_RESULT();
  12145. } /* END test_wc_Sha224Final */
  12146. /*
  12147. * Unit test function for wc_Sha224SetFlags()
  12148. */
  12149. static int test_wc_Sha224SetFlags(void)
  12150. {
  12151. EXPECT_DECLS;
  12152. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12153. wc_Sha224 sha224;
  12154. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12155. /* Initialize */
  12156. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12157. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12158. flags = 0;
  12159. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12160. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12161. wc_Sha224Free(&sha224);
  12162. #endif
  12163. return EXPECT_RESULT();
  12164. } /* END test_wc_Sha224SetFlags */
  12165. /*
  12166. * Unit test function for wc_Sha224GetFlags()
  12167. */
  12168. static int test_wc_Sha224GetFlags(void)
  12169. {
  12170. EXPECT_DECLS;
  12171. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12172. wc_Sha224 sha224;
  12173. word32 flags = 0;
  12174. /* Initialize */
  12175. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12176. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12177. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12178. wc_Sha224Free(&sha224);
  12179. #endif
  12180. return EXPECT_RESULT();
  12181. } /* END test_wc_Sha224GetFlags */
  12182. /*
  12183. * Unit test function for wc_Sha224Free()
  12184. */
  12185. static int test_wc_Sha224Free(void)
  12186. {
  12187. EXPECT_DECLS;
  12188. #ifdef WOLFSSL_SHA224
  12189. wc_Sha224Free(NULL);
  12190. /* Set result to SUCCESS. */
  12191. ExpectTrue(1);
  12192. #endif
  12193. return EXPECT_RESULT();
  12194. } /* END test_wc_Sha224Free */
  12195. /*
  12196. * Unit test function for wc_Sha224GetHash()
  12197. */
  12198. static int test_wc_Sha224GetHash(void)
  12199. {
  12200. EXPECT_DECLS;
  12201. #ifdef WOLFSSL_SHA224
  12202. wc_Sha224 sha224;
  12203. byte hash1[WC_SHA224_DIGEST_SIZE];
  12204. /* Initialize */
  12205. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12206. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12207. /* test bad arguments*/
  12208. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12209. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12210. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12211. wc_Sha224Free(&sha224);
  12212. #endif
  12213. return EXPECT_RESULT();
  12214. } /* END test_wc_Sha224GetHash */
  12215. /*
  12216. * Unit test function for wc_Sha224Copy()
  12217. */
  12218. static int test_wc_Sha224Copy(void)
  12219. {
  12220. EXPECT_DECLS;
  12221. #ifdef WOLFSSL_SHA224
  12222. wc_Sha224 sha224;
  12223. wc_Sha224 temp;
  12224. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12225. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12226. /* Initialize */
  12227. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12228. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12229. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12230. /* test bad arguments*/
  12231. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12232. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12233. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12234. wc_Sha224Free(&sha224);
  12235. wc_Sha224Free(&temp);
  12236. #endif
  12237. return EXPECT_RESULT();
  12238. } /* END test_wc_Sha224Copy */
  12239. /*
  12240. * Testing wc_InitRipeMd()
  12241. */
  12242. static int test_wc_InitRipeMd(void)
  12243. {
  12244. EXPECT_DECLS;
  12245. #ifdef WOLFSSL_RIPEMD
  12246. RipeMd ripemd;
  12247. /* Test good arg. */
  12248. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12249. /* Test bad arg. */
  12250. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12251. #endif
  12252. return EXPECT_RESULT();
  12253. } /* END test_wc_InitRipeMd */
  12254. /*
  12255. * Testing wc_RipeMdUpdate()
  12256. */
  12257. static int test_wc_RipeMdUpdate(void)
  12258. {
  12259. EXPECT_DECLS;
  12260. #ifdef WOLFSSL_RIPEMD
  12261. RipeMd ripemd;
  12262. byte hash[RIPEMD_DIGEST_SIZE];
  12263. testVector a, b, c;
  12264. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12265. /* Input */
  12266. a.input = "a";
  12267. a.inLen = XSTRLEN(a.input);
  12268. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12269. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12270. /* Update input. */
  12271. a.input = "abc";
  12272. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12273. "\xb0\x87\xf1\x5a\x0b\xfc";
  12274. a.inLen = XSTRLEN(a.input);
  12275. a.outLen = XSTRLEN(a.output);
  12276. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12277. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12278. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12279. /* Pass in bad values. */
  12280. b.input = NULL;
  12281. b.inLen = 0;
  12282. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12283. c.input = NULL;
  12284. c.inLen = RIPEMD_DIGEST_SIZE;
  12285. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12286. BAD_FUNC_ARG);
  12287. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12288. BAD_FUNC_ARG);
  12289. #endif
  12290. return EXPECT_RESULT();
  12291. } /* END test_wc_RipeMdUdpate */
  12292. /*
  12293. * Unit test function for wc_RipeMdFinal()
  12294. */
  12295. static int test_wc_RipeMdFinal(void)
  12296. {
  12297. EXPECT_DECLS;
  12298. #ifdef WOLFSSL_RIPEMD
  12299. RipeMd ripemd;
  12300. byte* hash_test[3];
  12301. byte hash1[RIPEMD_DIGEST_SIZE];
  12302. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12303. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12304. int times, i;
  12305. /* Initialize */
  12306. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12307. hash_test[0] = hash1;
  12308. hash_test[1] = hash2;
  12309. hash_test[2] = hash3;
  12310. times = sizeof(hash_test) / sizeof(byte*);
  12311. /* Testing oversized buffers. */
  12312. for (i = 0; i < times; i++) {
  12313. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12314. }
  12315. /* Test bad args. */
  12316. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12317. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12318. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12319. #endif
  12320. return EXPECT_RESULT();
  12321. } /* END test_wc_RipeMdFinal */
  12322. /*
  12323. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12324. * wc_InitSha3_512
  12325. */
  12326. static int test_wc_InitSha3(void)
  12327. {
  12328. EXPECT_DECLS;
  12329. #if defined(WOLFSSL_SHA3)
  12330. wc_Sha3 sha3;
  12331. (void)sha3;
  12332. #if !defined(WOLFSSL_NOSHA3_224)
  12333. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12334. /* Test bad args. */
  12335. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12336. wc_Sha3_224_Free(&sha3);
  12337. #endif /* NOSHA3_224 */
  12338. #if !defined(WOLFSSL_NOSHA3_256)
  12339. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12340. /* Test bad args. */
  12341. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12342. wc_Sha3_256_Free(&sha3);
  12343. #endif /* NOSHA3_256 */
  12344. #if !defined(WOLFSSL_NOSHA3_384)
  12345. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12346. /* Test bad args. */
  12347. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12348. wc_Sha3_384_Free(&sha3);
  12349. #endif /* NOSHA3_384 */
  12350. #if !defined(WOLFSSL_NOSHA3_512)
  12351. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12352. /* Test bad args. */
  12353. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12354. wc_Sha3_512_Free(&sha3);
  12355. #endif /* NOSHA3_512 */
  12356. #endif
  12357. return EXPECT_RESULT();
  12358. } /* END test_wc_InitSha3 */
  12359. /*
  12360. * Testing wc_Sha3_Update()
  12361. */
  12362. static int testing_wc_Sha3_Update(void)
  12363. {
  12364. EXPECT_DECLS;
  12365. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12366. !defined(WOLFSSL_AFALG_XILINX)
  12367. wc_Sha3 sha3;
  12368. byte msg[] = "Everybody's working for the weekend.";
  12369. byte msg2[] = "Everybody gets Friday off.";
  12370. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12371. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12372. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12373. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12374. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12375. word32 msglen = sizeof(msg) - 1;
  12376. word32 msg2len = sizeof(msg2);
  12377. word32 msgCmplen = sizeof(msgCmp);
  12378. #if !defined(WOLFSSL_NOSHA3_224)
  12379. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12380. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12381. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12382. ExpectTrue(sha3.i == msglen);
  12383. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12384. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12385. /* Pass bad args. */
  12386. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12387. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12388. wc_Sha3_224_Free(&sha3);
  12389. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12390. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12391. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12392. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12393. wc_Sha3_224_Free(&sha3);
  12394. #endif /* SHA3_224 */
  12395. #if !defined(WOLFSSL_NOSHA3_256)
  12396. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12397. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12398. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12399. ExpectTrue(sha3.i == msglen);
  12400. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12401. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12402. /* Pass bad args. */
  12403. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12404. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12405. wc_Sha3_256_Free(&sha3);
  12406. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12407. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12408. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12409. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12410. wc_Sha3_256_Free(&sha3);
  12411. #endif /* SHA3_256 */
  12412. #if !defined(WOLFSSL_NOSHA3_384)
  12413. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12414. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12415. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12416. ExpectTrue(sha3.i == msglen);
  12417. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12418. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12419. /* Pass bad args. */
  12420. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12421. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12422. wc_Sha3_384_Free(&sha3);
  12423. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12424. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12425. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12426. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12427. wc_Sha3_384_Free(&sha3);
  12428. #endif /* SHA3_384 */
  12429. #if !defined(WOLFSSL_NOSHA3_512)
  12430. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12431. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12432. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12433. ExpectTrue(sha3.i == msglen);
  12434. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12435. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12436. /* Pass bad args. */
  12437. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12438. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12439. wc_Sha3_512_Free(&sha3);
  12440. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12441. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12442. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12443. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12444. wc_Sha3_512_Free(&sha3);
  12445. #endif /* SHA3_512 */
  12446. #endif /* WOLFSSL_SHA3 */
  12447. return EXPECT_RESULT();
  12448. } /* END testing_wc_Sha3_Update */
  12449. /*
  12450. * Testing wc_Sha3_224_Final()
  12451. */
  12452. static int test_wc_Sha3_224_Final(void)
  12453. {
  12454. EXPECT_DECLS;
  12455. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12456. wc_Sha3 sha3;
  12457. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12458. "nopnopq";
  12459. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12460. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12461. "\x64\xea\xd0\xfc\xce\x33";
  12462. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12463. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12464. /* Init stack variables. */
  12465. XMEMSET(hash, 0, sizeof(hash));
  12466. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12467. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12468. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12469. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12470. /* Test bad args. */
  12471. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12472. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12473. wc_Sha3_224_Free(&sha3);
  12474. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12475. /* Init stack variables. */
  12476. XMEMSET(hash, 0, sizeof(hash));
  12477. XMEMSET(hashRet, 0, sizeof(hashRet));
  12478. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12479. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12480. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12481. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12482. /* Test bad args. */
  12483. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12484. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12485. wc_Sha3_224_Free(&sha3);
  12486. #endif
  12487. return EXPECT_RESULT();
  12488. } /* END test_wc_Sha3_224_Final */
  12489. /*
  12490. * Testing wc_Sha3_256_Final()
  12491. */
  12492. static int test_wc_Sha3_256_Final(void)
  12493. {
  12494. EXPECT_DECLS;
  12495. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12496. wc_Sha3 sha3;
  12497. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12498. "nopnopq";
  12499. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12500. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12501. "\xdd\x97\x49\x6d\x33\x76";
  12502. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12503. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12504. /* Init stack variables. */
  12505. XMEMSET(hash, 0, sizeof(hash));
  12506. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12507. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12508. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12509. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12510. /* Test bad args. */
  12511. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12512. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12513. wc_Sha3_256_Free(&sha3);
  12514. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12515. /* Init stack variables. */
  12516. XMEMSET(hash, 0, sizeof(hash));
  12517. XMEMSET(hashRet, 0, sizeof(hashRet));
  12518. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12519. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12520. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12521. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12522. /* Test bad args. */
  12523. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12524. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12525. wc_Sha3_256_Free(&sha3);
  12526. #endif
  12527. return EXPECT_RESULT();
  12528. } /* END test_wc_Sha3_256_Final */
  12529. /*
  12530. * Testing wc_Sha3_384_Final()
  12531. */
  12532. static int test_wc_Sha3_384_Final(void)
  12533. {
  12534. EXPECT_DECLS;
  12535. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12536. wc_Sha3 sha3;
  12537. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12538. "nopnopq";
  12539. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12540. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12541. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12542. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12543. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12544. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12545. /* Init stack variables. */
  12546. XMEMSET(hash, 0, sizeof(hash));
  12547. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12548. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12549. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12550. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12551. /* Test bad args. */
  12552. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12553. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12554. wc_Sha3_384_Free(&sha3);
  12555. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12556. /* Init stack variables. */
  12557. XMEMSET(hash, 0, sizeof(hash));
  12558. XMEMSET(hashRet, 0, sizeof(hashRet));
  12559. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12560. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12561. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12562. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12563. /* Test bad args. */
  12564. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12565. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12566. wc_Sha3_384_Free(&sha3);
  12567. #endif
  12568. return EXPECT_RESULT();
  12569. } /* END test_wc_Sha3_384_Final */
  12570. /*
  12571. * Testing wc_Sha3_512_Final()
  12572. */
  12573. static int test_wc_Sha3_512_Final(void)
  12574. {
  12575. EXPECT_DECLS;
  12576. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12577. !defined(WOLFSSL_NOSHA3_384)
  12578. wc_Sha3 sha3;
  12579. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12580. "nopnopq";
  12581. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12582. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12583. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12584. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12585. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12586. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12587. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12588. /* Init stack variables. */
  12589. XMEMSET(hash, 0, sizeof(hash));
  12590. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12591. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12592. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12593. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12594. /* Test bad args. */
  12595. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12596. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12597. wc_Sha3_512_Free(&sha3);
  12598. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12599. /* Init stack variables. */
  12600. XMEMSET(hash, 0, sizeof(hash));
  12601. XMEMSET(hashRet, 0, sizeof(hashRet));
  12602. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12603. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12604. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12605. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12606. /* Test bad args. */
  12607. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12608. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12609. wc_Sha3_512_Free(&sha3);
  12610. #endif
  12611. return EXPECT_RESULT();
  12612. } /* END test_wc_Sha3_512_Final */
  12613. /*
  12614. * Testing wc_Sha3_224_Copy()
  12615. */
  12616. static int test_wc_Sha3_224_Copy(void)
  12617. {
  12618. EXPECT_DECLS;
  12619. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12620. wc_Sha3 sha3, sha3Cpy;
  12621. const char* msg = TEST_STRING;
  12622. word32 msglen = (word32)TEST_STRING_SZ;
  12623. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12624. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12625. XMEMSET(hash, 0, sizeof(hash));
  12626. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12627. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12628. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12629. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12630. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12631. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12632. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12633. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12634. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12635. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12636. /* Test bad args. */
  12637. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12638. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12639. wc_Sha3_224_Free(&sha3);
  12640. wc_Sha3_224_Free(&sha3Cpy);
  12641. #endif
  12642. return EXPECT_RESULT();
  12643. } /* END test_wc_Sha3_224_Copy */
  12644. /*
  12645. * Testing wc_Sha3_256_Copy()
  12646. */
  12647. static int test_wc_Sha3_256_Copy(void)
  12648. {
  12649. EXPECT_DECLS;
  12650. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12651. wc_Sha3 sha3, sha3Cpy;
  12652. const char* msg = TEST_STRING;
  12653. word32 msglen = (word32)TEST_STRING_SZ;
  12654. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12655. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12656. XMEMSET(hash, 0, sizeof(hash));
  12657. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12658. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12659. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12660. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12661. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12662. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  12663. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  12664. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12665. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  12666. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12667. /* Test bad args. */
  12668. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12669. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12670. wc_Sha3_256_Free(&sha3);
  12671. wc_Sha3_256_Free(&sha3Cpy);
  12672. #endif
  12673. return EXPECT_RESULT();
  12674. } /* END test_wc_Sha3_256_Copy */
  12675. /*
  12676. * Testing wc_Sha3_384_Copy()
  12677. */
  12678. static int test_wc_Sha3_384_Copy(void)
  12679. {
  12680. EXPECT_DECLS;
  12681. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12682. wc_Sha3 sha3, sha3Cpy;
  12683. const char* msg = TEST_STRING;
  12684. word32 msglen = (word32)TEST_STRING_SZ;
  12685. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12686. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12687. XMEMSET(hash, 0, sizeof(hash));
  12688. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12689. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12690. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12691. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12692. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12693. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  12694. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  12695. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12696. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  12697. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12698. /* Test bad args. */
  12699. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12700. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12701. wc_Sha3_384_Free(&sha3);
  12702. wc_Sha3_384_Free(&sha3Cpy);
  12703. #endif
  12704. return EXPECT_RESULT();
  12705. } /* END test_wc_Sha3_384_Copy */
  12706. /*
  12707. * Testing wc_Sha3_512_Copy()
  12708. */
  12709. static int test_wc_Sha3_512_Copy(void)
  12710. {
  12711. EXPECT_DECLS;
  12712. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12713. wc_Sha3 sha3, sha3Cpy;
  12714. const char* msg = TEST_STRING;
  12715. word32 msglen = (word32)TEST_STRING_SZ;
  12716. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12717. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12718. XMEMSET(hash, 0, sizeof(hash));
  12719. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12720. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12721. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12722. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12723. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12724. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  12725. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  12726. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12727. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  12728. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12729. /* Test bad args. */
  12730. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12731. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12732. wc_Sha3_512_Free(&sha3);
  12733. wc_Sha3_512_Free(&sha3Cpy);
  12734. #endif
  12735. return EXPECT_RESULT();
  12736. } /* END test_wc_Sha3_512_Copy */
  12737. /*
  12738. * Unit test function for wc_Sha3_GetFlags()
  12739. */
  12740. static int test_wc_Sha3_GetFlags(void)
  12741. {
  12742. EXPECT_DECLS;
  12743. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12744. wc_Sha3 sha3;
  12745. word32 flags = 0;
  12746. /* Initialize */
  12747. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12748. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  12749. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12750. wc_Sha3_224_Free(&sha3);
  12751. #endif
  12752. return EXPECT_RESULT();
  12753. } /* END test_wc_Sha3_GetFlags */
  12754. static int test_wc_InitShake256(void)
  12755. {
  12756. EXPECT_DECLS;
  12757. #ifdef WOLFSSL_SHAKE256
  12758. wc_Shake shake;
  12759. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12760. /* Test bad args. */
  12761. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12762. wc_Shake256_Free(&shake);
  12763. #endif
  12764. return EXPECT_RESULT();
  12765. }
  12766. static int testing_wc_Shake256_Update(void)
  12767. {
  12768. EXPECT_DECLS;
  12769. #ifdef WOLFSSL_SHAKE256
  12770. wc_Shake shake;
  12771. byte msg[] = "Everybody's working for the weekend.";
  12772. byte msg2[] = "Everybody gets Friday off.";
  12773. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12774. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12775. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12776. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12777. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12778. word32 msglen = sizeof(msg) - 1;
  12779. word32 msg2len = sizeof(msg2);
  12780. word32 msgCmplen = sizeof(msgCmp);
  12781. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12782. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  12783. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  12784. ExpectTrue(shake.i == msglen);
  12785. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12786. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  12787. /* Pass bad args. */
  12788. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12789. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  12790. wc_Shake256_Free(&shake);
  12791. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12792. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  12793. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12794. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  12795. wc_Shake256_Free(&shake);
  12796. #endif /* WOLFSSL_SHAKE256 */
  12797. return EXPECT_RESULT();
  12798. }
  12799. static int test_wc_Shake256_Final(void)
  12800. {
  12801. EXPECT_DECLS;
  12802. #ifdef WOLFSSL_SHAKE256
  12803. wc_Shake shake;
  12804. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12805. "nopnopq";
  12806. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12807. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12808. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12809. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12810. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12811. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12812. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12813. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12814. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12815. byte hash[114];
  12816. /* Init stack variables. */
  12817. XMEMSET(hash, 0, sizeof(hash));
  12818. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12819. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  12820. 0);
  12821. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  12822. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  12823. /* Test bad args. */
  12824. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  12825. BAD_FUNC_ARG);
  12826. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  12827. BAD_FUNC_ARG);
  12828. wc_Shake256_Free(&shake);
  12829. #endif
  12830. return EXPECT_RESULT();
  12831. }
  12832. /*
  12833. * Testing wc_Shake256_Copy()
  12834. */
  12835. static int test_wc_Shake256_Copy(void)
  12836. {
  12837. EXPECT_DECLS;
  12838. #ifdef WOLFSSL_SHAKE256
  12839. wc_Shake shake, shakeCpy;
  12840. const char* msg = TEST_STRING;
  12841. word32 msglen = (word32)TEST_STRING_SZ;
  12842. byte hash[144];
  12843. byte hashCpy[144];
  12844. word32 hashLen = sizeof(hash);
  12845. word32 hashLenCpy = sizeof(hashCpy);
  12846. XMEMSET(hash, 0, sizeof(hash));
  12847. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12848. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12849. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  12850. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  12851. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  12852. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  12853. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  12854. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12855. /* Test bad args. */
  12856. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  12857. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  12858. wc_Shake256_Free(&shake);
  12859. wc_Shake256_Free(&shakeCpy);
  12860. #endif
  12861. return EXPECT_RESULT();
  12862. } /* END test_wc_Shake256_Copy */
  12863. /*
  12864. * Unit test function for wc_Shake256Hash()
  12865. */
  12866. static int test_wc_Shake256Hash(void)
  12867. {
  12868. EXPECT_DECLS;
  12869. #ifdef WOLFSSL_SHAKE256
  12870. const byte data[] = { /* Hello World */
  12871. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12872. 0x72,0x6c,0x64
  12873. };
  12874. word32 len = sizeof(data);
  12875. byte hash[144];
  12876. word32 hashLen = sizeof(hash);
  12877. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  12878. #endif
  12879. return EXPECT_RESULT();
  12880. } /* END test_wc_Shake256Hash */
  12881. /*
  12882. * Testing wc_InitSm3(), wc_Sm3Free()
  12883. */
  12884. static int test_wc_InitSm3Free(void)
  12885. {
  12886. EXPECT_DECLS;
  12887. #ifdef WOLFSSL_SM3
  12888. wc_Sm3 sm3;
  12889. /* Invalid Parameters */
  12890. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  12891. /* Valid Parameters */
  12892. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12893. wc_Sm3Free(NULL);
  12894. wc_Sm3Free(&sm3);
  12895. #endif
  12896. return EXPECT_RESULT();
  12897. } /* END test_wc_InitSm3 */
  12898. /*
  12899. * Testing wc_Sm3Update(), wc_Sm3Final()
  12900. */
  12901. static int test_wc_Sm3UpdateFinal(void)
  12902. {
  12903. EXPECT_DECLS;
  12904. #ifdef WOLFSSL_SM3
  12905. wc_Sm3 sm3;
  12906. byte data[WC_SM3_BLOCK_SIZE * 4];
  12907. byte hash[WC_SM3_DIGEST_SIZE];
  12908. byte calcHash[WC_SM3_DIGEST_SIZE];
  12909. byte expHash[WC_SM3_DIGEST_SIZE] = {
  12910. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  12911. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  12912. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  12913. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  12914. };
  12915. word32 chunk;
  12916. word32 i;
  12917. XMEMSET(data, 0, sizeof(data));
  12918. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12919. /* Invalid Parameters */
  12920. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  12921. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  12922. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  12923. /* Valid Parameters */
  12924. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  12925. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12926. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12927. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  12928. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  12929. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  12930. /* Ensure too many bytes for lengths. */
  12931. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  12932. /* Invalid Parameters */
  12933. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  12934. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  12935. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  12936. /* Valid Parameters */
  12937. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12938. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  12939. /* Chunk tests. */
  12940. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12941. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12942. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  12943. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  12944. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  12945. }
  12946. if (i < (word32)sizeof(data)) {
  12947. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  12948. 0);
  12949. }
  12950. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12951. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12952. }
  12953. /* Not testing when the low 32-bit length overflows. */
  12954. wc_Sm3Free(&sm3);
  12955. #endif
  12956. return EXPECT_RESULT();
  12957. } /* END test_wc_Sm3Update */
  12958. /*
  12959. * Testing wc_Sm3GetHash()
  12960. */
  12961. static int test_wc_Sm3GetHash(void)
  12962. {
  12963. EXPECT_DECLS;
  12964. #ifdef WOLFSSL_SM3
  12965. wc_Sm3 sm3;
  12966. byte hash[WC_SM3_DIGEST_SIZE];
  12967. byte calcHash[WC_SM3_DIGEST_SIZE];
  12968. byte data[WC_SM3_BLOCK_SIZE];
  12969. XMEMSET(data, 0, sizeof(data));
  12970. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12971. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12972. /* Invalid Parameters */
  12973. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  12974. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  12975. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  12976. /* Valid Parameters */
  12977. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12978. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12979. /* With update. */
  12980. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12981. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12982. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12983. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12984. wc_Sm3Free(&sm3);
  12985. #endif
  12986. return EXPECT_RESULT();
  12987. } /* END test_wc_Sm3Update */
  12988. /*
  12989. * Testing wc_Sm3Copy()
  12990. */
  12991. static int test_wc_Sm3Copy(void)
  12992. {
  12993. EXPECT_DECLS;
  12994. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12995. wc_Sm3 sm3;
  12996. wc_Sm3 sm3Copy;
  12997. byte hash[WC_SM3_DIGEST_SIZE];
  12998. byte hashCopy[WC_SM3_DIGEST_SIZE];
  12999. byte data[WC_SM3_BLOCK_SIZE + 1];
  13000. int i;
  13001. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13002. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13003. /* Invalid Parameters */
  13004. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13005. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13006. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13007. /* Valid Parameters */
  13008. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13009. /* Ensure all parts of data updated during hashing are copied. */
  13010. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13011. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13012. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13013. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13014. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13015. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13016. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13017. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13018. }
  13019. wc_Sm3Free(&sm3Copy);
  13020. wc_Sm3Free(&sm3);
  13021. #endif
  13022. return EXPECT_RESULT();
  13023. } /* END test_wc_Sm3Copy */
  13024. /*
  13025. * Testing wc_Sm3FinalRaw()
  13026. */
  13027. static int test_wc_Sm3FinalRaw(void)
  13028. {
  13029. EXPECT_DECLS;
  13030. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13031. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13032. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13033. !defined(WOLFSSL_NO_HASH_RAW)
  13034. wc_Sm3 sm3;
  13035. byte hash1[WC_SM3_DIGEST_SIZE];
  13036. byte hash2[WC_SM3_DIGEST_SIZE];
  13037. byte hash3[WC_SM3_DIGEST_SIZE];
  13038. byte* hash_test[3] = { hash1, hash2, hash3 };
  13039. int times;
  13040. int i;
  13041. XMEMSET(&sm3, 0, sizeof(sm3));
  13042. /* Initialize */
  13043. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13044. /* Invalid Parameters */
  13045. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13046. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13047. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13048. times = sizeof(hash_test) / sizeof(byte*);
  13049. for (i = 0; i < times; i++) {
  13050. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13051. }
  13052. wc_Sm3Free(&sm3);
  13053. #endif
  13054. return EXPECT_RESULT();
  13055. } /* END test_wc_Sm3FinalRaw */
  13056. /*
  13057. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13058. */
  13059. static int test_wc_Sm3GetSetFlags(void)
  13060. {
  13061. EXPECT_DECLS;
  13062. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13063. wc_Sm3 sm3;
  13064. wc_Sm3 sm3Copy;
  13065. word32 flags = 0;
  13066. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13067. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13068. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13069. ExpectIntEQ(flags, 0);
  13070. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13071. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13072. ExpectIntEQ(flags, 0);
  13073. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13074. ExpectIntEQ(flags, 0);
  13075. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13076. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13077. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13078. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13079. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13080. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13081. wc_Sm3Free(&sm3Copy);
  13082. wc_Sm3Free(&sm3);
  13083. #endif
  13084. return EXPECT_RESULT();
  13085. } /* END test_wc_Sm3Update */
  13086. /*
  13087. * Testing wc_Sm3Hash()
  13088. */
  13089. static int test_wc_Sm3Hash(void)
  13090. {
  13091. EXPECT_DECLS;
  13092. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13093. byte data[WC_SM3_BLOCK_SIZE];
  13094. byte hash[WC_SM3_DIGEST_SIZE];
  13095. /* Invalid parameters. */
  13096. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13097. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13098. /* Valid parameters. */
  13099. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13100. #endif
  13101. return EXPECT_RESULT();
  13102. } /* END test_wc_Sm3Hash */
  13103. /*
  13104. * Test function for wc_HmacSetKey
  13105. */
  13106. static int test_wc_Md5HmacSetKey(void)
  13107. {
  13108. EXPECT_DECLS;
  13109. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13110. Hmac hmac;
  13111. int ret, times, itr;
  13112. const char* keys[]=
  13113. {
  13114. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13115. #ifndef HAVE_FIPS
  13116. "Jefe", /* smaller than minimum FIPS key size */
  13117. #endif
  13118. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13119. };
  13120. times = sizeof(keys) / sizeof(char*);
  13121. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13122. for (itr = 0; itr < times; itr++) {
  13123. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13124. (word32)XSTRLEN(keys[itr]));
  13125. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13126. wc_HmacFree(&hmac);
  13127. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13128. #else
  13129. ExpectIntEQ(ret, 0);
  13130. #endif
  13131. }
  13132. /* Bad args. */
  13133. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13134. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13135. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13136. BAD_FUNC_ARG);
  13137. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13138. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13139. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13140. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13141. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13142. #elif defined(HAVE_FIPS)
  13143. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13144. #else
  13145. ExpectIntEQ(ret, 0);
  13146. #endif
  13147. wc_HmacFree(&hmac);
  13148. #endif
  13149. return EXPECT_RESULT();
  13150. } /* END test_wc_Md5HmacSetKey */
  13151. /*
  13152. * testing wc_HmacSetKey() on wc_Sha hash.
  13153. */
  13154. static int test_wc_ShaHmacSetKey(void)
  13155. {
  13156. EXPECT_DECLS;
  13157. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13158. Hmac hmac;
  13159. int ret, times, itr;
  13160. const char* keys[]=
  13161. {
  13162. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13163. "\x0b\x0b\x0b",
  13164. #ifndef HAVE_FIPS
  13165. "Jefe", /* smaller than minimum FIPS key size */
  13166. #endif
  13167. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13168. "\xAA\xAA\xAA"
  13169. };
  13170. times = sizeof(keys) / sizeof(char*);
  13171. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13172. for (itr = 0; itr < times; itr++) {
  13173. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13174. (word32)XSTRLEN(keys[itr])), 0);
  13175. }
  13176. /* Bad args. */
  13177. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13178. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13179. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13180. BAD_FUNC_ARG);
  13181. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13182. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13183. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13184. #ifdef HAVE_FIPS
  13185. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13186. #else
  13187. ExpectIntEQ(ret, 0);
  13188. #endif
  13189. wc_HmacFree(&hmac);
  13190. #endif
  13191. return EXPECT_RESULT();
  13192. } /* END test_wc_ShaHmacSetKey() */
  13193. /*
  13194. * testing wc_HmacSetKey() on Sha224 hash.
  13195. */
  13196. static int test_wc_Sha224HmacSetKey(void)
  13197. {
  13198. EXPECT_DECLS;
  13199. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13200. Hmac hmac;
  13201. int ret, times, itr;
  13202. const char* keys[]=
  13203. {
  13204. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13205. "\x0b\x0b\x0b",
  13206. #ifndef HAVE_FIPS
  13207. "Jefe", /* smaller than minimum FIPS key size */
  13208. #endif
  13209. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13210. "\xAA\xAA\xAA"
  13211. };
  13212. times = sizeof(keys) / sizeof(char*);
  13213. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13214. for (itr = 0; itr < times; itr++) {
  13215. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13216. (word32)XSTRLEN(keys[itr])), 0);
  13217. }
  13218. /* Bad args. */
  13219. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13220. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13221. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13222. BAD_FUNC_ARG);
  13223. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13224. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13225. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13226. #ifdef HAVE_FIPS
  13227. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13228. #else
  13229. ExpectIntEQ(ret, 0);
  13230. #endif
  13231. wc_HmacFree(&hmac);
  13232. #endif
  13233. return EXPECT_RESULT();
  13234. } /* END test_wc_Sha224HmacSetKey() */
  13235. /*
  13236. * testing wc_HmacSetKey() on Sha256 hash
  13237. */
  13238. static int test_wc_Sha256HmacSetKey(void)
  13239. {
  13240. EXPECT_DECLS;
  13241. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13242. Hmac hmac;
  13243. int ret, times, itr;
  13244. const char* keys[]=
  13245. {
  13246. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13247. "\x0b\x0b\x0b",
  13248. #ifndef HAVE_FIPS
  13249. "Jefe", /* smaller than minimum FIPS key size */
  13250. #endif
  13251. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13252. "\xAA\xAA\xAA"
  13253. };
  13254. times = sizeof(keys) / sizeof(char*);
  13255. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13256. for (itr = 0; itr < times; itr++) {
  13257. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13258. (word32)XSTRLEN(keys[itr])), 0);
  13259. }
  13260. /* Bad args. */
  13261. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13262. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13263. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13264. BAD_FUNC_ARG);
  13265. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13266. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13267. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13268. #ifdef HAVE_FIPS
  13269. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13270. #else
  13271. ExpectIntEQ(ret, 0);
  13272. #endif
  13273. wc_HmacFree(&hmac);
  13274. #endif
  13275. return EXPECT_RESULT();
  13276. } /* END test_wc_Sha256HmacSetKey() */
  13277. /*
  13278. * testing wc_HmacSetKey on Sha384 hash.
  13279. */
  13280. static int test_wc_Sha384HmacSetKey(void)
  13281. {
  13282. EXPECT_DECLS;
  13283. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13284. Hmac hmac;
  13285. int ret, times, itr;
  13286. const char* keys[]=
  13287. {
  13288. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13289. "\x0b\x0b\x0b",
  13290. #ifndef HAVE_FIPS
  13291. "Jefe", /* smaller than minimum FIPS key size */
  13292. #endif
  13293. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13294. "\xAA\xAA\xAA"
  13295. };
  13296. times = sizeof(keys) / sizeof(char*);
  13297. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13298. for (itr = 0; itr < times; itr++) {
  13299. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13300. (word32)XSTRLEN(keys[itr])), 0);
  13301. }
  13302. /* Bad args. */
  13303. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13304. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13305. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13306. BAD_FUNC_ARG);
  13307. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13308. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13309. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13310. #ifdef HAVE_FIPS
  13311. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13312. #else
  13313. ExpectIntEQ(ret, 0);
  13314. #endif
  13315. wc_HmacFree(&hmac);
  13316. #endif
  13317. return EXPECT_RESULT();
  13318. } /* END test_wc_Sha384HmacSetKey() */
  13319. /*
  13320. * testing wc_HmacUpdate on wc_Md5 hash.
  13321. */
  13322. static int test_wc_Md5HmacUpdate(void)
  13323. {
  13324. EXPECT_DECLS;
  13325. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13326. Hmac hmac;
  13327. testVector a, b;
  13328. #ifdef HAVE_FIPS
  13329. const char* keys =
  13330. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13331. #else
  13332. const char* keys = "Jefe";
  13333. #endif
  13334. a.input = "what do ya want for nothing?";
  13335. a.inLen = XSTRLEN(a.input);
  13336. b.input = "Hi There";
  13337. b.inLen = XSTRLEN(b.input);
  13338. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13339. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13340. (word32)XSTRLEN(keys)), 0);
  13341. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13342. /* Update Hmac. */
  13343. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13344. /* Test bad args. */
  13345. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13346. BAD_FUNC_ARG);
  13347. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13348. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13349. wc_HmacFree(&hmac);
  13350. #endif
  13351. return EXPECT_RESULT();
  13352. } /* END test_wc_Md5HmacUpdate */
  13353. /*
  13354. * testing wc_HmacUpdate on SHA hash.
  13355. */
  13356. static int test_wc_ShaHmacUpdate(void)
  13357. {
  13358. EXPECT_DECLS;
  13359. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13360. Hmac hmac;
  13361. testVector a, b;
  13362. #ifdef HAVE_FIPS
  13363. const char* keys =
  13364. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13365. #else
  13366. const char* keys = "Jefe";
  13367. #endif
  13368. a.input = "what do ya want for nothing?";
  13369. a.inLen = XSTRLEN(a.input);
  13370. b.input = "Hi There";
  13371. b.inLen = XSTRLEN(b.input);
  13372. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13373. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13374. (word32)XSTRLEN(keys)), 0);
  13375. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13376. /* Update Hmac. */
  13377. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13378. /* Test bad args. */
  13379. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13380. BAD_FUNC_ARG);
  13381. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13382. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13383. wc_HmacFree(&hmac);
  13384. #endif
  13385. return EXPECT_RESULT();
  13386. } /* END test_wc_ShaHmacUpdate */
  13387. /*
  13388. * testing wc_HmacUpdate on SHA224 hash.
  13389. */
  13390. static int test_wc_Sha224HmacUpdate(void)
  13391. {
  13392. EXPECT_DECLS;
  13393. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13394. Hmac hmac;
  13395. testVector a, b;
  13396. #ifdef HAVE_FIPS
  13397. const char* keys =
  13398. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13399. #else
  13400. const char* keys = "Jefe";
  13401. #endif
  13402. a.input = "what do ya want for nothing?";
  13403. a.inLen = XSTRLEN(a.input);
  13404. b.input = "Hi There";
  13405. b.inLen = XSTRLEN(b.input);
  13406. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13407. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13408. (word32)XSTRLEN(keys)), 0);
  13409. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13410. /* Update Hmac. */
  13411. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13412. /* Test bad args. */
  13413. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13414. BAD_FUNC_ARG);
  13415. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13416. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13417. wc_HmacFree(&hmac);
  13418. #endif
  13419. return EXPECT_RESULT();
  13420. } /* END test_wc_Sha224HmacUpdate */
  13421. /*
  13422. * testing wc_HmacUpdate on SHA256 hash.
  13423. */
  13424. static int test_wc_Sha256HmacUpdate(void)
  13425. {
  13426. EXPECT_DECLS;
  13427. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13428. Hmac hmac;
  13429. testVector a, b;
  13430. #ifdef HAVE_FIPS
  13431. const char* keys =
  13432. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13433. #else
  13434. const char* keys = "Jefe";
  13435. #endif
  13436. a.input = "what do ya want for nothing?";
  13437. a.inLen = XSTRLEN(a.input);
  13438. b.input = "Hi There";
  13439. b.inLen = XSTRLEN(b.input);
  13440. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13441. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13442. (word32)XSTRLEN(keys)), 0);
  13443. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13444. /* Update Hmac. */
  13445. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13446. /* Test bad args. */
  13447. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13448. BAD_FUNC_ARG);
  13449. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13450. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13451. wc_HmacFree(&hmac);
  13452. #endif
  13453. return EXPECT_RESULT();
  13454. } /* END test_wc_Sha256HmacUpdate */
  13455. /*
  13456. * testing wc_HmacUpdate on SHA384 hash.
  13457. */
  13458. static int test_wc_Sha384HmacUpdate(void)
  13459. {
  13460. EXPECT_DECLS;
  13461. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13462. Hmac hmac;
  13463. testVector a, b;
  13464. #ifdef HAVE_FIPS
  13465. const char* keys =
  13466. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13467. #else
  13468. const char* keys = "Jefe";
  13469. #endif
  13470. a.input = "what do ya want for nothing?";
  13471. a.inLen = XSTRLEN(a.input);
  13472. b.input = "Hi There";
  13473. b.inLen = XSTRLEN(b.input);
  13474. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13475. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13476. (word32)XSTRLEN(keys)), 0);
  13477. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13478. /* Update Hmac. */
  13479. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13480. /* Test bad args. */
  13481. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13482. BAD_FUNC_ARG);
  13483. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13484. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13485. wc_HmacFree(&hmac);
  13486. #endif
  13487. return EXPECT_RESULT();
  13488. } /* END test_wc_Sha384HmacUpdate */
  13489. /*
  13490. * Testing wc_HmacFinal() with MD5
  13491. */
  13492. static int test_wc_Md5HmacFinal(void)
  13493. {
  13494. EXPECT_DECLS;
  13495. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13496. Hmac hmac;
  13497. byte hash[WC_MD5_DIGEST_SIZE];
  13498. testVector a;
  13499. const char* key;
  13500. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13501. a.input = "Hi There";
  13502. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13503. "\x9d";
  13504. a.inLen = XSTRLEN(a.input);
  13505. a.outLen = XSTRLEN(a.output);
  13506. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13507. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13508. 0);
  13509. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13510. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13511. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13512. /* Try bad parameters. */
  13513. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13514. #ifndef HAVE_FIPS
  13515. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13516. #endif
  13517. wc_HmacFree(&hmac);
  13518. #endif
  13519. return EXPECT_RESULT();
  13520. } /* END test_wc_Md5HmacFinal */
  13521. /*
  13522. * Testing wc_HmacFinal() with SHA
  13523. */
  13524. static int test_wc_ShaHmacFinal(void)
  13525. {
  13526. EXPECT_DECLS;
  13527. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13528. Hmac hmac;
  13529. byte hash[WC_SHA_DIGEST_SIZE];
  13530. testVector a;
  13531. const char* key;
  13532. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13533. "\x0b\x0b\x0b";
  13534. a.input = "Hi There";
  13535. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13536. "\x8e\xf1\x46\xbe\x00";
  13537. a.inLen = XSTRLEN(a.input);
  13538. a.outLen = XSTRLEN(a.output);
  13539. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13540. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13541. 0);
  13542. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13543. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13544. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13545. /* Try bad parameters. */
  13546. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13547. #ifndef HAVE_FIPS
  13548. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13549. #endif
  13550. wc_HmacFree(&hmac);
  13551. #endif
  13552. return EXPECT_RESULT();
  13553. } /* END test_wc_ShaHmacFinal */
  13554. /*
  13555. * Testing wc_HmacFinal() with SHA224
  13556. */
  13557. static int test_wc_Sha224HmacFinal(void)
  13558. {
  13559. EXPECT_DECLS;
  13560. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13561. Hmac hmac;
  13562. byte hash[WC_SHA224_DIGEST_SIZE];
  13563. testVector a;
  13564. const char* key;
  13565. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13566. "\x0b\x0b\x0b";
  13567. a.input = "Hi There";
  13568. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13569. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13570. a.inLen = XSTRLEN(a.input);
  13571. a.outLen = XSTRLEN(a.output);
  13572. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13573. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13574. (word32)XSTRLEN(key)), 0);
  13575. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13576. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13577. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13578. /* Try bad parameters. */
  13579. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13580. #ifndef HAVE_FIPS
  13581. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13582. #endif
  13583. wc_HmacFree(&hmac);
  13584. #endif
  13585. return EXPECT_RESULT();
  13586. } /* END test_wc_Sha224HmacFinal */
  13587. /*
  13588. * Testing wc_HmacFinal() with SHA256
  13589. */
  13590. static int test_wc_Sha256HmacFinal(void)
  13591. {
  13592. EXPECT_DECLS;
  13593. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13594. Hmac hmac;
  13595. byte hash[WC_SHA256_DIGEST_SIZE];
  13596. testVector a;
  13597. const char* key;
  13598. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13599. "\x0b\x0b\x0b";
  13600. a.input = "Hi There";
  13601. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13602. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13603. "\xcf\xf7";
  13604. a.inLen = XSTRLEN(a.input);
  13605. a.outLen = XSTRLEN(a.output);
  13606. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13607. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13608. (word32)XSTRLEN(key)), 0);
  13609. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13610. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13611. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13612. /* Try bad parameters. */
  13613. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13614. #ifndef HAVE_FIPS
  13615. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13616. #endif
  13617. wc_HmacFree(&hmac);
  13618. #endif
  13619. return EXPECT_RESULT();
  13620. } /* END test_wc_Sha256HmacFinal */
  13621. /*
  13622. * Testing wc_HmacFinal() with SHA384
  13623. */
  13624. static int test_wc_Sha384HmacFinal(void)
  13625. {
  13626. EXPECT_DECLS;
  13627. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13628. Hmac hmac;
  13629. byte hash[WC_SHA384_DIGEST_SIZE];
  13630. testVector a;
  13631. const char* key;
  13632. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13633. "\x0b\x0b\x0b";
  13634. a.input = "Hi There";
  13635. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13636. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13637. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13638. "\xfa\x9c\xb6";
  13639. a.inLen = XSTRLEN(a.input);
  13640. a.outLen = XSTRLEN(a.output);
  13641. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13642. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13643. (word32)XSTRLEN(key)), 0);
  13644. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13645. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13646. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13647. /* Try bad parameters. */
  13648. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13649. #ifndef HAVE_FIPS
  13650. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13651. #endif
  13652. wc_HmacFree(&hmac);
  13653. #endif
  13654. return EXPECT_RESULT();
  13655. } /* END test_wc_Sha384HmacFinal */
  13656. /*
  13657. * Testing wc_InitCmac()
  13658. */
  13659. static int test_wc_InitCmac(void)
  13660. {
  13661. EXPECT_DECLS;
  13662. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13663. Cmac cmac1;
  13664. Cmac cmac2;
  13665. Cmac cmac3;
  13666. /* AES 128 key. */
  13667. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13668. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13669. /* AES 192 key. */
  13670. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13671. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13672. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13673. /* AES 256 key. */
  13674. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13675. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13676. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13677. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13678. word32 key1Sz = (word32)sizeof(key1) - 1;
  13679. word32 key2Sz = (word32)sizeof(key2) - 1;
  13680. word32 key3Sz = (word32)sizeof(key3) - 1;
  13681. int type = WC_CMAC_AES;
  13682. (void)key1;
  13683. (void)key1Sz;
  13684. (void)key2;
  13685. (void)key2Sz;
  13686. XMEMSET(&cmac1, 0, sizeof(Cmac));
  13687. XMEMSET(&cmac2, 0, sizeof(Cmac));
  13688. XMEMSET(&cmac3, 0, sizeof(Cmac));
  13689. #ifdef WOLFSSL_AES_128
  13690. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  13691. #endif
  13692. #ifdef WOLFSSL_AES_192
  13693. wc_AesFree(&cmac1.aes);
  13694. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  13695. #endif
  13696. #ifdef WOLFSSL_AES_256
  13697. wc_AesFree(&cmac2.aes);
  13698. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  13699. #endif
  13700. wc_AesFree(&cmac3.aes);
  13701. /* Test bad args. */
  13702. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  13703. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  13704. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  13705. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  13706. #endif
  13707. return EXPECT_RESULT();
  13708. } /* END test_wc_InitCmac */
  13709. /*
  13710. * Testing wc_CmacUpdate()
  13711. */
  13712. static int test_wc_CmacUpdate(void)
  13713. {
  13714. EXPECT_DECLS;
  13715. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13716. Cmac cmac;
  13717. byte key[] = {
  13718. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13719. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13720. };
  13721. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13722. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13723. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13724. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13725. "\xf4";
  13726. word32 inSz = (word32)sizeof(in) - 1;
  13727. word32 keySz = (word32)sizeof(key);
  13728. int type = WC_CMAC_AES;
  13729. XMEMSET(&cmac, 0, sizeof(Cmac));
  13730. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13731. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  13732. /* Test bad args. */
  13733. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  13734. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  13735. wc_AesFree(&cmac.aes);
  13736. #endif
  13737. return EXPECT_RESULT();
  13738. } /* END test_wc_CmacUpdate */
  13739. /*
  13740. * Testing wc_CmacFinal()
  13741. */
  13742. static int test_wc_CmacFinal(void)
  13743. {
  13744. EXPECT_DECLS;
  13745. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13746. Cmac cmac;
  13747. byte key[] = {
  13748. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13749. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13750. };
  13751. byte msg[] = {
  13752. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13753. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13754. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13755. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13756. 0xf4
  13757. };
  13758. /* Test vectors from CMACGenAES128.rsp from
  13759. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13760. * Per RFC4493 truncation of lsb is possible.
  13761. */
  13762. byte expMac[] = {
  13763. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13764. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13765. };
  13766. byte mac[AES_BLOCK_SIZE];
  13767. word32 msgSz = (word32)sizeof(msg);
  13768. word32 keySz = (word32)sizeof(key);
  13769. word32 macSz = sizeof(mac);
  13770. word32 badMacSz = 17;
  13771. int expMacSz = sizeof(expMac);
  13772. int type = WC_CMAC_AES;
  13773. XMEMSET(&cmac, 0, sizeof(Cmac));
  13774. XMEMSET(mac, 0, macSz);
  13775. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13776. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  13777. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  13778. /* Pass in bad args. */
  13779. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  13780. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13781. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  13782. /* For the last call, use the API with implicit wc_CmacFree(). */
  13783. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13784. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13785. #else /* !HAVE_FIPS || FIPS>=5.3 */
  13786. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13787. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13788. /* Pass in bad args. */
  13789. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  13790. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13791. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  13792. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  13793. #endif
  13794. return EXPECT_RESULT();
  13795. } /* END test_wc_CmacFinal */
  13796. /*
  13797. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13798. */
  13799. static int test_wc_AesCmacGenerate(void)
  13800. {
  13801. EXPECT_DECLS;
  13802. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13803. byte key[] = {
  13804. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13805. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13806. };
  13807. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13808. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13809. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13810. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13811. byte mac[AES_BLOCK_SIZE];
  13812. word32 keySz = sizeof(key);
  13813. word32 macSz = sizeof(mac);
  13814. word32 msgSz = sizeof(msg) - 1;
  13815. word32 expMacSz = sizeof(expMac) - 1;
  13816. XMEMSET(mac, 0, macSz);
  13817. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  13818. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13819. /* Pass in bad args. */
  13820. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  13821. BAD_FUNC_ARG);
  13822. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  13823. BAD_FUNC_ARG);
  13824. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  13825. BAD_FUNC_ARG);
  13826. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  13827. BAD_FUNC_ARG);
  13828. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  13829. /* Test bad args. */
  13830. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  13831. BAD_FUNC_ARG);
  13832. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  13833. BAD_FUNC_ARG);
  13834. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  13835. BAD_FUNC_ARG);
  13836. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  13837. BAD_FUNC_ARG);
  13838. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  13839. BAD_FUNC_ARG);
  13840. #endif
  13841. return EXPECT_RESULT();
  13842. } /* END test_wc_AesCmacGenerate */
  13843. /*
  13844. * Testing streaming AES-GCM API.
  13845. */
  13846. static int test_wc_AesGcmStream(void)
  13847. {
  13848. EXPECT_DECLS;
  13849. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13850. defined(WOLFSSL_AESGCM_STREAM)
  13851. int i;
  13852. WC_RNG rng[1];
  13853. Aes aesEnc[1];
  13854. Aes aesDec[1];
  13855. byte tag[AES_BLOCK_SIZE];
  13856. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13857. byte out[AES_BLOCK_SIZE * 3 + 2];
  13858. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13859. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13860. byte key[AES_128_KEY_SIZE] = { 0, };
  13861. byte iv[AES_IV_SIZE] = { 1, };
  13862. byte ivOut[AES_IV_SIZE];
  13863. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13864. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13865. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13866. };
  13867. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13868. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13869. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13870. };
  13871. static const byte expTag[AES_BLOCK_SIZE] = {
  13872. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13873. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13874. };
  13875. XMEMSET(&rng, 0, sizeof(WC_RNG));
  13876. XMEMSET(&aesEnc, 0, sizeof(Aes));
  13877. XMEMSET(&aesDec, 0, sizeof(Aes));
  13878. /* Create a random for generating IV/nonce. */
  13879. ExpectIntEQ(wc_InitRng(rng), 0);
  13880. /* Initialize data structures. */
  13881. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13882. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13883. /* BadParameters to streaming init. */
  13884. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13885. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13886. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13887. BAD_FUNC_ARG);
  13888. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13889. BAD_FUNC_ARG);
  13890. /* Bad parameters to encrypt update. */
  13891. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13892. BAD_FUNC_ARG);
  13893. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13894. BAD_FUNC_ARG);
  13895. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13896. BAD_FUNC_ARG);
  13897. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13898. BAD_FUNC_ARG);
  13899. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13900. BAD_FUNC_ARG);
  13901. /* Bad parameters to decrypt update. */
  13902. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13903. BAD_FUNC_ARG);
  13904. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13905. BAD_FUNC_ARG);
  13906. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13907. BAD_FUNC_ARG);
  13908. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13909. BAD_FUNC_ARG);
  13910. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13911. BAD_FUNC_ARG);
  13912. /* Bad parameters to encrypt final. */
  13913. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13914. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13915. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13916. BAD_FUNC_ARG);
  13917. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13918. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13919. BAD_FUNC_ARG);
  13920. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13921. BAD_FUNC_ARG);
  13922. /* Bad parameters to decrypt final. */
  13923. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13924. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13925. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13926. BAD_FUNC_ARG);
  13927. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13928. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13929. BAD_FUNC_ARG);
  13930. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13931. BAD_FUNC_ARG);
  13932. /* Check calling final before setting key fails. */
  13933. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13934. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13935. /* Check calling update before setting key else fails. */
  13936. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13937. MISSING_KEY);
  13938. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13939. MISSING_KEY);
  13940. /* Set key but not IV. */
  13941. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13942. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13943. /* Check calling final before setting IV fails. */
  13944. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13945. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13946. /* Check calling update before setting IV else fails. */
  13947. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13948. MISSING_IV);
  13949. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13950. MISSING_IV);
  13951. /* Set IV using fixed part IV and external IV APIs. */
  13952. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13953. rng), 0);
  13954. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13955. GCM_NONCE_MID_SZ), 0);
  13956. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13957. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13958. /* Encrypt and decrypt data. */
  13959. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13960. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13961. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13962. /* Finalize and check tag matches. */
  13963. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13964. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13965. /* Set key and IV through streaming init API. */
  13966. wc_AesFree(aesEnc);
  13967. wc_AesFree(aesDec);
  13968. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13969. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13970. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13971. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13972. /* Encrypt/decrypt one block and AAD of one block. */
  13973. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13974. AES_BLOCK_SIZE), 0);
  13975. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13976. AES_BLOCK_SIZE), 0);
  13977. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13978. /* Finalize and check tag matches. */
  13979. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13980. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13981. /* Set key and IV through streaming init API. */
  13982. wc_AesFree(aesEnc);
  13983. wc_AesFree(aesDec);
  13984. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13985. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13986. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13987. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13988. /* No data to encrypt/decrypt one byte of AAD. */
  13989. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13990. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13991. /* Finalize and check tag matches. */
  13992. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13993. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13994. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13995. /* Set key and IV through streaming init API. */
  13996. wc_AesFree(aesEnc);
  13997. wc_AesFree(aesDec);
  13998. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13999. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14000. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14001. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14002. /* Encrypt/decrypt one byte and no AAD. */
  14003. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14004. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14005. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14006. /* Finalize and check tag matches. */
  14007. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14008. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14009. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14010. /* Set key and IV through streaming init API. */
  14011. wc_AesFree(aesEnc);
  14012. wc_AesFree(aesDec);
  14013. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14014. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14015. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14016. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14017. /* Encryption AES is one byte at a time */
  14018. for (i = 0; i < (int)sizeof(aad); i++) {
  14019. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14020. 0);
  14021. }
  14022. for (i = 0; i < (int)sizeof(in); i++) {
  14023. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14024. 0);
  14025. }
  14026. /* Decryption AES is two bytes at a time */
  14027. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14028. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14029. 0);
  14030. }
  14031. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14032. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14033. 0), 0);
  14034. }
  14035. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14036. /* Finalize and check tag matches. */
  14037. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14038. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14039. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14040. /* Check streaming encryption can be decrypted with one shot. */
  14041. wc_AesFree(aesDec);
  14042. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14043. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14044. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14045. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14046. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14047. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14048. wc_AesFree(aesEnc);
  14049. wc_AesFree(aesDec);
  14050. wc_FreeRng(rng);
  14051. #endif
  14052. return EXPECT_RESULT();
  14053. } /* END test_wc_AesGcmStream */
  14054. /*
  14055. * Testing streaming SM4 API.
  14056. */
  14057. static int test_wc_Sm4(void)
  14058. {
  14059. int res = TEST_SKIPPED;
  14060. #ifdef WOLFSSL_SM4
  14061. EXPECT_DECLS;
  14062. wc_Sm4 sm4;
  14063. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14064. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14065. unsigned char key[SM4_KEY_SIZE];
  14066. #endif
  14067. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14068. unsigned char iv[SM4_IV_SIZE];
  14069. #endif
  14070. /* Invalid parameters - wc_Sm4Init */
  14071. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14072. /* Valid cases - wc_Sm4Init */
  14073. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14074. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14075. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14076. XMEMSET(key, 0, sizeof(key));
  14077. /* Invalid parameters - wc_Sm4SetKey. */
  14078. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14079. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14080. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14081. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14082. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14083. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14084. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14085. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14086. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14087. /* Valid cases - wc_Sm4SetKey. */
  14088. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14089. #endif
  14090. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14091. XMEMSET(iv, 0, sizeof(iv));
  14092. /* Invalid parameters - wc_Sm4SetIV. */
  14093. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14094. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14095. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14096. /* Valid cases - wc_Sm4SetIV. */
  14097. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14098. #endif
  14099. /* Valid cases - wc_Sm4Free */
  14100. wc_Sm4Free(NULL);
  14101. wc_Sm4Free(&sm4);
  14102. res = EXPECT_RESULT();
  14103. #endif
  14104. return res;
  14105. } /* END test_wc_Sm4 */
  14106. /*
  14107. * Testing block based SM4-ECB API.
  14108. */
  14109. static int test_wc_Sm4Ecb(void)
  14110. {
  14111. int res = TEST_SKIPPED;
  14112. #ifdef WOLFSSL_SM4_ECB
  14113. EXPECT_DECLS;
  14114. wc_Sm4 sm4;
  14115. unsigned char key[SM4_KEY_SIZE];
  14116. unsigned char in[SM4_BLOCK_SIZE * 2];
  14117. unsigned char out[SM4_BLOCK_SIZE * 2];
  14118. unsigned char out2[SM4_BLOCK_SIZE];
  14119. XMEMSET(key, 0, sizeof(key));
  14120. XMEMSET(in, 0, sizeof(in));
  14121. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14122. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14123. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14124. /* Tested in test_wc_Sm4. */
  14125. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14126. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14127. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14128. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14129. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14130. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14131. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14132. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14133. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14134. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14135. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14136. /* Valid cases - wc_Sm4EcbEncrypt. */
  14137. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14138. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14139. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14140. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14141. /* In and out are same pointer. */
  14142. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14143. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14144. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14145. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14146. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14147. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14148. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14149. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14150. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14151. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14152. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14153. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14154. /* Valid cases - wc_Sm4EcbDecrypt. */
  14155. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14156. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14157. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14158. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14159. /* In and out are same pointer. */
  14160. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14161. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14162. wc_Sm4Free(&sm4);
  14163. res = EXPECT_RESULT();
  14164. #endif
  14165. return res;
  14166. } /* END test_wc_Sm4Ecb */
  14167. /*
  14168. * Testing block based SM4-CBC API.
  14169. */
  14170. static int test_wc_Sm4Cbc(void)
  14171. {
  14172. int res = TEST_SKIPPED;
  14173. #ifdef WOLFSSL_SM4_CBC
  14174. EXPECT_DECLS;
  14175. wc_Sm4 sm4;
  14176. unsigned char key[SM4_KEY_SIZE];
  14177. unsigned char iv[SM4_IV_SIZE];
  14178. unsigned char in[SM4_BLOCK_SIZE * 2];
  14179. unsigned char out[SM4_BLOCK_SIZE * 2];
  14180. unsigned char out2[SM4_BLOCK_SIZE];
  14181. XMEMSET(key, 0, sizeof(key));
  14182. XMEMSET(iv, 0, sizeof(iv));
  14183. XMEMSET(in, 0, sizeof(in));
  14184. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14185. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14186. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14187. /* Tested in test_wc_Sm4. */
  14188. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14189. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14190. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14191. /* Tested in test_wc_Sm4. */
  14192. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14193. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14194. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14195. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14196. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14197. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14198. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14199. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14200. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14201. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14202. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14203. /* Valid cases - wc_Sm4CbcEncrypt. */
  14204. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14205. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14206. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14207. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14208. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14209. /* In and out are same pointer. */
  14210. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14211. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14212. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14213. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14214. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14215. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14216. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14217. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14218. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14219. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14220. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14221. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14222. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14223. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14224. /* Valid cases - wc_Sm4CbcDecrypt. */
  14225. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14226. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14227. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14228. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14229. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14230. /* In and out are same pointer. */
  14231. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14232. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14233. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14234. wc_Sm4Free(&sm4);
  14235. res = EXPECT_RESULT();
  14236. #endif
  14237. return res;
  14238. } /* END test_wc_Sm4Cbc */
  14239. /*
  14240. * Testing streaming SM4-CTR API.
  14241. */
  14242. static int test_wc_Sm4Ctr(void)
  14243. {
  14244. int res = TEST_SKIPPED;
  14245. #ifdef WOLFSSL_SM4_CTR
  14246. EXPECT_DECLS;
  14247. wc_Sm4 sm4;
  14248. unsigned char key[SM4_KEY_SIZE];
  14249. unsigned char iv[SM4_IV_SIZE];
  14250. unsigned char in[SM4_BLOCK_SIZE * 4];
  14251. unsigned char out[SM4_BLOCK_SIZE * 4];
  14252. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14253. word32 chunk;
  14254. word32 i;
  14255. XMEMSET(key, 0, sizeof(key));
  14256. XMEMSET(iv, 0, sizeof(iv));
  14257. XMEMSET(in, 0, sizeof(in));
  14258. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14259. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14260. /* Tested in test_wc_Sm4. */
  14261. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14262. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14263. /* Tested in test_wc_Sm4. */
  14264. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14265. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14266. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14267. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14268. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14269. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14270. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14271. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14272. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14273. /* Valid cases - wc_Sm4CtrEncrypt. */
  14274. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14275. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14276. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14277. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14278. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14279. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14280. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14281. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14282. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14283. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14284. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14285. /* In and out are same pointer. Also check encrypt of cipher text produces
  14286. * plaintext.
  14287. */
  14288. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14289. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14290. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14291. /* Chunking tests. */
  14292. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14293. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14294. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14295. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14296. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14297. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  14298. }
  14299. if (i < (word32)sizeof(in)) {
  14300. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  14301. (word32)sizeof(in) - i), 0);
  14302. }
  14303. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  14304. }
  14305. for (i = 0; i < (word32)sizeof(iv); i++) {
  14306. iv[i] = 0xff;
  14307. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14308. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14309. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14310. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  14311. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  14312. }
  14313. wc_Sm4Free(&sm4);
  14314. res = EXPECT_RESULT();
  14315. #endif
  14316. return res;
  14317. } /* END test_wc_Sm4Ctr */
  14318. /*
  14319. * Testing stream SM4-GCM API.
  14320. */
  14321. static int test_wc_Sm4Gcm(void)
  14322. {
  14323. int res = TEST_SKIPPED;
  14324. #ifdef WOLFSSL_SM4_GCM
  14325. EXPECT_DECLS;
  14326. wc_Sm4 sm4;
  14327. unsigned char key[SM4_KEY_SIZE];
  14328. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14329. unsigned char in[SM4_BLOCK_SIZE * 2];
  14330. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14331. unsigned char out[SM4_BLOCK_SIZE * 2];
  14332. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14333. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14334. unsigned char tag[SM4_BLOCK_SIZE];
  14335. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14336. word32 i;
  14337. XMEMSET(key, 0, sizeof(key));
  14338. XMEMSET(nonce, 0, sizeof(nonce));
  14339. XMEMSET(in, 0, sizeof(in));
  14340. XMEMSET(in2, 0, sizeof(in2));
  14341. XMEMSET(aad, 0, sizeof(aad));
  14342. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14343. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14344. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14345. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14346. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14347. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14348. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14349. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14350. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14351. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14352. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14353. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14354. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14355. /* Valid parameters - wc_Sm4GcmSetKey. */
  14356. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14357. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14358. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14359. 0), BAD_FUNC_ARG);
  14360. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14361. 0), BAD_FUNC_ARG);
  14362. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14363. 0), BAD_FUNC_ARG);
  14364. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14365. 0), BAD_FUNC_ARG);
  14366. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14367. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14368. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14369. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14370. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14371. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14372. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14373. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14374. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14375. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14376. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14377. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14378. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14379. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14380. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14381. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14382. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14383. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14384. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14385. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14386. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14387. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14388. 0), BAD_FUNC_ARG);
  14389. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14390. 0), BAD_FUNC_ARG);
  14391. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14392. 0), BAD_FUNC_ARG);
  14393. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14394. 0), BAD_FUNC_ARG);
  14395. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14396. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14397. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14398. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14399. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14400. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14401. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14402. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14403. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14404. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14405. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14406. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14407. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14408. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14409. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14410. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14411. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14412. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14413. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14414. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14415. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14416. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14417. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14418. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14419. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14420. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14421. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14422. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14423. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14424. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14425. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14426. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14427. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14428. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14429. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14430. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14431. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14432. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14433. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14434. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14435. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14436. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14437. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14438. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14439. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14440. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14441. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14442. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14443. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14444. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14445. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14446. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14447. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14448. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14449. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14450. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14451. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14452. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14453. SM4_GCM_AUTH_E);
  14454. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14455. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14456. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14457. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14458. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14459. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14460. }
  14461. /* Check different in/out sizes. */
  14462. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14463. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14464. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14465. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14466. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14467. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14468. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14469. XMEMCPY(out2, out, i - 1);
  14470. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14471. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14472. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14473. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14474. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14475. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14476. }
  14477. /* Force the counter to roll over in first byte. */
  14478. {
  14479. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14480. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14481. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14482. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14483. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14484. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14485. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14486. }
  14487. wc_Sm4Free(&sm4);
  14488. res = EXPECT_RESULT();
  14489. #endif
  14490. return res;
  14491. } /* END test_wc_Sm4Gcm */
  14492. /*
  14493. * Testing stream SM4-CCM API.
  14494. */
  14495. static int test_wc_Sm4Ccm(void)
  14496. {
  14497. int res = TEST_SKIPPED;
  14498. #ifdef WOLFSSL_SM4_CCM
  14499. EXPECT_DECLS;
  14500. wc_Sm4 sm4;
  14501. unsigned char key[SM4_KEY_SIZE];
  14502. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14503. unsigned char in[SM4_BLOCK_SIZE * 2];
  14504. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14505. unsigned char out[SM4_BLOCK_SIZE * 2];
  14506. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14507. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14508. unsigned char tag[SM4_BLOCK_SIZE];
  14509. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14510. word32 i;
  14511. XMEMSET(key, 0, sizeof(key));
  14512. XMEMSET(nonce, 0, sizeof(nonce));
  14513. XMEMSET(in, 0, sizeof(in));
  14514. XMEMSET(in2, 0, sizeof(in2));
  14515. XMEMSET(aad, 0, sizeof(aad));
  14516. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14517. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14518. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14519. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14520. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14521. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14522. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14523. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14524. 0), BAD_FUNC_ARG);
  14525. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14526. 0), BAD_FUNC_ARG);
  14527. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14528. 0), BAD_FUNC_ARG);
  14529. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14530. 0), BAD_FUNC_ARG);
  14531. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14532. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14533. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14534. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14535. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14536. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14537. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14538. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14539. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14540. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14541. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14542. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14543. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14544. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14545. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14546. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14547. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14548. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14549. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14550. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14551. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14552. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14553. 0), BAD_FUNC_ARG);
  14554. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14555. 0), BAD_FUNC_ARG);
  14556. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14557. 0), BAD_FUNC_ARG);
  14558. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14559. 0), BAD_FUNC_ARG);
  14560. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14561. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14562. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14563. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14564. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14565. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14566. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14567. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14568. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14569. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14570. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14571. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14572. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14573. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14574. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14575. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14576. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14577. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14578. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14579. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14580. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14581. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14582. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14583. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14584. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14585. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14586. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14587. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14588. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14589. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14590. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14591. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14592. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14593. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14594. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14595. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14596. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14597. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14598. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14599. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14600. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14601. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14602. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14603. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14604. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14605. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14606. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14607. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14608. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14609. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14610. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14611. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14612. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14613. }
  14614. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14615. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14616. SM4_CCM_AUTH_E);
  14617. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14618. for (i = 0; i < 4; i++) {
  14619. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14620. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14621. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14622. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14623. }
  14624. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14625. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14626. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14627. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14628. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14629. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14630. }
  14631. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14632. * Even values in range 4..SM4_BLOCK_SIZE.
  14633. */
  14634. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14635. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14636. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14637. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14638. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14639. }
  14640. /* Check different in/out sizes. */
  14641. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14642. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14643. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14644. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14645. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14646. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14647. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14648. XMEMCPY(out2, out, i - 1);
  14649. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  14650. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14651. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14652. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  14653. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14654. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14655. }
  14656. /* Force the counter to roll over in first byte. */
  14657. {
  14658. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14659. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14660. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14661. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14662. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14663. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14664. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14665. }
  14666. wc_Sm4Free(&sm4);
  14667. res = EXPECT_RESULT();
  14668. #endif
  14669. return res;
  14670. } /* END test_wc_Sm4Ccm */
  14671. /*
  14672. * unit test for wc_Des3_SetIV()
  14673. */
  14674. static int test_wc_Des3_SetIV(void)
  14675. {
  14676. EXPECT_DECLS;
  14677. #ifndef NO_DES3
  14678. Des3 des;
  14679. const byte key[] = {
  14680. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14681. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14682. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14683. };
  14684. const byte iv[] = {
  14685. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14686. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14687. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14688. };
  14689. XMEMSET(&des, 0, sizeof(Des3));
  14690. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14691. /* DES_ENCRYPTION or DES_DECRYPTION */
  14692. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14693. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14694. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14695. /* Test explicitly wc_Des3_SetIV() */
  14696. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  14697. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  14698. #endif
  14699. wc_Des3Free(&des);
  14700. #endif
  14701. return EXPECT_RESULT();
  14702. } /* END test_wc_Des3_SetIV */
  14703. /*
  14704. * unit test for wc_Des3_SetKey()
  14705. */
  14706. static int test_wc_Des3_SetKey(void)
  14707. {
  14708. EXPECT_DECLS;
  14709. #ifndef NO_DES3
  14710. Des3 des;
  14711. const byte key[] = {
  14712. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14713. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14714. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14715. };
  14716. const byte iv[] = {
  14717. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14718. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14719. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14720. };
  14721. XMEMSET(&des, 0, sizeof(Des3));
  14722. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14723. /* DES_ENCRYPTION or DES_DECRYPTION */
  14724. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14725. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14726. /* Test bad args. */
  14727. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14728. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14729. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  14730. /* Default case. Should return 0. */
  14731. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  14732. wc_Des3Free(&des);
  14733. #endif
  14734. return EXPECT_RESULT();
  14735. } /* END test_wc_Des3_SetKey */
  14736. /*
  14737. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14738. */
  14739. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14740. {
  14741. EXPECT_DECLS;
  14742. #ifndef NO_DES3
  14743. Des3 des;
  14744. byte cipher[24];
  14745. byte plain[24];
  14746. const byte key[] = {
  14747. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14748. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14749. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14750. };
  14751. const byte iv[] = {
  14752. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14753. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14754. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14755. };
  14756. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14757. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14758. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14759. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14760. };
  14761. XMEMSET(&des, 0, sizeof(Des3));
  14762. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14763. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14764. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  14765. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  14766. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  14767. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14768. /* Pass in bad args. */
  14769. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  14770. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  14771. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  14772. BAD_FUNC_ARG);
  14773. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  14774. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  14775. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  14776. wc_Des3Free(&des);
  14777. #endif
  14778. return EXPECT_RESULT();
  14779. } /* END wc_Des3_CbcEncrypt */
  14780. /*
  14781. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14782. */
  14783. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14784. {
  14785. EXPECT_DECLS;
  14786. #ifndef NO_DES3
  14787. word32 vectorSz, cipherSz;
  14788. byte cipher[24];
  14789. byte plain[24];
  14790. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  14791. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14792. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14793. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14794. };
  14795. byte key[] = {
  14796. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14797. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14798. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14799. };
  14800. byte iv[] = {
  14801. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14802. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14803. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14804. };
  14805. vectorSz = sizeof(byte) * 24;
  14806. cipherSz = sizeof(byte) * 24;
  14807. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  14808. 0);
  14809. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  14810. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14811. /* pass in bad args. */
  14812. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  14813. BAD_FUNC_ARG);
  14814. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  14815. BAD_FUNC_ARG);
  14816. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  14817. BAD_FUNC_ARG);
  14818. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  14819. 0);
  14820. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  14821. BAD_FUNC_ARG);
  14822. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  14823. BAD_FUNC_ARG);
  14824. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  14825. BAD_FUNC_ARG);
  14826. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  14827. 0);
  14828. #endif
  14829. return EXPECT_RESULT();
  14830. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14831. /*
  14832. * Unit test for wc_Des3_EcbEncrypt
  14833. */
  14834. static int test_wc_Des3_EcbEncrypt(void)
  14835. {
  14836. EXPECT_DECLS;
  14837. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14838. Des3 des;
  14839. byte cipher[24];
  14840. word32 cipherSz = sizeof(cipher);
  14841. const byte key[] = {
  14842. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14843. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14844. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14845. };
  14846. const byte iv[] = {
  14847. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14848. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14849. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14850. };
  14851. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14852. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14853. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14854. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14855. };
  14856. XMEMSET(&des, 0, sizeof(Des3));
  14857. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14858. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14859. /* Bad Cases */
  14860. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14861. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  14862. BAD_FUNC_ARG);
  14863. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  14864. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  14865. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  14866. /* Good Cases */
  14867. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  14868. wc_Des3Free(&des);
  14869. #endif
  14870. return EXPECT_RESULT();
  14871. } /* END test_wc_Des3_EcbEncrypt */
  14872. /*
  14873. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14874. */
  14875. static int test_wc_Chacha_SetKey(void)
  14876. {
  14877. EXPECT_DECLS;
  14878. #ifdef HAVE_CHACHA
  14879. ChaCha ctx;
  14880. const byte key[] = {
  14881. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14882. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14883. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14884. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14885. };
  14886. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14887. byte cipher[128];
  14888. XMEMSET(cipher, 0, sizeof(cipher));
  14889. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  14890. /* Test bad args. */
  14891. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14893. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  14894. /* Test bad args. */
  14895. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  14896. #endif
  14897. return EXPECT_RESULT();
  14898. } /* END test_wc_Chacha_SetKey */
  14899. /*
  14900. * unit test for wc_Poly1305SetKey()
  14901. */
  14902. static int test_wc_Poly1305SetKey(void)
  14903. {
  14904. EXPECT_DECLS;
  14905. #ifdef HAVE_POLY1305
  14906. Poly1305 ctx;
  14907. const byte key[] =
  14908. {
  14909. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14910. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14911. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14912. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14913. };
  14914. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14915. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  14916. /* Test bad args. */
  14917. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  14918. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  14919. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14920. #endif
  14921. return EXPECT_RESULT();
  14922. } /* END test_wc_Poly1305_SetKey() */
  14923. /*
  14924. * Testing wc_Chacha_Process()
  14925. */
  14926. static int test_wc_Chacha_Process(void)
  14927. {
  14928. EXPECT_DECLS;
  14929. #ifdef HAVE_CHACHA
  14930. ChaCha enc, dec;
  14931. byte cipher[128];
  14932. byte plain[128];
  14933. const byte key[] =
  14934. {
  14935. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14938. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14939. };
  14940. const char* input = "Everybody gets Friday off.";
  14941. word32 keySz = sizeof(key)/sizeof(byte);
  14942. unsigned long int inlen = XSTRLEN(input);
  14943. /* Initialize stack variables. */
  14944. XMEMSET(cipher, 0, 128);
  14945. XMEMSET(plain, 0, 128);
  14946. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  14947. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  14948. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14949. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14950. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  14951. 0);
  14952. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  14953. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14954. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14955. /* test checking and using leftovers, currently just in C code */
  14956. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14957. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14958. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  14959. (word32)inlen - 2), 0);
  14960. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14961. (byte*)input + (inlen - 2), 2), 0);
  14962. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  14963. (word32)inlen - 2), 0);
  14964. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14965. (byte*)input + (inlen - 2), 2), 0);
  14966. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14967. /* check edge cases with counter increment */
  14968. {
  14969. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14970. const byte expected[] = {
  14971. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14972. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14973. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14974. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14975. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14976. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14977. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14978. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14979. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14980. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14981. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14982. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14983. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14984. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14985. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14986. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14987. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14988. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14989. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14990. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14991. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14992. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14993. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14994. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14995. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14996. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14997. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14998. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14999. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15000. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15001. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15002. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15003. };
  15004. const byte iv2[] = {
  15005. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15006. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15007. };
  15008. byte input2[256];
  15009. int i;
  15010. for (i = 0; i < 256; i++)
  15011. input2[i] = i;
  15012. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15013. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15014. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15015. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15016. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15017. /* partial */
  15018. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15019. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15020. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15021. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15022. }
  15023. #endif
  15024. /* Test bad args. */
  15025. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15026. BAD_FUNC_ARG);
  15027. #endif
  15028. return EXPECT_RESULT();
  15029. } /* END test_wc_Chacha_Process */
  15030. /*
  15031. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15032. */
  15033. static int test_wc_ChaCha20Poly1305_aead(void)
  15034. {
  15035. EXPECT_DECLS;
  15036. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15037. const byte key[] = {
  15038. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15039. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15040. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15041. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15042. };
  15043. const byte plaintext[] = {
  15044. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15045. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15046. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15047. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15048. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15049. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15050. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15051. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15052. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15053. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15054. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15055. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15056. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15057. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15058. 0x74, 0x2e
  15059. };
  15060. const byte iv[] = {
  15061. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15062. 0x44, 0x45, 0x46, 0x47
  15063. };
  15064. const byte aad[] = { /* additional data */
  15065. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15066. 0xc4, 0xc5, 0xc6, 0xc7
  15067. };
  15068. const byte cipher[] = { /* expected output from operation */
  15069. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15070. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15071. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15072. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15073. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15074. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15075. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15076. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15077. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15078. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15079. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15080. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15081. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15082. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15083. 0x61, 0x16
  15084. };
  15085. const byte authTag[] = { /* expected output from operation */
  15086. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15087. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15088. };
  15089. byte generatedCiphertext[272];
  15090. byte generatedPlaintext[272];
  15091. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15092. /* Initialize stack variables. */
  15093. XMEMSET(generatedCiphertext, 0, 272);
  15094. XMEMSET(generatedPlaintext, 0, 272);
  15095. /* Test Encrypt */
  15096. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15097. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15098. 0);
  15099. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15100. sizeof(cipher)/sizeof(byte)), 0);
  15101. /* Test bad args. */
  15102. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15103. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15104. BAD_FUNC_ARG);
  15105. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15106. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15107. BAD_FUNC_ARG);
  15108. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15109. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15110. BAD_FUNC_ARG);
  15111. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15112. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15113. BAD_FUNC_ARG);
  15114. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15115. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15116. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15117. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15118. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15119. sizeof(cipher), authTag, generatedPlaintext), 0);
  15120. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15121. sizeof(plaintext)/sizeof(byte)), 0);
  15122. /* Test bad args. */
  15123. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15124. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15125. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15126. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15127. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15128. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15129. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15130. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15131. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15132. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15133. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15134. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15135. #endif
  15136. return EXPECT_RESULT();
  15137. } /* END test_wc_ChaCha20Poly1305_aead */
  15138. /*
  15139. * Testing function for wc_Rc2SetKey().
  15140. */
  15141. static int test_wc_Rc2SetKey(void)
  15142. {
  15143. EXPECT_DECLS;
  15144. #ifdef WC_RC2
  15145. Rc2 rc2;
  15146. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15147. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15148. /* valid key and IV */
  15149. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15150. iv, 40), 0);
  15151. /* valid key, no IV */
  15152. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15153. NULL, 40), 0);
  15154. /* bad arguments */
  15155. /* null Rc2 struct */
  15156. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15157. iv, 40), BAD_FUNC_ARG);
  15158. /* null key */
  15159. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15160. iv, 40), BAD_FUNC_ARG);
  15161. /* key size == 0 */
  15162. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15163. /* key size > 128 */
  15164. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15165. /* effective bits == 0 */
  15166. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15167. iv, 0), WC_KEY_SIZE_E);
  15168. /* effective bits > 1024 */
  15169. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15170. iv, 1025), WC_KEY_SIZE_E);
  15171. #endif
  15172. return EXPECT_RESULT();
  15173. } /* END test_wc_Rc2SetKey */
  15174. /*
  15175. * Testing function for wc_Rc2SetIV().
  15176. */
  15177. static int test_wc_Rc2SetIV(void)
  15178. {
  15179. EXPECT_DECLS;
  15180. #ifdef WC_RC2
  15181. Rc2 rc2;
  15182. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15183. /* valid IV */
  15184. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15185. /* valid NULL IV */
  15186. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15187. /* bad arguments */
  15188. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15189. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15190. #endif
  15191. return EXPECT_RESULT();
  15192. } /* END test_wc_Rc2SetIV */
  15193. /*
  15194. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15195. */
  15196. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15197. {
  15198. EXPECT_DECLS;
  15199. #ifdef WC_RC2
  15200. Rc2 rc2;
  15201. int effectiveKeyBits = 63;
  15202. byte cipher[RC2_BLOCK_SIZE];
  15203. byte plain[RC2_BLOCK_SIZE];
  15204. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15205. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15206. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15207. XMEMSET(cipher, 0, sizeof(cipher));
  15208. XMEMSET(plain, 0, sizeof(plain));
  15209. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15210. NULL, effectiveKeyBits), 0);
  15211. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15212. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15213. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15214. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15215. /* Rc2EcbEncrypt bad arguments */
  15216. /* null Rc2 struct */
  15217. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15218. BAD_FUNC_ARG);
  15219. /* null out buffer */
  15220. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15221. BAD_FUNC_ARG);
  15222. /* null input buffer */
  15223. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15224. BAD_FUNC_ARG);
  15225. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15226. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15227. /* Rc2EcbDecrypt bad arguments */
  15228. /* null Rc2 struct */
  15229. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15230. BAD_FUNC_ARG);
  15231. /* null out buffer */
  15232. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15233. BAD_FUNC_ARG);
  15234. /* null input buffer */
  15235. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15236. BAD_FUNC_ARG);
  15237. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15238. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15239. #endif
  15240. return EXPECT_RESULT();
  15241. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15242. /*
  15243. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15244. */
  15245. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15246. {
  15247. EXPECT_DECLS;
  15248. #ifdef WC_RC2
  15249. Rc2 rc2;
  15250. int effectiveKeyBits = 63;
  15251. byte cipher[RC2_BLOCK_SIZE*2];
  15252. byte plain[RC2_BLOCK_SIZE*2];
  15253. /* vector taken from test.c */
  15254. byte key[] = {
  15255. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15256. };
  15257. byte iv[] = {
  15258. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15259. };
  15260. byte input[] = {
  15261. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15262. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15263. };
  15264. byte output[] = {
  15265. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15266. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15267. };
  15268. XMEMSET(cipher, 0, sizeof(cipher));
  15269. XMEMSET(plain, 0, sizeof(plain));
  15270. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15271. iv, effectiveKeyBits), 0);
  15272. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15273. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15274. /* reset IV for decrypt */
  15275. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15276. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15277. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15278. /* Rc2CbcEncrypt bad arguments */
  15279. /* null Rc2 struct */
  15280. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15281. BAD_FUNC_ARG);
  15282. /* null out buffer */
  15283. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15284. BAD_FUNC_ARG);
  15285. /* null input buffer */
  15286. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15287. BAD_FUNC_ARG);
  15288. /* Rc2CbcDecrypt bad arguments */
  15289. /* in size is 0 */
  15290. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15291. /* null Rc2 struct */
  15292. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15293. BAD_FUNC_ARG);
  15294. /* null out buffer */
  15295. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15296. BAD_FUNC_ARG);
  15297. /* null input buffer */
  15298. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  15299. BAD_FUNC_ARG);
  15300. #endif
  15301. return EXPECT_RESULT();
  15302. } /* END test_wc_Rc2CbcEncryptDecrypt */
  15303. /*
  15304. * Testing function for wc_AesSetIV
  15305. */
  15306. static int test_wc_AesSetIV(void)
  15307. {
  15308. int res = TEST_SKIPPED;
  15309. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15310. Aes aes;
  15311. int ret = 0;
  15312. byte key16[] =
  15313. {
  15314. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15315. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15316. };
  15317. byte iv1[] = "1234567890abcdef";
  15318. byte iv2[] = "0987654321fedcba";
  15319. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15320. if (ret != 0)
  15321. return ret;
  15322. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15323. iv1, AES_ENCRYPTION);
  15324. if (ret == 0) {
  15325. ret = wc_AesSetIV(&aes, iv2);
  15326. }
  15327. /* Test bad args. */
  15328. if (ret == 0) {
  15329. ret = wc_AesSetIV(NULL, iv1);
  15330. if (ret == BAD_FUNC_ARG) {
  15331. /* NULL iv should return 0. */
  15332. ret = wc_AesSetIV(&aes, NULL);
  15333. }
  15334. else {
  15335. ret = WOLFSSL_FATAL_ERROR;
  15336. }
  15337. }
  15338. wc_AesFree(&aes);
  15339. res = TEST_RES_CHECK(ret == 0);
  15340. #endif
  15341. return res;
  15342. } /* test_wc_AesSetIV */
  15343. /*
  15344. * Testing function for wc_AesSetKey().
  15345. */
  15346. static int test_wc_AesSetKey(void)
  15347. {
  15348. EXPECT_DECLS;
  15349. #ifndef NO_AES
  15350. Aes aes;
  15351. byte key16[] = {
  15352. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15353. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15354. };
  15355. #ifdef WOLFSSL_AES_192
  15356. byte key24[] = {
  15357. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15358. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15359. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15360. };
  15361. #endif
  15362. #ifdef WOLFSSL_AES_256
  15363. byte key32[] = {
  15364. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15365. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15366. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15367. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15368. };
  15369. #endif
  15370. byte badKey16[] = {
  15371. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15372. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15373. };
  15374. byte iv[] = "1234567890abcdef";
  15375. XMEMSET(&aes, 0, sizeof(Aes));
  15376. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15377. #ifdef WOLFSSL_AES_128
  15378. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15379. iv, AES_ENCRYPTION), 0);
  15380. #endif
  15381. #ifdef WOLFSSL_AES_192
  15382. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15383. iv, AES_ENCRYPTION), 0);
  15384. #endif
  15385. #ifdef WOLFSSL_AES_256
  15386. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15387. iv, AES_ENCRYPTION), 0);
  15388. #endif
  15389. /* Pass in bad args. */
  15390. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15391. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15392. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15393. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15394. BAD_FUNC_ARG);
  15395. wc_AesFree(&aes);
  15396. #endif
  15397. return EXPECT_RESULT();
  15398. } /* END test_wc_AesSetKey */
  15399. /*
  15400. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15401. * and wc_AesCbcDecryptWithKey()
  15402. */
  15403. static int test_wc_AesCbcEncryptDecrypt(void)
  15404. {
  15405. EXPECT_DECLS;
  15406. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15407. defined(WOLFSSL_AES_256)
  15408. Aes aes;
  15409. byte key32[] = {
  15410. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15411. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15412. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15413. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15414. };
  15415. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15416. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15417. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15418. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15419. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15420. };
  15421. byte iv[] = "1234567890abcdef";
  15422. byte enc[sizeof(vector)];
  15423. byte dec[sizeof(vector)];
  15424. byte dec2[sizeof(vector)];
  15425. /* Init stack variables. */
  15426. XMEMSET(&aes, 0, sizeof(Aes));
  15427. XMEMSET(enc, 0, sizeof(enc));
  15428. XMEMSET(dec, 0, sizeof(vector));
  15429. XMEMSET(dec2, 0, sizeof(vector));
  15430. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15431. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15432. AES_ENCRYPTION), 0);
  15433. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15434. /* Re init for decrypt and set flag. */
  15435. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15436. AES_DECRYPTION), 0);
  15437. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15438. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15439. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15440. sizeof(key32)/sizeof(byte), iv), 0);
  15441. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15442. /* Pass in bad args */
  15443. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15444. BAD_FUNC_ARG);
  15445. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15446. BAD_FUNC_ARG);
  15447. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15448. BAD_FUNC_ARG);
  15449. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15450. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15451. BAD_LENGTH_E);
  15452. #endif
  15453. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15454. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15455. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15456. "mode (v2), skip test");
  15457. #else
  15458. /* Test passing in size of 0 */
  15459. XMEMSET(enc, 0, sizeof(enc));
  15460. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15461. /* Check enc was not modified */
  15462. {
  15463. int i;
  15464. for (i = 0; i < (int)sizeof(enc); i++)
  15465. ExpectIntEQ(enc[i], 0);
  15466. }
  15467. #endif
  15468. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15469. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15470. BAD_FUNC_ARG);
  15471. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15472. BAD_FUNC_ARG);
  15473. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15474. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15475. BAD_LENGTH_E);
  15476. #else
  15477. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15478. BAD_FUNC_ARG);
  15479. #endif
  15480. /* Test passing in size of 0 */
  15481. XMEMSET(dec, 0, sizeof(dec));
  15482. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15483. /* Check dec was not modified */
  15484. {
  15485. int i;
  15486. for (i = 0; i < (int)sizeof(dec); i++)
  15487. ExpectIntEQ(dec[i], 0);
  15488. }
  15489. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15490. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15491. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15492. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15493. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15494. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15495. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15496. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15497. wc_AesFree(&aes);
  15498. #endif
  15499. return EXPECT_RESULT();
  15500. } /* END test_wc_AesCbcEncryptDecrypt */
  15501. /*
  15502. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15503. */
  15504. static int test_wc_AesCtrEncryptDecrypt(void)
  15505. {
  15506. EXPECT_DECLS;
  15507. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15508. Aes aesEnc;
  15509. Aes aesDec;
  15510. byte key32[] = {
  15511. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15512. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15513. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15514. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15515. };
  15516. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15517. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15518. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15519. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15520. };
  15521. byte iv[] = "1234567890abcdef";
  15522. byte enc[AES_BLOCK_SIZE * 2];
  15523. byte dec[AES_BLOCK_SIZE * 2];
  15524. /* Init stack variables. */
  15525. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15526. XMEMSET(&aesDec, 0, sizeof(Aes));
  15527. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15528. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15529. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15530. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15531. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15532. AES_ENCRYPTION), 0);
  15533. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15534. sizeof(vector)/sizeof(byte)), 0);
  15535. /* Decrypt with wc_AesCtrEncrypt() */
  15536. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15537. AES_ENCRYPTION), 0);
  15538. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15539. 0);
  15540. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15541. /* Test bad args. */
  15542. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15543. BAD_FUNC_ARG);
  15544. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15545. BAD_FUNC_ARG);
  15546. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15547. BAD_FUNC_ARG);
  15548. wc_AesFree(&aesEnc);
  15549. wc_AesFree(&aesDec);
  15550. #endif
  15551. return EXPECT_RESULT();
  15552. } /* END test_wc_AesCtrEncryptDecrypt */
  15553. /*
  15554. * test function for wc_AesGcmSetKey()
  15555. */
  15556. static int test_wc_AesGcmSetKey(void)
  15557. {
  15558. EXPECT_DECLS;
  15559. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15560. Aes aes;
  15561. #ifdef WOLFSSL_AES_128
  15562. byte key16[] = {
  15563. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15564. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15565. };
  15566. #endif
  15567. #ifdef WOLFSSL_AES_192
  15568. byte key24[] = {
  15569. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15570. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15571. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15572. };
  15573. #endif
  15574. #ifdef WOLFSSL_AES_256
  15575. byte key32[] = {
  15576. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15577. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15578. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15579. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15580. };
  15581. #endif
  15582. byte badKey16[] = {
  15583. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15584. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15585. };
  15586. byte badKey24[] = {
  15587. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15588. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15589. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15590. };
  15591. byte badKey32[] = {
  15592. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15593. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15594. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15595. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15596. };
  15597. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15598. #ifdef WOLFSSL_AES_128
  15599. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15600. #endif
  15601. #ifdef WOLFSSL_AES_192
  15602. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15603. #endif
  15604. #ifdef WOLFSSL_AES_256
  15605. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15606. #endif
  15607. /* Pass in bad args. */
  15608. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15609. BAD_FUNC_ARG);
  15610. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15611. BAD_FUNC_ARG);
  15612. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15613. BAD_FUNC_ARG);
  15614. wc_AesFree(&aes);
  15615. #endif
  15616. return EXPECT_RESULT();
  15617. } /* END test_wc_AesGcmSetKey */
  15618. /*
  15619. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15620. */
  15621. static int test_wc_AesGcmEncryptDecrypt(void)
  15622. {
  15623. EXPECT_DECLS;
  15624. /* WOLFSSL_AFALG requires 12 byte IV */
  15625. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15626. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15627. Aes aes;
  15628. byte key32[] = {
  15629. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15630. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15631. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15632. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15633. };
  15634. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15635. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15636. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15637. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15638. };
  15639. const byte a[] = {
  15640. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15641. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15642. 0xab, 0xad, 0xda, 0xd2
  15643. };
  15644. byte iv[] = "1234567890a";
  15645. byte longIV[] = "1234567890abcdefghij";
  15646. byte enc[sizeof(vector)];
  15647. byte resultT[AES_BLOCK_SIZE];
  15648. byte dec[sizeof(vector)];
  15649. /* Init stack variables. */
  15650. XMEMSET(&aes, 0, sizeof(Aes));
  15651. XMEMSET(enc, 0, sizeof(vector));
  15652. XMEMSET(dec, 0, sizeof(vector));
  15653. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15654. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15655. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15656. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15657. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15658. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  15659. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15660. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15661. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15662. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  15663. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15664. BAD_FUNC_ARG);
  15665. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15666. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15667. BAD_FUNC_ARG);
  15668. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15669. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  15670. BAD_FUNC_ARG);
  15671. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15672. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15673. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15674. /* FIPS does not check the lower bound of ivSz */
  15675. #else
  15676. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  15677. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15678. #endif
  15679. /* This case is now considered good. Long IVs are now allowed.
  15680. * Except for the original FIPS release, it still has an upper
  15681. * bound on the IV length. */
  15682. #if (!defined(HAVE_FIPS) || \
  15683. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15684. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15685. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15686. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15687. 0);
  15688. #else
  15689. (void)longIV;
  15690. #endif /* Old FIPS */
  15691. /* END wc_AesGcmEncrypt */
  15692. #ifdef HAVE_AES_DECRYPT
  15693. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15694. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15695. BAD_FUNC_ARG);
  15696. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  15697. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15698. BAD_FUNC_ARG);
  15699. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  15700. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15701. BAD_FUNC_ARG);
  15702. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  15703. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15704. BAD_FUNC_ARG);
  15705. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15706. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  15707. BAD_FUNC_ARG);
  15708. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15709. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15710. BAD_FUNC_ARG);
  15711. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15712. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15713. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15714. /* FIPS does not check the lower bound of ivSz */
  15715. #else
  15716. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15717. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15718. #endif
  15719. #endif /* HAVE_AES_DECRYPT */
  15720. wc_AesFree(&aes);
  15721. #endif
  15722. return EXPECT_RESULT();
  15723. } /* END test_wc_AesGcmEncryptDecrypt */
  15724. /*
  15725. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15726. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15727. */
  15728. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15729. {
  15730. EXPECT_DECLS;
  15731. #if (!defined(HAVE_FIPS) || \
  15732. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15733. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15734. const byte key[] = {
  15735. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15736. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15737. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15738. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15739. };
  15740. const byte in[] = {
  15741. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15742. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15743. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15744. };
  15745. const byte aad[] = {
  15746. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15747. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15748. 0xab, 0xad, 0xda, 0xd2
  15749. };
  15750. Aes aesEnc;
  15751. Aes aesDec;
  15752. byte iv[] = "1234567890abcdefghij";
  15753. byte out[sizeof(in)];
  15754. byte plain[sizeof(in)];
  15755. byte tag[AES_BLOCK_SIZE];
  15756. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15757. XMEMSET(&aesDec, 0, sizeof(Aes));
  15758. XMEMSET(out, 0, sizeof(out));
  15759. XMEMSET(plain, 0, sizeof(plain));
  15760. XMEMSET(tag, 0, sizeof(tag));
  15761. /* Perform one-shot encryption using long IV */
  15762. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15763. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15764. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15765. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15766. /* Perform streaming decryption using long IV */
  15767. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15768. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15769. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15770. sizeof(aad)), 0);
  15771. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15772. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15773. /* Free resources */
  15774. wc_AesFree(&aesEnc);
  15775. wc_AesFree(&aesDec);
  15776. #endif
  15777. return EXPECT_RESULT();
  15778. } /* END wc_AesGcmMixedEncDecLongIV */
  15779. /*
  15780. * unit test for wc_GmacSetKey()
  15781. */
  15782. static int test_wc_GmacSetKey(void)
  15783. {
  15784. EXPECT_DECLS;
  15785. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15786. Gmac gmac;
  15787. byte key16[] = {
  15788. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15789. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15790. };
  15791. #ifdef WOLFSSL_AES_192
  15792. byte key24[] = {
  15793. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15794. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15795. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15796. };
  15797. #endif
  15798. #ifdef WOLFSSL_AES_256
  15799. byte key32[] = {
  15800. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15801. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15802. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15803. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15804. };
  15805. #endif
  15806. byte badKey16[] = {
  15807. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15808. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15809. };
  15810. byte badKey24[] = {
  15811. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15812. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15813. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15814. };
  15815. byte badKey32[] = {
  15816. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15817. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15818. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15819. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15820. };
  15821. XMEMSET(&gmac, 0, sizeof(Gmac));
  15822. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15823. #ifdef WOLFSSL_AES_128
  15824. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  15825. #endif
  15826. #ifdef WOLFSSL_AES_192
  15827. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15828. #endif
  15829. #ifdef WOLFSSL_AES_256
  15830. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15831. #endif
  15832. /* Pass in bad args. */
  15833. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  15834. BAD_FUNC_ARG);
  15835. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  15836. BAD_FUNC_ARG);
  15837. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  15838. BAD_FUNC_ARG);
  15839. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  15840. BAD_FUNC_ARG);
  15841. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  15842. BAD_FUNC_ARG);
  15843. wc_AesFree(&gmac.aes);
  15844. #endif
  15845. return EXPECT_RESULT();
  15846. } /* END test_wc_GmacSetKey */
  15847. /*
  15848. * unit test for wc_GmacUpdate
  15849. */
  15850. static int test_wc_GmacUpdate(void)
  15851. {
  15852. EXPECT_DECLS;
  15853. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15854. Gmac gmac;
  15855. #ifdef WOLFSSL_AES_128
  15856. const byte key16[] = {
  15857. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15858. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15859. };
  15860. #endif
  15861. #ifdef WOLFSSL_AES_192
  15862. byte key24[] = {
  15863. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15864. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15865. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15866. };
  15867. #endif
  15868. #ifdef WOLFSSL_AES_256
  15869. byte key32[] = {
  15870. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15871. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15872. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15873. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15874. };
  15875. #endif
  15876. #ifdef WOLFSSL_AES_128
  15877. const byte authIn[] = {
  15878. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15879. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15880. };
  15881. #endif
  15882. #ifdef WOLFSSL_AES_192
  15883. const byte authIn2[] = {
  15884. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15885. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15886. };
  15887. #endif
  15888. const byte authIn3[] = {
  15889. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15890. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15891. };
  15892. #ifdef WOLFSSL_AES_128
  15893. const byte tag1[] = { /* Known. */
  15894. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15895. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15896. };
  15897. #endif
  15898. #ifdef WOLFSSL_AES_192
  15899. const byte tag2[] = { /* Known */
  15900. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15901. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15902. };
  15903. #endif
  15904. const byte tag3[] = { /* Known */
  15905. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15906. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15907. };
  15908. #ifdef WOLFSSL_AES_128
  15909. const byte iv[] = {
  15910. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15911. 0xe2, 0x8c, 0x8f, 0x16
  15912. };
  15913. #endif
  15914. #ifdef WOLFSSL_AES_192
  15915. const byte iv2[] = {
  15916. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15917. 0x7e, 0x1a, 0x6f, 0xbc
  15918. };
  15919. #endif
  15920. const byte iv3[] = {
  15921. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15922. 0xc3, 0xfb, 0x6c, 0x8a
  15923. };
  15924. byte tagOut[16];
  15925. byte tagOut2[24];
  15926. byte tagOut3[32];
  15927. /* Init stack variables. */
  15928. XMEMSET(&gmac, 0, sizeof(Gmac));
  15929. XMEMSET(tagOut, 0, sizeof(tagOut));
  15930. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15931. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15932. #ifdef WOLFSSL_AES_128
  15933. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15934. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  15935. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15936. tagOut, sizeof(tag1)), 0);
  15937. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  15938. wc_AesFree(&gmac.aes);
  15939. #endif
  15940. #ifdef WOLFSSL_AES_192
  15941. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15942. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  15943. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15944. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  15945. tagOut2, sizeof(tag2)), 0);
  15946. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  15947. wc_AesFree(&gmac.aes);
  15948. #endif
  15949. #ifdef WOLFSSL_AES_256
  15950. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15951. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  15952. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15953. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15954. tagOut3, sizeof(tag3)), 0);
  15955. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  15956. wc_AesFree(&gmac.aes);
  15957. #endif
  15958. /* Pass bad args. */
  15959. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15960. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15961. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  15962. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15963. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  15964. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15965. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  15966. wc_AesFree(&gmac.aes);
  15967. #endif
  15968. return EXPECT_RESULT();
  15969. } /* END test_wc_GmacUpdate */
  15970. /*
  15971. * testing wc_CamelliaSetKey
  15972. */
  15973. static int test_wc_CamelliaSetKey(void)
  15974. {
  15975. EXPECT_DECLS;
  15976. #ifdef HAVE_CAMELLIA
  15977. Camellia camellia;
  15978. /*128-bit key*/
  15979. static const byte key16[] = {
  15980. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15981. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15982. };
  15983. /* 192-bit key */
  15984. static const byte key24[] = {
  15985. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15986. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15987. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15988. };
  15989. /* 256-bit key */
  15990. static const byte key32[] = {
  15991. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15992. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15993. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15994. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15995. };
  15996. static const byte iv[] = {
  15997. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15998. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15999. };
  16000. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16001. 0);
  16002. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16003. NULL), 0);
  16004. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16005. 0);
  16006. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16007. NULL), 0);
  16008. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16009. 0);
  16010. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16011. NULL), 0);
  16012. /* Bad args. */
  16013. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16014. BAD_FUNC_ARG);
  16015. #endif
  16016. return EXPECT_RESULT();
  16017. } /* END test_wc_CammeliaSetKey */
  16018. /*
  16019. * Testing wc_CamelliaSetIV()
  16020. */
  16021. static int test_wc_CamelliaSetIV(void)
  16022. {
  16023. EXPECT_DECLS;
  16024. #ifdef HAVE_CAMELLIA
  16025. Camellia camellia;
  16026. static const byte iv[] = {
  16027. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16028. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16029. };
  16030. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16031. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16032. /* Bad args. */
  16033. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16034. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16035. #endif
  16036. return EXPECT_RESULT();
  16037. } /* END test_wc_CamelliaSetIV*/
  16038. /*
  16039. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16040. */
  16041. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16042. {
  16043. EXPECT_DECLS;
  16044. #ifdef HAVE_CAMELLIA
  16045. Camellia camellia;
  16046. static const byte key24[] = {
  16047. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16048. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16049. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16050. };
  16051. static const byte iv[] = {
  16052. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16053. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16054. };
  16055. static const byte plainT[] = {
  16056. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16057. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16058. };
  16059. byte enc[sizeof(plainT)];
  16060. byte dec[sizeof(enc)];
  16061. /* Init stack variables.*/
  16062. XMEMSET(enc, 0, 16);
  16063. XMEMSET(enc, 0, 16);
  16064. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16065. 0);
  16066. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16067. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16068. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16069. /* Pass bad args. */
  16070. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16071. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16072. BAD_FUNC_ARG);
  16073. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16074. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16075. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16076. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16077. #endif
  16078. return EXPECT_RESULT();
  16079. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16080. /*
  16081. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16082. */
  16083. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16084. {
  16085. EXPECT_DECLS;
  16086. #ifdef HAVE_CAMELLIA
  16087. Camellia camellia;
  16088. static const byte key24[] = {
  16089. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16090. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16091. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16092. };
  16093. static const byte plainT[] = {
  16094. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16095. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16096. };
  16097. byte enc[CAMELLIA_BLOCK_SIZE];
  16098. byte dec[CAMELLIA_BLOCK_SIZE];
  16099. /* Init stack variables. */
  16100. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16101. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16102. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16103. NULL), 0);
  16104. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16105. CAMELLIA_BLOCK_SIZE), 0);
  16106. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16107. NULL), 0);
  16108. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16109. 0);
  16110. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16111. /* Pass in bad args. */
  16112. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16113. BAD_FUNC_ARG);
  16114. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16115. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16116. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16117. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16118. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16119. BAD_FUNC_ARG);
  16120. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16121. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16122. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16123. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16124. #endif
  16125. return EXPECT_RESULT();
  16126. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16127. /*
  16128. * Testing wc_Arc4SetKey()
  16129. */
  16130. static int test_wc_Arc4SetKey(void)
  16131. {
  16132. EXPECT_DECLS;
  16133. #ifndef NO_RC4
  16134. Arc4 arc;
  16135. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16136. int keyLen = 8;
  16137. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16138. /* Test bad args. */
  16139. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16140. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16141. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16142. #endif
  16143. return EXPECT_RESULT();
  16144. } /* END test_wc_Arc4SetKey */
  16145. /*
  16146. * Testing wc_Arc4Process for ENC/DEC.
  16147. */
  16148. static int test_wc_Arc4Process(void)
  16149. {
  16150. EXPECT_DECLS;
  16151. #ifndef NO_RC4
  16152. Arc4 enc;
  16153. Arc4 dec;
  16154. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16155. int keyLen = 8;
  16156. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16157. byte cipher[8];
  16158. byte plain[8];
  16159. /* Init stack variables */
  16160. XMEMSET(&enc, 0, sizeof(Arc4));
  16161. XMEMSET(&dec, 0, sizeof(Arc4));
  16162. XMEMSET(cipher, 0, sizeof(cipher));
  16163. XMEMSET(plain, 0, sizeof(plain));
  16164. /* Use for async. */
  16165. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16166. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16167. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16168. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16169. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16170. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16171. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16172. /* Bad args. */
  16173. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16174. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16175. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16176. wc_Arc4Free(&enc);
  16177. wc_Arc4Free(&dec);
  16178. #endif
  16179. return EXPECT_RESULT();
  16180. } /* END test_wc_Arc4Process */
  16181. /*
  16182. * Testing wc_Init RsaKey()
  16183. */
  16184. static int test_wc_InitRsaKey(void)
  16185. {
  16186. EXPECT_DECLS;
  16187. #ifndef NO_RSA
  16188. RsaKey key;
  16189. XMEMSET(&key, 0, sizeof(RsaKey));
  16190. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16191. /* Test bad args. */
  16192. #ifndef HAVE_USER_RSA
  16193. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16194. #else
  16195. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), USER_CRYPTO_ERROR);
  16196. #endif
  16197. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16198. #endif
  16199. return EXPECT_RESULT();
  16200. } /* END test_wc_InitRsaKey */
  16201. /*
  16202. * Testing wc_RsaPrivateKeyDecode()
  16203. */
  16204. static int test_wc_RsaPrivateKeyDecode(void)
  16205. {
  16206. EXPECT_DECLS;
  16207. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16208. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16209. RsaKey key;
  16210. byte* tmp = NULL;
  16211. word32 idx = 0;
  16212. int bytes = 0;
  16213. XMEMSET(&key, 0, sizeof(RsaKey));
  16214. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16215. DYNAMIC_TYPE_TMP_BUFFER));
  16216. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16217. if (tmp != NULL) {
  16218. #ifdef USE_CERT_BUFFERS_1024
  16219. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16220. bytes = sizeof_client_key_der_1024;
  16221. #else
  16222. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16223. bytes = sizeof_client_key_der_2048;
  16224. #endif /* Use cert buffers. */
  16225. }
  16226. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16227. #ifndef HAVE_USER_RSA
  16228. /* Test bad args. */
  16229. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16230. BAD_FUNC_ARG);
  16231. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16232. BAD_FUNC_ARG);
  16233. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16234. BAD_FUNC_ARG);
  16235. #else
  16236. /* Test bad args. User RSA. */
  16237. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16238. USER_CRYPTO_ERROR);
  16239. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16240. USER_CRYPTO_ERROR);
  16241. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16242. USER_CRYPTO_ERROR);
  16243. #endif
  16244. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16245. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16246. #endif
  16247. return EXPECT_RESULT();
  16248. } /* END test_wc_RsaPrivateKeyDecode */
  16249. /*
  16250. * Testing wc_RsaPublicKeyDecode()
  16251. */
  16252. static int test_wc_RsaPublicKeyDecode(void)
  16253. {
  16254. EXPECT_DECLS;
  16255. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16256. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16257. RsaKey keyPub;
  16258. byte* tmp = NULL;
  16259. word32 idx = 0;
  16260. int bytes = 0;
  16261. word32 keySz = 0;
  16262. word32 tstKeySz = 0;
  16263. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16264. XFILE f = XBADFILE;
  16265. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16266. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16267. byte buf[4096];
  16268. #endif
  16269. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16270. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16271. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16272. if (tmp != NULL) {
  16273. #ifdef USE_CERT_BUFFERS_1024
  16274. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16275. bytes = sizeof_client_keypub_der_1024;
  16276. keySz = 1024;
  16277. #else
  16278. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16279. bytes = sizeof_client_keypub_der_2048;
  16280. keySz = 2048;
  16281. #endif
  16282. }
  16283. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16284. #ifndef HAVE_USER_RSA
  16285. /* Pass in bad args. */
  16286. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16287. BAD_FUNC_ARG);
  16288. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16289. BAD_FUNC_ARG);
  16290. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16291. BAD_FUNC_ARG);
  16292. #else
  16293. /* Pass in bad args. */
  16294. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16295. USER_CRYPTO_ERROR);
  16296. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16297. USER_CRYPTO_ERROR);
  16298. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16299. USER_CRYPTO_ERROR);
  16300. #endif
  16301. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16302. /* Test for getting modulus key size */
  16303. idx = 0;
  16304. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16305. &tstKeySz, NULL, NULL), 0);
  16306. ExpectIntEQ(tstKeySz, keySz/8);
  16307. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16308. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16309. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16310. if (f != XBADFILE) {
  16311. XFCLOSE(f);
  16312. f = XBADFILE;
  16313. }
  16314. idx = 0;
  16315. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16316. NULL), 0);
  16317. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  16318. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16319. if (f != XBADFILE)
  16320. XFCLOSE(f);
  16321. idx = 0;
  16322. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16323. NULL), 0);
  16324. #endif
  16325. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16326. #endif
  16327. return EXPECT_RESULT();
  16328. } /* END test_wc_RsaPublicKeyDecode */
  16329. /*
  16330. * Testing wc_RsaPublicKeyDecodeRaw()
  16331. */
  16332. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16333. {
  16334. EXPECT_DECLS;
  16335. #if !defined(NO_RSA)
  16336. RsaKey key;
  16337. const byte n = 0x23;
  16338. const byte e = 0x03;
  16339. int nSz = sizeof(n);
  16340. int eSz = sizeof(e);
  16341. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16342. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16343. #ifndef HAVE_USER_RSA
  16344. /* Pass in bad args. */
  16345. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16346. BAD_FUNC_ARG);
  16347. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16348. BAD_FUNC_ARG);
  16349. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16350. BAD_FUNC_ARG);
  16351. #else
  16352. /* Pass in bad args. User RSA. */
  16353. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16354. USER_CRYPTO_ERROR);
  16355. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16356. USER_CRYPTO_ERROR);
  16357. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16358. USER_CRYPTO_ERROR);
  16359. #endif
  16360. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16361. #endif
  16362. return EXPECT_RESULT();
  16363. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16364. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16365. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16366. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16367. * trying until it gets a probable prime. */
  16368. #ifdef HAVE_FIPS
  16369. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16370. {
  16371. int ret;
  16372. for (;;) {
  16373. ret = wc_MakeRsaKey(key, size, e, rng);
  16374. if (ret != PRIME_GEN_E) break;
  16375. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16376. "trying again.\n");
  16377. }
  16378. return ret;
  16379. }
  16380. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16381. #else
  16382. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16383. #endif
  16384. #endif
  16385. /*
  16386. * Testing wc_MakeRsaKey()
  16387. */
  16388. static int test_wc_MakeRsaKey(void)
  16389. {
  16390. EXPECT_DECLS;
  16391. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16392. RsaKey genKey;
  16393. WC_RNG rng;
  16394. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16395. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16396. int bits = 1024;
  16397. #else
  16398. int bits = 2048;
  16399. #endif
  16400. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16402. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16403. ExpectIntEQ(wc_InitRng(&rng), 0);
  16404. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16405. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16406. #ifndef HAVE_USER_RSA
  16407. /* Test bad args. */
  16408. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16409. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16410. BAD_FUNC_ARG);
  16411. /* e < 3 */
  16412. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16413. /* e & 1 == 0 */
  16414. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16415. #else
  16416. /* Test bad args. */
  16417. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng),
  16418. USER_CRYPTO_ERROR);
  16419. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16420. USER_CRYPTO_ERROR);
  16421. /* e < 3 */
  16422. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), USER_CRYPTO_ERROR);
  16423. /* e & 1 == 0 */
  16424. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), USER_CRYPTO_ERROR);
  16425. #endif /* HAVE_USER_RSA */
  16426. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16427. #endif
  16428. return EXPECT_RESULT();
  16429. } /* END test_wc_MakeRsaKey */
  16430. /*
  16431. * Test the bounds checking on the cipher text versus the key modulus.
  16432. * 1. Make a new RSA key.
  16433. * 2. Set c to 1.
  16434. * 3. Decrypt c into k. (error)
  16435. * 4. Copy the key modulus to c and sub 1 from the copy.
  16436. * 5. Decrypt c into k. (error)
  16437. * Valid bounds test cases are covered by all the other RSA tests.
  16438. */
  16439. static int test_RsaDecryptBoundsCheck(void)
  16440. {
  16441. EXPECT_DECLS;
  16442. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16443. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16444. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16445. WC_RNG rng;
  16446. RsaKey key;
  16447. byte flatC[256];
  16448. word32 flatCSz;
  16449. byte out[256];
  16450. word32 outSz = sizeof(out);
  16451. XMEMSET(&key, 0, sizeof(RsaKey));
  16452. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16453. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16454. ExpectIntEQ(wc_InitRng(&rng), 0);
  16455. if (EXPECT_SUCCESS()) {
  16456. const byte* derKey;
  16457. word32 derKeySz;
  16458. word32 idx = 0;
  16459. #ifdef USE_CERT_BUFFERS_1024
  16460. derKey = server_key_der_1024;
  16461. derKeySz = (word32)sizeof_server_key_der_1024;
  16462. flatCSz = 128;
  16463. #else
  16464. derKey = server_key_der_2048;
  16465. derKeySz = (word32)sizeof_server_key_der_2048;
  16466. flatCSz = 256;
  16467. #endif
  16468. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16469. }
  16470. if (EXPECT_SUCCESS()) {
  16471. XMEMSET(flatC, 0, flatCSz);
  16472. flatC[flatCSz-1] = 1;
  16473. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16474. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16475. if (EXPECT_SUCCESS()) {
  16476. mp_int c;
  16477. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16478. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16479. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16480. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16481. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16482. mp_clear(&c);
  16483. }
  16484. }
  16485. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16486. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16487. #endif
  16488. return EXPECT_RESULT();
  16489. } /* END test_wc_RsaDecryptBoundsCheck */
  16490. /*
  16491. * Testing wc_SetKeyUsage()
  16492. */
  16493. static int test_wc_SetKeyUsage(void)
  16494. {
  16495. EXPECT_DECLS;
  16496. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16497. Cert myCert;
  16498. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16499. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16500. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16501. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16502. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16503. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16504. /* Test bad args. */
  16505. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16506. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16507. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16508. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16509. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16510. KEYUSAGE_E);
  16511. #endif
  16512. return EXPECT_RESULT();
  16513. } /* END test_wc_SetKeyUsage */
  16514. /*
  16515. * Testing wc_CheckProbablePrime()
  16516. */
  16517. static int test_wc_CheckProbablePrime(void)
  16518. {
  16519. EXPECT_DECLS;
  16520. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16521. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16522. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16523. RsaKey key;
  16524. WC_RNG rng;
  16525. byte e[3];
  16526. word32 eSz = (word32)sizeof(e);
  16527. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16528. word32 nSz = (word32)sizeof(n);
  16529. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16530. word32 dSz = (word32)sizeof(d);
  16531. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16532. word32 pSz = (word32)sizeof(p);
  16533. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16534. word32 qSz = (word32)sizeof(q);
  16535. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16536. int* isPrime;
  16537. int test[5];
  16538. isPrime = test;
  16539. XMEMSET(&key, 0, sizeof(RsaKey));
  16540. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16541. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16542. ExpectIntEQ(wc_InitRng(&rng), 0);
  16543. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16544. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16545. WC_RSA_EXPONENT, &rng), 0);
  16546. PRIVATE_KEY_UNLOCK();
  16547. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16548. &qSz), 0);
  16549. PRIVATE_KEY_LOCK();
  16550. /* Bad cases */
  16551. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16552. BAD_FUNC_ARG);
  16553. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16554. BAD_FUNC_ARG);
  16555. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16556. BAD_FUNC_ARG);
  16557. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16558. BAD_FUNC_ARG);
  16559. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16560. BAD_FUNC_ARG);
  16561. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16562. BAD_FUNC_ARG);
  16563. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16564. BAD_FUNC_ARG);
  16565. /* Good case */
  16566. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16567. 0);
  16568. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16569. wc_FreeRng(&rng);
  16570. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16571. #endif
  16572. return EXPECT_RESULT();
  16573. } /* END test_wc_CheckProbablePrime */
  16574. /*
  16575. * Testing wc_RsaPSS_Verify()
  16576. */
  16577. static int test_wc_RsaPSS_Verify(void)
  16578. {
  16579. EXPECT_DECLS;
  16580. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16581. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16582. RsaKey key;
  16583. WC_RNG rng;
  16584. int sz = 256;
  16585. const char* szMessage = "This is the string to be signed";
  16586. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16587. unsigned char pDecrypted[2048/8];
  16588. byte* pt = pDecrypted;
  16589. word32 outLen = sizeof(pDecrypted);
  16590. XMEMSET(&key, 0, sizeof(RsaKey));
  16591. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16592. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16593. ExpectIntEQ(wc_InitRng(&rng), 0);
  16594. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16595. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16596. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16597. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16598. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16599. /* Bad cases */
  16600. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16601. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16602. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16603. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16604. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16605. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16606. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16607. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16608. /* Good case */
  16609. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16610. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16611. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16612. wc_FreeRng(&rng);
  16613. #endif
  16614. return EXPECT_RESULT();
  16615. } /* END test_wc_RsaPSS_Verify */
  16616. /*
  16617. * Testing wc_RsaPSS_VerifyCheck()
  16618. */
  16619. static int test_wc_RsaPSS_VerifyCheck(void)
  16620. {
  16621. EXPECT_DECLS;
  16622. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16623. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16624. RsaKey key;
  16625. WC_RNG rng;
  16626. int sz = 256; /* 2048/8 */
  16627. byte digest[32];
  16628. word32 digestSz = sizeof(digest);
  16629. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16630. word32 pSignatureSz = sizeof(pSignature);
  16631. unsigned char pDecrypted[2048/8];
  16632. byte* pt = pDecrypted;
  16633. word32 outLen = sizeof(pDecrypted);
  16634. XMEMSET(&key, 0, sizeof(RsaKey));
  16635. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16636. XMEMSET(digest, 0, sizeof(digest));
  16637. XMEMSET(pSignature, 0, sizeof(pSignature));
  16638. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16639. ExpectIntEQ(wc_InitRng(&rng), 0);
  16640. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16641. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16642. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16643. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16644. 0);
  16645. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16646. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16647. /* Bad cases */
  16648. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16649. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16650. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16651. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16652. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16653. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16654. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16655. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16656. /* Good case */
  16657. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16658. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16659. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16660. wc_FreeRng(&rng);
  16661. #endif
  16662. return EXPECT_RESULT();
  16663. } /* END test_wc_RsaPSS_VerifyCheck */
  16664. /*
  16665. * Testing wc_RsaPSS_VerifyCheckInline()
  16666. */
  16667. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16668. {
  16669. EXPECT_DECLS;
  16670. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16671. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16672. RsaKey key;
  16673. WC_RNG rng;
  16674. int sz = 256;
  16675. byte digest[32];
  16676. word32 digestSz = sizeof(digest);
  16677. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16678. unsigned char pDecrypted[2048/8];
  16679. byte* pt = pDecrypted;
  16680. XMEMSET(&key, 0, sizeof(RsaKey));
  16681. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16682. XMEMSET(digest, 0, sizeof(digest));
  16683. XMEMSET(pSignature, 0, sizeof(pSignature));
  16684. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16685. ExpectIntEQ(wc_InitRng(&rng), 0);
  16686. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16687. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16688. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16689. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16690. 0);
  16691. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  16692. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16693. /* Bad Cases */
  16694. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  16695. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16696. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  16697. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16698. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  16699. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16700. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16701. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16702. /* Good case */
  16703. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16704. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16705. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16706. wc_FreeRng(&rng);
  16707. #endif
  16708. return EXPECT_RESULT();
  16709. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16710. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16711. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16712. {
  16713. (void)flag;
  16714. (void)type;
  16715. (void)file;
  16716. (void)line;
  16717. }
  16718. #endif
  16719. /*
  16720. * Testing wc_LockMutex_ex
  16721. */
  16722. static int test_wc_LockMutex_ex(void)
  16723. {
  16724. EXPECT_DECLS;
  16725. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16726. int flag = CRYPTO_LOCK;
  16727. int type = 0;
  16728. const char* file = "./test-LockMutex_ex.txt";
  16729. int line = 0;
  16730. /* without SetMutexCb */
  16731. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  16732. /* with SetMutexCb */
  16733. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16734. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  16735. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16736. #endif
  16737. return EXPECT_RESULT();
  16738. } /* End test_wc_LockMutex_ex*/
  16739. /*
  16740. * Testing wc_SetMutexCb
  16741. */
  16742. static int test_wc_SetMutexCb(void)
  16743. {
  16744. EXPECT_DECLS;
  16745. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16746. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16747. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16748. #endif
  16749. return EXPECT_RESULT();
  16750. } /* End test_wc_SetMutexCb*/
  16751. /*
  16752. * Testing wc_RsaKeyToDer()
  16753. */
  16754. static int test_wc_RsaKeyToDer(void)
  16755. {
  16756. EXPECT_DECLS;
  16757. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16758. RsaKey genKey;
  16759. WC_RNG rng;
  16760. byte* der = NULL;
  16761. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16762. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16763. int bits = 1024;
  16764. word32 derSz = 611;
  16765. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16766. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16767. #else
  16768. int bits = 2048;
  16769. word32 derSz = 1196;
  16770. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16771. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16772. #endif
  16773. XMEMSET(&rng, 0, sizeof(rng));
  16774. XMEMSET(&genKey, 0, sizeof(genKey));
  16775. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16776. /* Init structures. */
  16777. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16778. ExpectIntEQ(wc_InitRng(&rng), 0);
  16779. /* Make key. */
  16780. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16781. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  16782. #ifndef HAVE_USER_RSA
  16783. /* Pass good/bad args. */
  16784. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  16785. /* Get just the output length */
  16786. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16787. /* Try Public Key. */
  16788. genKey.type = 0;
  16789. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  16790. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16791. /* Put back to Private Key */
  16792. genKey.type = 1;
  16793. #endif
  16794. #else
  16795. /* Pass good/bad args. */
  16796. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16797. /* Get just the output length */
  16798. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16799. /* Try Public Key. */
  16800. genKey.type = 0;
  16801. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16802. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16803. /* Put back to Private Key */
  16804. genKey.type = 1;
  16805. #endif
  16806. #endif
  16807. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16808. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16809. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16810. #endif
  16811. return EXPECT_RESULT();
  16812. } /* END test_wc_RsaKeyToDer */
  16813. /*
  16814. * Testing wc_RsaKeyToPublicDer()
  16815. */
  16816. static int test_wc_RsaKeyToPublicDer(void)
  16817. {
  16818. EXPECT_DECLS;
  16819. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16820. RsaKey key;
  16821. WC_RNG rng;
  16822. byte* der = NULL;
  16823. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16824. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16825. int bits = 1024;
  16826. word32 derLen = 162;
  16827. #else
  16828. int bits = 2048;
  16829. word32 derLen = 294;
  16830. #endif
  16831. #ifndef HAVE_USER_RSA
  16832. int ret;
  16833. #endif
  16834. XMEMSET(&rng, 0, sizeof(rng));
  16835. XMEMSET(&key, 0, sizeof(key));
  16836. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16837. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16838. ExpectIntEQ(wc_InitRng(&rng), 0);
  16839. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16840. /* test getting size only */
  16841. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  16842. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  16843. /* test getting size only */
  16844. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  16845. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  16846. #ifndef HAVE_USER_RSA
  16847. /* Pass in bad args. */
  16848. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  16849. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  16850. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  16851. #else
  16852. /* Pass in bad args. */
  16853. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), USER_CRYPTO_ERROR);
  16854. ExpectIntEQ(wc_RsaKeyToPublicDer(&key, der, -1), USER_CRYPTO_ERROR);
  16855. #endif
  16856. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16857. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16858. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16859. #endif
  16860. return EXPECT_RESULT();
  16861. } /* END test_wc_RsaKeyToPublicDer */
  16862. /*
  16863. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16864. */
  16865. static int test_wc_RsaPublicEncryptDecrypt(void)
  16866. {
  16867. EXPECT_DECLS;
  16868. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16869. RsaKey key;
  16870. WC_RNG rng;
  16871. const char inStr[] = TEST_STRING;
  16872. const word32 plainLen = (word32)TEST_STRING_SZ;
  16873. const word32 inLen = (word32)TEST_STRING_SZ;
  16874. int bits = TEST_RSA_BITS;
  16875. const word32 cipherLen = TEST_RSA_BYTES;
  16876. word32 cipherLenResult = cipherLen;
  16877. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16878. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16879. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16880. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16881. ExpectNotNull(in);
  16882. ExpectNotNull(plain);
  16883. ExpectNotNull(cipher);
  16884. #endif
  16885. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16886. /* Initialize stack structures. */
  16887. XMEMSET(&key, 0, sizeof(RsaKey));
  16888. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16889. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16890. ExpectIntEQ(wc_InitRng(&rng), 0);
  16891. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16892. /* Encrypt. */
  16893. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  16894. cipherLen, &key, &rng), 0);
  16895. /* Pass bad args - tested in another testing function.*/
  16896. /* Decrypt */
  16897. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16898. /* Bind rng */
  16899. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16900. #endif
  16901. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  16902. &key), 0);
  16903. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  16904. /* Pass bad args - tested in another testing function.*/
  16905. WC_FREE_VAR(in, NULL);
  16906. WC_FREE_VAR(plain, NULL);
  16907. WC_FREE_VAR(cipher, NULL);
  16908. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16909. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16910. #endif
  16911. return EXPECT_RESULT();
  16912. } /* END test_wc_RsaPublicEncryptDecrypt */
  16913. /*
  16914. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16915. */
  16916. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16917. {
  16918. EXPECT_DECLS;
  16919. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16920. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16921. && !defined(NO_SHA256)
  16922. RsaKey key;
  16923. WC_RNG rng;
  16924. const char inStr[] = TEST_STRING;
  16925. const word32 inLen = (word32)TEST_STRING_SZ;
  16926. const word32 plainSz = (word32)TEST_STRING_SZ;
  16927. byte* res = NULL;
  16928. int idx = 0;
  16929. int bits = TEST_RSA_BITS;
  16930. const word32 cipherSz = TEST_RSA_BYTES;
  16931. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16932. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16933. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16934. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16935. ExpectNotNull(in);
  16936. ExpectNotNull(plain);
  16937. ExpectNotNull(cipher);
  16938. #endif
  16939. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16940. /* Initialize stack structures. */
  16941. XMEMSET(&key, 0, sizeof(RsaKey));
  16942. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16943. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  16944. ExpectIntEQ(wc_InitRng(&rng), 0);
  16945. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16946. /* Encrypt */
  16947. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  16948. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16949. /* Pass bad args - tested in another testing function.*/
  16950. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16951. /* Decrypt */
  16952. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16953. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16954. #endif
  16955. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  16956. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16957. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  16958. /* Pass bad args - tested in another testing function.*/
  16959. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  16960. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16961. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  16962. #endif
  16963. WC_FREE_VAR(in, NULL);
  16964. WC_FREE_VAR(plain, NULL);
  16965. WC_FREE_VAR(cipher, NULL);
  16966. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16967. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16968. #endif
  16969. return EXPECT_RESULT();
  16970. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16971. /*
  16972. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16973. */
  16974. static int test_wc_RsaSSL_SignVerify(void)
  16975. {
  16976. EXPECT_DECLS;
  16977. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16978. RsaKey key;
  16979. WC_RNG rng;
  16980. const char inStr[] = TEST_STRING;
  16981. const word32 plainSz = (word32)TEST_STRING_SZ;
  16982. const word32 inLen = (word32)TEST_STRING_SZ;
  16983. word32 idx = 0;
  16984. int bits = TEST_RSA_BITS;
  16985. const word32 outSz = TEST_RSA_BYTES;
  16986. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16987. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16988. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16989. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16990. ExpectNotNull(in);
  16991. ExpectNotNull(out);
  16992. ExpectNotNull(plain);
  16993. #endif
  16994. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16995. XMEMSET(&key, 0, sizeof(RsaKey));
  16996. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16997. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16998. ExpectIntEQ(wc_InitRng(&rng), 0);
  16999. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17000. /* Sign. */
  17001. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17002. idx = (int)outSz;
  17003. #ifndef HAVE_USER_RSA
  17004. /* Test bad args. */
  17005. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17006. BAD_FUNC_ARG);
  17007. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17008. BAD_FUNC_ARG);
  17009. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17010. BAD_FUNC_ARG);
  17011. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17012. BAD_FUNC_ARG);
  17013. #else
  17014. /* Test bad args. */
  17015. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17016. USER_CRYPTO_ERROR);
  17017. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17018. USER_CRYPTO_ERROR);
  17019. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17020. USER_CRYPTO_ERROR);
  17021. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17022. USER_CRYPTO_ERROR);
  17023. #endif
  17024. /* Verify. */
  17025. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17026. #ifndef HAVE_USER_RSA
  17027. /* Pass bad args. */
  17028. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17029. BAD_FUNC_ARG);
  17030. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17031. BAD_FUNC_ARG);
  17032. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17033. BAD_FUNC_ARG);
  17034. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17035. BAD_FUNC_ARG);
  17036. #else
  17037. /* Pass bad args. */
  17038. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17039. USER_CRYPTO_ERROR);
  17040. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17041. USER_CRYPTO_ERROR);
  17042. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17043. USER_CRYPTO_ERROR);
  17044. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17045. USER_CRYPTO_ERROR);
  17046. #endif
  17047. WC_FREE_VAR(in, NULL);
  17048. WC_FREE_VAR(out, NULL);
  17049. WC_FREE_VAR(plain, NULL);
  17050. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17051. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17052. #endif
  17053. return EXPECT_RESULT();
  17054. } /* END test_wc_RsaSSL_SignVerify */
  17055. /*
  17056. * Testing wc_RsaEncryptSize()
  17057. */
  17058. static int test_wc_RsaEncryptSize(void)
  17059. {
  17060. EXPECT_DECLS;
  17061. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17062. RsaKey key;
  17063. WC_RNG rng;
  17064. XMEMSET(&key, 0, sizeof(RsaKey));
  17065. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17066. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17067. ExpectIntEQ(wc_InitRng(&rng), 0);
  17068. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17069. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17070. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17071. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17072. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17073. #endif
  17074. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17075. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17076. /* Pass in bad arg. */
  17077. #ifndef HAVE_USER_RSA
  17078. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17079. #else
  17080. ExpectIntEQ(wc_RsaEncryptSize(NULL), 0);
  17081. #endif
  17082. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17083. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17084. #endif
  17085. return EXPECT_RESULT();
  17086. } /* END test_wc_RsaEncryptSize*/
  17087. /*
  17088. * Testing wc_RsaFlattenPublicKey()
  17089. */
  17090. static int test_wc_RsaFlattenPublicKey(void)
  17091. {
  17092. EXPECT_DECLS;
  17093. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17094. RsaKey key;
  17095. WC_RNG rng;
  17096. byte e[256];
  17097. byte n[256];
  17098. word32 eSz = sizeof(e);
  17099. word32 nSz = sizeof(n);
  17100. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17101. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17102. int bits = 1024;
  17103. #else
  17104. int bits = 2048;
  17105. #endif
  17106. XMEMSET(&key, 0, sizeof(RsaKey));
  17107. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17108. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17109. ExpectIntEQ(wc_InitRng(&rng), 0);
  17110. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17111. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17112. #ifndef HAVE_USER_RSA
  17113. /* Pass bad args. */
  17114. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17115. BAD_FUNC_ARG);
  17116. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17117. BAD_FUNC_ARG);
  17118. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17119. BAD_FUNC_ARG);
  17120. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17121. BAD_FUNC_ARG);
  17122. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17123. BAD_FUNC_ARG);
  17124. #else
  17125. /* Pass bad args. */
  17126. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17127. USER_CRYPTO_ERROR);
  17128. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17129. USER_CRYPTO_ERROR);
  17130. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17131. USER_CRYPTO_ERROR);
  17132. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17133. USER_CRYPTO_ERROR);
  17134. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17135. USER_CRYPTO_ERROR);
  17136. #endif
  17137. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17138. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17139. #endif
  17140. return EXPECT_RESULT();
  17141. } /* END test_wc_RsaFlattenPublicKey */
  17142. /*
  17143. * unit test for wc_AesCcmSetKey
  17144. */
  17145. static int test_wc_AesCcmSetKey(void)
  17146. {
  17147. EXPECT_DECLS;
  17148. #ifdef HAVE_AESCCM
  17149. Aes aes;
  17150. const byte key16[] = {
  17151. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17152. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17153. };
  17154. const byte key24[] = {
  17155. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17156. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17157. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17158. };
  17159. const byte key32[] = {
  17160. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17161. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17162. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17163. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17164. };
  17165. XMEMSET(&aes, 0, sizeof(Aes));
  17166. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17167. #ifdef WOLFSSL_AES_128
  17168. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17169. #endif
  17170. #ifdef WOLFSSL_AES_192
  17171. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17172. #endif
  17173. #ifdef WOLFSSL_AES_256
  17174. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17175. #endif
  17176. /* Test bad args. */
  17177. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17178. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17179. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17180. wc_AesFree(&aes);
  17181. #endif
  17182. return EXPECT_RESULT();
  17183. } /* END test_wc_AesCcmSetKey */
  17184. /*
  17185. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17186. */
  17187. static int test_wc_AesCcmEncryptDecrypt(void)
  17188. {
  17189. EXPECT_DECLS;
  17190. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17191. Aes aes;
  17192. const byte key16[] = {
  17193. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17194. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17195. };
  17196. /* plaintext */
  17197. const byte plainT[] = {
  17198. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17199. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17200. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17201. };
  17202. /* nonce */
  17203. const byte iv[] = {
  17204. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17205. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17206. };
  17207. const byte c[] = { /* cipher text. */
  17208. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17209. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17210. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17211. };
  17212. const byte t[] = { /* Auth tag */
  17213. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17214. };
  17215. const byte authIn[] = {
  17216. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17217. };
  17218. byte cipherOut[sizeof(plainT)];
  17219. byte authTag[sizeof(t)];
  17220. #ifdef HAVE_AES_DECRYPT
  17221. byte plainOut[sizeof(cipherOut)];
  17222. #endif
  17223. XMEMSET(&aes, 0, sizeof(Aes));
  17224. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17225. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17226. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17227. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17228. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17229. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17230. #ifdef HAVE_AES_DECRYPT
  17231. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17232. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17233. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17234. #endif
  17235. /* Pass in bad args. Encrypt*/
  17236. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17237. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17238. BAD_FUNC_ARG);
  17239. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17240. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17241. BAD_FUNC_ARG);
  17242. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17243. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17244. BAD_FUNC_ARG);
  17245. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17246. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17247. BAD_FUNC_ARG);
  17248. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17249. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17250. BAD_FUNC_ARG);
  17251. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17252. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17253. BAD_FUNC_ARG);
  17254. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17255. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17256. BAD_FUNC_ARG);
  17257. #ifdef HAVE_AES_DECRYPT
  17258. /* Pass in bad args. Decrypt*/
  17259. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17260. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17261. BAD_FUNC_ARG);
  17262. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17263. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17264. BAD_FUNC_ARG);
  17265. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17266. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17267. BAD_FUNC_ARG);
  17268. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17269. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17270. BAD_FUNC_ARG);
  17271. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17272. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17273. BAD_FUNC_ARG);
  17274. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17275. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17276. BAD_FUNC_ARG);
  17277. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17278. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17279. BAD_FUNC_ARG);
  17280. #endif
  17281. wc_AesFree(&aes);
  17282. #endif /* HAVE_AESCCM */
  17283. return EXPECT_RESULT();
  17284. } /* END test_wc_AesCcmEncryptDecrypt */
  17285. #if defined(WOLFSSL_AES_EAX) && \
  17286. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17287. /*
  17288. * Testing test_wc_AesEaxVectors()
  17289. */
  17290. static int test_wc_AesEaxVectors(void)
  17291. {
  17292. EXPECT_DECLS;
  17293. typedef struct {
  17294. byte key[AES_256_KEY_SIZE];
  17295. int key_length;
  17296. byte iv[AES_BLOCK_SIZE];
  17297. int iv_length;
  17298. byte aad[AES_BLOCK_SIZE * 2];
  17299. int aad_length;
  17300. byte msg[AES_BLOCK_SIZE * 5];
  17301. int msg_length;
  17302. byte ct[AES_BLOCK_SIZE * 5];
  17303. int ct_length;
  17304. byte tag[AES_BLOCK_SIZE];
  17305. int tag_length;
  17306. int valid;
  17307. } AadVector;
  17308. /* Test vectors obtained from Google wycheproof project
  17309. * https://github.com/google/wycheproof
  17310. * from testvectors/aes_eax_test.json
  17311. */
  17312. const AadVector vectors[] = {
  17313. {
  17314. /* key, key length */
  17315. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17316. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17317. /* iv, iv length */
  17318. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17319. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17320. /* aad, aad length */
  17321. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17322. /* msg, msg length */
  17323. {0x00}, 0,
  17324. /* ct, ct length */
  17325. {0x00}, 0,
  17326. /* tag, tag length */
  17327. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17328. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17329. /* valid */
  17330. 1,
  17331. },
  17332. {
  17333. /* key, key length */
  17334. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17335. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17336. /* iv, iv length */
  17337. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17338. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17339. /* aad, aad length */
  17340. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17341. /* msg, msg length */
  17342. {0xf7, 0xfb}, 2,
  17343. /* ct, ct length */
  17344. {0x19, 0xdd}, 2,
  17345. /* tag, tag length */
  17346. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17347. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17348. /* valid */
  17349. 1,
  17350. },
  17351. {
  17352. /* key, key length */
  17353. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17354. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17355. /* iv, iv length */
  17356. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17357. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17358. /* aad, aad length */
  17359. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17360. /* msg, msg length */
  17361. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17362. /* ct, ct length */
  17363. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17364. /* tag, tag length */
  17365. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17366. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17367. /* valid */
  17368. 1,
  17369. },
  17370. {
  17371. /* key, key length */
  17372. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17373. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17374. /* iv, iv length */
  17375. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17376. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17377. /* aad, aad length */
  17378. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17379. /* msg, msg length */
  17380. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17381. /* ct, ct length */
  17382. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17383. /* tag, tag length */
  17384. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17385. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17386. /* valid */
  17387. 1,
  17388. },
  17389. {
  17390. /* key, key length */
  17391. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17392. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17393. /* iv, iv length */
  17394. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  17395. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  17396. /* aad, aad length */
  17397. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  17398. /* msg, msg length */
  17399. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  17400. /* ct, ct length */
  17401. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  17402. /* tag, tag length */
  17403. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  17404. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  17405. /* valid */
  17406. 1,
  17407. },
  17408. {
  17409. /* key, key length */
  17410. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  17411. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  17412. /* iv, iv length */
  17413. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  17414. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  17415. /* aad, aad length */
  17416. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  17417. /* msg, msg length */
  17418. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  17419. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  17420. /* ct, ct length */
  17421. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  17422. 0x0e, 0x72, 0x84, 0x14}, 12,
  17423. /* tag, tag length */
  17424. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  17425. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  17426. /* valid */
  17427. 1,
  17428. },
  17429. {
  17430. /* key, key length */
  17431. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  17432. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  17433. /* iv, iv length */
  17434. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  17435. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  17436. /* aad, aad length */
  17437. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  17438. /* msg, msg length */
  17439. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  17440. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  17441. 0x36}, 17,
  17442. /* ct, ct length */
  17443. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  17444. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  17445. 0x30}, 17,
  17446. /* tag, tag length */
  17447. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  17448. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  17449. /* valid */
  17450. 1,
  17451. },
  17452. {
  17453. /* key, key length */
  17454. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  17455. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  17456. /* iv, iv length */
  17457. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  17458. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  17459. /* aad, aad length */
  17460. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  17461. /* msg, msg length */
  17462. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  17463. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  17464. 0x2d, 0x56}, 18,
  17465. /* ct, ct length */
  17466. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  17467. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  17468. 0x8c, 0xc3}, 18,
  17469. /* tag, tag length */
  17470. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  17471. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  17472. /* valid */
  17473. 1,
  17474. },
  17475. {
  17476. /* key, key length */
  17477. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  17478. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  17479. /* iv, iv length */
  17480. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  17481. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  17482. /* aad, aad length */
  17483. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  17484. /* msg, msg length */
  17485. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  17486. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  17487. 0xef, 0x11}, 18,
  17488. /* ct, ct length */
  17489. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  17490. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  17491. 0xb1, 0xe8}, 18,
  17492. /* tag, tag length */
  17493. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  17494. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  17495. /* valid */
  17496. 1,
  17497. },
  17498. {
  17499. /* key, key length */
  17500. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  17501. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  17502. /* iv, iv length */
  17503. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  17504. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  17505. /* aad, aad length */
  17506. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  17507. /* msg, msg length */
  17508. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  17509. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  17510. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  17511. /* ct, ct length */
  17512. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  17513. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  17514. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  17515. /* tag, tag length */
  17516. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  17517. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  17518. /* valid */
  17519. 1,
  17520. },
  17521. {
  17522. /* key, key length */
  17523. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17524. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17525. /* iv, iv length */
  17526. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  17527. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  17528. /* aad, aad length */
  17529. {0x00}, 0,
  17530. /* msg, msg length */
  17531. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17532. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17533. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17534. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17535. /* ct, ct length */
  17536. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  17537. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  17538. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  17539. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  17540. /* tag, tag length */
  17541. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  17542. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  17543. /* valid */
  17544. 1,
  17545. },
  17546. {
  17547. /* key, key length */
  17548. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17549. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17550. /* iv, iv length */
  17551. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  17552. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  17553. /* aad, aad length */
  17554. {0x00}, 0,
  17555. /* msg, msg length */
  17556. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17557. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17558. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17559. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17560. /* ct, ct length */
  17561. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  17562. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  17563. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  17564. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  17565. /* tag, tag length */
  17566. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  17567. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  17568. /* valid */
  17569. 1,
  17570. },
  17571. {
  17572. /* key, key length */
  17573. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17574. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17575. /* iv, iv length */
  17576. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  17577. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  17578. /* aad, aad length */
  17579. {0x00}, 0,
  17580. /* msg, msg length */
  17581. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17582. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17583. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17584. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17585. /* ct, ct length */
  17586. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  17587. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  17588. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  17589. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  17590. /* tag, tag length */
  17591. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  17592. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  17593. /* valid */
  17594. 1,
  17595. },
  17596. {
  17597. /* key, key length */
  17598. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17599. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17600. /* iv, iv length */
  17601. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  17602. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  17603. /* aad, aad length */
  17604. {0x00}, 0,
  17605. /* msg, msg length */
  17606. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17607. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17608. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17609. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17610. /* ct, ct length */
  17611. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  17612. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  17613. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  17614. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  17615. /* tag, tag length */
  17616. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  17617. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  17618. /* valid */
  17619. 1,
  17620. },
  17621. {
  17622. /* key, key length */
  17623. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17624. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17625. /* iv, iv length */
  17626. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  17627. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  17628. /* aad, aad length */
  17629. {0x00}, 0,
  17630. /* msg, msg length */
  17631. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17632. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17633. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17634. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17635. /* ct, ct length */
  17636. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  17637. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  17638. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  17639. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  17640. /* tag, tag length */
  17641. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  17642. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  17643. /* valid */
  17644. 1,
  17645. },
  17646. {
  17647. /* key, key length */
  17648. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17649. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17650. /* iv, iv length */
  17651. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  17652. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  17653. /* aad, aad length */
  17654. {0x00}, 0,
  17655. /* msg, msg length */
  17656. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17657. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17658. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17659. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17660. /* ct, ct length */
  17661. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  17662. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  17663. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  17664. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  17665. /* tag, tag length */
  17666. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  17667. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  17668. /* valid */
  17669. 1,
  17670. },
  17671. {
  17672. /* key, key length */
  17673. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17674. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17675. /* iv, iv length */
  17676. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  17677. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  17678. /* aad, aad length */
  17679. {0x00}, 0,
  17680. /* msg, msg length */
  17681. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17682. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17683. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17684. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17685. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17686. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17687. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17688. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  17689. /* ct, ct length */
  17690. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  17691. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  17692. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  17693. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  17694. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  17695. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  17696. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  17697. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  17698. /* tag, tag length */
  17699. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  17700. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  17701. /* valid */
  17702. 1,
  17703. },
  17704. {
  17705. /* key, key length */
  17706. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17707. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17708. /* iv, iv length */
  17709. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  17710. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  17711. /* aad, aad length */
  17712. {0x00}, 0,
  17713. /* msg, msg length */
  17714. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17715. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17716. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17717. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17718. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17719. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17720. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17721. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17722. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  17723. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  17724. /* ct, ct length */
  17725. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  17726. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  17727. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  17728. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  17729. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  17730. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  17731. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  17732. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  17733. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  17734. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  17735. /* tag, tag length */
  17736. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  17737. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  17738. /* valid */
  17739. 1,
  17740. },
  17741. {
  17742. /* key, key length */
  17743. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  17744. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  17745. /* iv, iv length */
  17746. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  17747. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  17748. /* aad, aad length */
  17749. {0x00}, 0,
  17750. /* msg, msg length */
  17751. {0x00}, 0,
  17752. /* ct, ct length */
  17753. {0x00}, 0,
  17754. /* tag, tag length */
  17755. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  17756. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  17757. /* valid */
  17758. 1,
  17759. },
  17760. {
  17761. /* key, key length */
  17762. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  17763. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  17764. /* iv, iv length */
  17765. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  17766. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  17767. /* aad, aad length */
  17768. {0x00}, 0,
  17769. /* msg, msg length */
  17770. {0x01}, 1,
  17771. /* ct, ct length */
  17772. {0x1d}, 1,
  17773. /* tag, tag length */
  17774. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  17775. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  17776. /* valid */
  17777. 1,
  17778. },
  17779. {
  17780. /* key, key length */
  17781. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  17782. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  17783. /* iv, iv length */
  17784. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  17785. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  17786. /* aad, aad length */
  17787. {0x00}, 0,
  17788. /* msg, msg length */
  17789. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  17790. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  17791. /* ct, ct length */
  17792. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  17793. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  17794. /* tag, tag length */
  17795. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  17796. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  17797. /* valid */
  17798. 1,
  17799. },
  17800. {
  17801. /* key, key length */
  17802. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  17803. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  17804. /* iv, iv length */
  17805. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  17806. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  17807. /* aad, aad length */
  17808. {0x00}, 0,
  17809. /* msg, msg length */
  17810. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  17811. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  17812. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  17813. /* ct, ct length */
  17814. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  17815. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  17816. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  17817. /* tag, tag length */
  17818. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  17819. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  17820. /* valid */
  17821. 1,
  17822. },
  17823. {
  17824. /* key, key length */
  17825. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  17826. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  17827. /* iv, iv length */
  17828. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  17829. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  17830. /* aad, aad length */
  17831. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  17832. /* msg, msg length */
  17833. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  17834. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  17835. /* ct, ct length */
  17836. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  17837. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  17838. /* tag, tag length */
  17839. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  17840. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  17841. /* valid */
  17842. 1,
  17843. },
  17844. {
  17845. /* key, key length */
  17846. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  17847. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  17848. /* iv, iv length */
  17849. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  17850. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  17851. /* aad, aad length */
  17852. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  17853. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  17854. /* msg, msg length */
  17855. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  17856. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  17857. /* ct, ct length */
  17858. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  17859. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  17860. /* tag, tag length */
  17861. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  17862. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  17863. /* valid */
  17864. 1,
  17865. },
  17866. {
  17867. /* key, key length */
  17868. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  17869. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  17870. /* iv, iv length */
  17871. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  17872. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  17873. /* aad, aad length */
  17874. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  17875. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  17876. /* msg, msg length */
  17877. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  17878. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  17879. /* ct, ct length */
  17880. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  17881. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  17882. /* tag, tag length */
  17883. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  17884. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  17885. /* valid */
  17886. 1,
  17887. },
  17888. {
  17889. /* key, key length */
  17890. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  17891. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  17892. /* iv, iv length */
  17893. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  17894. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  17895. /* aad, aad length */
  17896. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  17897. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  17898. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  17899. /* msg, msg length */
  17900. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  17901. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  17902. /* ct, ct length */
  17903. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  17904. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  17905. /* tag, tag length */
  17906. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  17907. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  17908. /* valid */
  17909. 1,
  17910. },
  17911. {
  17912. /* key, key length */
  17913. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17914. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17915. /* iv, iv length */
  17916. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17917. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17918. /* aad, aad length */
  17919. {0x00}, 0,
  17920. /* msg, msg length */
  17921. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17922. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17923. /* ct, ct length */
  17924. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17925. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17926. /* tag, tag length */
  17927. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17928. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17929. /* valid */
  17930. 0,
  17931. },
  17932. {
  17933. /* key, key length */
  17934. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17935. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17936. /* iv, iv length */
  17937. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17938. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17939. /* aad, aad length */
  17940. {0x00}, 0,
  17941. /* msg, msg length */
  17942. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17943. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17944. /* ct, ct length */
  17945. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17946. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17947. /* tag, tag length */
  17948. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17949. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17950. /* valid */
  17951. 0,
  17952. },
  17953. {
  17954. /* key, key length */
  17955. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17956. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17957. /* iv, iv length */
  17958. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17959. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17960. /* aad, aad length */
  17961. {0x00}, 0,
  17962. /* msg, msg length */
  17963. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17964. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17965. /* ct, ct length */
  17966. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17967. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17968. /* tag, tag length */
  17969. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17970. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17971. /* valid */
  17972. 0,
  17973. },
  17974. {
  17975. /* key, key length */
  17976. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17977. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17978. /* iv, iv length */
  17979. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17980. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17981. /* aad, aad length */
  17982. {0x00}, 0,
  17983. /* msg, msg length */
  17984. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17985. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17986. /* ct, ct length */
  17987. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17988. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17989. /* tag, tag length */
  17990. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17991. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17992. /* valid */
  17993. 0,
  17994. },
  17995. {
  17996. /* key, key length */
  17997. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17998. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17999. /* iv, iv length */
  18000. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18001. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18002. /* aad, aad length */
  18003. {0x00}, 0,
  18004. /* msg, msg length */
  18005. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18006. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18007. /* ct, ct length */
  18008. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18009. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18010. /* tag, tag length */
  18011. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18012. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18013. /* valid */
  18014. 0,
  18015. },
  18016. {
  18017. /* key, key length */
  18018. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18019. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18020. /* iv, iv length */
  18021. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18022. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18023. /* aad, aad length */
  18024. {0x00}, 0,
  18025. /* msg, msg length */
  18026. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18027. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18028. /* ct, ct length */
  18029. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18030. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18031. /* tag, tag length */
  18032. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18033. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18034. /* valid */
  18035. 0,
  18036. },
  18037. {
  18038. /* key, key length */
  18039. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18040. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18041. /* iv, iv length */
  18042. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18043. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18044. /* aad, aad length */
  18045. {0x00}, 0,
  18046. /* msg, msg length */
  18047. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18048. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18049. /* ct, ct length */
  18050. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18051. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18052. /* tag, tag length */
  18053. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18054. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18055. /* valid */
  18056. 0,
  18057. },
  18058. {
  18059. /* key, key length */
  18060. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18061. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18062. /* iv, iv length */
  18063. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18064. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18065. /* aad, aad length */
  18066. {0x00}, 0,
  18067. /* msg, msg length */
  18068. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18069. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18070. /* ct, ct length */
  18071. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18072. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18073. /* tag, tag length */
  18074. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18075. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18076. /* valid */
  18077. 0,
  18078. },
  18079. {
  18080. /* key, key length */
  18081. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18082. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18083. /* iv, iv length */
  18084. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18085. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18086. /* aad, aad length */
  18087. {0x00}, 0,
  18088. /* msg, msg length */
  18089. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18090. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18091. /* ct, ct length */
  18092. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18093. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18094. /* tag, tag length */
  18095. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18096. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18097. /* valid */
  18098. 0,
  18099. },
  18100. {
  18101. /* key, key length */
  18102. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18103. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18104. /* iv, iv length */
  18105. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18106. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18107. /* aad, aad length */
  18108. {0x00}, 0,
  18109. /* msg, msg length */
  18110. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18111. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18112. /* ct, ct length */
  18113. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18114. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18115. /* tag, tag length */
  18116. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18117. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18118. /* valid */
  18119. 0,
  18120. },
  18121. {
  18122. /* key, key length */
  18123. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18124. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18125. /* iv, iv length */
  18126. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18127. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18128. /* aad, aad length */
  18129. {0x00}, 0,
  18130. /* msg, msg length */
  18131. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18132. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18133. /* ct, ct length */
  18134. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18135. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18136. /* tag, tag length */
  18137. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18138. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18139. /* valid */
  18140. 0,
  18141. },
  18142. {
  18143. /* key, key length */
  18144. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18145. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18146. /* iv, iv length */
  18147. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18148. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18149. /* aad, aad length */
  18150. {0x00}, 0,
  18151. /* msg, msg length */
  18152. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18153. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18154. /* ct, ct length */
  18155. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18156. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18157. /* tag, tag length */
  18158. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18159. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18160. /* valid */
  18161. 0,
  18162. },
  18163. {
  18164. /* key, key length */
  18165. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18166. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18167. /* iv, iv length */
  18168. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18169. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18170. /* aad, aad length */
  18171. {0x00}, 0,
  18172. /* msg, msg length */
  18173. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18174. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18175. /* ct, ct length */
  18176. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18177. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18178. /* tag, tag length */
  18179. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18180. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18181. /* valid */
  18182. 0,
  18183. },
  18184. {
  18185. /* key, key length */
  18186. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18187. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18188. /* iv, iv length */
  18189. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18190. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18191. /* aad, aad length */
  18192. {0x00}, 0,
  18193. /* msg, msg length */
  18194. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18195. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18196. /* ct, ct length */
  18197. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18198. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18199. /* tag, tag length */
  18200. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18201. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18202. /* valid */
  18203. 0,
  18204. },
  18205. {
  18206. /* key, key length */
  18207. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18208. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18209. /* iv, iv length */
  18210. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18211. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18212. /* aad, aad length */
  18213. {0x00}, 0,
  18214. /* msg, msg length */
  18215. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18216. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18217. /* ct, ct length */
  18218. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18219. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18220. /* tag, tag length */
  18221. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18222. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18223. /* valid */
  18224. 0,
  18225. },
  18226. {
  18227. /* key, key length */
  18228. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18229. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18230. /* iv, iv length */
  18231. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18232. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18233. /* aad, aad length */
  18234. {0x00}, 0,
  18235. /* msg, msg length */
  18236. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18237. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18238. /* ct, ct length */
  18239. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18240. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18241. /* tag, tag length */
  18242. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18243. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18244. /* valid */
  18245. 0,
  18246. },
  18247. {
  18248. /* key, key length */
  18249. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18250. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18251. /* iv, iv length */
  18252. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18253. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18254. /* aad, aad length */
  18255. {0x00}, 0,
  18256. /* msg, msg length */
  18257. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18258. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18259. /* ct, ct length */
  18260. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18261. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18262. /* tag, tag length */
  18263. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18264. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18265. /* valid */
  18266. 0,
  18267. },
  18268. {
  18269. /* key, key length */
  18270. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18271. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18272. /* iv, iv length */
  18273. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18274. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18275. /* aad, aad length */
  18276. {0x00}, 0,
  18277. /* msg, msg length */
  18278. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18279. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18280. /* ct, ct length */
  18281. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18282. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18283. /* tag, tag length */
  18284. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18285. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18286. /* valid */
  18287. 0,
  18288. },
  18289. {
  18290. /* key, key length */
  18291. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18292. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18293. /* iv, iv length */
  18294. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18295. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18296. /* aad, aad length */
  18297. {0x00}, 0,
  18298. /* msg, msg length */
  18299. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18300. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18301. /* ct, ct length */
  18302. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18303. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18304. /* tag, tag length */
  18305. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18306. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18307. /* valid */
  18308. 0,
  18309. },
  18310. {
  18311. /* key, key length */
  18312. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18313. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18314. /* iv, iv length */
  18315. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18316. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18317. /* aad, aad length */
  18318. {0x00}, 0,
  18319. /* msg, msg length */
  18320. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18321. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18322. /* ct, ct length */
  18323. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18324. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18325. /* tag, tag length */
  18326. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18327. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18328. /* valid */
  18329. 0,
  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. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18337. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18338. /* aad, aad length */
  18339. {0x00}, 0,
  18340. /* msg, msg length */
  18341. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18342. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18343. /* ct, ct length */
  18344. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18345. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18346. /* tag, tag length */
  18347. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18348. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18349. /* valid */
  18350. 0,
  18351. },
  18352. {
  18353. /* key, key length */
  18354. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18355. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18356. /* iv, iv length */
  18357. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18358. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18359. /* aad, aad length */
  18360. {0x00}, 0,
  18361. /* msg, msg length */
  18362. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18363. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18364. /* ct, ct length */
  18365. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18366. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18367. /* tag, tag length */
  18368. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18369. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18370. /* valid */
  18371. 0,
  18372. },
  18373. {
  18374. /* key, key length */
  18375. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18376. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18377. /* iv, iv length */
  18378. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18379. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18380. /* aad, aad length */
  18381. {0x00}, 0,
  18382. /* msg, msg length */
  18383. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18384. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18385. /* ct, ct length */
  18386. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18387. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18388. /* tag, tag length */
  18389. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18390. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18391. /* valid */
  18392. 0,
  18393. },
  18394. {
  18395. /* key, key length */
  18396. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18397. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18398. /* iv, iv length */
  18399. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18400. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18401. /* aad, aad length */
  18402. {0x00}, 0,
  18403. /* msg, msg length */
  18404. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18405. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18406. /* ct, ct length */
  18407. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18408. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18409. /* tag, tag length */
  18410. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  18412. /* valid */
  18413. 0,
  18414. },
  18415. {
  18416. /* key, key length */
  18417. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18418. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18419. /* iv, iv length */
  18420. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18421. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18422. /* aad, aad length */
  18423. {0x00}, 0,
  18424. /* msg, msg length */
  18425. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18426. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18427. /* ct, ct length */
  18428. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18429. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18430. /* tag, tag length */
  18431. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18432. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  18433. /* valid */
  18434. 0,
  18435. },
  18436. {
  18437. /* key, key length */
  18438. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18439. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18440. /* iv, iv length */
  18441. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18442. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18443. /* aad, aad length */
  18444. {0x00}, 0,
  18445. /* msg, msg length */
  18446. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18447. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18448. /* ct, ct length */
  18449. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18450. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18451. /* tag, tag length */
  18452. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  18453. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  18454. /* valid */
  18455. 0,
  18456. },
  18457. {
  18458. /* key, key length */
  18459. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18460. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18461. /* iv, iv length */
  18462. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18463. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18464. /* aad, aad length */
  18465. {0x00}, 0,
  18466. /* msg, msg length */
  18467. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18468. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18469. /* ct, ct length */
  18470. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18471. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18472. /* tag, tag length */
  18473. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  18474. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  18475. /* valid */
  18476. 0,
  18477. },
  18478. };
  18479. byte ciphertext[sizeof(vectors[0].ct)];
  18480. byte authtag[sizeof(vectors[0].tag)];
  18481. int i;
  18482. int len;
  18483. int ret;
  18484. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  18485. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18486. len = sizeof(authtag);
  18487. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  18488. ciphertext,
  18489. vectors[i].msg, vectors[i].msg_length,
  18490. vectors[i].iv, vectors[i].iv_length,
  18491. authtag, len,
  18492. vectors[i].aad, vectors[i].aad_length),
  18493. 0);
  18494. /* check ciphertext matches vector */
  18495. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  18496. 0);
  18497. /* check that computed tag matches vector only for vectors marked as valid */
  18498. ret = XMEMCMP(authtag, vectors[i].tag, len);
  18499. if (vectors[i].valid) {
  18500. ExpectIntEQ(ret, 0);
  18501. }
  18502. else {
  18503. ExpectIntNE(ret, 0);
  18504. }
  18505. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18506. /* Decrypt, checking that the computed auth tags match */
  18507. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  18508. ciphertext,
  18509. vectors[i].ct, vectors[i].ct_length,
  18510. vectors[i].iv, vectors[i].iv_length,
  18511. authtag, len,
  18512. vectors[i].aad, vectors[i].aad_length),
  18513. 0);
  18514. /* check decrypted ciphertext matches vector plaintext */
  18515. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  18516. 0);
  18517. }
  18518. return EXPECT_RESULT();
  18519. } /* END test_wc_AesEaxVectors */
  18520. /*
  18521. * Testing test_wc_AesEaxEncryptAuth()
  18522. */
  18523. static int test_wc_AesEaxEncryptAuth(void)
  18524. {
  18525. EXPECT_DECLS;
  18526. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18527. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18528. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18529. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18530. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18531. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18532. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18533. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18534. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18535. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18536. byte ciphertext[sizeof(msg)];
  18537. byte authtag[AES_BLOCK_SIZE];
  18538. int i;
  18539. int len;
  18540. len = sizeof(authtag);
  18541. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18542. ciphertext,
  18543. msg, sizeof(msg),
  18544. iv, sizeof(iv),
  18545. authtag, len,
  18546. aad, sizeof(aad)),
  18547. 0);
  18548. /* Test null checking */
  18549. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  18550. ciphertext,
  18551. msg, sizeof(msg),
  18552. iv, sizeof(iv),
  18553. authtag, len,
  18554. aad, sizeof(aad)),
  18555. BAD_FUNC_ARG);
  18556. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18557. NULL,
  18558. msg, sizeof(msg),
  18559. iv, sizeof(iv),
  18560. authtag, len,
  18561. aad, sizeof(aad)),
  18562. BAD_FUNC_ARG);
  18563. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18564. ciphertext,
  18565. NULL, sizeof(msg),
  18566. iv, sizeof(iv),
  18567. authtag, len,
  18568. aad, sizeof(aad)),
  18569. BAD_FUNC_ARG);
  18570. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18571. ciphertext,
  18572. msg, sizeof(msg),
  18573. NULL, sizeof(iv),
  18574. authtag, len,
  18575. aad, sizeof(aad)),
  18576. BAD_FUNC_ARG);
  18577. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18578. ciphertext,
  18579. msg, sizeof(msg),
  18580. iv, sizeof(iv),
  18581. NULL, len,
  18582. aad, sizeof(aad)),
  18583. BAD_FUNC_ARG);
  18584. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18585. ciphertext,
  18586. msg, sizeof(msg),
  18587. iv, sizeof(iv),
  18588. authtag, len,
  18589. NULL, sizeof(aad)),
  18590. BAD_FUNC_ARG);
  18591. /* Test bad key lengths */
  18592. for (i = 0; i <= 32; i++) {
  18593. int exp_ret;
  18594. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18595. || i == AES_256_KEY_SIZE) {
  18596. exp_ret = 0;
  18597. }
  18598. else {
  18599. exp_ret = BAD_FUNC_ARG;
  18600. }
  18601. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  18602. ciphertext,
  18603. msg, sizeof(msg),
  18604. iv, sizeof(iv),
  18605. authtag, len,
  18606. aad, sizeof(aad)),
  18607. exp_ret);
  18608. }
  18609. /* Test auth tag size out of range */
  18610. len = AES_BLOCK_SIZE + 1;
  18611. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18612. ciphertext,
  18613. msg, sizeof(msg),
  18614. iv, sizeof(iv),
  18615. authtag, len,
  18616. aad, sizeof(aad)),
  18617. BAD_FUNC_ARG);
  18618. return EXPECT_RESULT();
  18619. } /* END test_wc_AesEaxEncryptAuth() */
  18620. /*
  18621. * Testing test_wc_AesEaxDecryptAuth()
  18622. */
  18623. static int test_wc_AesEaxDecryptAuth(void)
  18624. {
  18625. EXPECT_DECLS;
  18626. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18627. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18628. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18629. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18630. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18631. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18632. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18633. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18634. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18635. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18636. /* Garbage tag that should always fail for above aad */
  18637. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  18638. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  18639. byte plaintext[sizeof(ct)];
  18640. int i;
  18641. int len;
  18642. len = sizeof(tag);
  18643. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18644. plaintext,
  18645. ct, sizeof(ct),
  18646. iv, sizeof(iv),
  18647. tag, len,
  18648. aad, sizeof(aad)),
  18649. AES_EAX_AUTH_E);
  18650. /* Test null checking */
  18651. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  18652. plaintext,
  18653. ct, sizeof(ct),
  18654. iv, sizeof(iv),
  18655. tag, len,
  18656. aad, sizeof(aad)),
  18657. BAD_FUNC_ARG);
  18658. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18659. NULL,
  18660. ct, sizeof(ct),
  18661. iv, sizeof(iv),
  18662. tag, len,
  18663. aad, sizeof(aad)),
  18664. BAD_FUNC_ARG);
  18665. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18666. plaintext,
  18667. NULL, sizeof(ct),
  18668. iv, sizeof(iv),
  18669. tag, len,
  18670. aad, sizeof(aad)),
  18671. BAD_FUNC_ARG);
  18672. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18673. plaintext,
  18674. ct, sizeof(ct),
  18675. NULL, sizeof(iv),
  18676. tag, len,
  18677. aad, sizeof(aad)),
  18678. BAD_FUNC_ARG);
  18679. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18680. plaintext,
  18681. ct, sizeof(ct),
  18682. iv, sizeof(iv),
  18683. NULL, len,
  18684. aad, sizeof(aad)),
  18685. BAD_FUNC_ARG);
  18686. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18687. plaintext,
  18688. ct, sizeof(ct),
  18689. iv, sizeof(iv),
  18690. tag, len,
  18691. NULL, sizeof(aad)),
  18692. BAD_FUNC_ARG);
  18693. /* Test bad key lengths */
  18694. for (i = 0; i <= 32; i++) {
  18695. int exp_ret;
  18696. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18697. || i == AES_256_KEY_SIZE) {
  18698. exp_ret = AES_EAX_AUTH_E;
  18699. }
  18700. else {
  18701. exp_ret = BAD_FUNC_ARG;
  18702. }
  18703. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  18704. plaintext,
  18705. ct, sizeof(ct),
  18706. iv, sizeof(iv),
  18707. tag, len,
  18708. aad, sizeof(aad)),
  18709. exp_ret);
  18710. }
  18711. /* Test auth tag size out of range */
  18712. len = AES_BLOCK_SIZE + 1;
  18713. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18714. plaintext,
  18715. ct, sizeof(ct),
  18716. iv, sizeof(iv),
  18717. tag, len,
  18718. aad, sizeof(aad)),
  18719. BAD_FUNC_ARG);
  18720. return EXPECT_RESULT();
  18721. } /* END test_wc_AesEaxDecryptAuth() */
  18722. #endif /* WOLFSSL_AES_EAX &&
  18723. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  18724. */
  18725. /*
  18726. * Testing wc_InitDsaKey()
  18727. */
  18728. static int test_wc_InitDsaKey(void)
  18729. {
  18730. EXPECT_DECLS;
  18731. #ifndef NO_DSA
  18732. DsaKey key;
  18733. XMEMSET(&key, 0, sizeof(DsaKey));
  18734. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18735. /* Pass in bad args. */
  18736. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  18737. wc_FreeDsaKey(&key);
  18738. #endif
  18739. return EXPECT_RESULT();
  18740. } /* END test_wc_InitDsaKey */
  18741. /*
  18742. * Testing wc_DsaSign() and wc_DsaVerify()
  18743. */
  18744. static int test_wc_DsaSignVerify(void)
  18745. {
  18746. EXPECT_DECLS;
  18747. #if !defined(NO_DSA)
  18748. DsaKey key;
  18749. WC_RNG rng;
  18750. wc_Sha sha;
  18751. byte signature[DSA_SIG_SIZE];
  18752. byte hash[WC_SHA_DIGEST_SIZE];
  18753. word32 idx = 0;
  18754. word32 bytes;
  18755. int answer;
  18756. #ifdef USE_CERT_BUFFERS_1024
  18757. byte tmp[ONEK_BUF];
  18758. XMEMSET(tmp, 0, sizeof(tmp));
  18759. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18760. bytes = sizeof_dsa_key_der_1024;
  18761. #elif defined(USE_CERT_BUFFERS_2048)
  18762. byte tmp[TWOK_BUF];
  18763. XMEMSET(tmp, 0, sizeof(tmp));
  18764. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18765. bytes = sizeof_dsa_key_der_2048;
  18766. #else
  18767. byte tmp[TWOK_BUF];
  18768. XFILE fp = XBADFILE;
  18769. XMEMSET(tmp, 0, sizeof(tmp));
  18770. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18771. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18772. if (fp != XBADFILE)
  18773. XFCLOSE(fp);
  18774. #endif /* END USE_CERT_BUFFERS_1024 */
  18775. ExpectIntEQ(wc_InitSha(&sha), 0);
  18776. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  18777. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  18778. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18779. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18780. ExpectIntEQ(wc_InitRng(&rng), 0);
  18781. /* Sign. */
  18782. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  18783. /* Test bad args. */
  18784. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  18785. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  18786. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  18787. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18788. /* Verify. */
  18789. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  18790. ExpectIntEQ(answer, 1);
  18791. /* Pass in bad args. */
  18792. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  18793. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  18794. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  18795. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18796. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18797. /* hard set q to 0 and test fail case */
  18798. mp_free(&key.q);
  18799. mp_init(&key.q);
  18800. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18801. mp_set(&key.q, 1);
  18802. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18803. #endif
  18804. DoExpectIntEQ(wc_FreeRng(&rng),0);
  18805. wc_FreeDsaKey(&key);
  18806. wc_ShaFree(&sha);
  18807. #endif
  18808. return EXPECT_RESULT();
  18809. } /* END test_wc_DsaSign */
  18810. /*
  18811. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18812. */
  18813. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18814. {
  18815. EXPECT_DECLS;
  18816. #if !defined(NO_DSA)
  18817. DsaKey key;
  18818. word32 bytes;
  18819. word32 idx = 0;
  18820. int ret;
  18821. #ifdef USE_CERT_BUFFERS_1024
  18822. byte tmp[ONEK_BUF];
  18823. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18824. bytes = sizeof_dsa_key_der_1024;
  18825. #elif defined(USE_CERT_BUFFERS_2048)
  18826. byte tmp[TWOK_BUF];
  18827. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18828. bytes = sizeof_dsa_key_der_2048;
  18829. #else
  18830. byte tmp[TWOK_BUF];
  18831. XFILE fp = XBADFILE;
  18832. XMEMSET(tmp, 0, sizeof(tmp));
  18833. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18834. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18835. if (fp != XBADFILE)
  18836. XFCLOSE(fp);
  18837. #endif /* END USE_CERT_BUFFERS_1024 */
  18838. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18839. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18840. /* Test bad args. */
  18841. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18842. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18843. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18844. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18845. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18846. wc_FreeDsaKey(&key);
  18847. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18848. idx = 0; /* Reset */
  18849. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18850. /* Test bad args. */
  18851. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18852. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18853. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18854. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18855. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18856. wc_FreeDsaKey(&key);
  18857. #endif /* !NO_DSA */
  18858. return EXPECT_RESULT();
  18859. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18860. /*
  18861. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18862. */
  18863. static int test_wc_MakeDsaKey(void)
  18864. {
  18865. EXPECT_DECLS;
  18866. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18867. DsaKey genKey;
  18868. WC_RNG rng;
  18869. XMEMSET(&genKey, 0, sizeof(genKey));
  18870. XMEMSET(&rng, 0, sizeof(rng));
  18871. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  18872. ExpectIntEQ(wc_InitRng(&rng), 0);
  18873. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  18874. /* Test bad args. */
  18875. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  18876. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  18877. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  18878. BAD_FUNC_ARG);
  18879. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  18880. /* Test bad args. */
  18881. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  18882. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  18883. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18884. wc_FreeDsaKey(&genKey);
  18885. #endif
  18886. return EXPECT_RESULT();
  18887. } /* END test_wc_MakeDsaKey */
  18888. /*
  18889. * Testing wc_DsaKeyToDer()
  18890. */
  18891. static int test_wc_DsaKeyToDer(void)
  18892. {
  18893. EXPECT_DECLS;
  18894. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18895. DsaKey key;
  18896. word32 bytes;
  18897. word32 idx = 0;
  18898. #ifdef USE_CERT_BUFFERS_1024
  18899. byte tmp[ONEK_BUF];
  18900. byte der[ONEK_BUF];
  18901. XMEMSET(tmp, 0, sizeof(tmp));
  18902. XMEMSET(der, 0, sizeof(der));
  18903. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18904. bytes = sizeof_dsa_key_der_1024;
  18905. #elif defined(USE_CERT_BUFFERS_2048)
  18906. byte tmp[TWOK_BUF];
  18907. byte der[TWOK_BUF];
  18908. XMEMSET(tmp, 0, sizeof(tmp));
  18909. XMEMSET(der, 0, sizeof(der));
  18910. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18911. bytes = sizeof_dsa_key_der_2048;
  18912. #else
  18913. byte tmp[TWOK_BUF];
  18914. byte der[TWOK_BUF];
  18915. XFILE fp = XBADFILE;
  18916. XMEMSET(tmp, 0, sizeof(tmp));
  18917. XMEMSET(der, 0, sizeof(der));
  18918. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18919. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18920. if (fp != XBADFILE)
  18921. XFCLOSE(fp);
  18922. #endif /* END USE_CERT_BUFFERS_1024 */
  18923. XMEMSET(&key, 0, sizeof(DsaKey));
  18924. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18925. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18926. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  18927. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  18928. /* Test bad args. */
  18929. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18930. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18931. wc_FreeDsaKey(&key);
  18932. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18933. return EXPECT_RESULT();
  18934. } /* END test_wc_DsaKeyToDer */
  18935. /*
  18936. * Testing wc_DsaKeyToPublicDer()
  18937. * (indirectly testing setDsaPublicKey())
  18938. */
  18939. static int test_wc_DsaKeyToPublicDer(void)
  18940. {
  18941. EXPECT_DECLS;
  18942. #ifndef HAVE_SELFTEST
  18943. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18944. DsaKey key;
  18945. WC_RNG rng;
  18946. byte* der = NULL;
  18947. word32 sz = 0;
  18948. word32 idx = 0;
  18949. XMEMSET(&key, 0, sizeof(DsaKey));
  18950. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18951. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  18952. DYNAMIC_TYPE_TMP_BUFFER));
  18953. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18954. ExpectIntEQ(wc_InitRng(&rng), 0);
  18955. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  18956. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18957. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  18958. wc_FreeDsaKey(&key);
  18959. idx = 0;
  18960. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18961. /* Test without the SubjectPublicKeyInfo header */
  18962. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  18963. wc_FreeDsaKey(&key);
  18964. idx = 0;
  18965. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18966. /* Test bad args. */
  18967. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18968. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18969. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18970. wc_FreeDsaKey(&key);
  18971. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18972. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18973. #endif /* !HAVE_SELFTEST */
  18974. return EXPECT_RESULT();
  18975. } /* END test_wc_DsaKeyToPublicDer */
  18976. /*
  18977. * Testing wc_DsaImportParamsRaw()
  18978. */
  18979. static int test_wc_DsaImportParamsRaw(void)
  18980. {
  18981. EXPECT_DECLS;
  18982. #if !defined(NO_DSA)
  18983. DsaKey key;
  18984. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18985. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18986. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18987. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18988. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18989. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18990. "80a0093113a8bd73";
  18991. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18992. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18993. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18994. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18995. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18996. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18997. "76341a7e7d9";
  18998. /* invalid p and q parameters */
  18999. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19000. const char* invalidQ = "96c5390a";
  19001. XMEMSET(&key, 0, sizeof(DsaKey));
  19002. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19003. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19004. /* test bad args */
  19005. /* null key struct */
  19006. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19007. /* null param pointers */
  19008. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19009. /* illegal p length */
  19010. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19011. /* illegal q length */
  19012. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19013. wc_FreeDsaKey(&key);
  19014. #endif
  19015. return EXPECT_RESULT();
  19016. } /* END test_wc_DsaImportParamsRaw */
  19017. /*
  19018. * Testing wc_DsaImportParamsRawCheck()
  19019. */
  19020. static int test_wc_DsaImportParamsRawCheck(void)
  19021. {
  19022. EXPECT_DECLS;
  19023. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19024. DsaKey key;
  19025. int trusted = 0;
  19026. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19027. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19028. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19029. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19030. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19031. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19032. "80a0093113a8bd73";
  19033. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19034. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19035. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19036. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19037. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19038. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19039. "76341a7e7d9";
  19040. /* invalid p and q parameters */
  19041. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19042. const char* invalidQ = "96c5390a";
  19043. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19044. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19045. /* test bad args */
  19046. /* null key struct */
  19047. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19048. BAD_FUNC_ARG);
  19049. /* null param pointers */
  19050. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19051. NULL), BAD_FUNC_ARG);
  19052. /* illegal p length */
  19053. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19054. BAD_FUNC_ARG);
  19055. /* illegal q length */
  19056. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19057. BAD_FUNC_ARG);
  19058. wc_FreeDsaKey(&key);
  19059. #endif
  19060. return EXPECT_RESULT();
  19061. } /* END test_wc_DsaImportParamsRawCheck */
  19062. /*
  19063. * Testing wc_DsaExportParamsRaw()
  19064. */
  19065. static int test_wc_DsaExportParamsRaw(void)
  19066. {
  19067. EXPECT_DECLS;
  19068. #if !defined(NO_DSA)
  19069. DsaKey key;
  19070. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19071. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19072. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19073. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19074. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19075. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19076. "80a0093113a8bd73";
  19077. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19078. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19079. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19080. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19081. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19082. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19083. "76341a7e7d9";
  19084. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19085. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19086. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19087. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19088. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19089. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19090. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19091. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19092. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19093. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19094. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19095. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19096. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19097. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19098. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19099. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19100. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19101. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19102. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19103. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19104. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19105. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19106. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19107. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19108. byte pOut[MAX_DSA_PARAM_SIZE];
  19109. byte qOut[MAX_DSA_PARAM_SIZE];
  19110. byte gOut[MAX_DSA_PARAM_SIZE];
  19111. word32 pOutSz;
  19112. word32 qOutSz;
  19113. word32 gOutSz;
  19114. XMEMSET(&key, 0, sizeof(DsaKey));
  19115. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19116. /* first test using imported raw parameters, for expected */
  19117. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19118. pOutSz = sizeof(pOut);
  19119. qOutSz = sizeof(qOut);
  19120. gOutSz = sizeof(gOut);
  19121. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19122. &gOutSz), 0);
  19123. /* validate exported parameters are correct */
  19124. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19125. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19126. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19127. /* test bad args */
  19128. /* null key struct */
  19129. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19130. &gOutSz), BAD_FUNC_ARG);
  19131. /* null output pointers */
  19132. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19133. &gOutSz), LENGTH_ONLY_E);
  19134. /* null output size pointers */
  19135. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19136. NULL), BAD_FUNC_ARG);
  19137. /* p output buffer size too small */
  19138. pOutSz = 1;
  19139. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19140. &gOutSz), BUFFER_E);
  19141. pOutSz = sizeof(pOut);
  19142. /* q output buffer size too small */
  19143. qOutSz = 1;
  19144. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19145. &gOutSz), BUFFER_E);
  19146. qOutSz = sizeof(qOut);
  19147. /* g output buffer size too small */
  19148. gOutSz = 1;
  19149. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19150. &gOutSz), BUFFER_E);
  19151. wc_FreeDsaKey(&key);
  19152. #endif
  19153. return EXPECT_RESULT();
  19154. } /* END test_wc_DsaExportParamsRaw */
  19155. /*
  19156. * Testing wc_DsaExportKeyRaw()
  19157. */
  19158. static int test_wc_DsaExportKeyRaw(void)
  19159. {
  19160. EXPECT_DECLS;
  19161. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19162. DsaKey key;
  19163. WC_RNG rng;
  19164. byte xOut[MAX_DSA_PARAM_SIZE];
  19165. byte yOut[MAX_DSA_PARAM_SIZE];
  19166. word32 xOutSz, yOutSz;
  19167. XMEMSET(&key, 0, sizeof(key));
  19168. XMEMSET(&rng, 0, sizeof(rng));
  19169. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19170. ExpectIntEQ(wc_InitRng(&rng), 0);
  19171. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19172. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19173. /* try successful export */
  19174. xOutSz = sizeof(xOut);
  19175. yOutSz = sizeof(yOut);
  19176. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19177. /* test bad args */
  19178. /* null key struct */
  19179. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19180. BAD_FUNC_ARG);
  19181. /* null output pointers */
  19182. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19183. LENGTH_ONLY_E);
  19184. /* null output size pointers */
  19185. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19186. BAD_FUNC_ARG);
  19187. /* x output buffer size too small */
  19188. xOutSz = 1;
  19189. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19190. BUFFER_E);
  19191. xOutSz = sizeof(xOut);
  19192. /* y output buffer size too small */
  19193. yOutSz = 1;
  19194. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19195. BUFFER_E);
  19196. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19197. wc_FreeDsaKey(&key);
  19198. #endif
  19199. return EXPECT_RESULT();
  19200. } /* END test_wc_DsaExportParamsRaw */
  19201. /*
  19202. * Testing wc_ed25519_make_key().
  19203. */
  19204. static int test_wc_ed25519_make_key(void)
  19205. {
  19206. EXPECT_DECLS;
  19207. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19208. ed25519_key key;
  19209. WC_RNG rng;
  19210. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19211. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19212. XMEMSET(&key, 0, sizeof(ed25519_key));
  19213. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19214. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19215. ExpectIntEQ(wc_InitRng(&rng), 0);
  19216. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19217. ECC_PRIV_KEY_E);
  19218. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19219. ECC_PRIV_KEY_E);
  19220. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19221. /* Test bad args. */
  19222. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19223. BAD_FUNC_ARG);
  19224. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19225. BAD_FUNC_ARG);
  19226. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19227. BAD_FUNC_ARG);
  19228. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19229. BAD_FUNC_ARG);
  19230. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19231. wc_ed25519_free(&key);
  19232. #endif
  19233. return EXPECT_RESULT();
  19234. } /* END test_wc_ed25519_make_key */
  19235. /*
  19236. * Testing wc_ed25519_init()
  19237. */
  19238. static int test_wc_ed25519_init(void)
  19239. {
  19240. EXPECT_DECLS;
  19241. #if defined(HAVE_ED25519)
  19242. ed25519_key key;
  19243. XMEMSET(&key, 0, sizeof(ed25519_key));
  19244. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19245. /* Test bad args. */
  19246. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19247. wc_ed25519_free(&key);
  19248. #endif
  19249. return EXPECT_RESULT();
  19250. } /* END test_wc_ed25519_init */
  19251. /*
  19252. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19253. */
  19254. static int test_wc_ed25519_sign_msg(void)
  19255. {
  19256. EXPECT_DECLS;
  19257. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19258. WC_RNG rng;
  19259. ed25519_key key;
  19260. byte msg[] = "Everybody gets Friday off.\n";
  19261. byte sig[ED25519_SIG_SIZE+1];
  19262. word32 msglen = sizeof(msg);
  19263. word32 siglen = ED25519_SIG_SIZE;
  19264. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19265. #ifdef HAVE_ED25519_VERIFY
  19266. int verify_ok = 0; /*1 = Verify success.*/
  19267. #endif
  19268. /* Initialize stack variables. */
  19269. XMEMSET(&key, 0, sizeof(ed25519_key));
  19270. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19271. XMEMSET(sig, 0, sizeof(sig));
  19272. /* Initialize key. */
  19273. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19274. ExpectIntEQ(wc_InitRng(&rng), 0);
  19275. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19276. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19277. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19278. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19279. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19280. /* Test bad args. */
  19281. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19282. BAD_FUNC_ARG);
  19283. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19284. BAD_FUNC_ARG);
  19285. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19286. BAD_FUNC_ARG);
  19287. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19288. BAD_FUNC_ARG);
  19289. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19290. BUFFER_E);
  19291. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19292. badSigLen -= 1;
  19293. #ifdef HAVE_ED25519_VERIFY
  19294. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19295. &key), 0);
  19296. ExpectIntEQ(verify_ok, 1);
  19297. /* Test bad args. */
  19298. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19299. &verify_ok, &key), BAD_FUNC_ARG);
  19300. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19301. &verify_ok, &key), BAD_FUNC_ARG);
  19302. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19303. &key), BAD_FUNC_ARG);
  19304. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19305. &key), BAD_FUNC_ARG);
  19306. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19307. BAD_FUNC_ARG);
  19308. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19309. NULL), BAD_FUNC_ARG);
  19310. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19311. &key), BAD_FUNC_ARG);
  19312. #endif /* Verify. */
  19313. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19314. wc_ed25519_free(&key);
  19315. #endif
  19316. return EXPECT_RESULT();
  19317. } /* END test_wc_ed25519_sign_msg */
  19318. /*
  19319. * Testing wc_ed25519_import_public()
  19320. */
  19321. static int test_wc_ed25519_import_public(void)
  19322. {
  19323. EXPECT_DECLS;
  19324. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19325. ed25519_key pubKey;
  19326. WC_RNG rng;
  19327. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19328. word32 inlen = sizeof(in);
  19329. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19330. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19331. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19332. ExpectIntEQ(wc_InitRng(&rng), 0);
  19333. #ifdef HAVE_ED25519_MAKE_KEY
  19334. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19335. #endif
  19336. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19337. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19338. /* Test bad args. */
  19339. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19340. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19341. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19342. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19343. wc_ed25519_free(&pubKey);
  19344. #endif
  19345. return EXPECT_RESULT();
  19346. } /* END wc_ed25519_import_public */
  19347. /*
  19348. * Testing wc_ed25519_import_private_key()
  19349. */
  19350. static int test_wc_ed25519_import_private_key(void)
  19351. {
  19352. EXPECT_DECLS;
  19353. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19354. ed25519_key key;
  19355. WC_RNG rng;
  19356. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19357. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19358. word32 privKeySz = sizeof(privKey);
  19359. word32 pubKeySz = sizeof(pubKey);
  19360. #ifdef HAVE_ED25519_KEY_EXPORT
  19361. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19362. word32 bothKeysSz = sizeof(bothKeys);
  19363. #endif
  19364. XMEMSET(&key, 0, sizeof(ed25519_key));
  19365. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19366. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19367. ExpectIntEQ(wc_InitRng(&rng), 0);
  19368. #ifdef HAVE_ED25519_MAKE_KEY
  19369. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19370. #endif
  19371. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19372. pubKeySz, &key, 1), 0);
  19373. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19374. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19375. #ifdef HAVE_ED25519_KEY_EXPORT
  19376. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19377. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19378. &key, 1), 0);
  19379. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19380. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19381. #endif
  19382. /* Test bad args. */
  19383. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19384. &key), BAD_FUNC_ARG);
  19385. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19386. pubKeySz, &key), BAD_FUNC_ARG);
  19387. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19388. pubKeySz, NULL), BAD_FUNC_ARG);
  19389. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19390. pubKeySz, &key), BAD_FUNC_ARG);
  19391. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19392. pubKeySz - 1, &key), BAD_FUNC_ARG);
  19393. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  19394. &key), BAD_FUNC_ARG);
  19395. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19396. wc_ed25519_free(&key);
  19397. #endif
  19398. return EXPECT_RESULT();
  19399. } /* END test_wc_ed25519_import_private_key */
  19400. /*
  19401. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19402. */
  19403. static int test_wc_ed25519_export(void)
  19404. {
  19405. EXPECT_DECLS;
  19406. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19407. ed25519_key key;
  19408. WC_RNG rng;
  19409. byte priv[ED25519_PRV_KEY_SIZE];
  19410. byte pub[ED25519_PUB_KEY_SIZE];
  19411. word32 privSz = sizeof(priv);
  19412. word32 pubSz = sizeof(pub);
  19413. #ifndef HAVE_ED25519_MAKE_KEY
  19414. const byte privKey[] = {
  19415. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19416. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19417. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19418. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19419. };
  19420. const byte pubKey[] = {
  19421. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19422. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19423. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19424. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19425. };
  19426. #endif
  19427. XMEMSET(&key, 0, sizeof(ed25519_key));
  19428. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19429. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19430. ExpectIntEQ(wc_InitRng(&rng), 0);
  19431. #ifdef HAVE_ED25519_MAKE_KEY
  19432. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19433. #else
  19434. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19435. pubKey, sizeof(pubKey), &key, 1), 0);
  19436. #endif
  19437. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  19438. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  19439. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  19440. /* Test bad args. */
  19441. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  19442. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  19443. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  19444. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  19445. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  19446. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  19447. /* Test bad args. */
  19448. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  19449. BAD_FUNC_ARG);
  19450. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  19451. BAD_FUNC_ARG);
  19452. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  19453. BAD_FUNC_ARG);
  19454. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19455. wc_ed25519_free(&key);
  19456. #endif
  19457. return EXPECT_RESULT();
  19458. } /* END test_wc_ed25519_export */
  19459. /*
  19460. * Testing wc_ed25519_size()
  19461. */
  19462. static int test_wc_ed25519_size(void)
  19463. {
  19464. EXPECT_DECLS;
  19465. #if defined(HAVE_ED25519)
  19466. ed25519_key key;
  19467. WC_RNG rng;
  19468. #ifndef HAVE_ED25519_MAKE_KEY
  19469. const byte privKey[] = {
  19470. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19471. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19472. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19473. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19474. };
  19475. const byte pubKey[] = {
  19476. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19477. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19478. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19479. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19480. };
  19481. #endif
  19482. XMEMSET(&key, 0, sizeof(ed25519_key));
  19483. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19484. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19485. ExpectIntEQ(wc_InitRng(&rng), 0);
  19486. #ifdef HAVE_ED25519_MAKE_KEY
  19487. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19488. #else
  19489. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19490. pubKey, sizeof(pubKey), &key, 1), 0);
  19491. #endif
  19492. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  19493. /* Test bad args. */
  19494. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  19495. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  19496. /* Test bad args. */
  19497. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  19498. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  19499. /* Test bad args. */
  19500. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  19501. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  19502. /* Test bad args. */
  19503. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  19504. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19505. wc_ed25519_free(&key);
  19506. #endif
  19507. return EXPECT_RESULT();
  19508. } /* END test_wc_ed25519_size */
  19509. /*
  19510. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19511. */
  19512. static int test_wc_ed25519_exportKey(void)
  19513. {
  19514. EXPECT_DECLS;
  19515. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19516. WC_RNG rng;
  19517. ed25519_key key;
  19518. byte priv[ED25519_PRV_KEY_SIZE];
  19519. byte pub[ED25519_PUB_KEY_SIZE];
  19520. byte privOnly[ED25519_PRV_KEY_SIZE];
  19521. word32 privSz = sizeof(priv);
  19522. word32 pubSz = sizeof(pub);
  19523. word32 privOnlySz = sizeof(privOnly);
  19524. #ifndef HAVE_ED25519_MAKE_KEY
  19525. const byte privKey[] = {
  19526. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19527. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19528. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19529. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19530. };
  19531. const byte pubKey[] = {
  19532. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19533. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19534. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19535. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19536. };
  19537. #endif
  19538. XMEMSET(&key, 0, sizeof(ed25519_key));
  19539. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19540. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19541. ExpectIntEQ(wc_InitRng(&rng), 0);
  19542. #ifdef HAVE_ED25519_MAKE_KEY
  19543. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19544. #else
  19545. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19546. pubKey, sizeof(pubKey), &key, 1), 0);
  19547. #endif
  19548. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  19549. /* Test bad args. */
  19550. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  19551. BAD_FUNC_ARG);
  19552. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  19553. BAD_FUNC_ARG);
  19554. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  19555. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  19556. /* Test bad args. */
  19557. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  19558. BAD_FUNC_ARG);
  19559. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  19560. BAD_FUNC_ARG);
  19561. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  19562. BAD_FUNC_ARG);
  19563. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  19564. BAD_FUNC_ARG);
  19565. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  19566. BAD_FUNC_ARG);
  19567. /* Cross check output. */
  19568. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  19569. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19570. wc_ed25519_free(&key);
  19571. #endif
  19572. return EXPECT_RESULT();
  19573. } /* END test_wc_ed25519_exportKey */
  19574. /*
  19575. * Testing wc_Ed25519PublicKeyToDer
  19576. */
  19577. static int test_wc_Ed25519PublicKeyToDer(void)
  19578. {
  19579. EXPECT_DECLS;
  19580. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19581. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19582. ed25519_key key;
  19583. byte derBuf[1024];
  19584. XMEMSET(&key, 0, sizeof(ed25519_key));
  19585. /* Test bad args */
  19586. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  19587. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19588. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  19589. wc_ed25519_free(&key);
  19590. /* Test good args */
  19591. if (EXPECT_SUCCESS()) {
  19592. WC_RNG rng;
  19593. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19594. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19595. ExpectIntEQ(wc_InitRng(&rng), 0);
  19596. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19597. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  19598. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19599. wc_ed25519_free(&key);
  19600. }
  19601. #endif
  19602. return EXPECT_RESULT();
  19603. } /* END testing wc_Ed25519PublicKeyToDer */
  19604. /*
  19605. * Testing wc_curve25519_init and wc_curve25519_free.
  19606. */
  19607. static int test_wc_curve25519_init(void)
  19608. {
  19609. EXPECT_DECLS;
  19610. #if defined(HAVE_CURVE25519)
  19611. curve25519_key key;
  19612. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19613. /* Test bad args for wc_curve25519_init */
  19614. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  19615. /* Test good args for wc_curve_25519_free */
  19616. wc_curve25519_free(&key);
  19617. /* Test bad args for wc_curve25519 free. */
  19618. wc_curve25519_free(NULL);
  19619. #endif
  19620. return EXPECT_RESULT();
  19621. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19622. /*
  19623. * Testing test_wc_curve25519_size.
  19624. */
  19625. static int test_wc_curve25519_size(void)
  19626. {
  19627. EXPECT_DECLS;
  19628. #if defined(HAVE_CURVE25519)
  19629. curve25519_key key;
  19630. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19631. /* Test good args for wc_curve25519_size */
  19632. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19633. /* Test bad args for wc_curve25519_size */
  19634. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  19635. wc_curve25519_free(&key);
  19636. #endif
  19637. return EXPECT_RESULT();
  19638. } /* END test_wc_curve25519_size*/
  19639. /*
  19640. * Testing test_wc_curve25519_export_key_raw().
  19641. */
  19642. static int test_wc_curve25519_export_key_raw(void)
  19643. {
  19644. EXPECT_DECLS;
  19645. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19646. curve25519_key key;
  19647. WC_RNG rng;
  19648. byte privateKey[CURVE25519_KEYSIZE];
  19649. byte publicKey[CURVE25519_KEYSIZE];
  19650. word32 prvkSz;
  19651. word32 pubkSz;
  19652. byte prik[CURVE25519_KEYSIZE];
  19653. byte pubk[CURVE25519_KEYSIZE];
  19654. word32 prksz;
  19655. word32 pbksz;
  19656. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19657. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19658. ExpectIntEQ(wc_InitRng(&rng), 0);
  19659. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19660. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19661. prvkSz = CURVE25519_KEYSIZE;
  19662. pubkSz = CURVE25519_KEYSIZE;
  19663. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  19664. publicKey, &pubkSz), BAD_FUNC_ARG);
  19665. prvkSz = CURVE25519_KEYSIZE;
  19666. pubkSz = CURVE25519_KEYSIZE;
  19667. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  19668. &pubkSz), BAD_FUNC_ARG);
  19669. prvkSz = CURVE25519_KEYSIZE;
  19670. pubkSz = CURVE25519_KEYSIZE;
  19671. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  19672. publicKey, &pubkSz), BAD_FUNC_ARG);
  19673. /* prvkSz = CURVE25519_KEYSIZE; */
  19674. pubkSz = CURVE25519_KEYSIZE;
  19675. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19676. NULL, &pubkSz), BAD_FUNC_ARG);
  19677. prvkSz = CURVE25519_KEYSIZE;
  19678. pubkSz = CURVE25519_KEYSIZE;
  19679. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19680. publicKey, NULL), BAD_FUNC_ARG);
  19681. /* cross-testing */
  19682. prksz = CURVE25519_KEYSIZE;
  19683. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  19684. pbksz = CURVE25519_KEYSIZE;
  19685. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  19686. prvkSz = CURVE25519_KEYSIZE;
  19687. /* pubkSz = CURVE25519_KEYSIZE; */
  19688. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19689. publicKey, &pubkSz), 0);
  19690. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19691. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19692. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19693. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19694. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19695. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19696. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19697. wc_curve25519_free(&key);
  19698. #endif
  19699. return EXPECT_RESULT();
  19700. } /* end of test_wc_curve25519_export_key_raw */
  19701. /*
  19702. * Testing test_wc_curve25519_export_key_raw_ex().
  19703. */
  19704. static int test_wc_curve25519_export_key_raw_ex(void)
  19705. {
  19706. EXPECT_DECLS;
  19707. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19708. curve25519_key key;
  19709. WC_RNG rng;
  19710. byte privateKey[CURVE25519_KEYSIZE];
  19711. byte publicKey[CURVE25519_KEYSIZE];
  19712. word32 prvkSz;
  19713. word32 pubkSz;
  19714. byte prik[CURVE25519_KEYSIZE];
  19715. byte pubk[CURVE25519_KEYSIZE];
  19716. word32 prksz;
  19717. word32 pbksz;
  19718. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19719. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19720. ExpectIntEQ(wc_InitRng(&rng), 0);
  19721. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19722. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19723. prvkSz = CURVE25519_KEYSIZE;
  19724. pubkSz = CURVE25519_KEYSIZE;
  19725. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19726. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19727. prvkSz = CURVE25519_KEYSIZE;
  19728. pubkSz = CURVE25519_KEYSIZE;
  19729. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19730. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19731. prvkSz = CURVE25519_KEYSIZE;
  19732. pubkSz = CURVE25519_KEYSIZE;
  19733. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19734. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19735. /* prvkSz = CURVE25519_KEYSIZE; */
  19736. pubkSz = CURVE25519_KEYSIZE;
  19737. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19738. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19739. prvkSz = CURVE25519_KEYSIZE;
  19740. pubkSz = CURVE25519_KEYSIZE;
  19741. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19742. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19743. prvkSz = CURVE25519_KEYSIZE;
  19744. /* pubkSz = CURVE25519_KEYSIZE; */
  19745. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19746. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19747. prvkSz = CURVE25519_KEYSIZE;
  19748. pubkSz = CURVE25519_KEYSIZE;
  19749. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19750. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19751. prvkSz = CURVE25519_KEYSIZE;
  19752. pubkSz = CURVE25519_KEYSIZE;
  19753. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19754. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19755. /* prvkSz = CURVE25519_KEYSIZE; */
  19756. pubkSz = CURVE25519_KEYSIZE;
  19757. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19758. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19759. prvkSz = CURVE25519_KEYSIZE;
  19760. pubkSz = CURVE25519_KEYSIZE;
  19761. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19762. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19763. /* illegal value for endian */
  19764. prvkSz = CURVE25519_KEYSIZE;
  19765. /* pubkSz = CURVE25519_KEYSIZE; */
  19766. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19767. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  19768. /* cross-testing */
  19769. prksz = CURVE25519_KEYSIZE;
  19770. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  19771. pbksz = CURVE25519_KEYSIZE;
  19772. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  19773. prvkSz = CURVE25519_KEYSIZE;
  19774. /* pubkSz = CURVE25519_KEYSIZE; */
  19775. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19776. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19777. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19778. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19779. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19780. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19781. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19782. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19783. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19784. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  19785. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19786. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19787. /* try once with another endian */
  19788. prvkSz = CURVE25519_KEYSIZE;
  19789. pubkSz = CURVE25519_KEYSIZE;
  19790. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19791. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19792. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19793. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19794. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19795. wc_curve25519_free(&key);
  19796. #endif
  19797. return EXPECT_RESULT();
  19798. } /* end of test_wc_curve25519_export_key_raw_ex */
  19799. /*
  19800. * Testing wc_curve25519_make_key
  19801. */
  19802. static int test_wc_curve25519_make_key(void)
  19803. {
  19804. EXPECT_DECLS;
  19805. #if defined(HAVE_CURVE25519)
  19806. curve25519_key key;
  19807. WC_RNG rng;
  19808. int keysize;
  19809. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19810. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19811. ExpectIntEQ(wc_InitRng(&rng), 0);
  19812. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19813. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19814. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  19815. /* test bad cases*/
  19816. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  19817. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  19818. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  19819. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  19820. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19821. wc_curve25519_free(&key);
  19822. #endif
  19823. return EXPECT_RESULT();
  19824. } /* END test_wc_curve25519_make_key*/
  19825. /*
  19826. * Testing wc_curve25519_shared_secret_ex
  19827. */
  19828. static int test_wc_curve25519_shared_secret_ex(void)
  19829. {
  19830. EXPECT_DECLS;
  19831. #if defined(HAVE_CURVE25519)
  19832. curve25519_key private_key;
  19833. curve25519_key public_key;
  19834. WC_RNG rng;
  19835. byte out[CURVE25519_KEYSIZE];
  19836. word32 outLen = sizeof(out);
  19837. int endian = EC25519_BIG_ENDIAN;
  19838. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  19839. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  19840. ExpectIntEQ(wc_InitRng(&rng), 0);
  19841. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  19842. 0);
  19843. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  19844. 0);
  19845. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19846. &outLen, endian), 0);
  19847. /* test bad cases*/
  19848. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  19849. BAD_FUNC_ARG);
  19850. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  19851. endian), BAD_FUNC_ARG);
  19852. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  19853. endian), BAD_FUNC_ARG);
  19854. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19855. &outLen, endian), BAD_FUNC_ARG);
  19856. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19857. NULL, endian), BAD_FUNC_ARG);
  19858. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  19859. * increasing to 0x8f checks for error being returned*/
  19860. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19861. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19862. &outLen, endian), ECC_BAD_ARG_E);
  19863. outLen = outLen - 2;
  19864. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19865. &outLen, endian), BAD_FUNC_ARG);
  19866. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19867. wc_curve25519_free(&private_key);
  19868. wc_curve25519_free(&public_key);
  19869. #endif
  19870. return EXPECT_RESULT();
  19871. } /* END test_wc_curve25519_shared_secret_ex*/
  19872. /*
  19873. * Testing wc_curve25519_make_pub
  19874. */
  19875. static int test_wc_curve25519_make_pub(void)
  19876. {
  19877. EXPECT_DECLS;
  19878. #ifdef HAVE_CURVE25519
  19879. curve25519_key key;
  19880. WC_RNG rng;
  19881. byte out[CURVE25519_KEYSIZE];
  19882. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19883. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19884. ExpectIntEQ(wc_InitRng(&rng), 0);
  19885. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19886. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  19887. (int)sizeof(key.k), key.k), 0);
  19888. /* test bad cases*/
  19889. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  19890. (int)sizeof out, out), ECC_BAD_ARG_E);
  19891. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19892. NULL), ECC_BAD_ARG_E);
  19893. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  19894. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19895. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  19896. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19897. /* verify clamping test */
  19898. key.k[0] |= ~248;
  19899. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19900. key.k), ECC_BAD_ARG_E);
  19901. key.k[0] &= 248;
  19902. /* repeat the expected-to-succeed test. */
  19903. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19904. key.k), 0);
  19905. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19906. wc_curve25519_free(&key);
  19907. #endif
  19908. return EXPECT_RESULT();
  19909. } /* END test_wc_curve25519_make_pub */
  19910. /*
  19911. * Testing test_wc_curve25519_export_public_ex
  19912. */
  19913. static int test_wc_curve25519_export_public_ex(void)
  19914. {
  19915. EXPECT_DECLS;
  19916. #if defined(HAVE_CURVE25519)
  19917. curve25519_key key;
  19918. WC_RNG rng;
  19919. byte out[CURVE25519_KEYSIZE];
  19920. word32 outLen = sizeof(out);
  19921. int endian = EC25519_BIG_ENDIAN;
  19922. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19923. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19924. ExpectIntEQ(wc_InitRng(&rng), 0);
  19925. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19926. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  19927. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  19928. /* test bad cases*/
  19929. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  19930. BAD_FUNC_ARG);
  19931. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  19932. BAD_FUNC_ARG);
  19933. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  19934. BAD_FUNC_ARG);
  19935. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  19936. BAD_FUNC_ARG);
  19937. outLen = outLen - 2;
  19938. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  19939. ECC_BAD_ARG_E);
  19940. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19941. wc_curve25519_free(&key);
  19942. #endif
  19943. return EXPECT_RESULT();
  19944. } /* END test_wc_curve25519_export_public_ex*/
  19945. /*
  19946. * Testing test_wc_curve25519_import_private_raw_ex
  19947. */
  19948. static int test_wc_curve25519_import_private_raw_ex(void)
  19949. {
  19950. EXPECT_DECLS;
  19951. #if defined(HAVE_CURVE25519)
  19952. curve25519_key key;
  19953. WC_RNG rng;
  19954. byte priv[CURVE25519_KEYSIZE];
  19955. byte pub[CURVE25519_KEYSIZE];
  19956. word32 privSz = sizeof(priv);
  19957. word32 pubSz = sizeof(pub);
  19958. int endian = EC25519_BIG_ENDIAN;
  19959. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19960. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19961. ExpectIntEQ(wc_InitRng(&rng), 0);
  19962. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19963. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  19964. endian), 0);
  19965. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  19966. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19967. &key, endian), 0);
  19968. /* test bad cases*/
  19969. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19970. endian), BAD_FUNC_ARG);
  19971. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19972. &key, endian), BAD_FUNC_ARG);
  19973. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19974. &key, endian), BAD_FUNC_ARG);
  19975. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19976. NULL, endian), BAD_FUNC_ARG);
  19977. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19978. &key, endian), ECC_BAD_ARG_E);
  19979. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19980. &key, endian), ECC_BAD_ARG_E);
  19981. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19982. &key, EC25519_LITTLE_ENDIAN), 0);
  19983. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19984. wc_curve25519_free(&key);
  19985. #endif
  19986. return EXPECT_RESULT();
  19987. } /* END test_wc_curve25519_import_private_raw_ex*/
  19988. /*
  19989. * Testing test_wc_curve25519_import_private
  19990. */
  19991. static int test_wc_curve25519_import_private(void)
  19992. {
  19993. EXPECT_DECLS;
  19994. #if defined(HAVE_CURVE25519)
  19995. curve25519_key key;
  19996. WC_RNG rng;
  19997. byte priv[CURVE25519_KEYSIZE];
  19998. word32 privSz = sizeof(priv);
  19999. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20000. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20001. ExpectIntEQ(wc_InitRng(&rng), 0);
  20002. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20003. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20004. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20005. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20006. wc_curve25519_free(&key);
  20007. #endif
  20008. return EXPECT_RESULT();
  20009. } /* END test_wc_curve25519_import*/
  20010. /*
  20011. * Testing test_wc_curve25519_export_private_raw_ex
  20012. */
  20013. static int test_wc_curve25519_export_private_raw_ex(void)
  20014. {
  20015. EXPECT_DECLS;
  20016. #if defined(HAVE_CURVE25519)
  20017. curve25519_key key;
  20018. byte out[CURVE25519_KEYSIZE];
  20019. word32 outLen = sizeof(out);
  20020. int endian = EC25519_BIG_ENDIAN;
  20021. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20022. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20023. 0);
  20024. /* test bad cases*/
  20025. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20026. BAD_FUNC_ARG);
  20027. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20028. BAD_FUNC_ARG);
  20029. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20030. endian), BAD_FUNC_ARG);
  20031. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20032. BAD_FUNC_ARG);
  20033. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20034. EC25519_LITTLE_ENDIAN), 0);
  20035. outLen = outLen - 2;
  20036. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20037. ECC_BAD_ARG_E);
  20038. wc_curve25519_free(&key);
  20039. #endif
  20040. return EXPECT_RESULT();
  20041. } /* END test_wc_curve25519_export_private_raw_ex*/
  20042. /*
  20043. * Testing wc_ed448_make_key().
  20044. */
  20045. static int test_wc_ed448_make_key(void)
  20046. {
  20047. EXPECT_DECLS;
  20048. #if defined(HAVE_ED448)
  20049. ed448_key key;
  20050. WC_RNG rng;
  20051. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20052. XMEMSET(&key, 0, sizeof(ed448_key));
  20053. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20054. ExpectIntEQ(wc_ed448_init(&key), 0);
  20055. ExpectIntEQ(wc_InitRng(&rng), 0);
  20056. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20057. ECC_PRIV_KEY_E);
  20058. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20059. /* Test bad args. */
  20060. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20061. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20062. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20063. BAD_FUNC_ARG);
  20064. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20065. BAD_FUNC_ARG);
  20066. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20067. wc_ed448_free(&key);
  20068. #endif
  20069. return EXPECT_RESULT();
  20070. } /* END test_wc_ed448_make_key */
  20071. /*
  20072. * Testing wc_ed448_init()
  20073. */
  20074. static int test_wc_ed448_init(void)
  20075. {
  20076. EXPECT_DECLS;
  20077. #if defined(HAVE_ED448)
  20078. ed448_key key;
  20079. XMEMSET(&key, 0, sizeof(ed448_key));
  20080. ExpectIntEQ(wc_ed448_init(&key), 0);
  20081. /* Test bad args. */
  20082. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20083. wc_ed448_free(&key);
  20084. #endif
  20085. return EXPECT_RESULT();
  20086. } /* END test_wc_ed448_init */
  20087. /*
  20088. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20089. */
  20090. static int test_wc_ed448_sign_msg(void)
  20091. {
  20092. EXPECT_DECLS;
  20093. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20094. ed448_key key;
  20095. WC_RNG rng;
  20096. byte msg[] = "Everybody gets Friday off.\n";
  20097. byte sig[ED448_SIG_SIZE];
  20098. word32 msglen = sizeof(msg);
  20099. word32 siglen = sizeof(sig);
  20100. word32 badSigLen = sizeof(sig) - 1;
  20101. #ifdef HAVE_ED448_VERIFY
  20102. int verify_ok = 0; /*1 = Verify success.*/
  20103. #endif
  20104. /* Initialize stack variables. */
  20105. XMEMSET(&key, 0, sizeof(ed448_key));
  20106. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20107. XMEMSET(sig, 0, siglen);
  20108. /* Initialize key. */
  20109. ExpectIntEQ(wc_ed448_init(&key), 0);
  20110. ExpectIntEQ(wc_InitRng(&rng), 0);
  20111. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20112. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20113. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20114. /* Test bad args. */
  20115. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20116. BAD_FUNC_ARG);
  20117. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20118. BAD_FUNC_ARG);
  20119. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20120. BAD_FUNC_ARG);
  20121. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20122. BAD_FUNC_ARG);
  20123. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20124. BUFFER_E);
  20125. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20126. badSigLen -= 1;
  20127. #ifdef HAVE_ED448_VERIFY
  20128. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20129. NULL, 0), 0);
  20130. ExpectIntEQ(verify_ok, 1);
  20131. /* Test bad args. */
  20132. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20133. &key, NULL, 0), BAD_FUNC_ARG);
  20134. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20135. &key, NULL, 0), BAD_FUNC_ARG);
  20136. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20137. &key, NULL, 0), BAD_FUNC_ARG);
  20138. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20139. &key, NULL, 0), BAD_FUNC_ARG);
  20140. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20141. &key, NULL, 0), BAD_FUNC_ARG);
  20142. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20143. NULL, NULL, 0), BAD_FUNC_ARG);
  20144. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20145. &key, NULL, 0), BAD_FUNC_ARG);
  20146. #endif /* Verify. */
  20147. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20148. wc_ed448_free(&key);
  20149. #endif
  20150. return EXPECT_RESULT();
  20151. } /* END test_wc_ed448_sign_msg */
  20152. /*
  20153. * Testing wc_ed448_import_public()
  20154. */
  20155. static int test_wc_ed448_import_public(void)
  20156. {
  20157. EXPECT_DECLS;
  20158. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20159. ed448_key pubKey;
  20160. WC_RNG rng;
  20161. const byte in[] =
  20162. "Ed448PublicKeyUnitTest.................................\n";
  20163. word32 inlen = sizeof(in);
  20164. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20165. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20166. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20167. ExpectIntEQ(wc_InitRng(&rng), 0);
  20168. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20169. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20170. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20171. /* Test bad args. */
  20172. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20173. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20174. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20175. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20176. wc_ed448_free(&pubKey);
  20177. #endif
  20178. return EXPECT_RESULT();
  20179. } /* END wc_ed448_import_public */
  20180. /*
  20181. * Testing wc_ed448_import_private_key()
  20182. */
  20183. static int test_wc_ed448_import_private_key(void)
  20184. {
  20185. EXPECT_DECLS;
  20186. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20187. ed448_key key;
  20188. WC_RNG rng;
  20189. const byte privKey[] =
  20190. "Ed448PrivateKeyUnitTest................................\n";
  20191. const byte pubKey[] =
  20192. "Ed448PublicKeyUnitTest.................................\n";
  20193. word32 privKeySz = sizeof(privKey);
  20194. word32 pubKeySz = sizeof(pubKey);
  20195. #ifdef HAVE_ED448_KEY_EXPORT
  20196. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20197. word32 bothKeysSz = sizeof(bothKeys);
  20198. #endif
  20199. XMEMSET(&key, 0, sizeof(ed448_key));
  20200. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20201. ExpectIntEQ(wc_ed448_init(&key), 0);
  20202. ExpectIntEQ(wc_InitRng(&rng), 0);
  20203. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20204. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20205. pubKeySz, &key, 1), 0);
  20206. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20207. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20208. #ifdef HAVE_ED448_KEY_EXPORT
  20209. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20210. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20211. &key, 1), 0);
  20212. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20213. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20214. #endif
  20215. /* Test bad args. */
  20216. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20217. &key), BAD_FUNC_ARG);
  20218. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20219. &key), BAD_FUNC_ARG);
  20220. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20221. pubKeySz, NULL), BAD_FUNC_ARG);
  20222. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20223. pubKeySz, &key), BAD_FUNC_ARG);
  20224. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20225. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20226. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20227. BAD_FUNC_ARG);
  20228. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20229. wc_ed448_free(&key);
  20230. #endif
  20231. return EXPECT_RESULT();
  20232. } /* END test_wc_ed448_import_private_key */
  20233. /*
  20234. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20235. */
  20236. static int test_wc_ed448_export(void)
  20237. {
  20238. EXPECT_DECLS;
  20239. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20240. ed448_key key;
  20241. WC_RNG rng;
  20242. byte priv[ED448_PRV_KEY_SIZE];
  20243. byte pub[ED448_PUB_KEY_SIZE];
  20244. word32 privSz = sizeof(priv);
  20245. word32 pubSz = sizeof(pub);
  20246. XMEMSET(&key, 0, sizeof(ed448_key));
  20247. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20248. ExpectIntEQ(wc_ed448_init(&key), 0);
  20249. ExpectIntEQ(wc_InitRng(&rng), 0);
  20250. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20251. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20252. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20253. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20254. /* Test bad args. */
  20255. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20256. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20257. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20258. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20259. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20260. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20261. /* Test bad args. */
  20262. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20263. BAD_FUNC_ARG);
  20264. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20265. BAD_FUNC_ARG);
  20266. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20267. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20268. wc_ed448_free(&key);
  20269. #endif
  20270. return EXPECT_RESULT();
  20271. } /* END test_wc_ed448_export */
  20272. /*
  20273. * Testing wc_ed448_size()
  20274. */
  20275. static int test_wc_ed448_size(void)
  20276. {
  20277. EXPECT_DECLS;
  20278. #if defined(HAVE_ED448)
  20279. ed448_key key;
  20280. WC_RNG rng;
  20281. XMEMSET(&key, 0, sizeof(ed448_key));
  20282. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20283. ExpectIntEQ(wc_ed448_init(&key), 0);
  20284. ExpectIntEQ(wc_InitRng(&rng), 0);
  20285. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20286. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20287. /* Test bad args. */
  20288. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20289. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20290. /* Test bad args. */
  20291. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20292. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20293. /* Test bad args. */
  20294. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20295. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20296. /* Test bad args. */
  20297. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20298. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20299. wc_ed448_free(&key);
  20300. #endif
  20301. return EXPECT_RESULT();
  20302. } /* END test_wc_ed448_size */
  20303. /*
  20304. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20305. */
  20306. static int test_wc_ed448_exportKey(void)
  20307. {
  20308. EXPECT_DECLS;
  20309. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20310. ed448_key key;
  20311. WC_RNG rng;
  20312. byte priv[ED448_PRV_KEY_SIZE];
  20313. byte pub[ED448_PUB_KEY_SIZE];
  20314. byte privOnly[ED448_PRV_KEY_SIZE];
  20315. word32 privSz = sizeof(priv);
  20316. word32 pubSz = sizeof(pub);
  20317. word32 privOnlySz = sizeof(privOnly);
  20318. XMEMSET(&key, 0, sizeof(ed448_key));
  20319. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20320. ExpectIntEQ(wc_ed448_init(&key), 0);
  20321. ExpectIntEQ(wc_InitRng(&rng), 0);
  20322. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20323. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20324. /* Test bad args. */
  20325. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20326. BAD_FUNC_ARG);
  20327. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20328. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20329. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20330. /* Test bad args. */
  20331. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20332. BAD_FUNC_ARG);
  20333. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20334. BAD_FUNC_ARG);
  20335. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20336. BAD_FUNC_ARG);
  20337. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20338. BAD_FUNC_ARG);
  20339. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20340. BAD_FUNC_ARG);
  20341. /* Cross check output. */
  20342. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20343. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20344. wc_ed448_free(&key);
  20345. #endif
  20346. return EXPECT_RESULT();
  20347. } /* END test_wc_ed448_exportKey */
  20348. /*
  20349. * Testing wc_Ed448PublicKeyToDer
  20350. */
  20351. static int test_wc_Ed448PublicKeyToDer(void)
  20352. {
  20353. EXPECT_DECLS;
  20354. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20355. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20356. ed448_key key;
  20357. byte derBuf[1024];
  20358. XMEMSET(&key, 0, sizeof(ed448_key));
  20359. /* Test bad args */
  20360. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20361. ExpectIntEQ(wc_ed448_init(&key), 0);
  20362. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20363. wc_ed448_free(&key);
  20364. /* Test good args */
  20365. if (EXPECT_SUCCESS()) {
  20366. WC_RNG rng;
  20367. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20368. ExpectIntEQ(wc_ed448_init(&key), 0);
  20369. ExpectIntEQ(wc_InitRng(&rng), 0);
  20370. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20371. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20372. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20373. wc_ed448_free(&key);
  20374. }
  20375. #endif
  20376. return EXPECT_RESULT();
  20377. } /* END testing wc_Ed448PublicKeyToDer */
  20378. /*
  20379. * Testing wc_curve448_init and wc_curve448_free.
  20380. */
  20381. static int test_wc_curve448_init(void)
  20382. {
  20383. EXPECT_DECLS;
  20384. #if defined(HAVE_CURVE448)
  20385. curve448_key key;
  20386. /* Test bad args for wc_curve448_init */
  20387. ExpectIntEQ(wc_curve448_init(&key), 0);
  20388. /* Test bad args for wc_curve448_init */
  20389. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  20390. /* Test good args for wc_curve_448_free */
  20391. wc_curve448_free(&key);
  20392. /* Test bad args for wc_curve448_free */
  20393. wc_curve448_free(NULL);
  20394. #endif
  20395. return EXPECT_RESULT();
  20396. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20397. /*
  20398. * Testing wc_curve448_make_key
  20399. */
  20400. static int test_wc_curve448_make_key(void)
  20401. {
  20402. EXPECT_DECLS;
  20403. #if defined(HAVE_CURVE448)
  20404. curve448_key key;
  20405. WC_RNG rng;
  20406. int keysize;
  20407. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20408. ExpectIntEQ(wc_curve448_init(&key), 0);
  20409. ExpectIntEQ(wc_InitRng(&rng), 0);
  20410. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20411. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20412. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  20413. /* test bad cases */
  20414. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20415. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20416. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20417. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20418. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20419. wc_curve448_free(&key);
  20420. #endif
  20421. return EXPECT_RESULT();
  20422. } /* END test_wc_curve448_make_key*/
  20423. /*
  20424. * Testing test_wc_curve448_shared_secret_ex
  20425. */
  20426. static int test_wc_curve448_shared_secret_ex(void)
  20427. {
  20428. EXPECT_DECLS;
  20429. #if defined(HAVE_CURVE448)
  20430. curve448_key private_key;
  20431. curve448_key public_key;
  20432. WC_RNG rng;
  20433. byte out[CURVE448_KEY_SIZE];
  20434. word32 outLen = sizeof(out);
  20435. int endian = EC448_BIG_ENDIAN;
  20436. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20437. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  20438. ExpectIntEQ(wc_InitRng(&rng), 0);
  20439. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  20440. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  20441. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  20442. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20443. &outLen, endian), 0);
  20444. /* test bad cases */
  20445. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20446. BAD_FUNC_ARG);
  20447. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  20448. endian), BAD_FUNC_ARG);
  20449. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  20450. endian), BAD_FUNC_ARG);
  20451. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20452. &outLen, endian), BAD_FUNC_ARG);
  20453. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20454. NULL, endian), BAD_FUNC_ARG);
  20455. outLen = outLen - 2;
  20456. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20457. &outLen, endian), BAD_FUNC_ARG);
  20458. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20459. wc_curve448_free(&private_key);
  20460. wc_curve448_free(&public_key);
  20461. #endif
  20462. return EXPECT_RESULT();
  20463. } /* END test_wc_curve448_shared_secret_ex*/
  20464. /*
  20465. * Testing test_wc_curve448_export_public_ex
  20466. */
  20467. static int test_wc_curve448_export_public_ex(void)
  20468. {
  20469. EXPECT_DECLS;
  20470. #if defined(HAVE_CURVE448)
  20471. WC_RNG rng;
  20472. curve448_key key;
  20473. byte out[CURVE448_KEY_SIZE];
  20474. word32 outLen = sizeof(out);
  20475. int endian = EC448_BIG_ENDIAN;
  20476. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20477. ExpectIntEQ(wc_curve448_init(&key), 0);
  20478. ExpectIntEQ(wc_InitRng(&rng), 0);
  20479. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20480. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  20481. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  20482. /* test bad cases*/
  20483. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  20484. BAD_FUNC_ARG);
  20485. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  20486. BAD_FUNC_ARG);
  20487. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  20488. BAD_FUNC_ARG);
  20489. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  20490. BAD_FUNC_ARG);
  20491. outLen = outLen - 2;
  20492. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  20493. ECC_BAD_ARG_E);
  20494. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20495. wc_curve448_free(&key);
  20496. #endif
  20497. return EXPECT_RESULT();
  20498. } /* END test_wc_curve448_export_public_ex*/
  20499. /*
  20500. * Testing test_wc_curve448_export_private_raw_ex
  20501. */
  20502. static int test_wc_curve448_export_private_raw_ex(void)
  20503. {
  20504. EXPECT_DECLS;
  20505. #if defined(HAVE_CURVE448)
  20506. curve448_key key;
  20507. byte out[CURVE448_KEY_SIZE];
  20508. word32 outLen = sizeof(out);
  20509. int endian = EC448_BIG_ENDIAN;
  20510. ExpectIntEQ(wc_curve448_init(&key), 0);
  20511. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20512. 0);
  20513. /* test bad cases*/
  20514. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  20515. BAD_FUNC_ARG);
  20516. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  20517. BAD_FUNC_ARG);
  20518. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  20519. BAD_FUNC_ARG);
  20520. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  20521. BAD_FUNC_ARG);
  20522. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20523. EC448_LITTLE_ENDIAN), 0);
  20524. outLen = outLen - 2;
  20525. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20526. ECC_BAD_ARG_E);
  20527. wc_curve448_free(&key);
  20528. #endif
  20529. return EXPECT_RESULT();
  20530. } /* END test_wc_curve448_export_private_raw_ex*/
  20531. /*
  20532. * Testing test_wc_curve448_import_private_raw_ex
  20533. */
  20534. static int test_wc_curve448_import_private_raw_ex(void)
  20535. {
  20536. EXPECT_DECLS;
  20537. #if defined(HAVE_CURVE448)
  20538. curve448_key key;
  20539. WC_RNG rng;
  20540. byte priv[CURVE448_KEY_SIZE];
  20541. byte pub[CURVE448_KEY_SIZE];
  20542. word32 privSz = sizeof(priv);
  20543. word32 pubSz = sizeof(pub);
  20544. int endian = EC448_BIG_ENDIAN;
  20545. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20546. ExpectIntEQ(wc_curve448_init(&key), 0);
  20547. ExpectIntEQ(wc_InitRng(&rng), 0);
  20548. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20549. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20550. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20551. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20552. &key, endian), 0);
  20553. /* test bad cases */
  20554. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  20555. BAD_FUNC_ARG);
  20556. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20557. &key, endian), BAD_FUNC_ARG);
  20558. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20559. &key, endian), BAD_FUNC_ARG);
  20560. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20561. NULL, endian), BAD_FUNC_ARG);
  20562. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20563. &key, endian), ECC_BAD_ARG_E);
  20564. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20565. &key, endian), ECC_BAD_ARG_E);
  20566. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20567. &key, EC448_LITTLE_ENDIAN), 0);
  20568. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20569. wc_curve448_free(&key);
  20570. #endif
  20571. return EXPECT_RESULT();
  20572. } /* END test_wc_curve448_import_private_raw_ex*/
  20573. /*
  20574. * Testing test_curve448_export_key_raw
  20575. */
  20576. static int test_wc_curve448_export_key_raw(void)
  20577. {
  20578. EXPECT_DECLS;
  20579. #if defined(HAVE_CURVE448)
  20580. curve448_key key;
  20581. WC_RNG rng;
  20582. byte priv[CURVE448_KEY_SIZE];
  20583. byte pub[CURVE448_KEY_SIZE];
  20584. word32 privSz = sizeof(priv);
  20585. word32 pubSz = sizeof(pub);
  20586. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20587. ExpectIntEQ(wc_curve448_init(&key), 0);
  20588. ExpectIntEQ(wc_InitRng(&rng), 0);
  20589. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20590. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20591. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20592. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  20593. 0);
  20594. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20595. wc_curve448_free(&key);
  20596. #endif
  20597. return EXPECT_RESULT();
  20598. } /* END test_wc_curve448_import_private_raw_ex*/
  20599. /*
  20600. * Testing test_wc_curve448_import_private
  20601. */
  20602. static int test_wc_curve448_import_private(void)
  20603. {
  20604. EXPECT_DECLS;
  20605. #if defined(HAVE_CURVE448)
  20606. curve448_key key;
  20607. WC_RNG rng;
  20608. byte priv[CURVE448_KEY_SIZE];
  20609. word32 privSz = sizeof(priv);
  20610. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20611. ExpectIntEQ(wc_curve448_init(&key), 0);
  20612. ExpectIntEQ(wc_InitRng(&rng), 0);
  20613. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20614. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20615. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  20616. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20617. wc_curve448_free(&key);
  20618. #endif
  20619. return EXPECT_RESULT();
  20620. } /* END test_wc_curve448_import*/
  20621. /*
  20622. * Testing test_wc_curve448_size.
  20623. */
  20624. static int test_wc_curve448_size(void)
  20625. {
  20626. EXPECT_DECLS;
  20627. #if defined(HAVE_CURVE448)
  20628. curve448_key key;
  20629. ExpectIntEQ(wc_curve448_init(&key), 0);
  20630. /* Test good args for wc_curve448_size */
  20631. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20632. /* Test bad args for wc_curve448_size */
  20633. ExpectIntEQ(wc_curve448_size(NULL), 0);
  20634. wc_curve448_free(&key);
  20635. #endif
  20636. return EXPECT_RESULT();
  20637. } /* END test_wc_curve448_size*/
  20638. /*
  20639. * Testing wc_ecc_make_key.
  20640. */
  20641. static int test_wc_ecc_make_key(void)
  20642. {
  20643. EXPECT_DECLS;
  20644. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20645. ecc_key key;
  20646. WC_RNG rng;
  20647. int ret;
  20648. XMEMSET(&key, 0, sizeof(ecc_key));
  20649. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20650. ExpectIntEQ(wc_ecc_init(&key), 0);
  20651. ExpectIntEQ(wc_InitRng(&rng), 0);
  20652. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20653. #if defined(WOLFSSL_ASYNC_CRYPT)
  20654. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20655. #endif
  20656. ExpectIntEQ(ret, 0);
  20657. /* Pass in bad args. */
  20658. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  20659. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  20660. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20661. wc_ecc_free(&key);
  20662. #ifdef FP_ECC
  20663. wc_ecc_fp_free();
  20664. #endif
  20665. #endif
  20666. return EXPECT_RESULT();
  20667. } /* END test_wc_ecc_make_key */
  20668. /*
  20669. * Testing wc_ecc_init()
  20670. */
  20671. static int test_wc_ecc_init(void)
  20672. {
  20673. EXPECT_DECLS;
  20674. #ifdef HAVE_ECC
  20675. ecc_key key;
  20676. XMEMSET(&key, 0, sizeof(ecc_key));
  20677. ExpectIntEQ(wc_ecc_init(&key), 0);
  20678. /* Pass in bad args. */
  20679. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  20680. wc_ecc_free(&key);
  20681. #endif
  20682. return EXPECT_RESULT();
  20683. } /* END test_wc_ecc_init */
  20684. /*
  20685. * Testing wc_ecc_check_key()
  20686. */
  20687. static int test_wc_ecc_check_key(void)
  20688. {
  20689. EXPECT_DECLS;
  20690. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20691. ecc_key key;
  20692. WC_RNG rng;
  20693. int ret;
  20694. XMEMSET(&rng, 0, sizeof(rng));
  20695. XMEMSET(&key, 0, sizeof(key));
  20696. ExpectIntEQ(wc_ecc_init(&key), 0);
  20697. ExpectIntEQ(wc_InitRng(&rng), 0);
  20698. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20699. #if defined(WOLFSSL_ASYNC_CRYPT)
  20700. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20701. #endif
  20702. ExpectIntEQ(ret, 0);
  20703. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  20704. /* Pass in bad args. */
  20705. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  20706. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20707. wc_ecc_free(&key);
  20708. #ifdef FP_ECC
  20709. wc_ecc_fp_free();
  20710. #endif
  20711. #endif
  20712. return EXPECT_RESULT();
  20713. } /* END test_wc_ecc_check_key */
  20714. /*
  20715. * Testing wc_ecc_get_generator()
  20716. */
  20717. static int test_wc_ecc_get_generator(void)
  20718. {
  20719. EXPECT_DECLS;
  20720. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20721. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20722. ecc_point* pt = NULL;
  20723. ExpectNotNull(pt = wc_ecc_new_point());
  20724. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20725. MP_OKAY);
  20726. /* Test bad args. */
  20727. /* Returns Zero for bad arg. */
  20728. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  20729. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20730. MP_OKAY);
  20731. /* If we ever get to 1000 curves increase this number */
  20732. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  20733. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  20734. wc_ecc_del_point(pt);
  20735. #endif
  20736. return EXPECT_RESULT();
  20737. } /* END test_wc_ecc_get_generator */
  20738. /*
  20739. * Testing wc_ecc_size()
  20740. */
  20741. static int test_wc_ecc_size(void)
  20742. {
  20743. EXPECT_DECLS;
  20744. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20745. WC_RNG rng;
  20746. ecc_key key;
  20747. int ret;
  20748. XMEMSET(&key, 0, sizeof(ecc_key));
  20749. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20750. ExpectIntEQ(wc_ecc_init(&key), 0);
  20751. ExpectIntEQ(wc_InitRng(&rng), 0);
  20752. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20753. #if defined(WOLFSSL_ASYNC_CRYPT)
  20754. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20755. #endif
  20756. ExpectIntEQ(ret, 0);
  20757. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  20758. /* Test bad args. */
  20759. /* Returns Zero for bad arg. */
  20760. ExpectIntEQ(wc_ecc_size(NULL), 0);
  20761. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20762. wc_ecc_free(&key);
  20763. #endif
  20764. return EXPECT_RESULT();
  20765. } /* END test_wc_ecc_size */
  20766. static int test_wc_ecc_params(void)
  20767. {
  20768. EXPECT_DECLS;
  20769. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  20770. It was added after certifications */
  20771. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20772. const ecc_set_type* ecc_set;
  20773. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  20774. /* Test for SECP256R1 curve */
  20775. int curve_id = ECC_SECP256R1;
  20776. int curve_idx;
  20777. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  20778. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  20779. ExpectIntEQ(ecc_set->id, curve_id);
  20780. #endif
  20781. /* Test case when SECP256R1 is not enabled */
  20782. /* Test that we get curve params for index 0 */
  20783. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  20784. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  20785. return EXPECT_RESULT();
  20786. }
  20787. /*
  20788. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  20789. */
  20790. static int test_wc_ecc_signVerify_hash(void)
  20791. {
  20792. EXPECT_DECLS;
  20793. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  20794. ecc_key key;
  20795. WC_RNG rng;
  20796. int ret;
  20797. #ifdef HAVE_ECC_VERIFY
  20798. int verify = 0;
  20799. #endif
  20800. word32 siglen = ECC_BUFSIZE;
  20801. byte sig[ECC_BUFSIZE];
  20802. byte adjustedSig[ECC_BUFSIZE+1];
  20803. byte digest[] = TEST_STRING;
  20804. word32 digestlen = (word32)TEST_STRING_SZ;
  20805. /* Init stack var */
  20806. XMEMSET(&key, 0, sizeof(ecc_key));
  20807. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20808. XMEMSET(sig, 0, siglen);
  20809. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  20810. /* Init structs. */
  20811. ExpectIntEQ(wc_ecc_init(&key), 0);
  20812. ExpectIntEQ(wc_InitRng(&rng), 0);
  20813. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20814. #if defined(WOLFSSL_ASYNC_CRYPT)
  20815. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20816. #endif
  20817. ExpectIntEQ(ret, 0);
  20818. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  20819. 0);
  20820. /* Check bad args. */
  20821. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  20822. ECC_BAD_ARG_E);
  20823. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  20824. ECC_BAD_ARG_E);
  20825. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  20826. ECC_BAD_ARG_E);
  20827. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  20828. ECC_BAD_ARG_E);
  20829. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  20830. ECC_BAD_ARG_E);
  20831. #ifdef HAVE_ECC_VERIFY
  20832. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20833. &key), 0);
  20834. ExpectIntEQ(verify, 1);
  20835. /* test check on length of signature passed in */
  20836. XMEMCPY(adjustedSig, sig, siglen);
  20837. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  20838. #ifndef NO_STRICT_ECDSA_LEN
  20839. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20840. &verify, &key), 0);
  20841. #else
  20842. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  20843. * is allowed */
  20844. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20845. &verify, &key), 0);
  20846. #endif
  20847. /* Test bad args. */
  20848. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  20849. &key), ECC_BAD_ARG_E);
  20850. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  20851. ECC_BAD_ARG_E);
  20852. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  20853. ECC_BAD_ARG_E);
  20854. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20855. NULL), ECC_BAD_ARG_E);
  20856. #endif /* HAVE_ECC_VERIFY */
  20857. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20858. wc_ecc_free(&key);
  20859. #ifdef FP_ECC
  20860. wc_ecc_fp_free();
  20861. #endif
  20862. #endif
  20863. return EXPECT_RESULT();
  20864. } /* END test_wc_ecc_sign_hash */
  20865. /*
  20866. * Testing wc_ecc_shared_secret()
  20867. */
  20868. static int test_wc_ecc_shared_secret(void)
  20869. {
  20870. EXPECT_DECLS;
  20871. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20872. ecc_key key;
  20873. ecc_key pubKey;
  20874. WC_RNG rng;
  20875. #if defined(NO_ECC256)
  20876. int ret;
  20877. #endif
  20878. byte out[KEY32];
  20879. int keySz = sizeof(out);
  20880. word32 outlen = (word32)sizeof(out);
  20881. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20882. const char* qx =
  20883. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20884. const char* qy =
  20885. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20886. const char* d =
  20887. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20888. const char* curveName = "SECP256R1";
  20889. const byte expected_shared_secret[] =
  20890. {
  20891. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  20892. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  20893. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  20894. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  20895. };
  20896. #endif
  20897. PRIVATE_KEY_UNLOCK();
  20898. /* Initialize variables. */
  20899. XMEMSET(&key, 0, sizeof(ecc_key));
  20900. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20901. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20902. XMEMSET(out, 0, keySz);
  20903. ExpectIntEQ(wc_ecc_init(&key), 0);
  20904. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20905. ExpectIntEQ(wc_InitRng(&rng), 0);
  20906. #if !defined(NO_ECC256)
  20907. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  20908. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  20909. #else
  20910. ret = wc_ecc_make_key(&rng, keySz, &key);
  20911. #if defined(WOLFSSL_ASYNC_CRYPT)
  20912. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20913. #endif
  20914. ExpectIntEQ(ret, 0);
  20915. ret = wc_ecc_make_key(&rng, keySz, &key);
  20916. #if defined(WOLFSSL_ASYNC_CRYPT)
  20917. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20918. #endif
  20919. ExpectIntEQ(ret, 0);
  20920. #endif
  20921. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20922. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20923. !defined(HAVE_SELFTEST)
  20924. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  20925. #endif
  20926. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  20927. #if !defined(NO_ECC256)
  20928. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  20929. #endif
  20930. /* Test bad args. */
  20931. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  20932. BAD_FUNC_ARG);
  20933. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  20934. BAD_FUNC_ARG);
  20935. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  20936. BAD_FUNC_ARG);
  20937. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  20938. BAD_FUNC_ARG);
  20939. /* Invalid length */
  20940. outlen = 1;
  20941. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  20942. BUFFER_E);
  20943. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20944. wc_ecc_free(&pubKey);
  20945. wc_ecc_free(&key);
  20946. #ifdef FP_ECC
  20947. wc_ecc_fp_free();
  20948. #endif
  20949. PRIVATE_KEY_LOCK();
  20950. #endif
  20951. return EXPECT_RESULT();
  20952. } /* END tests_wc_ecc_shared_secret */
  20953. /*
  20954. * testint wc_ecc_export_x963()
  20955. */
  20956. static int test_wc_ecc_export_x963(void)
  20957. {
  20958. EXPECT_DECLS;
  20959. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20960. ecc_key key;
  20961. WC_RNG rng;
  20962. byte out[ECC_ASN963_MAX_BUF_SZ];
  20963. word32 outlen = sizeof(out);
  20964. int ret;
  20965. PRIVATE_KEY_UNLOCK();
  20966. /* Initialize variables. */
  20967. XMEMSET(&key, 0, sizeof(ecc_key));
  20968. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20969. XMEMSET(out, 0, outlen);
  20970. ExpectIntEQ(wc_ecc_init(&key), 0);
  20971. ExpectIntEQ(wc_InitRng(&rng), 0);
  20972. ret = wc_ecc_make_key(&rng, KEY20, &key);
  20973. #if defined(WOLFSSL_ASYNC_CRYPT)
  20974. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20975. #endif
  20976. ExpectIntEQ(ret, 0);
  20977. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  20978. /* Test bad args. */
  20979. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  20980. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  20981. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  20982. key.idx = -4;
  20983. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  20984. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20985. wc_ecc_free(&key);
  20986. #ifdef FP_ECC
  20987. wc_ecc_fp_free();
  20988. #endif
  20989. PRIVATE_KEY_LOCK();
  20990. #endif
  20991. return EXPECT_RESULT();
  20992. } /* END test_wc_ecc_export_x963 */
  20993. /*
  20994. * Testing wc_ecc_export_x963_ex()
  20995. * compile with --enable-compkey will use compression.
  20996. */
  20997. static int test_wc_ecc_export_x963_ex(void)
  20998. {
  20999. EXPECT_DECLS;
  21000. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21001. ecc_key key;
  21002. WC_RNG rng;
  21003. int ret;
  21004. byte out[ECC_ASN963_MAX_BUF_SZ];
  21005. word32 outlen = sizeof(out);
  21006. #ifdef HAVE_COMP_KEY
  21007. word32 badOutLen = 5;
  21008. #endif
  21009. /* Init stack variables. */
  21010. XMEMSET(&key, 0, sizeof(ecc_key));
  21011. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21012. XMEMSET(out, 0, outlen);
  21013. ExpectIntEQ(wc_ecc_init(&key), 0);
  21014. ExpectIntEQ(wc_InitRng(&rng), 0);
  21015. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21016. #if defined(WOLFSSL_ASYNC_CRYPT)
  21017. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21018. #endif
  21019. ExpectIntEQ(ret, 0);
  21020. #ifdef HAVE_COMP_KEY
  21021. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21022. #else
  21023. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21024. #endif
  21025. /* Test bad args. */
  21026. #ifdef HAVE_COMP_KEY
  21027. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21028. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21029. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21030. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21031. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21032. #else
  21033. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21034. LENGTH_ONLY_E);
  21035. #endif
  21036. key.idx = -4;
  21037. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21038. #else
  21039. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21040. ECC_BAD_ARG_E);
  21041. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21042. LENGTH_ONLY_E);
  21043. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21044. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21045. ECC_BAD_ARG_E);
  21046. key.idx = -4;
  21047. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21048. ECC_BAD_ARG_E);
  21049. #endif
  21050. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21051. wc_ecc_free(&key);
  21052. #ifdef FP_ECC
  21053. wc_ecc_fp_free();
  21054. #endif
  21055. #endif
  21056. return EXPECT_RESULT();
  21057. } /* END test_wc_ecc_export_x963_ex */
  21058. /*
  21059. * testing wc_ecc_import_x963()
  21060. */
  21061. static int test_wc_ecc_import_x963(void)
  21062. {
  21063. EXPECT_DECLS;
  21064. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21065. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21066. ecc_key pubKey;
  21067. ecc_key key;
  21068. WC_RNG rng;
  21069. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21070. word32 x963Len = (word32)sizeof(x963);
  21071. int ret;
  21072. /* Init stack variables. */
  21073. XMEMSET(&key, 0, sizeof(ecc_key));
  21074. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21075. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21076. XMEMSET(x963, 0, x963Len);
  21077. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21078. ExpectIntEQ(wc_ecc_init(&key), 0);
  21079. ExpectIntEQ(wc_InitRng(&rng), 0);
  21080. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21081. #if defined(WOLFSSL_ASYNC_CRYPT)
  21082. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21083. #endif
  21084. ExpectIntEQ(ret, 0);
  21085. PRIVATE_KEY_UNLOCK();
  21086. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21087. PRIVATE_KEY_LOCK();
  21088. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21089. /* Test bad args. */
  21090. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21091. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21092. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21093. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21094. wc_ecc_free(&key);
  21095. wc_ecc_free(&pubKey);
  21096. #ifdef FP_ECC
  21097. wc_ecc_fp_free();
  21098. #endif
  21099. #endif
  21100. return EXPECT_RESULT();
  21101. } /* END wc_ecc_import_x963 */
  21102. /*
  21103. * testing wc_ecc_import_private_key()
  21104. */
  21105. static int test_wc_ecc_import_private_key(void)
  21106. {
  21107. EXPECT_DECLS;
  21108. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21109. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21110. ecc_key key;
  21111. ecc_key keyImp;
  21112. WC_RNG rng;
  21113. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21114. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21115. word32 privKeySz = (word32)sizeof(privKey);
  21116. word32 x963KeySz = (word32)sizeof(x963Key);
  21117. int ret;
  21118. /* Init stack variables. */
  21119. XMEMSET(&key, 0, sizeof(ecc_key));
  21120. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21121. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21122. XMEMSET(privKey, 0, privKeySz);
  21123. XMEMSET(x963Key, 0, x963KeySz);
  21124. ExpectIntEQ(wc_ecc_init(&key), 0);
  21125. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21126. ExpectIntEQ(wc_InitRng(&rng), 0);
  21127. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21128. #if defined(WOLFSSL_ASYNC_CRYPT)
  21129. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21130. #endif
  21131. ExpectIntEQ(ret, 0);
  21132. PRIVATE_KEY_UNLOCK();
  21133. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21134. PRIVATE_KEY_LOCK();
  21135. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21136. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21137. x963KeySz, &keyImp), 0);
  21138. /* Pass in bad args. */
  21139. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21140. x963KeySz, NULL), BAD_FUNC_ARG);
  21141. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21142. &keyImp), BAD_FUNC_ARG);
  21143. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21144. wc_ecc_free(&keyImp);
  21145. wc_ecc_free(&key);
  21146. #ifdef FP_ECC
  21147. wc_ecc_fp_free();
  21148. #endif
  21149. #endif
  21150. return EXPECT_RESULT();
  21151. } /* END test_wc_ecc_import_private_key */
  21152. /*
  21153. * Testing wc_ecc_export_private_only()
  21154. */
  21155. static int test_wc_ecc_export_private_only(void)
  21156. {
  21157. EXPECT_DECLS;
  21158. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21159. ecc_key key;
  21160. WC_RNG rng;
  21161. byte out[ECC_PRIV_KEY_BUF];
  21162. word32 outlen = sizeof(out);
  21163. int ret;
  21164. /* Init stack variables. */
  21165. XMEMSET(&key, 0, sizeof(ecc_key));
  21166. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21167. XMEMSET(out, 0, outlen);
  21168. ExpectIntEQ(wc_ecc_init(&key), 0);
  21169. ExpectIntEQ(wc_InitRng(&rng), 0);
  21170. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21171. #if defined(WOLFSSL_ASYNC_CRYPT)
  21172. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21173. #endif
  21174. ExpectIntEQ(ret, 0);
  21175. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21176. /* Pass in bad args. */
  21177. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21178. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21179. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21180. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21181. wc_ecc_free(&key);
  21182. #ifdef FP_ECC
  21183. wc_ecc_fp_free();
  21184. #endif
  21185. #endif
  21186. return EXPECT_RESULT();
  21187. } /* END test_wc_ecc_export_private_only */
  21188. /*
  21189. * Testing wc_ecc_rs_to_sig()
  21190. */
  21191. static int test_wc_ecc_rs_to_sig(void)
  21192. {
  21193. EXPECT_DECLS;
  21194. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21195. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21196. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21197. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21198. const char* zeroStr = "0";
  21199. byte sig[ECC_MAX_SIG_SIZE];
  21200. word32 siglen = (word32)sizeof(sig);
  21201. /* R and S max size is the order of curve. 2^192.*/
  21202. int keySz = KEY24;
  21203. byte r[KEY24];
  21204. byte s[KEY24];
  21205. word32 rlen = (word32)sizeof(r);
  21206. word32 slen = (word32)sizeof(s);
  21207. /* Init stack variables. */
  21208. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21209. XMEMSET(r, 0, keySz);
  21210. XMEMSET(s, 0, keySz);
  21211. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21212. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21213. /* Test bad args. */
  21214. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21215. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21216. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21217. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21218. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21219. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21220. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21221. ECC_BAD_ARG_E);
  21222. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21223. ECC_BAD_ARG_E);
  21224. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21225. ECC_BAD_ARG_E);
  21226. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21227. ECC_BAD_ARG_E);
  21228. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21229. ECC_BAD_ARG_E);
  21230. #endif
  21231. return EXPECT_RESULT();
  21232. } /* END test_wc_ecc_rs_to_sig */
  21233. static int test_wc_ecc_import_raw(void)
  21234. {
  21235. EXPECT_DECLS;
  21236. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21237. ecc_key key;
  21238. const char* qx =
  21239. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21240. const char* qy =
  21241. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21242. const char* d =
  21243. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21244. const char* curveName = "SECP256R1";
  21245. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21246. const char* kNullStr = "";
  21247. int ret;
  21248. #endif
  21249. XMEMSET(&key, 0, sizeof(ecc_key));
  21250. ExpectIntEQ(wc_ecc_init(&key), 0);
  21251. /* Test good import */
  21252. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21253. /* Test bad args. */
  21254. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21255. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21256. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21257. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21258. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21259. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21260. wc_ecc_free(&key);
  21261. #endif
  21262. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21263. curveName), 0);
  21264. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21265. #endif
  21266. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21267. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21268. wc_ecc_free(&key);
  21269. #endif
  21270. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21271. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21272. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21273. #else
  21274. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21275. #endif
  21276. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21277. wc_ecc_free(&key);
  21278. #endif
  21279. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21280. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21281. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21282. #else
  21283. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21284. #endif
  21285. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21286. wc_ecc_free(&key);
  21287. #endif
  21288. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21289. #endif
  21290. wc_ecc_free(&key);
  21291. #endif
  21292. return EXPECT_RESULT();
  21293. } /* END test_wc_ecc_import_raw */
  21294. static int test_wc_ecc_import_unsigned(void)
  21295. {
  21296. EXPECT_DECLS;
  21297. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21298. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21299. HAVE_FIPS_VERSION >= 2))
  21300. ecc_key key;
  21301. const byte qx[] = {
  21302. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21303. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21304. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21305. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21306. };
  21307. const byte qy[] = {
  21308. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21309. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21310. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21311. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21312. };
  21313. const byte d[] = {
  21314. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21315. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21316. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21317. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21318. };
  21319. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21320. const byte nullBytes[32] = {0};
  21321. int ret;
  21322. #endif
  21323. int curveId = ECC_SECP256R1;
  21324. XMEMSET(&key, 0, sizeof(ecc_key));
  21325. ExpectIntEQ(wc_ecc_init(&key), 0);
  21326. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21327. curveId), 0);
  21328. /* Test bad args. */
  21329. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21330. curveId), BAD_FUNC_ARG);
  21331. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21332. curveId), BAD_FUNC_ARG);
  21333. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21334. curveId), BAD_FUNC_ARG);
  21335. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21336. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21337. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21338. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21339. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21340. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21341. #endif
  21342. wc_ecc_free(&key);
  21343. #endif
  21344. return EXPECT_RESULT();
  21345. } /* END test_wc_ecc_import_unsigned */
  21346. /*
  21347. * Testing wc_ecc_sig_size()
  21348. */
  21349. static int test_wc_ecc_sig_size(void)
  21350. {
  21351. EXPECT_DECLS;
  21352. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21353. ecc_key key;
  21354. WC_RNG rng;
  21355. int keySz = KEY16;
  21356. int ret;
  21357. XMEMSET(&rng, 0, sizeof(rng));
  21358. XMEMSET(&key, 0, sizeof(key));
  21359. ExpectIntEQ(wc_ecc_init(&key), 0);
  21360. ExpectIntEQ(wc_InitRng(&rng), 0);
  21361. ret = wc_ecc_make_key(&rng, keySz, &key);
  21362. #if defined(WOLFSSL_ASYNC_CRYPT)
  21363. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21364. #endif
  21365. ExpectIntEQ(ret, 0);
  21366. ExpectIntLE(wc_ecc_sig_size(&key),
  21367. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21368. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21369. wc_ecc_free(&key);
  21370. #endif
  21371. return EXPECT_RESULT();
  21372. } /* END test_wc_ecc_sig_size */
  21373. /*
  21374. * Testing wc_ecc_ctx_new()
  21375. */
  21376. static int test_wc_ecc_ctx_new(void)
  21377. {
  21378. EXPECT_DECLS;
  21379. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21380. WC_RNG rng;
  21381. ecEncCtx* cli = NULL;
  21382. ecEncCtx* srv = NULL;
  21383. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21384. ExpectIntEQ(wc_InitRng(&rng), 0);
  21385. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21386. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21387. wc_ecc_ctx_free(cli);
  21388. cli = NULL;
  21389. wc_ecc_ctx_free(srv);
  21390. /* Test bad args. */
  21391. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21392. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  21393. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  21394. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21395. wc_ecc_ctx_free(cli);
  21396. #endif
  21397. return EXPECT_RESULT();
  21398. } /* END test_wc_ecc_ctx_new */
  21399. /*
  21400. * Tesing wc_ecc_reset()
  21401. */
  21402. static int test_wc_ecc_ctx_reset(void)
  21403. {
  21404. EXPECT_DECLS;
  21405. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21406. ecEncCtx* ctx = NULL;
  21407. WC_RNG rng;
  21408. XMEMSET(&rng, 0, sizeof(rng));
  21409. ExpectIntEQ(wc_InitRng(&rng), 0);
  21410. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21411. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  21412. /* Pass in bad args. */
  21413. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  21414. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  21415. wc_ecc_ctx_free(ctx);
  21416. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21417. #endif
  21418. return EXPECT_RESULT();
  21419. } /* END test_wc_ecc_ctx_reset */
  21420. /*
  21421. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21422. */
  21423. static int test_wc_ecc_ctx_set_peer_salt(void)
  21424. {
  21425. EXPECT_DECLS;
  21426. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21427. WC_RNG rng;
  21428. ecEncCtx* cliCtx = NULL;
  21429. ecEncCtx* servCtx = NULL;
  21430. const byte* cliSalt = NULL;
  21431. const byte* servSalt = NULL;
  21432. XMEMSET(&rng, 0, sizeof(rng));
  21433. ExpectIntEQ(wc_InitRng(&rng), 0);
  21434. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21435. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21436. /* Test bad args. */
  21437. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  21438. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  21439. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  21440. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  21441. /* Test bad args. */
  21442. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  21443. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  21444. wc_ecc_ctx_free(cliCtx);
  21445. wc_ecc_ctx_free(servCtx);
  21446. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21447. #endif
  21448. return EXPECT_RESULT();
  21449. } /* END test_wc_ecc_ctx_set_peer_salt */
  21450. /*
  21451. * Testing wc_ecc_ctx_set_info()
  21452. */
  21453. static int test_wc_ecc_ctx_set_info(void)
  21454. {
  21455. EXPECT_DECLS;
  21456. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21457. ecEncCtx* ctx = NULL;
  21458. WC_RNG rng;
  21459. const char* optInfo = "Optional Test Info.";
  21460. int optInfoSz = (int)XSTRLEN(optInfo);
  21461. const char* badOptInfo = NULL;
  21462. XMEMSET(&rng, 0, sizeof(rng));
  21463. ExpectIntEQ(wc_InitRng(&rng), 0);
  21464. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21465. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  21466. /* Test bad args. */
  21467. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  21468. BAD_FUNC_ARG);
  21469. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  21470. BAD_FUNC_ARG);
  21471. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  21472. wc_ecc_ctx_free(ctx);
  21473. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21474. #endif
  21475. return EXPECT_RESULT();
  21476. } /* END test_wc_ecc_ctx_set_info */
  21477. /*
  21478. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21479. */
  21480. static int test_wc_ecc_encryptDecrypt(void)
  21481. {
  21482. EXPECT_DECLS;
  21483. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21484. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21485. ecc_key srvKey;
  21486. ecc_key cliKey;
  21487. ecc_key tmpKey;
  21488. WC_RNG rng;
  21489. int ret;
  21490. const char* msg = "EccBlock Size 16";
  21491. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21492. #ifdef WOLFSSL_ECIES_OLD
  21493. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21494. #elif defined(WOLFSSL_ECIES_GEN_IV)
  21495. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  21496. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21497. #else
  21498. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  21499. WC_SHA256_DIGEST_SIZE];
  21500. #endif
  21501. word32 outSz = (word32)sizeof(out);
  21502. byte plain[sizeof("EccBlock Size 16")];
  21503. word32 plainSz = (word32)sizeof(plain);
  21504. int keySz = KEY20;
  21505. /* Init stack variables. */
  21506. XMEMSET(out, 0, outSz);
  21507. XMEMSET(plain, 0, plainSz);
  21508. XMEMSET(&rng, 0, sizeof(rng));
  21509. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  21510. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  21511. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  21512. ExpectIntEQ(wc_InitRng(&rng), 0);
  21513. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  21514. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21515. #if defined(WOLFSSL_ASYNC_CRYPT)
  21516. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21517. #endif
  21518. ExpectIntEQ(ret, 0);
  21519. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  21520. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21521. #if defined(WOLFSSL_ASYNC_CRYPT)
  21522. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21523. #endif
  21524. ExpectIntEQ(ret, 0);
  21525. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  21526. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21527. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21528. !defined(HAVE_SELFTEST)
  21529. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  21530. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  21531. #endif
  21532. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21533. &outSz, NULL), 0);
  21534. /* Test bad args. */
  21535. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  21536. NULL), BAD_FUNC_ARG);
  21537. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  21538. NULL), BAD_FUNC_ARG);
  21539. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  21540. NULL), BAD_FUNC_ARG);
  21541. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21542. &outSz, NULL), BAD_FUNC_ARG);
  21543. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  21544. NULL), BAD_FUNC_ARG);
  21545. #ifdef WOLFSSL_ECIES_OLD
  21546. tmpKey.dp = cliKey.dp;
  21547. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  21548. #endif
  21549. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  21550. NULL), 0);
  21551. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  21552. NULL), BAD_FUNC_ARG);
  21553. #ifdef WOLFSSL_ECIES_OLD
  21554. /* NULL parameter allowed in new implementations - public key comes from
  21555. * the message. */
  21556. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  21557. NULL), BAD_FUNC_ARG);
  21558. #endif
  21559. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  21560. NULL), BAD_FUNC_ARG);
  21561. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  21562. NULL), BAD_FUNC_ARG);
  21563. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  21564. BAD_FUNC_ARG);
  21565. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  21566. wc_ecc_free(&tmpKey);
  21567. wc_ecc_free(&srvKey);
  21568. wc_ecc_free(&cliKey);
  21569. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21570. #endif
  21571. return EXPECT_RESULT();
  21572. } /* END test_wc_ecc_encryptDecrypt */
  21573. /*
  21574. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21575. */
  21576. static int test_wc_ecc_del_point(void)
  21577. {
  21578. EXPECT_DECLS;
  21579. #if defined(HAVE_ECC)
  21580. ecc_point* pt = NULL;
  21581. ExpectNotNull(pt = wc_ecc_new_point());
  21582. wc_ecc_del_point(pt);
  21583. #endif
  21584. return EXPECT_RESULT();
  21585. } /* END test_wc_ecc_del_point */
  21586. /*
  21587. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21588. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21589. * and wc_ecc_cmp_point()
  21590. */
  21591. static int test_wc_ecc_pointFns(void)
  21592. {
  21593. EXPECT_DECLS;
  21594. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21595. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21596. !defined(WOLFSSL_ATECC608A)
  21597. ecc_key key;
  21598. WC_RNG rng;
  21599. int ret;
  21600. ecc_point* point = NULL;
  21601. ecc_point* cpypt = NULL;
  21602. int idx = 0;
  21603. int keySz = KEY32;
  21604. byte der[DER_SZ(KEY32)];
  21605. word32 derlenChk = 0;
  21606. word32 derSz = DER_SZ(KEY32);
  21607. /* Init stack variables. */
  21608. XMEMSET(der, 0, derSz);
  21609. XMEMSET(&key, 0, sizeof(ecc_key));
  21610. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21611. ExpectIntEQ(wc_InitRng(&rng), 0);
  21612. ExpectIntEQ(wc_ecc_init(&key), 0);
  21613. ret = wc_ecc_make_key(&rng, keySz, &key);
  21614. #if defined(WOLFSSL_ASYNC_CRYPT)
  21615. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21616. #endif
  21617. ExpectIntEQ(ret, 0);
  21618. ExpectNotNull(point = wc_ecc_new_point());
  21619. ExpectNotNull(cpypt = wc_ecc_new_point());
  21620. /* Export */
  21621. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  21622. &derlenChk), LENGTH_ONLY_E);
  21623. /* Check length value. */
  21624. ExpectIntEQ(derSz, derlenChk);
  21625. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21626. &derSz), 0);
  21627. /* Test bad args. */
  21628. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  21629. ECC_BAD_ARG_E);
  21630. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  21631. ECC_BAD_ARG_E);
  21632. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21633. NULL), ECC_BAD_ARG_E);
  21634. /* Import */
  21635. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  21636. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  21637. /* Test bad args. */
  21638. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  21639. ECC_BAD_ARG_E);
  21640. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  21641. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  21642. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  21643. ECC_BAD_ARG_E);
  21644. /* Copy */
  21645. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  21646. /* Test bad args. */
  21647. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  21648. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  21649. /* Compare point */
  21650. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  21651. /* Test bad args. */
  21652. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  21653. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  21654. /* At infinity if return == 1, otherwise return == 0. */
  21655. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  21656. /* Test bad args. */
  21657. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  21658. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21659. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  21660. #ifdef USE_ECC_B_PARAM
  21661. /* On curve if ret == 0 */
  21662. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  21663. /* Test bad args. */
  21664. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  21665. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  21666. #endif /* USE_ECC_B_PARAM */
  21667. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  21668. /* Free */
  21669. wc_ecc_del_point(point);
  21670. wc_ecc_del_point(cpypt);
  21671. wc_ecc_free(&key);
  21672. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21673. #endif
  21674. return EXPECT_RESULT();
  21675. } /* END test_wc_ecc_pointFns */
  21676. /*
  21677. * Testing wc_ecc_sahred_secret_ssh()
  21678. */
  21679. static int test_wc_ecc_shared_secret_ssh(void)
  21680. {
  21681. EXPECT_DECLS;
  21682. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  21683. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21684. !defined(WOLFSSL_ATECC608A)
  21685. ecc_key key;
  21686. ecc_key key2;
  21687. WC_RNG rng;
  21688. int ret;
  21689. int keySz = KEY32;
  21690. int key2Sz = KEY24;
  21691. byte secret[KEY32];
  21692. word32 secretLen = keySz;
  21693. /* Init stack variables. */
  21694. XMEMSET(&key, 0, sizeof(ecc_key));
  21695. XMEMSET(&key2, 0, sizeof(ecc_key));
  21696. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21697. XMEMSET(secret, 0, secretLen);
  21698. /* Make keys */
  21699. ExpectIntEQ(wc_ecc_init(&key), 0);
  21700. ExpectIntEQ(wc_InitRng(&rng), 0);
  21701. ret = wc_ecc_make_key(&rng, keySz, &key);
  21702. #if defined(WOLFSSL_ASYNC_CRYPT)
  21703. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21704. #endif
  21705. ExpectIntEQ(ret, 0);
  21706. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21707. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21708. ExpectIntEQ(wc_InitRng(&rng), 0);
  21709. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  21710. #if defined(WOLFSSL_ASYNC_CRYPT)
  21711. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  21712. #endif
  21713. ExpectIntEQ(ret, 0);
  21714. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21715. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21716. !defined(HAVE_SELFTEST)
  21717. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21718. #endif
  21719. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21720. &secretLen), 0);
  21721. /* Pass in bad args. */
  21722. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  21723. &secretLen), BAD_FUNC_ARG);
  21724. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  21725. BAD_FUNC_ARG);
  21726. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  21727. BAD_FUNC_ARG);
  21728. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  21729. BAD_FUNC_ARG);
  21730. key.type = ECC_PUBLICKEY;
  21731. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21732. &secretLen), ECC_BAD_ARG_E);
  21733. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21734. wc_ecc_free(&key);
  21735. wc_ecc_free(&key2);
  21736. #ifdef FP_ECC
  21737. wc_ecc_fp_free();
  21738. #endif
  21739. #endif
  21740. return EXPECT_RESULT();
  21741. } /* END test_wc_ecc_shared_secret_ssh */
  21742. /*
  21743. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  21744. */
  21745. static int test_wc_ecc_verify_hash_ex(void)
  21746. {
  21747. EXPECT_DECLS;
  21748. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  21749. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21750. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  21751. ecc_key key;
  21752. WC_RNG rng;
  21753. int ret;
  21754. mp_int r;
  21755. mp_int s;
  21756. mp_int z;
  21757. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  21758. unsigned char iHash[] = "Everyone gets Friday off.......";
  21759. unsigned char shortHash[] = TEST_STRING;
  21760. word32 hashlen = sizeof(hash);
  21761. word32 iHashLen = sizeof(iHash);
  21762. word32 shortHashLen = sizeof(shortHash);
  21763. int keySz = KEY32;
  21764. int verify_ok = 0;
  21765. XMEMSET(&key, 0, sizeof(ecc_key));
  21766. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21767. XMEMSET(&r, 0, sizeof(mp_int));
  21768. XMEMSET(&s, 0, sizeof(mp_int));
  21769. XMEMSET(&z, 0, sizeof(mp_int));
  21770. /* Initialize r, s and z. */
  21771. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  21772. ExpectIntEQ(wc_ecc_init(&key), 0);
  21773. ExpectIntEQ(wc_InitRng(&rng), 0);
  21774. ret = wc_ecc_make_key(&rng, keySz, &key);
  21775. #if defined(WOLFSSL_ASYNC_CRYPT)
  21776. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21777. #endif
  21778. ExpectIntEQ(ret, 0);
  21779. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  21780. /* verify_ok should be 1. */
  21781. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  21782. 0);
  21783. ExpectIntEQ(verify_ok, 1);
  21784. /* verify_ok should be 0 */
  21785. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  21786. &key), 0);
  21787. ExpectIntEQ(verify_ok, 0);
  21788. /* verify_ok should be 0. */
  21789. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21790. &verify_ok, &key), 0);
  21791. ExpectIntEQ(verify_ok, 0);
  21792. /* Test bad args. */
  21793. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  21794. ECC_BAD_ARG_E);
  21795. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  21796. ECC_BAD_ARG_E);
  21797. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  21798. ECC_BAD_ARG_E);
  21799. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  21800. ECC_BAD_ARG_E);
  21801. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  21802. ECC_BAD_ARG_E);
  21803. /* Test bad args. */
  21804. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  21805. &verify_ok, &key), ECC_BAD_ARG_E);
  21806. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  21807. &verify_ok, &key), ECC_BAD_ARG_E);
  21808. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  21809. &verify_ok, &key), MP_ZERO_E);
  21810. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  21811. &verify_ok, &key), MP_ZERO_E);
  21812. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  21813. &verify_ok, &key), MP_ZERO_E);
  21814. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  21815. &key), ECC_BAD_ARG_E);
  21816. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  21817. &key), ECC_BAD_ARG_E);
  21818. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21819. &verify_ok, NULL), ECC_BAD_ARG_E);
  21820. wc_ecc_free(&key);
  21821. mp_free(&r);
  21822. mp_free(&s);
  21823. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21824. #endif
  21825. return EXPECT_RESULT();
  21826. } /* END test_wc_ecc_verify_hash_ex */
  21827. /*
  21828. * Testing wc_ecc_mulmod()
  21829. */
  21830. static int test_wc_ecc_mulmod(void)
  21831. {
  21832. EXPECT_DECLS;
  21833. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  21834. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  21835. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21836. ecc_key key1;
  21837. ecc_key key2;
  21838. ecc_key key3;
  21839. WC_RNG rng;
  21840. int ret;
  21841. XMEMSET(&key1, 0, sizeof(ecc_key));
  21842. XMEMSET(&key2, 0, sizeof(ecc_key));
  21843. XMEMSET(&key3, 0, sizeof(ecc_key));
  21844. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21845. ExpectIntEQ(wc_ecc_init(&key1), 0);
  21846. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21847. ExpectIntEQ(wc_ecc_init(&key3), 0);
  21848. ExpectIntEQ(wc_InitRng(&rng), 0);
  21849. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  21850. #if defined(WOLFSSL_ASYNC_CRYPT)
  21851. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  21852. #endif
  21853. ExpectIntEQ(ret, 0);
  21854. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21855. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  21856. key1.dp->Af, ECC_SECP256R1), 0);
  21857. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  21858. key1.dp->prime, ECC_SECP256R1), 0);
  21859. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21860. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  21861. 1), 0);
  21862. /* Test bad args. */
  21863. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  21864. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21865. ECC_BAD_ARG_E);
  21866. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  21867. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21868. ECC_BAD_ARG_E);
  21869. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  21870. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21871. ECC_BAD_ARG_E);
  21872. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21873. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  21874. wc_ecc_free(&key1);
  21875. wc_ecc_free(&key2);
  21876. wc_ecc_free(&key3);
  21877. #ifdef FP_ECC
  21878. wc_ecc_fp_free();
  21879. #endif
  21880. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  21881. return EXPECT_RESULT();
  21882. } /* END test_wc_ecc_mulmod */
  21883. /*
  21884. * Testing wc_ecc_is_valid_idx()
  21885. */
  21886. static int test_wc_ecc_is_valid_idx(void)
  21887. {
  21888. EXPECT_DECLS;
  21889. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21890. ecc_key key;
  21891. WC_RNG rng;
  21892. int ret;
  21893. int iVal = -2;
  21894. int iVal2 = 3000;
  21895. XMEMSET(&key, 0, sizeof(ecc_key));
  21896. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21897. ExpectIntEQ(wc_ecc_init(&key), 0);
  21898. ExpectIntEQ(wc_InitRng(&rng), 0);
  21899. ret = wc_ecc_make_key(&rng, 32, &key);
  21900. #if defined(WOLFSSL_ASYNC_CRYPT)
  21901. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21902. #endif
  21903. ExpectIntEQ(ret, 0);
  21904. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  21905. /* Test bad args. */
  21906. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  21907. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  21908. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21909. wc_ecc_free(&key);
  21910. #ifdef FP_ECC
  21911. wc_ecc_fp_free();
  21912. #endif
  21913. #endif
  21914. return EXPECT_RESULT();
  21915. } /* END test_wc_ecc_is_valid_idx */
  21916. /*
  21917. * Testing wc_ecc_get_curve_id_from_oid()
  21918. */
  21919. static int test_wc_ecc_get_curve_id_from_oid(void)
  21920. {
  21921. EXPECT_DECLS;
  21922. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21923. !defined(HAVE_FIPS)
  21924. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  21925. word32 len = sizeof(oid);
  21926. /* Bad Cases */
  21927. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  21928. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  21929. /* Good Case */
  21930. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  21931. #endif
  21932. return EXPECT_RESULT();
  21933. } /* END test_wc_ecc_get_curve_id_from_oid */
  21934. /*
  21935. * Testing wc_ecc_sig_size_calc()
  21936. */
  21937. static int test_wc_ecc_sig_size_calc(void)
  21938. {
  21939. EXPECT_DECLS;
  21940. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  21941. ecc_key key;
  21942. WC_RNG rng;
  21943. int sz = 0;
  21944. int ret;
  21945. XMEMSET(&key, 0, sizeof(ecc_key));
  21946. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21947. ExpectIntEQ(wc_ecc_init(&key), 0);
  21948. ExpectIntEQ(wc_InitRng(&rng), 0);
  21949. ret = wc_ecc_make_key(&rng, 16, &key);
  21950. #if defined(WOLFSSL_ASYNC_CRYPT)
  21951. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21952. #endif
  21953. ExpectIntEQ(ret, 0);
  21954. sz = key.dp->size;
  21955. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  21956. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21957. wc_ecc_free(&key);
  21958. #endif
  21959. return EXPECT_RESULT();
  21960. } /* END test_wc_ecc_sig_size_calc */
  21961. /*
  21962. * Testing wc_ecc_sm2_make_key()
  21963. */
  21964. static int test_wc_ecc_sm2_make_key(void)
  21965. {
  21966. int res = TEST_SKIPPED;
  21967. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21968. EXPECT_DECLS;
  21969. WC_RNG rng[1];
  21970. ecc_key key[1];
  21971. XMEMSET(rng, 0, sizeof(*rng));
  21972. XMEMSET(key, 0, sizeof(*key));
  21973. ExpectIntEQ(wc_InitRng(rng), 0);
  21974. ExpectIntEQ(wc_ecc_init(key), 0);
  21975. /* Test invalid parameters. */
  21976. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  21977. BAD_FUNC_ARG);
  21978. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  21979. BAD_FUNC_ARG);
  21980. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  21981. BAD_FUNC_ARG);
  21982. /* Test valid parameters. */
  21983. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  21984. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  21985. wc_ecc_free(key);
  21986. wc_FreeRng(rng);
  21987. #ifdef FP_ECC
  21988. wc_ecc_fp_free();
  21989. #endif
  21990. res = EXPECT_RESULT();
  21991. #endif
  21992. return res;
  21993. }
  21994. /*
  21995. * Testing wc_ecc_sm2_shared_secret()
  21996. */
  21997. static int test_wc_ecc_sm2_shared_secret(void)
  21998. {
  21999. int res = TEST_SKIPPED;
  22000. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22001. EXPECT_DECLS;
  22002. WC_RNG rng[1];
  22003. ecc_key keyA[1];
  22004. ecc_key keyB[1];
  22005. byte outA[32];
  22006. byte outB[32];
  22007. word32 outALen = 32;
  22008. word32 outBLen = 32;
  22009. XMEMSET(rng, 0, sizeof(*rng));
  22010. XMEMSET(keyA, 0, sizeof(*keyA));
  22011. XMEMSET(keyB, 0, sizeof(*keyB));
  22012. ExpectIntEQ(wc_InitRng(rng), 0);
  22013. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22014. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22015. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22016. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22017. #ifdef ECC_TIMING_RESISTANT
  22018. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22019. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22020. #endif
  22021. /* Test invalid parameters. */
  22022. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22023. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22024. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22025. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22026. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22027. BAD_FUNC_ARG);
  22028. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22029. BAD_FUNC_ARG);
  22030. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22031. BAD_FUNC_ARG);
  22032. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22033. BAD_FUNC_ARG);
  22034. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22035. /* Test valid parameters. */
  22036. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22037. ExpectIntLE(outALen, 32);
  22038. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22039. ExpectIntLE(outBLen, 32);
  22040. ExpectIntEQ(outALen, outBLen);
  22041. ExpectBufEQ(outA, outB, outALen);
  22042. wc_ecc_free(keyB);
  22043. wc_ecc_free(keyA);
  22044. wc_FreeRng(rng);
  22045. #ifdef FP_ECC
  22046. wc_ecc_fp_free();
  22047. #endif
  22048. res = EXPECT_RESULT();
  22049. #endif
  22050. return res;
  22051. }
  22052. /*
  22053. * Testing wc_ecc_sm2_create_digest()
  22054. */
  22055. static int test_wc_ecc_sm2_create_digest(void)
  22056. {
  22057. int res = TEST_SKIPPED;
  22058. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22059. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22060. EXPECT_DECLS;
  22061. ecc_key key[1];
  22062. enum wc_HashType hashType;
  22063. unsigned char pub[] = {
  22064. 0x04,
  22065. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22066. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22067. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22068. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22069. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22070. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22071. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22072. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22073. };
  22074. unsigned char id[] = {
  22075. 0x01, 0x02, 0x03,
  22076. };
  22077. unsigned char msg[] = {
  22078. 0x01, 0x02, 0x03,
  22079. };
  22080. unsigned char hash[32];
  22081. #ifdef WOLFSSL_SM3
  22082. unsigned char expHash[32] = {
  22083. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22084. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22085. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22086. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22087. };
  22088. #else
  22089. unsigned char expHash[32] = {
  22090. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22091. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22092. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22093. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22094. };
  22095. #endif
  22096. #ifdef WOLFSSL_SM3
  22097. hashType = WC_HASH_TYPE_SM3;
  22098. #else
  22099. hashType = WC_HASH_TYPE_SHA256;
  22100. #endif
  22101. XMEMSET(key, 0, sizeof(*key));
  22102. ExpectIntEQ(wc_ecc_init(key), 0);
  22103. /* Test with no curve set. */
  22104. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22105. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22106. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22107. /* Test invalid parameters. */
  22108. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22109. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22110. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22111. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22112. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22113. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22114. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22115. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22116. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22117. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22118. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22119. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22120. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22121. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22122. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22123. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22124. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22125. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22126. /* Bad hash type. */
  22127. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22128. -1, hash, 0, key), BAD_FUNC_ARG);
  22129. /* Bad hash size. */
  22130. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22131. hashType, hash, 0, key), BUFFER_E);
  22132. /* Test valid parameters. */
  22133. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22134. hashType, hash, sizeof(hash), key), 0);
  22135. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22136. wc_ecc_free(key);
  22137. res = EXPECT_RESULT();
  22138. #endif
  22139. return res;
  22140. }
  22141. /*
  22142. * Testing wc_ecc_sm2_verify_hash_ex()
  22143. */
  22144. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22145. {
  22146. int res = TEST_SKIPPED;
  22147. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22148. defined(WOLFSSL_PUBLIC_MP)
  22149. EXPECT_DECLS;
  22150. ecc_key key[1];
  22151. mp_int r[1];
  22152. mp_int s[1];
  22153. int verified;
  22154. unsigned char pub[] = {
  22155. 0x04,
  22156. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22157. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22158. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22159. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22160. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22161. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22162. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22163. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22164. };
  22165. unsigned char hash[] = {
  22166. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22167. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22168. 0x50, 0x69, 0x5B, 0x20
  22169. };
  22170. unsigned char rData[] = {
  22171. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22172. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22173. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22174. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22175. };
  22176. unsigned char sData[] = {
  22177. 0x1D,
  22178. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22179. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22180. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22181. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22182. };
  22183. unsigned char rBadData[] = {
  22184. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22185. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22186. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22187. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22188. };
  22189. XMEMSET(key, 0, sizeof(*key));
  22190. XMEMSET(r, 0, sizeof(*r));
  22191. XMEMSET(s, 0, sizeof(*s));
  22192. ExpectIntEQ(mp_init(r), 0);
  22193. ExpectIntEQ(mp_init(s), 0);
  22194. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22195. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22196. ExpectIntEQ(wc_ecc_init(key), 0);
  22197. /* Test with no curve set. */
  22198. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22199. &verified, key), BAD_FUNC_ARG);
  22200. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22201. /* Test invalid parameters. */
  22202. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22203. NULL, NULL), BAD_FUNC_ARG);
  22204. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22205. NULL, NULL), BAD_FUNC_ARG);
  22206. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22207. NULL, NULL), BAD_FUNC_ARG);
  22208. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22209. NULL, NULL), BAD_FUNC_ARG);
  22210. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22211. &verified, NULL), BAD_FUNC_ARG);
  22212. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22213. NULL, key), BAD_FUNC_ARG);
  22214. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22215. &verified, key), BAD_FUNC_ARG);
  22216. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22217. &verified, key), BAD_FUNC_ARG);
  22218. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22219. &verified, key), BAD_FUNC_ARG);
  22220. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22221. NULL, key), BAD_FUNC_ARG);
  22222. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22223. &verified, NULL), BAD_FUNC_ARG);
  22224. /* Make key not on the SM2 curve. */
  22225. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22226. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22227. &verified, key), BAD_FUNC_ARG);
  22228. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22229. /* Test valid parameters. */
  22230. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22231. &verified, key), 0);
  22232. ExpectIntEQ(verified, 1);
  22233. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22234. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22235. &verified, key), 0);
  22236. ExpectIntEQ(verified, 0);
  22237. mp_free(s);
  22238. mp_free(r);
  22239. wc_ecc_free(key);
  22240. #ifdef FP_ECC
  22241. wc_ecc_fp_free();
  22242. #endif
  22243. res = EXPECT_RESULT();
  22244. #endif
  22245. return res;
  22246. }
  22247. /*
  22248. * Testing wc_ecc_sm2_verify_hash()
  22249. */
  22250. static int test_wc_ecc_sm2_verify_hash(void)
  22251. {
  22252. int res = TEST_SKIPPED;
  22253. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22254. EXPECT_DECLS;
  22255. ecc_key key[1];
  22256. int verified;
  22257. unsigned char pub[] = {
  22258. 0x04,
  22259. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22260. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22261. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22262. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22263. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22264. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22265. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22266. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22267. };
  22268. unsigned char hash[] = {
  22269. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22270. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22271. 0x50, 0x69, 0x5B, 0x20
  22272. };
  22273. unsigned char sig[] = {
  22274. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22275. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22276. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22277. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22278. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22279. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22280. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22281. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22282. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22283. };
  22284. unsigned char sigBad[] = {
  22285. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22286. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22287. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22288. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22289. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22290. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22291. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22292. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22293. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22294. };
  22295. XMEMSET(key, 0, sizeof(*key));
  22296. ExpectIntEQ(wc_ecc_init(key), 0);
  22297. /* Test with no curve set. */
  22298. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22299. &verified, key), BAD_FUNC_ARG);
  22300. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22301. /* Test invalid parameters. */
  22302. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22303. NULL, NULL), BAD_FUNC_ARG);
  22304. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22305. NULL, NULL), BAD_FUNC_ARG);
  22306. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22307. NULL, NULL), BAD_FUNC_ARG);
  22308. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22309. &verified, NULL), BAD_FUNC_ARG);
  22310. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22311. NULL, key), BAD_FUNC_ARG);
  22312. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22313. &verified, key), BAD_FUNC_ARG);
  22314. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22315. &verified, key), BAD_FUNC_ARG);
  22316. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22317. NULL, key), BAD_FUNC_ARG);
  22318. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22319. &verified, NULL), BAD_FUNC_ARG);
  22320. /* Make key not on the SM2 curve. */
  22321. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22322. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22323. &verified, key), BAD_FUNC_ARG);
  22324. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22325. /* Test valid parameters. */
  22326. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22327. &verified, key), 0);
  22328. ExpectIntEQ(verified, 1);
  22329. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22330. sizeof(hash), &verified, key), 0);
  22331. ExpectIntEQ(verified, 0);
  22332. wc_ecc_free(key);
  22333. #ifdef FP_ECC
  22334. wc_ecc_fp_free();
  22335. #endif
  22336. res = EXPECT_RESULT();
  22337. #endif
  22338. return res;
  22339. }
  22340. /*
  22341. * Testing wc_ecc_sm2_verify_hash_ex()
  22342. */
  22343. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22344. {
  22345. int res = TEST_SKIPPED;
  22346. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22347. defined(WOLFSSL_PUBLIC_MP)
  22348. EXPECT_DECLS;
  22349. WC_RNG rng[1];
  22350. ecc_key key[1];
  22351. mp_int r[1];
  22352. mp_int s[1];
  22353. unsigned char hash[32];
  22354. #ifdef HAVE_ECC_VERIFY
  22355. int verified;
  22356. #endif
  22357. XMEMSET(rng, 0, sizeof(*rng));
  22358. XMEMSET(key, 0, sizeof(*key));
  22359. XMEMSET(r, 0, sizeof(*r));
  22360. XMEMSET(s, 0, sizeof(*s));
  22361. ExpectIntEQ(wc_InitRng(rng), 0);
  22362. ExpectIntEQ(mp_init(r), 0);
  22363. ExpectIntEQ(mp_init(s), 0);
  22364. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22365. ExpectIntEQ(wc_ecc_init(key), 0);
  22366. /* Test with no curve set. */
  22367. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22368. BAD_FUNC_ARG);
  22369. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22370. /* Test invalid parameters. */
  22371. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22372. NULL), BAD_FUNC_ARG);
  22373. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  22374. NULL), BAD_FUNC_ARG);
  22375. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  22376. NULL), BAD_FUNC_ARG);
  22377. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  22378. NULL), BAD_FUNC_ARG);
  22379. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  22380. NULL), BAD_FUNC_ARG);
  22381. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22382. s), BAD_FUNC_ARG);
  22383. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  22384. BAD_FUNC_ARG);
  22385. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  22386. BAD_FUNC_ARG);
  22387. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  22388. BAD_FUNC_ARG);
  22389. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  22390. BAD_FUNC_ARG);
  22391. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  22392. BAD_FUNC_ARG);
  22393. /* Make key not on the SM2 curve. */
  22394. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22395. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22396. BAD_FUNC_ARG);
  22397. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22398. #ifdef WOLFSSL_SP_MATH_ALL
  22399. {
  22400. mp_int smallR[1];
  22401. sp_init_size(smallR, 1);
  22402. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  22403. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  22404. smallR, s), 0);
  22405. }
  22406. #endif
  22407. /* Test valid parameters. */
  22408. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22409. 0);
  22410. #ifdef HAVE_ECC_VERIFY
  22411. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  22412. key), 0);
  22413. ExpectIntEQ(verified, 1);
  22414. #endif
  22415. mp_free(s);
  22416. mp_free(r);
  22417. wc_ecc_free(key);
  22418. wc_FreeRng(rng);
  22419. #ifdef FP_ECC
  22420. wc_ecc_fp_free();
  22421. #endif
  22422. res = EXPECT_RESULT();
  22423. #endif
  22424. return res;
  22425. }
  22426. /*
  22427. * Testing wc_ecc_sm2_verify_hash()
  22428. */
  22429. static int test_wc_ecc_sm2_sign_hash(void)
  22430. {
  22431. int res = TEST_SKIPPED;
  22432. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  22433. EXPECT_DECLS;
  22434. WC_RNG rng[1];
  22435. ecc_key key[1];
  22436. unsigned char hash[32];
  22437. unsigned char sig[72];
  22438. word32 sigSz = sizeof(sig);
  22439. #ifdef HAVE_ECC_VERIFY
  22440. int verified;
  22441. #endif
  22442. XMEMSET(rng, 0, sizeof(*rng));
  22443. XMEMSET(key, 0, sizeof(*key));
  22444. ExpectIntEQ(wc_InitRng(rng), 0);
  22445. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22446. ExpectIntEQ(wc_ecc_init(key), 0);
  22447. /* Test with no curve set. */
  22448. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22449. BAD_FUNC_ARG);
  22450. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22451. /* Test invalid parameters. */
  22452. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22453. NULL), BAD_FUNC_ARG);
  22454. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  22455. NULL), BAD_FUNC_ARG);
  22456. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  22457. NULL), BAD_FUNC_ARG);
  22458. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  22459. NULL), BAD_FUNC_ARG);
  22460. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  22461. NULL), BAD_FUNC_ARG);
  22462. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22463. key), BAD_FUNC_ARG);
  22464. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  22465. key), BAD_FUNC_ARG);
  22466. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  22467. key), BAD_FUNC_ARG);
  22468. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  22469. key), BAD_FUNC_ARG);
  22470. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  22471. key), BAD_FUNC_ARG);
  22472. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  22473. NULL), BAD_FUNC_ARG);
  22474. /* Make key not on the SM2 curve. */
  22475. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22476. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22477. BAD_FUNC_ARG);
  22478. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22479. /* Test valid parameters. */
  22480. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22481. 0);
  22482. #ifdef HAVE_ECC_VERIFY
  22483. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  22484. &verified, key), 0);
  22485. ExpectIntEQ(verified, 1);
  22486. #endif
  22487. wc_ecc_free(key);
  22488. wc_FreeRng(rng);
  22489. #ifdef FP_ECC
  22490. wc_ecc_fp_free();
  22491. #endif
  22492. res = EXPECT_RESULT();
  22493. #endif
  22494. return res;
  22495. }
  22496. /*
  22497. * Testing ToTraditional
  22498. */
  22499. static int test_ToTraditional(void)
  22500. {
  22501. EXPECT_DECLS;
  22502. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22503. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22504. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  22505. XFILE f = XBADFILE;
  22506. byte input[TWOK_BUF];
  22507. word32 sz;
  22508. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  22509. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  22510. if (f != XBADFILE)
  22511. XFCLOSE(f);
  22512. /* Good case */
  22513. ExpectIntGT(ToTraditional(input, sz), 0);
  22514. /* Bad cases */
  22515. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  22516. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  22517. #ifdef WOLFSSL_ASN_TEMPLATE
  22518. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  22519. #else
  22520. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  22521. #endif
  22522. #endif
  22523. return EXPECT_RESULT();
  22524. } /* End test_ToTraditional*/
  22525. /*
  22526. * Testing wc_EccPrivateKeyToDer
  22527. */
  22528. static int test_wc_EccPrivateKeyToDer(void)
  22529. {
  22530. EXPECT_DECLS;
  22531. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22532. byte output[ONEK_BUF];
  22533. ecc_key eccKey;
  22534. WC_RNG rng;
  22535. word32 inLen;
  22536. word32 outLen = 0;
  22537. int ret;
  22538. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22539. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22540. ExpectIntEQ(wc_InitRng(&rng), 0);
  22541. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22542. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22543. #if defined(WOLFSSL_ASYNC_CRYPT)
  22544. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22545. #endif
  22546. ExpectIntEQ(ret, 0);
  22547. inLen = (word32)sizeof(output);
  22548. /* Bad Cases */
  22549. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22550. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22551. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  22552. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  22553. /* Good Case */
  22554. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  22555. wc_ecc_free(&eccKey);
  22556. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22557. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22558. {
  22559. /* test importing private only into a PKEY struct */
  22560. EC_KEY* ec = NULL;
  22561. EVP_PKEY* pkey = NULL;
  22562. const unsigned char* der;
  22563. der = output;
  22564. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  22565. der = output;
  22566. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  22567. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22568. if (EXPECT_FAIL()) {
  22569. EC_KEY_free(ec);
  22570. }
  22571. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22572. }
  22573. #endif
  22574. #endif
  22575. return EXPECT_RESULT();
  22576. } /* End test_wc_EccPrivateKeyToDer*/
  22577. /*
  22578. * Testing wc_DhPublicKeyDecode
  22579. */
  22580. static int test_wc_DhPublicKeyDecode(void)
  22581. {
  22582. EXPECT_DECLS;
  22583. #ifndef NO_DH
  22584. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22585. DhKey key;
  22586. word32 inOutIdx;
  22587. XMEMSET(&key, 0, sizeof(DhKey));
  22588. ExpectIntEQ(wc_InitDhKey(&key), 0);
  22589. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  22590. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22591. BAD_FUNC_ARG);
  22592. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22593. BAD_FUNC_ARG);
  22594. inOutIdx = 0;
  22595. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22596. BAD_FUNC_ARG);
  22597. inOutIdx = 0;
  22598. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22599. BAD_FUNC_ARG);
  22600. inOutIdx = 0;
  22601. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22602. sizeof_dh_pub_key_der_2048), 0);
  22603. ExpectIntNE(key.p.used, 0);
  22604. ExpectIntNE(key.g.used, 0);
  22605. ExpectIntEQ(key.q.used, 0);
  22606. ExpectIntNE(key.pub.used, 0);
  22607. ExpectIntEQ(key.priv.used, 0);
  22608. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  22609. #endif
  22610. #endif /* !NO_DH */
  22611. return EXPECT_RESULT();
  22612. }
  22613. /*
  22614. * Testing wc_Ed25519KeyToDer
  22615. */
  22616. static int test_wc_Ed25519KeyToDer(void)
  22617. {
  22618. EXPECT_DECLS;
  22619. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22620. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22621. byte output[ONEK_BUF];
  22622. ed25519_key ed25519Key;
  22623. WC_RNG rng;
  22624. word32 inLen;
  22625. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22626. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22627. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22628. ExpectIntEQ(wc_InitRng(&rng), 0);
  22629. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22630. inLen = (word32)sizeof(output);
  22631. /* Bad Cases */
  22632. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22633. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22634. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  22635. /* Good Cases */
  22636. /* length only */
  22637. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  22638. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  22639. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22640. wc_ed25519_free(&ed25519Key);
  22641. #endif
  22642. return EXPECT_RESULT();
  22643. } /* End test_wc_Ed25519KeyToDer*/
  22644. /*
  22645. * Testing wc_Ed25519PrivateKeyToDer
  22646. */
  22647. static int test_wc_Ed25519PrivateKeyToDer(void)
  22648. {
  22649. EXPECT_DECLS;
  22650. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22651. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22652. byte output[ONEK_BUF];
  22653. ed25519_key ed25519PrivKey;
  22654. WC_RNG rng;
  22655. word32 inLen;
  22656. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  22657. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22658. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  22659. ExpectIntEQ(wc_InitRng(&rng), 0);
  22660. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  22661. 0);
  22662. inLen = (word32)sizeof(output);
  22663. /* Bad Cases */
  22664. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22665. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22666. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  22667. BAD_FUNC_ARG);
  22668. /* Good Cases */
  22669. /* length only */
  22670. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  22671. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  22672. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22673. wc_ed25519_free(&ed25519PrivKey);
  22674. #endif
  22675. return EXPECT_RESULT();
  22676. } /* End test_wc_Ed25519PrivateKeyToDer*/
  22677. /*
  22678. * Testing wc_Ed448KeyToDer
  22679. */
  22680. static int test_wc_Ed448KeyToDer(void)
  22681. {
  22682. EXPECT_DECLS;
  22683. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22684. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22685. byte output[ONEK_BUF];
  22686. ed448_key ed448Key;
  22687. WC_RNG rng;
  22688. word32 inLen;
  22689. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22690. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22691. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22692. ExpectIntEQ(wc_InitRng(&rng), 0);
  22693. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22694. inLen = (word32)sizeof(output);
  22695. /* Bad Cases */
  22696. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22697. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22698. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  22699. /* Good Cases */
  22700. /* length only */
  22701. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  22702. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  22703. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22704. wc_ed448_free(&ed448Key);
  22705. #endif
  22706. return EXPECT_RESULT();
  22707. } /* End test_wc_Ed448KeyToDer*/
  22708. /*
  22709. * Testing wc_Ed448PrivateKeyToDer
  22710. */
  22711. static int test_wc_Ed448PrivateKeyToDer(void)
  22712. {
  22713. EXPECT_DECLS;
  22714. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22715. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22716. byte output[ONEK_BUF];
  22717. ed448_key ed448PrivKey;
  22718. WC_RNG rng;
  22719. word32 inLen;
  22720. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  22721. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22722. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  22723. ExpectIntEQ(wc_InitRng(&rng), 0);
  22724. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  22725. 0);
  22726. inLen = (word32)sizeof(output);
  22727. /* Bad Cases */
  22728. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22729. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22730. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  22731. BAD_FUNC_ARG);
  22732. /* Good cases */
  22733. /* length only */
  22734. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  22735. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  22736. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22737. wc_ed448_free(&ed448PrivKey);
  22738. #endif
  22739. return EXPECT_RESULT();
  22740. } /* End test_wc_Ed448PrivateKeyToDer*/
  22741. /*
  22742. * Testing wc_SetSubjectBuffer
  22743. */
  22744. static int test_wc_SetSubjectBuffer(void)
  22745. {
  22746. EXPECT_DECLS;
  22747. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  22748. Cert cert;
  22749. XFILE file = XBADFILE;
  22750. byte* der = NULL;
  22751. word32 derSz;
  22752. derSz = FOURK_BUF;
  22753. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22754. DYNAMIC_TYPE_TMP_BUFFER));
  22755. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  22756. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  22757. if (file != XBADFILE)
  22758. XFCLOSE(file);
  22759. ExpectIntEQ(wc_InitCert(&cert), 0);
  22760. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  22761. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  22762. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22763. #endif
  22764. return EXPECT_RESULT();
  22765. } /* End test_wc_SetSubjectBuffer*/
  22766. /*
  22767. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  22768. */
  22769. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  22770. {
  22771. EXPECT_DECLS;
  22772. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22773. WC_RNG rng;
  22774. Cert cert;
  22775. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22776. RsaKey rsaKey;
  22777. int bits = 2048;
  22778. #endif
  22779. #if defined(HAVE_ECC)
  22780. ecc_key eccKey;
  22781. int ret;
  22782. #endif
  22783. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22784. ed25519_key ed25519Key;
  22785. #endif
  22786. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22787. ed448_key ed448Key;
  22788. #endif
  22789. #ifndef HAVE_FIPS
  22790. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22791. #else
  22792. ExpectIntEQ(wc_InitRng(&rng), 0);
  22793. #endif
  22794. ExpectIntEQ(wc_InitCert(&cert), 0);
  22795. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22796. /* RSA */
  22797. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22798. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22799. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22800. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  22801. 0);
  22802. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22803. #endif
  22804. #if defined(HAVE_ECC)
  22805. /* ECC */
  22806. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22807. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22808. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22809. #if defined(WOLFSSL_ASYNC_CRYPT)
  22810. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22811. #endif
  22812. ExpectIntEQ(ret, 0);
  22813. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  22814. 0);
  22815. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22816. #endif
  22817. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22818. /* ED25519 */
  22819. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22820. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22821. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22822. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22823. &ed25519Key), 0);
  22824. wc_ed25519_free(&ed25519Key);
  22825. #endif
  22826. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22827. /* ED448 */
  22828. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22829. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22830. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22831. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22832. &ed448Key), 0);
  22833. wc_ed448_free(&ed448Key);
  22834. #endif
  22835. wc_FreeRng(&rng);
  22836. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22837. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  22838. return EXPECT_RESULT();
  22839. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  22840. /*
  22841. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  22842. */
  22843. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  22844. {
  22845. EXPECT_DECLS;
  22846. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22847. WC_RNG rng;
  22848. Cert cert;
  22849. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22850. RsaKey rsaKey;
  22851. int bits = 2048;
  22852. #endif
  22853. #if defined(HAVE_ECC)
  22854. ecc_key eccKey;
  22855. int ret;
  22856. #endif
  22857. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22858. ed25519_key ed25519Key;
  22859. #endif
  22860. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22861. ed448_key ed448Key;
  22862. #endif
  22863. #ifndef HAVE_FIPS
  22864. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22865. #else
  22866. ExpectIntEQ(wc_InitRng(&rng), 0);
  22867. #endif
  22868. ExpectIntEQ(wc_InitCert(&cert), 0);
  22869. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22870. /* RSA */
  22871. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22872. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22873. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22874. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  22875. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22876. #endif
  22877. #if defined(HAVE_ECC)
  22878. /* ECC */
  22879. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22880. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22881. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22882. #if defined(WOLFSSL_ASYNC_CRYPT)
  22883. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22884. #endif
  22885. ExpectIntEQ(ret, 0);
  22886. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  22887. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22888. #endif
  22889. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22890. /* ED25519 */
  22891. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22892. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22893. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22894. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22895. &ed25519Key), 0);
  22896. wc_ed25519_free(&ed25519Key);
  22897. #endif
  22898. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22899. /* ED448 */
  22900. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22901. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22902. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22903. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  22904. 0);
  22905. wc_ed448_free(&ed448Key);
  22906. #endif
  22907. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22908. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  22909. return EXPECT_RESULT();
  22910. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  22911. /*
  22912. * Testing wc_PKCS7_New()
  22913. */
  22914. static int test_wc_PKCS7_New(void)
  22915. {
  22916. EXPECT_DECLS;
  22917. #if defined(HAVE_PKCS7)
  22918. PKCS7* pkcs7 = NULL;
  22919. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  22920. wc_PKCS7_Free(pkcs7);
  22921. #endif
  22922. return EXPECT_RESULT();
  22923. } /* END test-wc_PKCS7_New */
  22924. /*
  22925. * Testing wc_PKCS7_Init()
  22926. */
  22927. static int test_wc_PKCS7_Init(void)
  22928. {
  22929. EXPECT_DECLS;
  22930. #if defined(HAVE_PKCS7)
  22931. PKCS7* pkcs7 = NULL;
  22932. void* heap = NULL;
  22933. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  22934. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  22935. /* Pass in bad args. */
  22936. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  22937. wc_PKCS7_Free(pkcs7);
  22938. #endif
  22939. return EXPECT_RESULT();
  22940. } /* END test-wc_PKCS7_Init */
  22941. /*
  22942. * Testing wc_PKCS7_InitWithCert()
  22943. */
  22944. static int test_wc_PKCS7_InitWithCert(void)
  22945. {
  22946. EXPECT_DECLS;
  22947. #if defined(HAVE_PKCS7)
  22948. PKCS7* pkcs7 = NULL;
  22949. #ifndef NO_RSA
  22950. #if defined(USE_CERT_BUFFERS_2048)
  22951. unsigned char cert[sizeof(client_cert_der_2048)];
  22952. int certSz = (int)sizeof(cert);
  22953. XMEMSET(cert, 0, certSz);
  22954. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  22955. #elif defined(USE_CERT_BUFFERS_1024)
  22956. unsigned char cert[sizeof(client_cert_der_1024)];
  22957. int certSz = (int)sizeof(cert);
  22958. XMEMSET(cert, 0, certSz);
  22959. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  22960. #else
  22961. unsigned char cert[ONEK_BUF];
  22962. XFILE fp = XBADFILE;
  22963. int certSz;
  22964. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22965. XBADFILE);
  22966. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22967. fp), 0);
  22968. if (fp != XBADFILE)
  22969. XFCLOSE(fp);
  22970. #endif
  22971. #elif defined(HAVE_ECC)
  22972. #if defined(USE_CERT_BUFFERS_256)
  22973. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22974. int certSz = (int)sizeof(cert);
  22975. XMEMSET(cert, 0, certSz);
  22976. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  22977. #else
  22978. unsigned char cert[ONEK_BUF];
  22979. XFILE fp = XBADFILE;
  22980. int certSz;
  22981. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22982. XBADFILE);
  22983. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  22984. fp), 0);
  22985. if (fp != XBADFILE)
  22986. XFCLOSE(fp);
  22987. #endif
  22988. #else
  22989. #error PKCS7 requires ECC or RSA
  22990. #endif
  22991. #ifdef HAVE_ECC
  22992. {
  22993. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  22994. static unsigned char certWithInvalidEccKey[] = {
  22995. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  22996. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  22997. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  22998. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  22999. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23000. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23001. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23002. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23003. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23004. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23005. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23006. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23007. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23008. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23009. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23010. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23011. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23012. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23013. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23014. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23015. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23016. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23017. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23018. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23019. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23020. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23021. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23022. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23023. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23024. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23025. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23026. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23027. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23028. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23029. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23030. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23031. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23032. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23033. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23034. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23035. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23036. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23037. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23038. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23039. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23040. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23041. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23042. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23043. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23044. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23045. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23046. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23047. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23048. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23049. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23050. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23051. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23052. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23053. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23054. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23055. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23056. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23057. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23058. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23059. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23060. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23061. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23062. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23063. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23064. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23065. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23066. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23067. 0x08, 0xA8, 0xB4
  23068. };
  23069. #endif
  23070. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23071. /* If initialization is not successful, it's free'd in init func. */
  23072. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23073. 0);
  23074. wc_PKCS7_Free(pkcs7);
  23075. pkcs7 = NULL;
  23076. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23077. /* Valid initialization usage. */
  23078. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23079. /* Pass in bad args. No need free for null checks, free at end.*/
  23080. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23081. BAD_FUNC_ARG);
  23082. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23083. BAD_FUNC_ARG);
  23084. #ifdef HAVE_ECC
  23085. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23086. sizeof(certWithInvalidEccKey)), 0);
  23087. }
  23088. #endif
  23089. wc_PKCS7_Free(pkcs7);
  23090. #endif
  23091. return EXPECT_RESULT();
  23092. } /* END test_wc_PKCS7_InitWithCert */
  23093. /*
  23094. * Testing wc_PKCS7_EncodeData()
  23095. */
  23096. static int test_wc_PKCS7_EncodeData(void)
  23097. {
  23098. EXPECT_DECLS;
  23099. #if defined(HAVE_PKCS7)
  23100. PKCS7* pkcs7 = NULL;
  23101. byte output[FOURK_BUF];
  23102. byte data[] = "My encoded DER cert.";
  23103. #ifndef NO_RSA
  23104. #if defined(USE_CERT_BUFFERS_2048)
  23105. unsigned char cert[sizeof(client_cert_der_2048)];
  23106. unsigned char key[sizeof(client_key_der_2048)];
  23107. int certSz = (int)sizeof(cert);
  23108. int keySz = (int)sizeof(key);
  23109. XMEMSET(cert, 0, certSz);
  23110. XMEMSET(key, 0, keySz);
  23111. XMEMCPY(cert, client_cert_der_2048, certSz);
  23112. XMEMCPY(key, client_key_der_2048, keySz);
  23113. #elif defined(USE_CERT_BUFFERS_1024)
  23114. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23115. unsigned char key[sizeof_client_key_der_1024];
  23116. int certSz = (int)sizeof(cert);
  23117. int keySz = (int)sizeof(key);
  23118. XMEMSET(cert, 0, certSz);
  23119. XMEMSET(key, 0, keySz);
  23120. XMEMCPY(cert, client_cert_der_1024, certSz);
  23121. XMEMCPY(key, client_key_der_1024, keySz);
  23122. #else
  23123. unsigned char cert[ONEK_BUF];
  23124. unsigned char key[ONEK_BUF];
  23125. XFILE fp = XBADFILE;
  23126. int certSz;
  23127. int keySz;
  23128. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23129. XBADFILE);
  23130. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23131. fp), 0);
  23132. if (fp != XBADFILE) {
  23133. XFCLOSE(fp);
  23134. fp = XBADFILE;
  23135. }
  23136. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23137. XBADFILE);
  23138. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23139. 0);
  23140. if (fp != XBADFILE)
  23141. XFCLOSE(fp);
  23142. #endif
  23143. #elif defined(HAVE_ECC)
  23144. #if defined(USE_CERT_BUFFERS_256)
  23145. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23146. unsigned char key[sizeof(ecc_clikey_der_256)];
  23147. int certSz = (int)sizeof(cert);
  23148. int keySz = (int)sizeof(key);
  23149. XMEMSET(cert, 0, certSz);
  23150. XMEMSET(key, 0, keySz);
  23151. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23152. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23153. #else
  23154. unsigned char cert[ONEK_BUF];
  23155. unsigned char key[ONEK_BUF];
  23156. XFILE fp = XBADFILE;
  23157. int certSz, keySz;
  23158. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23159. XBADFILE);
  23160. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23161. fp), 0);
  23162. if (fp != XBADFILE) {
  23163. XFCLOSE(fp);
  23164. fp = XBADFILE;
  23165. }
  23166. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23167. XBADFILE);
  23168. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23169. 0);
  23170. if (fp != XBADFILE)
  23171. XFCLOSE(fp);
  23172. #endif
  23173. #endif
  23174. XMEMSET(output, 0, sizeof(output));
  23175. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23176. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23177. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23178. if (pkcs7 != NULL) {
  23179. pkcs7->content = data;
  23180. pkcs7->contentSz = sizeof(data);
  23181. pkcs7->privateKey = key;
  23182. pkcs7->privateKeySz = keySz;
  23183. }
  23184. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23185. /* Test bad args. */
  23186. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23187. BAD_FUNC_ARG);
  23188. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23189. BAD_FUNC_ARG);
  23190. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23191. wc_PKCS7_Free(pkcs7);
  23192. #endif
  23193. return EXPECT_RESULT();
  23194. } /* END test_wc_PKCS7_EncodeData */
  23195. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23196. !defined(NO_RSA) && !defined(NO_SHA256)
  23197. /* RSA sign raw digest callback */
  23198. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23199. byte* out, word32 outSz, byte* privateKey,
  23200. word32 privateKeySz, int devid, int hashOID)
  23201. {
  23202. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23203. byte digInfoEncoding[] = {
  23204. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23205. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23206. 0x00, 0x04, 0x20
  23207. };
  23208. int ret;
  23209. byte digestInfo[ONEK_BUF];
  23210. byte sig[FOURK_BUF];
  23211. word32 digestInfoSz = 0;
  23212. word32 idx = 0;
  23213. RsaKey rsa;
  23214. /* SHA-256 required only for this example callback due to above
  23215. * digInfoEncoding[] */
  23216. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23217. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23218. (hashOID != SHA256h)) {
  23219. return -1;
  23220. }
  23221. /* build DigestInfo */
  23222. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23223. digestInfoSz += sizeof(digInfoEncoding);
  23224. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23225. digestInfoSz += digestSz;
  23226. /* set up RSA key */
  23227. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23228. if (ret != 0) {
  23229. return ret;
  23230. }
  23231. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23232. /* sign DigestInfo */
  23233. if (ret == 0) {
  23234. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23235. &rsa, pkcs7->rng);
  23236. if (ret > 0) {
  23237. if (ret > (int)outSz) {
  23238. /* output buffer too small */
  23239. ret = -1;
  23240. }
  23241. else {
  23242. /* success, ret holds sig size */
  23243. XMEMCPY(out, sig, ret);
  23244. }
  23245. }
  23246. }
  23247. wc_FreeRsaKey(&rsa);
  23248. return ret;
  23249. }
  23250. #endif
  23251. /*
  23252. * Testing wc_PKCS7_EncodeSignedData()
  23253. */
  23254. static int test_wc_PKCS7_EncodeSignedData(void)
  23255. {
  23256. EXPECT_DECLS;
  23257. #if defined(HAVE_PKCS7)
  23258. PKCS7* pkcs7 = NULL;
  23259. WC_RNG rng;
  23260. byte output[FOURK_BUF];
  23261. byte badOut[1];
  23262. word32 outputSz = (word32)sizeof(output);
  23263. word32 badOutSz = 0;
  23264. byte data[] = "Test data to encode.";
  23265. #ifndef NO_RSA
  23266. #if defined(USE_CERT_BUFFERS_2048)
  23267. byte key[sizeof(client_key_der_2048)];
  23268. byte cert[sizeof(client_cert_der_2048)];
  23269. word32 keySz = (word32)sizeof(key);
  23270. word32 certSz = (word32)sizeof(cert);
  23271. XMEMSET(key, 0, keySz);
  23272. XMEMSET(cert, 0, certSz);
  23273. XMEMCPY(key, client_key_der_2048, keySz);
  23274. XMEMCPY(cert, client_cert_der_2048, certSz);
  23275. #elif defined(USE_CERT_BUFFERS_1024)
  23276. byte key[sizeof_client_key_der_1024];
  23277. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23278. word32 keySz = (word32)sizeof(key);
  23279. word32 certSz = (word32)sizeof(cert);
  23280. XMEMSET(key, 0, keySz);
  23281. XMEMSET(cert, 0, certSz);
  23282. XMEMCPY(key, client_key_der_1024, keySz);
  23283. XMEMCPY(cert, client_cert_der_1024, certSz);
  23284. #else
  23285. unsigned char cert[ONEK_BUF];
  23286. unsigned char key[ONEK_BUF];
  23287. XFILE fp = XBADFILE;
  23288. int certSz;
  23289. int keySz;
  23290. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23291. XBADFILE);
  23292. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23293. fp), 0);
  23294. if (fp != XBADFILE) {
  23295. XFCLOSE(fp);
  23296. fp = XBADFILE;
  23297. }
  23298. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23299. XBADFILE);
  23300. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23301. 0);
  23302. if (fp != XBADFILE)
  23303. XFCLOSE(fp);
  23304. #endif
  23305. #elif defined(HAVE_ECC)
  23306. #if defined(USE_CERT_BUFFERS_256)
  23307. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23308. unsigned char key[sizeof(ecc_clikey_der_256)];
  23309. int certSz = (int)sizeof(cert);
  23310. int keySz = (int)sizeof(key);
  23311. XMEMSET(cert, 0, certSz);
  23312. XMEMSET(key, 0, keySz);
  23313. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23314. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23315. #else
  23316. unsigned char cert[ONEK_BUF];
  23317. unsigned char key[ONEK_BUF];
  23318. XFILE fp = XBADFILE;
  23319. int certSz;
  23320. int keySz;
  23321. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23322. XBADFILE);
  23323. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23324. if (fp != XBADFILE) {
  23325. XFCLOSE(fp);
  23326. fp = XBADFILE;
  23327. }
  23328. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23329. XBADFILE);
  23330. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23331. if (fp != XBADFILE)
  23332. XFCLOSE(fp);
  23333. #endif
  23334. #endif
  23335. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23336. XMEMSET(output, 0, outputSz);
  23337. ExpectIntEQ(wc_InitRng(&rng), 0);
  23338. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23339. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23340. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23341. if (pkcs7 != NULL) {
  23342. pkcs7->content = data;
  23343. pkcs7->contentSz = (word32)sizeof(data);
  23344. pkcs7->privateKey = key;
  23345. pkcs7->privateKeySz = (word32)sizeof(key);
  23346. pkcs7->encryptOID = RSAk;
  23347. #ifdef NO_SHA
  23348. pkcs7->hashOID = SHA256h;
  23349. #else
  23350. pkcs7->hashOID = SHAh;
  23351. #endif
  23352. pkcs7->rng = &rng;
  23353. }
  23354. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23355. wc_PKCS7_Free(pkcs7);
  23356. pkcs7 = NULL;
  23357. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23358. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23359. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23360. /* Pass in bad args. */
  23361. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23362. BAD_FUNC_ARG);
  23363. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23364. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23365. badOutSz), BAD_FUNC_ARG);
  23366. if (pkcs7 != NULL) {
  23367. pkcs7->hashOID = 0; /* bad hashOID */
  23368. }
  23369. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23370. BAD_FUNC_ARG);
  23371. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23372. !defined(NO_RSA) && !defined(NO_SHA256)
  23373. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23374. * Example callback assumes SHA-256, so only run test if compiled in. */
  23375. wc_PKCS7_Free(pkcs7);
  23376. pkcs7 = NULL;
  23377. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23378. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23379. if (pkcs7 != NULL) {
  23380. pkcs7->content = data;
  23381. pkcs7->contentSz = (word32)sizeof(data);
  23382. pkcs7->privateKey = key;
  23383. pkcs7->privateKeySz = (word32)sizeof(key);
  23384. pkcs7->encryptOID = RSAk;
  23385. pkcs7->hashOID = SHA256h;
  23386. pkcs7->rng = &rng;
  23387. }
  23388. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23389. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23390. #endif
  23391. wc_PKCS7_Free(pkcs7);
  23392. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23393. #endif
  23394. return EXPECT_RESULT();
  23395. } /* END test_wc_PKCS7_EncodeSignedData */
  23396. /*
  23397. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23398. */
  23399. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23400. {
  23401. EXPECT_DECLS;
  23402. #if defined(HAVE_PKCS7)
  23403. int i;
  23404. PKCS7* pkcs7 = NULL;
  23405. WC_RNG rng;
  23406. byte outputHead[FOURK_BUF/2];
  23407. byte outputFoot[FOURK_BUF/2];
  23408. word32 outputHeadSz = (word32)sizeof(outputHead);
  23409. word32 outputFootSz = (word32)sizeof(outputFoot);
  23410. byte data[FOURK_BUF];
  23411. wc_HashAlg hash;
  23412. #ifdef NO_SHA
  23413. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23414. #else
  23415. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23416. #endif
  23417. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23418. word32 hashSz = wc_HashGetDigestSize(hashType);
  23419. #ifndef NO_RSA
  23420. #if defined(USE_CERT_BUFFERS_2048)
  23421. byte key[sizeof(client_key_der_2048)];
  23422. byte cert[sizeof(client_cert_der_2048)];
  23423. word32 keySz = (word32)sizeof(key);
  23424. word32 certSz = (word32)sizeof(cert);
  23425. XMEMSET(key, 0, keySz);
  23426. XMEMSET(cert, 0, certSz);
  23427. XMEMCPY(key, client_key_der_2048, keySz);
  23428. XMEMCPY(cert, client_cert_der_2048, certSz);
  23429. #elif defined(USE_CERT_BUFFERS_1024)
  23430. byte key[sizeof_client_key_der_1024];
  23431. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23432. word32 keySz = (word32)sizeof(key);
  23433. word32 certSz = (word32)sizeof(cert);
  23434. XMEMSET(key, 0, keySz);
  23435. XMEMSET(cert, 0, certSz);
  23436. XMEMCPY(key, client_key_der_1024, keySz);
  23437. XMEMCPY(cert, client_cert_der_1024, certSz);
  23438. #else
  23439. unsigned char cert[ONEK_BUF];
  23440. unsigned char key[ONEK_BUF];
  23441. XFILE fp = XBADFILE;
  23442. int certSz;
  23443. int keySz;
  23444. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23445. XBADFILE);
  23446. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23447. fp), 0);
  23448. if (fp != XBADFILE) {
  23449. XFCLOSE(fp);
  23450. fp = XBADFILE;
  23451. }
  23452. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23453. XBADFILE);
  23454. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23455. 0);
  23456. if (fp != XBADFILE)
  23457. XFCLOSE(fp);
  23458. #endif
  23459. #elif defined(HAVE_ECC)
  23460. #if defined(USE_CERT_BUFFERS_256)
  23461. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23462. unsigned char key[sizeof(ecc_clikey_der_256)];
  23463. int certSz = (int)sizeof(cert);
  23464. int keySz = (int)sizeof(key);
  23465. XMEMSET(cert, 0, certSz);
  23466. XMEMSET(key, 0, keySz);
  23467. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23468. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23469. #else
  23470. unsigned char cert[ONEK_BUF];
  23471. unsigned char key[ONEK_BUF];
  23472. XFILE fp = XBADFILE;
  23473. int certSz;
  23474. int keySz;
  23475. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23476. XBADFILE);
  23477. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23478. fp), 0);
  23479. if (fp != XBADFILE) {
  23480. XFCLOSE(fp);
  23481. fp = XBADFILE;
  23482. }
  23483. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23484. XBADFILE);
  23485. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23486. 0);
  23487. if (fp != XBADFILE)
  23488. XFCLOSE(fp);
  23489. #endif
  23490. #endif
  23491. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23492. /* initialize large data with sequence */
  23493. for (i=0; i<(int)sizeof(data); i++)
  23494. data[i] = i & 0xff;
  23495. XMEMSET(outputHead, 0, outputHeadSz);
  23496. XMEMSET(outputFoot, 0, outputFootSz);
  23497. ExpectIntEQ(wc_InitRng(&rng), 0);
  23498. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23499. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23500. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23501. if (pkcs7 != NULL) {
  23502. pkcs7->content = NULL; /* not used for ex */
  23503. pkcs7->contentSz = (word32)sizeof(data);
  23504. pkcs7->privateKey = key;
  23505. pkcs7->privateKeySz = (word32)sizeof(key);
  23506. pkcs7->encryptOID = RSAk;
  23507. #ifdef NO_SHA
  23508. pkcs7->hashOID = SHA256h;
  23509. #else
  23510. pkcs7->hashOID = SHAh;
  23511. #endif
  23512. pkcs7->rng = &rng;
  23513. }
  23514. /* calculate hash for content */
  23515. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23516. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23517. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23518. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23519. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23520. /* Perform PKCS7 sign using hash directly */
  23521. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23522. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23523. ExpectIntGT(outputHeadSz, 0);
  23524. ExpectIntGT(outputFootSz, 0);
  23525. wc_PKCS7_Free(pkcs7);
  23526. pkcs7 = NULL;
  23527. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23528. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23529. /* required parameter even on verify when using _ex, if using outputHead
  23530. * and outputFoot */
  23531. if (pkcs7 != NULL) {
  23532. pkcs7->contentSz = (word32)sizeof(data);
  23533. }
  23534. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23535. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23536. wc_PKCS7_Free(pkcs7);
  23537. pkcs7 = NULL;
  23538. /* assembly complete PKCS7 sign and use normal verify */
  23539. {
  23540. byte* output = NULL;
  23541. word32 outputSz = 0;
  23542. ExpectNotNull(output = (byte*)XMALLOC(
  23543. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23544. DYNAMIC_TYPE_TMP_BUFFER));
  23545. if (output != NULL) {
  23546. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23547. outputSz += outputHeadSz;
  23548. XMEMCPY(&output[outputSz], data, sizeof(data));
  23549. outputSz += sizeof(data);
  23550. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23551. outputSz += outputFootSz;
  23552. }
  23553. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23554. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23555. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23556. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23557. }
  23558. /* Pass in bad args. */
  23559. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23560. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23561. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23562. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23563. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23564. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23565. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23566. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23567. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23568. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23569. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23570. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23571. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23572. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23573. if (pkcs7 != NULL) {
  23574. pkcs7->hashOID = 0; /* bad hashOID */
  23575. }
  23576. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23577. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23578. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23579. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23580. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23581. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23582. #ifndef NO_PKCS7_STREAM
  23583. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23584. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23585. #else
  23586. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23587. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23588. #endif
  23589. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23590. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23591. #ifndef NO_PKCS7_STREAM
  23592. /* can pass in 0 buffer length with streaming API */
  23593. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23594. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23595. #else
  23596. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23597. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23598. #endif
  23599. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23600. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23601. #ifndef NO_PKCS7_STREAM
  23602. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23603. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23604. #else
  23605. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23606. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  23607. #endif
  23608. wc_PKCS7_Free(pkcs7);
  23609. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23610. #endif
  23611. return EXPECT_RESULT();
  23612. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23613. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23614. /**
  23615. * Loads certs/keys from files or buffers into the argument buffers,
  23616. * helper function called by CreatePKCS7SignedData().
  23617. *
  23618. * Returns 0 on success, negative on error.
  23619. */
  23620. static int LoadPKCS7SignedDataCerts(
  23621. int useIntermediateCertChain, int pkAlgoType,
  23622. byte* intCARoot, word32* intCARootSz,
  23623. byte* intCA1, word32* intCA1Sz,
  23624. byte* intCA2, word32* intCA2Sz,
  23625. byte* cert, word32* certSz,
  23626. byte* key, word32* keySz)
  23627. {
  23628. EXPECT_DECLS;
  23629. int ret = 0;
  23630. XFILE fp = XBADFILE;
  23631. #ifndef NO_RSA
  23632. const char* intCARootRSA = "./certs/ca-cert.der";
  23633. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  23634. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  23635. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  23636. const char* intServKeyRSA = "./certs/server-key.der";
  23637. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  23638. const char* cli1024Cert = "./certs/1024/client-cert.der";
  23639. const char* cli1024Key = "./certs/1024/client-key.der";
  23640. #endif
  23641. #endif
  23642. #ifdef HAVE_ECC
  23643. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  23644. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  23645. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  23646. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  23647. const char* intServKeyECC = "./certs/ecc-key.der";
  23648. #ifndef USE_CERT_BUFFERS_256
  23649. const char* cliEccCert = "./certs/client-ecc-cert.der";
  23650. const char* cliEccKey = "./certs/client-ecc-key.der";
  23651. #endif
  23652. #endif
  23653. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  23654. ((useIntermediateCertChain == 1) &&
  23655. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  23656. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  23657. return BAD_FUNC_ARG;
  23658. }
  23659. /* Read/load certs and keys to use for signing based on PK type and chain */
  23660. switch (pkAlgoType) {
  23661. #ifndef NO_RSA
  23662. case RSA_TYPE:
  23663. if (useIntermediateCertChain == 1) {
  23664. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  23665. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  23666. if (fp != XBADFILE) {
  23667. XFCLOSE(fp);
  23668. fp = XBADFILE;
  23669. }
  23670. ExpectIntGT(*intCARootSz, 0);
  23671. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  23672. if (fp != XBADFILE) {
  23673. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23674. XFCLOSE(fp);
  23675. fp = XBADFILE;
  23676. }
  23677. ExpectIntGT(*intCA1Sz, 0);
  23678. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  23679. if (fp != XBADFILE) {
  23680. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23681. XFCLOSE(fp);
  23682. fp = XBADFILE;
  23683. }
  23684. ExpectIntGT(*intCA2Sz, 0);
  23685. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  23686. if (fp != XBADFILE) {
  23687. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23688. XFCLOSE(fp);
  23689. fp = XBADFILE;
  23690. }
  23691. ExpectIntGT(*certSz, 0);
  23692. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  23693. if (fp != XBADFILE) {
  23694. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23695. XFCLOSE(fp);
  23696. fp = XBADFILE;
  23697. }
  23698. ExpectIntGT(*keySz, 0);
  23699. }
  23700. else {
  23701. #if defined(USE_CERT_BUFFERS_2048)
  23702. *keySz = sizeof_client_key_der_2048;
  23703. *certSz = sizeof_client_cert_der_2048;
  23704. XMEMCPY(key, client_key_der_2048, *keySz);
  23705. XMEMCPY(cert, client_cert_der_2048, *certSz);
  23706. #elif defined(USE_CERT_BUFFERS_1024)
  23707. *keySz = sizeof_client_key_der_1024;
  23708. *certSz = sizeof_client_cert_der_1024;
  23709. XMEMCPY(key, client_key_der_1024, *keySz);
  23710. XMEMCPY(cert, client_cert_der_1024, *certSz);
  23711. #else
  23712. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  23713. if (fp != XBADFILE) {
  23714. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23715. XFCLOSE(fp);
  23716. fp = XBADFILE;
  23717. }
  23718. ExpectIntGT(*keySz, 0);
  23719. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  23720. if (fp != XBADFILE) {
  23721. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23722. XFCLOSE(fp);
  23723. fp = XBADFILE;
  23724. }
  23725. ExpectIntGT(*certSz, 0);
  23726. #endif /* USE_CERT_BUFFERS_2048 */
  23727. }
  23728. break;
  23729. #endif /* !NO_RSA */
  23730. #ifdef HAVE_ECC
  23731. case ECC_TYPE:
  23732. if (useIntermediateCertChain == 1) {
  23733. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  23734. if (fp != XBADFILE) {
  23735. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  23736. fp);
  23737. XFCLOSE(fp);
  23738. fp = XBADFILE;
  23739. }
  23740. ExpectIntGT(*intCARootSz, 0);
  23741. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  23742. if (fp != XBADFILE) {
  23743. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23744. XFCLOSE(fp);
  23745. fp = XBADFILE;
  23746. }
  23747. ExpectIntGT(*intCA1Sz, 0);
  23748. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  23749. if (fp != XBADFILE) {
  23750. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23751. XFCLOSE(fp);
  23752. fp = XBADFILE;
  23753. }
  23754. ExpectIntGT(*intCA2Sz, 0);
  23755. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  23756. if (fp != XBADFILE) {
  23757. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23758. XFCLOSE(fp);
  23759. fp = XBADFILE;
  23760. }
  23761. ExpectIntGT(*certSz, 0);
  23762. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  23763. if (fp != XBADFILE) {
  23764. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23765. XFCLOSE(fp);
  23766. fp = XBADFILE;
  23767. }
  23768. ExpectIntGT(*keySz, 0);
  23769. }
  23770. else {
  23771. #if defined(USE_CERT_BUFFERS_256)
  23772. *keySz = sizeof_ecc_clikey_der_256;
  23773. *certSz = sizeof_cliecc_cert_der_256;
  23774. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  23775. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  23776. #else
  23777. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  23778. if (fp != XBADFILE) {
  23779. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23780. XFCLOSE(fp);
  23781. fp = XBADFILE;
  23782. }
  23783. ExpectIntGT(*keySz, 0);
  23784. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  23785. if (fp != XBADFILE) {
  23786. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23787. XFCLOSE(fp);
  23788. fp = XBADFILE;
  23789. }
  23790. ExpectIntGT(*certSz, 0);
  23791. #endif /* USE_CERT_BUFFERS_256 */
  23792. }
  23793. break;
  23794. #endif /* HAVE_ECC */
  23795. default:
  23796. WOLFSSL_MSG("Unsupported SignedData PK type");
  23797. ret = BAD_FUNC_ARG;
  23798. break;
  23799. }
  23800. if (EXPECT_FAIL() && (ret == 0)) {
  23801. ret = BAD_FUNC_ARG;
  23802. }
  23803. return ret;
  23804. }
  23805. /**
  23806. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  23807. *
  23808. * output output buffer to place SignedData
  23809. * outputSz size of output buffer
  23810. * data data buffer to be signed
  23811. * dataSz size of data buffer
  23812. * withAttribs [1/0] include attributes in SignedData message
  23813. * detachedSig [1/0] create detached signature, no content
  23814. * useIntCertChain [1/0] use certificate chain and include intermediate and
  23815. * root CAs in bundle
  23816. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  23817. *
  23818. * Return size of bundle created on success, negative on error */
  23819. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  23820. byte* data, word32 dataSz,
  23821. int withAttribs, int detachedSig,
  23822. int useIntermediateCertChain,
  23823. int pkAlgoType)
  23824. {
  23825. EXPECT_DECLS;
  23826. int ret = 0;
  23827. WC_RNG rng;
  23828. PKCS7* pkcs7 = NULL;
  23829. static byte messageTypeOid[] =
  23830. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23831. 0x09, 0x02 };
  23832. static byte messageType[] = { 0x13, 2, '1', '9' };
  23833. PKCS7Attrib attribs[] =
  23834. {
  23835. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23836. sizeof(messageType) }
  23837. };
  23838. byte intCARoot[TWOK_BUF];
  23839. byte intCA1[TWOK_BUF];
  23840. byte intCA2[TWOK_BUF];
  23841. byte cert[TWOK_BUF];
  23842. byte key[TWOK_BUF];
  23843. word32 intCARootSz = sizeof(intCARoot);
  23844. word32 intCA1Sz = sizeof(intCA1);
  23845. word32 intCA2Sz = sizeof(intCA2);
  23846. word32 certSz = sizeof(cert);
  23847. word32 keySz = sizeof(key);
  23848. XMEMSET(intCARoot, 0, intCARootSz);
  23849. XMEMSET(intCA1, 0, intCA1Sz);
  23850. XMEMSET(intCA2, 0, intCA2Sz);
  23851. XMEMSET(cert, 0, certSz);
  23852. XMEMSET(key, 0, keySz);
  23853. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  23854. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  23855. cert, &certSz, key, &keySz);
  23856. ExpectIntEQ(ret, 0);
  23857. XMEMSET(output, 0, outputSz);
  23858. ExpectIntEQ(wc_InitRng(&rng), 0);
  23859. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23860. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23861. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23862. if (useIntermediateCertChain == 1) {
  23863. /* Add intermediate and root CA certs into SignedData Certs SET */
  23864. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  23865. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  23866. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  23867. }
  23868. if (pkcs7 != NULL) {
  23869. pkcs7->content = data;
  23870. pkcs7->contentSz = dataSz;
  23871. pkcs7->privateKey = key;
  23872. pkcs7->privateKeySz = (word32)sizeof(key);
  23873. if (pkAlgoType == RSA_TYPE) {
  23874. pkcs7->encryptOID = RSAk;
  23875. }
  23876. else {
  23877. pkcs7->encryptOID = ECDSAk;
  23878. }
  23879. #ifdef NO_SHA
  23880. pkcs7->hashOID = SHA256h;
  23881. #else
  23882. pkcs7->hashOID = SHAh;
  23883. #endif
  23884. pkcs7->rng = &rng;
  23885. if (withAttribs) {
  23886. /* include a signed attribute */
  23887. pkcs7->signedAttribs = attribs;
  23888. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  23889. }
  23890. }
  23891. if (detachedSig) {
  23892. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  23893. }
  23894. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  23895. ExpectIntGT(outputSz, 0);
  23896. wc_PKCS7_Free(pkcs7);
  23897. pkcs7 = NULL;
  23898. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23899. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23900. if (detachedSig && (pkcs7 != NULL)) {
  23901. pkcs7->content = data;
  23902. pkcs7->contentSz = dataSz;
  23903. }
  23904. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23905. wc_PKCS7_Free(pkcs7);
  23906. wc_FreeRng(&rng);
  23907. if (EXPECT_FAIL()) {
  23908. outputSz = 0;
  23909. }
  23910. return outputSz;
  23911. }
  23912. #endif
  23913. /*
  23914. * Testing wc_PKCS_VerifySignedData()
  23915. */
  23916. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  23917. {
  23918. EXPECT_DECLS;
  23919. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  23920. PKCS7* pkcs7 = NULL;
  23921. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23922. word32 outputSz = sizeof(output);
  23923. byte data[] = "Test data to encode.";
  23924. byte badOut[1];
  23925. word32 badOutSz = 0;
  23926. byte badContent[] = "This is different content than was signed";
  23927. wc_HashAlg hash;
  23928. #ifdef NO_SHA
  23929. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23930. #else
  23931. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23932. #endif
  23933. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23934. word32 hashSz = wc_HashGetDigestSize(hashType);
  23935. #ifndef NO_RSA
  23936. PKCS7DecodedAttrib* decodedAttrib = NULL;
  23937. /* contentType OID (1.2.840.113549.1.9.3) */
  23938. static const byte contentTypeOid[] =
  23939. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  23940. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  23941. static const byte dataType[] =
  23942. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  23943. /* messageDigest OID (1.2.840.113549.1.9.4) */
  23944. static const byte messageDigestOid[] =
  23945. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  23946. #ifndef NO_ASN_TIME
  23947. /* signingTime OID () */
  23948. static const byte signingTimeOid[] =
  23949. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  23950. #endif
  23951. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23952. int dateLength = 0;
  23953. byte dateFormat;
  23954. const byte* datePart = NULL;
  23955. struct tm timearg;
  23956. time_t now;
  23957. struct tm* nowTm = NULL;
  23958. #ifdef NEED_TMP_TIME
  23959. struct tm tmpTimeStorage;
  23960. struct tm* tmpTime = &tmpTimeStorage;
  23961. #endif
  23962. #endif /* !NO_ASN && !NO_ASN_TIME */
  23963. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23964. /* Success test with RSA certs/key */
  23965. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23966. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  23967. /* calculate hash for content, used later */
  23968. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23969. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23970. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23971. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23972. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23973. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23974. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23975. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23976. /* Check that decoded signed attributes are correct */
  23977. /* messageDigest should be first */
  23978. if (pkcs7 != NULL) {
  23979. decodedAttrib = pkcs7->decodedAttrib;
  23980. }
  23981. ExpectNotNull(decodedAttrib);
  23982. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  23983. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  23984. decodedAttrib->oidSz), 0);
  23985. /* + 2 for OCTET STRING and length bytes */
  23986. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  23987. ExpectNotNull(decodedAttrib->value);
  23988. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  23989. #ifndef NO_ASN_TIME
  23990. /* signingTime should be second */
  23991. if (decodedAttrib != NULL) {
  23992. decodedAttrib = decodedAttrib->next;
  23993. }
  23994. ExpectNotNull(decodedAttrib);
  23995. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  23996. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  23997. decodedAttrib->oidSz), 0);
  23998. ExpectIntGT(decodedAttrib->valueSz, 0);
  23999. ExpectNotNull(decodedAttrib->value);
  24000. #endif
  24001. /* Verify signingTime if ASN and time are available */
  24002. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24003. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24004. &datePart, &dateFormat, &dateLength), 0);
  24005. ExpectNotNull(datePart);
  24006. ExpectIntGT(dateLength, 0);
  24007. XMEMSET(&timearg, 0, sizeof(timearg));
  24008. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24009. &timearg), 0);
  24010. /* Get current time and compare year/month/day against attribute value */
  24011. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24012. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24013. ExpectNotNull(nowTm);
  24014. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24015. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24016. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24017. #endif /* !NO_ASN && !NO_ASN_TIME */
  24018. /* contentType should be third */
  24019. if (decodedAttrib != NULL) {
  24020. decodedAttrib = decodedAttrib->next;
  24021. }
  24022. ExpectNotNull(decodedAttrib);
  24023. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24024. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24025. decodedAttrib->oidSz), 0);
  24026. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24027. ExpectNotNull(decodedAttrib->value);
  24028. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24029. 0);
  24030. #endif /* !NO_RSA */
  24031. /* Test bad args. */
  24032. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24033. BAD_FUNC_ARG);
  24034. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24035. BAD_FUNC_ARG);
  24036. #ifndef NO_PKCS7_STREAM
  24037. /* can pass in 0 buffer length with streaming API */
  24038. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24039. badOutSz), WC_PKCS7_WANT_READ_E);
  24040. #else
  24041. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24042. badOutSz), BAD_FUNC_ARG);
  24043. #endif
  24044. wc_PKCS7_Free(pkcs7);
  24045. pkcs7 = NULL;
  24046. #ifndef NO_RSA
  24047. /* Try RSA certs/key/sig first */
  24048. outputSz = sizeof(output);
  24049. XMEMSET(output, 0, outputSz);
  24050. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24051. (word32)sizeof(data),
  24052. 1, 1, 0, RSA_TYPE)), 0);
  24053. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24054. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24055. if (pkcs7 != NULL) {
  24056. pkcs7->content = badContent;
  24057. pkcs7->contentSz = sizeof(badContent);
  24058. }
  24059. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24060. SIG_VERIFY_E);
  24061. wc_PKCS7_Free(pkcs7);
  24062. pkcs7 = NULL;
  24063. /* Test success case with detached signature and valid content */
  24064. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24065. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24066. if (pkcs7 != NULL) {
  24067. pkcs7->content = data;
  24068. pkcs7->contentSz = sizeof(data);
  24069. }
  24070. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24071. wc_PKCS7_Free(pkcs7);
  24072. pkcs7 = NULL;
  24073. /* verify using pre-computed content digest only (no content) */
  24074. {
  24075. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24076. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24077. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24078. output, outputSz, NULL, 0), 0);
  24079. wc_PKCS7_Free(pkcs7);
  24080. pkcs7 = NULL;
  24081. }
  24082. #endif /* !NO_RSA */
  24083. /* Test verify on signedData containing intermediate/root CA certs */
  24084. #ifndef NO_RSA
  24085. outputSz = sizeof(output);
  24086. XMEMSET(output, 0, outputSz);
  24087. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24088. (word32)sizeof(data),
  24089. 0, 0, 1, RSA_TYPE)), 0);
  24090. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24091. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24092. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24093. wc_PKCS7_Free(pkcs7);
  24094. pkcs7 = NULL;
  24095. #endif /* !NO_RSA */
  24096. #endif
  24097. return EXPECT_RESULT();
  24098. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  24099. /*
  24100. * Testing wc_PKCS_VerifySignedData()
  24101. */
  24102. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  24103. {
  24104. EXPECT_DECLS;
  24105. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  24106. PKCS7* pkcs7 = NULL;
  24107. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24108. word32 outputSz = sizeof(output);
  24109. byte data[] = "Test data to encode.";
  24110. byte badContent[] = "This is different content than was signed";
  24111. wc_HashAlg hash;
  24112. #ifdef NO_SHA
  24113. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24114. #else
  24115. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24116. #endif
  24117. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24118. word32 hashSz = wc_HashGetDigestSize(hashType);
  24119. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24120. /* Success test with ECC certs/key */
  24121. outputSz = sizeof(output);
  24122. XMEMSET(output, 0, outputSz);
  24123. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24124. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  24125. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24126. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24127. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24128. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24129. wc_PKCS7_Free(pkcs7);
  24130. pkcs7 = NULL;
  24131. /* Invalid content should error, use detached signature so we can
  24132. * easily change content */
  24133. outputSz = sizeof(output);
  24134. XMEMSET(output, 0, outputSz);
  24135. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24136. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  24137. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24138. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24139. if (pkcs7 != NULL) {
  24140. pkcs7->content = badContent;
  24141. pkcs7->contentSz = sizeof(badContent);
  24142. }
  24143. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24144. SIG_VERIFY_E);
  24145. wc_PKCS7_Free(pkcs7);
  24146. pkcs7 = NULL;
  24147. /* Test success case with detached signature and valid content */
  24148. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24149. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24150. if (pkcs7 != NULL) {
  24151. pkcs7->content = data;
  24152. pkcs7->contentSz = sizeof(data);
  24153. }
  24154. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24155. wc_PKCS7_Free(pkcs7);
  24156. pkcs7 = NULL;
  24157. /* verify using pre-computed content digest only (no content) */
  24158. {
  24159. /* calculate hash for content */
  24160. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24161. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24162. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24163. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24164. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24165. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24166. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24167. output, outputSz, NULL, 0), 0);
  24168. wc_PKCS7_Free(pkcs7);
  24169. pkcs7 = NULL;
  24170. }
  24171. /* Test verify on signedData containing intermediate/root CA certs */
  24172. outputSz = sizeof(output);
  24173. XMEMSET(output, 0, outputSz);
  24174. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24175. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  24176. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24177. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24178. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24179. wc_PKCS7_Free(pkcs7);
  24180. pkcs7 = NULL;
  24181. #endif
  24182. return EXPECT_RESULT();
  24183. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  24184. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24185. !defined(NO_AES_256)
  24186. static const byte defKey[] = {
  24187. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24188. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24189. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24190. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24191. };
  24192. static byte aesHandle[32]; /* simulated hardware key handle */
  24193. /* return 0 on success */
  24194. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24195. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24196. byte* in, int inSz, byte* out, void* usrCtx)
  24197. {
  24198. int ret;
  24199. Aes aes;
  24200. if (usrCtx == NULL) {
  24201. /* no simulated handle passed in */
  24202. return -1;
  24203. }
  24204. switch (encryptOID) {
  24205. case AES256CBCb:
  24206. if (ivSz != AES_BLOCK_SIZE)
  24207. return BAD_FUNC_ARG;
  24208. break;
  24209. default:
  24210. WOLFSSL_MSG("Unsupported content cipher type for test");
  24211. return ALGO_ID_E;
  24212. };
  24213. /* simulate using handle to get key */
  24214. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24215. if (ret == 0) {
  24216. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24217. if (ret == 0)
  24218. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24219. wc_AesFree(&aes);
  24220. }
  24221. (void)aad;
  24222. (void)aadSz;
  24223. (void)authTag;
  24224. (void)authTagSz;
  24225. (void)pkcs7;
  24226. return ret;
  24227. }
  24228. /* returns key size on success */
  24229. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24230. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24231. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24232. {
  24233. int ret = -1;
  24234. if (out == NULL)
  24235. return BAD_FUNC_ARG;
  24236. if (keyId[0] != 0x00) {
  24237. return -1;
  24238. }
  24239. if (type != (int)PKCS7_KEKRI) {
  24240. return -1;
  24241. }
  24242. switch (keyWrapAlgo) {
  24243. case AES256_WRAP:
  24244. /* simulate setting a handle for later decryption but use key
  24245. * as handle in the test case here */
  24246. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24247. aesHandle, sizeof(aesHandle), NULL);
  24248. if (ret < 0)
  24249. return ret;
  24250. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24251. if (ret < 0)
  24252. return ret;
  24253. /* return key size on success */
  24254. return sizeof(defKey);
  24255. default:
  24256. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24257. return BAD_KEYWRAP_ALG_E;
  24258. };
  24259. (void)cekSz;
  24260. (void)cek;
  24261. (void)outSz;
  24262. (void)keyIdSz;
  24263. (void)direction;
  24264. (void)orginKey; /* used with KAKRI */
  24265. (void)orginKeySz;
  24266. return ret;
  24267. }
  24268. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24269. /*
  24270. * Testing wc_PKCS7_EncodeEnvelopedData()
  24271. */
  24272. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24273. {
  24274. EXPECT_DECLS;
  24275. #if defined(HAVE_PKCS7)
  24276. PKCS7* pkcs7 = NULL;
  24277. #ifdef ECC_TIMING_RESISTANT
  24278. WC_RNG rng;
  24279. #endif
  24280. word32 tempWrd32 = 0;
  24281. byte* tmpBytePtr = NULL;
  24282. const char input[] = "Test data to encode.";
  24283. int i;
  24284. int testSz = 0;
  24285. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24286. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24287. byte* rsaCert = NULL;
  24288. byte* rsaPrivKey = NULL;
  24289. word32 rsaCertSz;
  24290. word32 rsaPrivKeySz;
  24291. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24292. !defined(USE_CERT_BUFFERS_2048) )
  24293. static const char* rsaClientCert = "./certs/client-cert.der";
  24294. static const char* rsaClientKey = "./certs/client-key.der";
  24295. rsaCertSz = (word32)sizeof(rsaClientCert);
  24296. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24297. #endif
  24298. #endif
  24299. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24300. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24301. byte* eccCert = NULL;
  24302. byte* eccPrivKey = NULL;
  24303. word32 eccCertSz;
  24304. word32 eccPrivKeySz;
  24305. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24306. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24307. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24308. #endif
  24309. #endif
  24310. /* Generic buffer size. */
  24311. byte output[ONEK_BUF];
  24312. byte decoded[sizeof(input)/sizeof(char)];
  24313. int decodedSz = 0;
  24314. #ifndef NO_FILESYSTEM
  24315. XFILE certFile = XBADFILE;
  24316. XFILE keyFile = XBADFILE;
  24317. #endif
  24318. #ifdef ECC_TIMING_RESISTANT
  24319. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24320. #endif
  24321. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24322. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24323. /* RSA certs and keys. */
  24324. #if defined(USE_CERT_BUFFERS_1024)
  24325. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24326. /* Allocate buffer space. */
  24327. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24328. DYNAMIC_TYPE_TMP_BUFFER));
  24329. /* Init buffer. */
  24330. if (rsaCert != NULL) {
  24331. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24332. }
  24333. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24334. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24335. DYNAMIC_TYPE_TMP_BUFFER));
  24336. if (rsaPrivKey != NULL) {
  24337. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24338. }
  24339. #elif defined(USE_CERT_BUFFERS_2048)
  24340. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24341. /* Allocate buffer */
  24342. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24343. DYNAMIC_TYPE_TMP_BUFFER));
  24344. /* Init buffer. */
  24345. if (rsaCert != NULL) {
  24346. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24347. }
  24348. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24349. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24350. DYNAMIC_TYPE_TMP_BUFFER));
  24351. if (rsaPrivKey != NULL) {
  24352. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24353. }
  24354. #else
  24355. /* File system. */
  24356. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  24357. rsaCertSz = (word32)FOURK_BUF;
  24358. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24359. DYNAMIC_TYPE_TMP_BUFFER));
  24360. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  24361. certFile)) > 0);
  24362. if (certFile != XBADFILE)
  24363. XFCLOSE(certFile);
  24364. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  24365. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24366. DYNAMIC_TYPE_TMP_BUFFER));
  24367. rsaPrivKeySz = (word32)FOURK_BUF;
  24368. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  24369. keyFile)) > 0);
  24370. if (keyFile != XBADFILE)
  24371. XFCLOSE(keyFile);
  24372. #endif /* USE_CERT_BUFFERS */
  24373. #endif /* NO_RSA */
  24374. /* ECC */
  24375. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24376. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24377. #ifdef USE_CERT_BUFFERS_256
  24378. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24379. DYNAMIC_TYPE_TMP_BUFFER));
  24380. /* Init buffer. */
  24381. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24382. if (eccCert != NULL) {
  24383. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24384. }
  24385. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24386. DYNAMIC_TYPE_TMP_BUFFER));
  24387. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24388. if (eccPrivKey != NULL) {
  24389. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24390. }
  24391. #else /* File system. */
  24392. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  24393. eccCertSz = (word32)FOURK_BUF;
  24394. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24395. DYNAMIC_TYPE_TMP_BUFFER));
  24396. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  24397. certFile)) > 0);
  24398. if (certFile != XBADFILE) {
  24399. XFCLOSE(certFile);
  24400. }
  24401. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  24402. eccPrivKeySz = (word32)FOURK_BUF;
  24403. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24404. DYNAMIC_TYPE_TMP_BUFFER));
  24405. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  24406. keyFile)) > 0);
  24407. if (keyFile != XBADFILE) {
  24408. XFCLOSE(keyFile);
  24409. }
  24410. #endif /* USE_CERT_BUFFERS_256 */
  24411. #endif /* END HAVE_ECC */
  24412. #ifndef NO_FILESYSTEM
  24413. /* Silence. */
  24414. (void)keyFile;
  24415. (void)certFile;
  24416. #endif
  24417. {
  24418. const pkcs7EnvelopedVector testVectors[] = {
  24419. /* DATA is a global variable defined in the makefile. */
  24420. #if !defined(NO_RSA)
  24421. #ifndef NO_DES3
  24422. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24423. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24424. #endif /* NO_DES3 */
  24425. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24426. #ifndef NO_AES_128
  24427. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24428. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24429. #endif
  24430. #ifndef NO_AES_192
  24431. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24432. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24433. #endif
  24434. #ifndef NO_AES_256
  24435. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24436. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24437. #endif
  24438. #endif /* NO_AES && HAVE_AES_CBC */
  24439. #endif /* NO_RSA */
  24440. #if defined(HAVE_ECC)
  24441. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24442. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24443. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24444. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  24445. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24446. #endif
  24447. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24448. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24449. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  24450. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24451. #endif
  24452. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24453. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24454. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  24455. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24456. #endif
  24457. #endif /* NO_AES && HAVE_AES_CBC*/
  24458. #endif /* END HAVE_ECC */
  24459. }; /* END pkcs7EnvelopedVector */
  24460. #ifdef ECC_TIMING_RESISTANT
  24461. ExpectIntEQ(wc_InitRng(&rng), 0);
  24462. #endif
  24463. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24464. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24465. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24466. for (i = 0; i < testSz; i++) {
  24467. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24468. (word32)(testVectors + i)->certSz), 0);
  24469. if (pkcs7 != NULL) {
  24470. #ifdef ECC_TIMING_RESISTANT
  24471. pkcs7->rng = &rng;
  24472. #endif
  24473. pkcs7->content = (byte*)(testVectors + i)->content;
  24474. pkcs7->contentSz = (testVectors + i)->contentSz;
  24475. pkcs7->contentOID = (testVectors + i)->contentOID;
  24476. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24477. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24478. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24479. pkcs7->privateKey = (testVectors + i)->privateKey;
  24480. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24481. }
  24482. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24483. (word32)sizeof(output)), 0);
  24484. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24485. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24486. ExpectIntGE(decodedSz, 0);
  24487. /* Verify the size of each buffer. */
  24488. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24489. /* Don't free the last time through the loop. */
  24490. if (i < testSz - 1) {
  24491. wc_PKCS7_Free(pkcs7);
  24492. pkcs7 = NULL;
  24493. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24494. }
  24495. } /* END test loop. */
  24496. }
  24497. /* Test bad args. */
  24498. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24499. (word32)sizeof(output)), BAD_FUNC_ARG);
  24500. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24501. (word32)sizeof(output)), BAD_FUNC_ARG);
  24502. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24503. /* Decode. */
  24504. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24505. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24506. BAD_FUNC_ARG);
  24507. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24508. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24509. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24510. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24511. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24512. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24513. BAD_FUNC_ARG);
  24514. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24515. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24516. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24517. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24518. /* only a failure for KARI test cases */
  24519. if (pkcs7 != NULL) {
  24520. tempWrd32 = pkcs7->singleCertSz;
  24521. pkcs7->singleCertSz = 0;
  24522. }
  24523. #if defined(WOLFSSL_ASN_TEMPLATE)
  24524. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24525. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24526. BUFFER_E);
  24527. #else
  24528. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24529. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24530. ASN_PARSE_E);
  24531. #endif
  24532. if (pkcs7 != NULL) {
  24533. pkcs7->singleCertSz = tempWrd32;
  24534. tmpBytePtr = pkcs7->singleCert;
  24535. pkcs7->singleCert = NULL;
  24536. }
  24537. #ifndef NO_RSA
  24538. #if defined(NO_PKCS7_STREAM)
  24539. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  24540. * is returned from kari parse which gets set to bad func arg */
  24541. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24542. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24543. BAD_FUNC_ARG);
  24544. #else
  24545. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24546. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24547. ASN_PARSE_E);
  24548. #endif
  24549. #endif /* !NO_RSA */
  24550. if (pkcs7 != NULL) {
  24551. pkcs7->singleCert = tmpBytePtr;
  24552. }
  24553. #endif
  24554. if (pkcs7 != NULL) {
  24555. tempWrd32 = pkcs7->privateKeySz;
  24556. pkcs7->privateKeySz = 0;
  24557. }
  24558. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24559. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24560. BAD_FUNC_ARG);
  24561. if (pkcs7 != NULL) {
  24562. pkcs7->privateKeySz = tempWrd32;
  24563. tmpBytePtr = pkcs7->privateKey;
  24564. pkcs7->privateKey = NULL;
  24565. }
  24566. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24567. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24568. BAD_FUNC_ARG);
  24569. if (pkcs7 != NULL) {
  24570. pkcs7->privateKey = tmpBytePtr;
  24571. }
  24572. wc_PKCS7_Free(pkcs7);
  24573. pkcs7 = NULL;
  24574. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24575. /* test of decrypt callback with KEKRI enveloped data */
  24576. {
  24577. int envelopedSz = 0;
  24578. const byte keyId[] = { 0x00 };
  24579. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24580. if (pkcs7 != NULL) {
  24581. pkcs7->content = (byte*)input;
  24582. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24583. pkcs7->contentOID = DATA;
  24584. pkcs7->encryptOID = AES256CBCb;
  24585. }
  24586. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24587. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24588. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24589. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24590. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24591. (word32)sizeof(output))), 0);
  24592. wc_PKCS7_Free(pkcs7);
  24593. pkcs7 = NULL;
  24594. /* decode envelopedData */
  24595. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24596. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24597. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24598. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24599. envelopedSz, decoded, sizeof(decoded))), 0);
  24600. wc_PKCS7_Free(pkcs7);
  24601. pkcs7 = NULL;
  24602. }
  24603. #endif /* !NO_AES && !NO_AES_256 */
  24604. #ifndef NO_RSA
  24605. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24606. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24607. #endif /* NO_RSA */
  24608. #ifdef HAVE_ECC
  24609. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24610. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24611. #endif /* HAVE_ECC */
  24612. #ifdef ECC_TIMING_RESISTANT
  24613. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24614. #endif
  24615. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  24616. !defined(NO_RSA) && !defined(NO_SHA)
  24617. {
  24618. byte out[7];
  24619. byte *cms = NULL;
  24620. word32 cmsSz;
  24621. XFILE cmsFile = XBADFILE;
  24622. XMEMSET(out, 0, sizeof(out));
  24623. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24624. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  24625. != XBADFILE);
  24626. cmsSz = (word32)FOURK_BUF;
  24627. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24628. DYNAMIC_TYPE_TMP_BUFFER));
  24629. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  24630. if (cmsFile != XBADFILE)
  24631. XFCLOSE(cmsFile);
  24632. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24633. sizeof_client_cert_der_2048), 0);
  24634. if (pkcs7 != NULL) {
  24635. pkcs7->privateKey = (byte*)client_key_der_2048;
  24636. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24637. }
  24638. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24639. 2), 0);
  24640. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24641. sizeof(out)), 0);
  24642. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24643. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  24644. wc_PKCS7_Free(pkcs7);
  24645. pkcs7 = NULL;
  24646. }
  24647. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  24648. #endif /* HAVE_PKCS7 */
  24649. return EXPECT_RESULT();
  24650. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  24651. /*
  24652. * Testing wc_PKCS7_EncodeEncryptedData()
  24653. */
  24654. static int test_wc_PKCS7_EncodeEncryptedData(void)
  24655. {
  24656. EXPECT_DECLS;
  24657. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24658. PKCS7* pkcs7 = NULL;
  24659. byte* tmpBytePtr = NULL;
  24660. byte encrypted[TWOK_BUF];
  24661. byte decoded[TWOK_BUF];
  24662. word32 tmpWrd32 = 0;
  24663. int tmpInt = 0;
  24664. int decodedSz;
  24665. int encryptedSz = 0;
  24666. int testSz;
  24667. int i;
  24668. const byte data[] = { /* Hello World */
  24669. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24670. 0x72,0x6c,0x64
  24671. };
  24672. #ifndef NO_DES3
  24673. byte desKey[] = {
  24674. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24675. };
  24676. byte des3Key[] = {
  24677. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24678. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24679. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24680. };
  24681. #endif
  24682. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24683. #ifndef NO_AES_128
  24684. byte aes128Key[] = {
  24685. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24686. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24687. };
  24688. #endif
  24689. #ifndef NO_AES_192
  24690. byte aes192Key[] = {
  24691. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24692. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24693. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24694. };
  24695. #endif
  24696. #ifndef NO_AES_256
  24697. byte aes256Key[] = {
  24698. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24699. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24700. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24701. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24702. };
  24703. #endif
  24704. #endif /* !NO_AES && HAVE_AES_CBC */
  24705. const pkcs7EncryptedVector testVectors[] =
  24706. {
  24707. #ifndef NO_DES3
  24708. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24709. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24710. #endif /* !NO_DES3 */
  24711. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24712. #ifndef NO_AES_128
  24713. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24714. sizeof(aes128Key)},
  24715. #endif
  24716. #ifndef NO_AES_192
  24717. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24718. sizeof(aes192Key)},
  24719. #endif
  24720. #ifndef NO_AES_256
  24721. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24722. sizeof(aes256Key)},
  24723. #endif
  24724. #endif /* !NO_AES && HAVE_AES_CBC */
  24725. };
  24726. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24727. for (i = 0; i < testSz; i++) {
  24728. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24729. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24730. if (pkcs7 != NULL) {
  24731. pkcs7->content = (byte*)testVectors[i].content;
  24732. pkcs7->contentSz = testVectors[i].contentSz;
  24733. pkcs7->contentOID = testVectors[i].contentOID;
  24734. pkcs7->encryptOID = testVectors[i].encryptOID;
  24735. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24736. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24737. pkcs7->heap = HEAP_HINT;
  24738. }
  24739. /* encode encryptedData */
  24740. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24741. sizeof(encrypted)), 0);
  24742. /* Decode encryptedData */
  24743. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  24744. encryptedSz, decoded, sizeof(decoded)), 0);
  24745. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24746. /* Keep values for last itr. */
  24747. if (i < testSz - 1) {
  24748. wc_PKCS7_Free(pkcs7);
  24749. pkcs7 = NULL;
  24750. }
  24751. }
  24752. if (pkcs7 == NULL || testSz == 0) {
  24753. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24754. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24755. }
  24756. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24757. sizeof(encrypted)),BAD_FUNC_ARG);
  24758. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24759. sizeof(encrypted)), BAD_FUNC_ARG);
  24760. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24761. 0), BAD_FUNC_ARG);
  24762. /* Testing the struct. */
  24763. if (pkcs7 != NULL) {
  24764. tmpBytePtr = pkcs7->content;
  24765. pkcs7->content = NULL;
  24766. }
  24767. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24768. sizeof(encrypted)), BAD_FUNC_ARG);
  24769. if (pkcs7 != NULL) {
  24770. pkcs7->content = tmpBytePtr;
  24771. tmpWrd32 = pkcs7->contentSz;
  24772. pkcs7->contentSz = 0;
  24773. }
  24774. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24775. sizeof(encrypted)), BAD_FUNC_ARG);
  24776. if (pkcs7 != NULL) {
  24777. pkcs7->contentSz = tmpWrd32;
  24778. tmpInt = pkcs7->encryptOID;
  24779. pkcs7->encryptOID = 0;
  24780. }
  24781. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24782. sizeof(encrypted)), BAD_FUNC_ARG);
  24783. if (pkcs7 != NULL) {
  24784. pkcs7->encryptOID = tmpInt;
  24785. tmpBytePtr = pkcs7->encryptionKey;
  24786. pkcs7->encryptionKey = NULL;
  24787. }
  24788. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24789. sizeof(encrypted)), BAD_FUNC_ARG);
  24790. if (pkcs7 != NULL) {
  24791. pkcs7->encryptionKey = tmpBytePtr;
  24792. tmpWrd32 = pkcs7->encryptionKeySz;
  24793. pkcs7->encryptionKeySz = 0;
  24794. }
  24795. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24796. sizeof(encrypted)), BAD_FUNC_ARG);
  24797. if (pkcs7 != NULL) {
  24798. pkcs7->encryptionKeySz = tmpWrd32;
  24799. }
  24800. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24801. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24802. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24803. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24804. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24805. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24806. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24807. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24808. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24809. decoded, 0), BAD_FUNC_ARG);
  24810. /* Test struct fields */
  24811. if (pkcs7 != NULL) {
  24812. tmpBytePtr = pkcs7->encryptionKey;
  24813. pkcs7->encryptionKey = NULL;
  24814. }
  24815. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24816. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24817. if (pkcs7 != NULL) {
  24818. pkcs7->encryptionKey = tmpBytePtr;
  24819. pkcs7->encryptionKeySz = 0;
  24820. }
  24821. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24822. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24823. wc_PKCS7_Free(pkcs7);
  24824. #endif
  24825. return EXPECT_RESULT();
  24826. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24827. /*
  24828. * Testing wc_PKCS7_Degenerate()
  24829. */
  24830. static int test_wc_PKCS7_Degenerate(void)
  24831. {
  24832. EXPECT_DECLS;
  24833. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24834. PKCS7* pkcs7 = NULL;
  24835. char fName[] = "./certs/test-degenerate.p7b";
  24836. XFILE f = XBADFILE;
  24837. byte der[4096];
  24838. word32 derSz = 0;
  24839. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24840. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24841. if (f != XBADFILE)
  24842. XFCLOSE(f);
  24843. /* test degenerate success */
  24844. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24845. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24846. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24847. #ifndef NO_RSA
  24848. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24849. #else
  24850. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24851. #endif
  24852. wc_PKCS7_Free(pkcs7);
  24853. pkcs7 = NULL;
  24854. /* test with turning off degenerate cases */
  24855. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24856. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24857. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24858. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24859. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  24860. PKCS7_NO_SIGNER_E);
  24861. wc_PKCS7_Free(pkcs7);
  24862. #endif
  24863. return EXPECT_RESULT();
  24864. } /* END test_wc_PKCS7_Degenerate() */
  24865. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24866. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  24867. static byte berContent[] = {
  24868. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24869. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24870. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24871. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24872. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24873. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24874. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24875. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24876. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24877. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24878. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24879. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24880. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24881. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24882. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24883. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24884. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24885. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24886. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24887. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24888. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24889. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24890. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24891. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24892. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24893. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24894. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24895. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24896. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24897. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24898. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24899. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24900. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24901. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24902. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24903. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24904. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24905. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24906. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24907. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24908. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24909. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24910. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24911. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24912. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24913. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24914. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24915. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24916. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24917. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24918. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24919. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24920. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24921. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24922. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24923. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24924. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24925. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24926. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24927. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24928. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24929. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24930. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24931. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24932. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24933. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24934. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24935. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24936. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24937. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24938. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24939. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24940. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24941. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24942. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24943. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24944. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24945. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24946. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24947. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24948. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24949. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24950. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24951. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24952. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24953. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24954. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24955. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24956. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24957. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24958. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24959. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24960. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24961. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24962. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24963. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24964. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  24965. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  24966. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  24967. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  24968. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  24969. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  24970. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  24971. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  24972. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  24973. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  24974. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  24975. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  24976. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  24977. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  24978. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  24979. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  24980. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  24981. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  24982. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  24983. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  24984. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  24985. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  24986. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  24987. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  24988. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  24989. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  24990. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  24991. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  24992. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  24993. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  24994. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  24995. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  24996. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  24997. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  24998. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  24999. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25000. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25001. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25002. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25003. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25004. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25005. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25006. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25007. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25008. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25009. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25010. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25011. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25012. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25013. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25014. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25015. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25016. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25017. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25018. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25019. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25020. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25021. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25022. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25023. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25024. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25025. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25026. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25027. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25028. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25029. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25030. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25031. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25032. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25033. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25034. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25035. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25036. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25037. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25038. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25039. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25040. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25041. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25042. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25043. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25044. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25045. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25046. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25047. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25048. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25049. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25050. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25051. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25052. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25053. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25054. 0x00, 0x00, 0x00, 0x00, 0x00
  25055. };
  25056. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  25057. * !NO_DES3 && !NO_SHA
  25058. */
  25059. /*
  25060. * Testing wc_PKCS7_BER()
  25061. */
  25062. static int test_wc_PKCS7_BER(void)
  25063. {
  25064. EXPECT_DECLS;
  25065. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25066. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  25067. PKCS7* pkcs7 = NULL;
  25068. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25069. XFILE f = XBADFILE;
  25070. byte der[4096];
  25071. #ifndef NO_DES3
  25072. byte decoded[2048];
  25073. #endif
  25074. word32 derSz = 0;
  25075. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25076. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25077. if (f != XBADFILE) {
  25078. XFCLOSE(f);
  25079. f = XBADFILE;
  25080. }
  25081. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25082. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25083. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25084. #ifndef NO_RSA
  25085. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25086. #else
  25087. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25088. #endif
  25089. wc_PKCS7_Free(pkcs7);
  25090. pkcs7 = NULL;
  25091. #ifndef NO_DES3
  25092. /* decode BER content */
  25093. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  25094. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25095. if (f != XBADFILE) {
  25096. XFCLOSE(f);
  25097. f = XBADFILE;
  25098. }
  25099. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25100. #ifndef NO_RSA
  25101. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25102. #else
  25103. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25104. #endif
  25105. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  25106. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25107. if (f != XBADFILE) {
  25108. XFCLOSE(f);
  25109. f = XBADFILE;
  25110. }
  25111. if (pkcs7 != NULL) {
  25112. pkcs7->privateKey = der;
  25113. pkcs7->privateKeySz = derSz;
  25114. }
  25115. #ifndef NO_RSA
  25116. #ifdef WOLFSSL_SP_MATH
  25117. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25118. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25119. #else
  25120. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25121. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25122. #endif
  25123. #else
  25124. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25125. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25126. #endif
  25127. wc_PKCS7_Free(pkcs7);
  25128. #endif /* !NO_DES3 */
  25129. #endif
  25130. return EXPECT_RESULT();
  25131. } /* END test_wc_PKCS7_BER() */
  25132. static int test_wc_PKCS7_signed_enveloped(void)
  25133. {
  25134. EXPECT_DECLS;
  25135. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25136. !defined(NO_FILESYSTEM)
  25137. XFILE f = XBADFILE;
  25138. PKCS7* pkcs7 = NULL;
  25139. #ifdef HAVE_AES_CBC
  25140. PKCS7* inner = NULL;
  25141. #endif
  25142. void* pt = NULL;
  25143. WC_RNG rng;
  25144. unsigned char key[FOURK_BUF/2];
  25145. unsigned char cert[FOURK_BUF/2];
  25146. unsigned char env[FOURK_BUF];
  25147. int envSz = FOURK_BUF;
  25148. int keySz = 0;
  25149. int certSz = 0;
  25150. unsigned char sig[FOURK_BUF * 2];
  25151. int sigSz = FOURK_BUF * 2;
  25152. #ifdef HAVE_AES_CBC
  25153. unsigned char decoded[FOURK_BUF];
  25154. int decodedSz = FOURK_BUF;
  25155. #endif
  25156. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25157. /* load cert */
  25158. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  25159. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25160. if (f != XBADFILE) {
  25161. XFCLOSE(f);
  25162. f = XBADFILE;
  25163. }
  25164. /* load key */
  25165. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  25166. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25167. if (f != XBADFILE) {
  25168. XFCLOSE(f);
  25169. f = XBADFILE;
  25170. }
  25171. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  25172. /* sign cert for envelope */
  25173. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25174. ExpectIntEQ(wc_InitRng(&rng), 0);
  25175. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25176. if (pkcs7 != NULL) {
  25177. pkcs7->content = cert;
  25178. pkcs7->contentSz = certSz;
  25179. pkcs7->contentOID = DATA;
  25180. pkcs7->privateKey = key;
  25181. pkcs7->privateKeySz = keySz;
  25182. pkcs7->encryptOID = RSAk;
  25183. pkcs7->hashOID = SHA256h;
  25184. pkcs7->rng = &rng;
  25185. }
  25186. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25187. wc_PKCS7_Free(pkcs7);
  25188. pkcs7 = NULL;
  25189. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25190. #ifdef HAVE_AES_CBC
  25191. /* create envelope */
  25192. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25193. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25194. if (pkcs7 != NULL) {
  25195. pkcs7->content = sig;
  25196. pkcs7->contentSz = sigSz;
  25197. pkcs7->contentOID = DATA;
  25198. pkcs7->encryptOID = AES256CBCb;
  25199. pkcs7->privateKey = key;
  25200. pkcs7->privateKeySz = keySz;
  25201. }
  25202. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25203. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25204. wc_PKCS7_Free(pkcs7);
  25205. pkcs7 = NULL;
  25206. #endif
  25207. /* create bad signed enveloped data */
  25208. sigSz = FOURK_BUF * 2;
  25209. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25210. ExpectIntEQ(wc_InitRng(&rng), 0);
  25211. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25212. if (pkcs7 != NULL) {
  25213. pkcs7->content = env;
  25214. pkcs7->contentSz = envSz;
  25215. pkcs7->contentOID = DATA;
  25216. pkcs7->privateKey = key;
  25217. pkcs7->privateKeySz = keySz;
  25218. pkcs7->encryptOID = RSAk;
  25219. pkcs7->hashOID = SHA256h;
  25220. pkcs7->rng = &rng;
  25221. }
  25222. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25223. * free it later. */
  25224. if (pkcs7 != NULL) {
  25225. pt = (void*)pkcs7->certList;
  25226. pkcs7->certList = NULL; /* no certs in bundle */
  25227. }
  25228. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25229. if (pkcs7 != NULL) {
  25230. /* restore pointer for PKCS7 free call */
  25231. pkcs7->certList = (Pkcs7Cert*)pt;
  25232. }
  25233. wc_PKCS7_Free(pkcs7);
  25234. pkcs7 = NULL;
  25235. /* check verify fails */
  25236. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25237. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25238. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25239. PKCS7_SIGNEEDS_CHECK);
  25240. /* try verifying the signature manually */
  25241. {
  25242. RsaKey rKey;
  25243. word32 idx = 0;
  25244. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25245. WC_MAX_DIGEST_SIZE];
  25246. int digestSz;
  25247. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25248. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25249. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  25250. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  25251. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25252. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25253. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  25254. /* verify was success */
  25255. }
  25256. wc_PKCS7_Free(pkcs7);
  25257. pkcs7 = NULL;
  25258. /* initializing the PKCS7 struct with the signing certificate should pass */
  25259. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25260. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25261. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25262. wc_PKCS7_Free(pkcs7);
  25263. pkcs7 = NULL;
  25264. /* create valid degenerate bundle */
  25265. sigSz = FOURK_BUF * 2;
  25266. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25267. if (pkcs7 != NULL) {
  25268. pkcs7->content = env;
  25269. pkcs7->contentSz = envSz;
  25270. pkcs7->contentOID = DATA;
  25271. pkcs7->privateKey = key;
  25272. pkcs7->privateKeySz = keySz;
  25273. pkcs7->encryptOID = RSAk;
  25274. pkcs7->hashOID = SHA256h;
  25275. pkcs7->rng = &rng;
  25276. }
  25277. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25278. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25279. wc_PKCS7_Free(pkcs7);
  25280. pkcs7 = NULL;
  25281. wc_FreeRng(&rng);
  25282. /* check verify */
  25283. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25284. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25285. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25286. ExpectNotNull(pkcs7->content);
  25287. #ifdef HAVE_AES_CBC
  25288. /* check decode */
  25289. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  25290. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25291. if (inner != NULL) {
  25292. inner->privateKey = key;
  25293. inner->privateKeySz = keySz;
  25294. }
  25295. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25296. pkcs7->contentSz, decoded, decodedSz)), 0);
  25297. wc_PKCS7_Free(inner);
  25298. inner = NULL;
  25299. #endif
  25300. wc_PKCS7_Free(pkcs7);
  25301. pkcs7 = NULL;
  25302. #ifdef HAVE_AES_CBC
  25303. /* check cert set */
  25304. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25305. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25306. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25307. ExpectNotNull(pkcs7->singleCert);
  25308. ExpectIntNE(pkcs7->singleCertSz, 0);
  25309. wc_PKCS7_Free(pkcs7);
  25310. pkcs7 = NULL;
  25311. #endif
  25312. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25313. return EXPECT_RESULT();
  25314. }
  25315. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  25316. {
  25317. EXPECT_DECLS;
  25318. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25319. && !defined(NO_AES)
  25320. PKCS7* pkcs7 = NULL;
  25321. void* heap = NULL;
  25322. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25323. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25324. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25325. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25326. wc_PKCS7_Free(pkcs7);
  25327. #endif
  25328. return EXPECT_RESULT();
  25329. }
  25330. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  25331. {
  25332. EXPECT_DECLS;
  25333. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25334. && !defined(NO_AES)
  25335. PKCS7* pkcs7 = NULL;
  25336. void* heap = NULL;
  25337. WOLFSSL_CTX* ctx = NULL;
  25338. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25339. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25340. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25341. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25342. wc_PKCS7_Free(pkcs7);
  25343. #endif
  25344. return EXPECT_RESULT();
  25345. }
  25346. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  25347. {
  25348. EXPECT_DECLS;
  25349. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25350. && !defined(NO_AES)
  25351. PKCS7* pkcs7 = NULL;
  25352. void* heap = NULL;
  25353. WOLFSSL_CTX* ctx = NULL;
  25354. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25355. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25356. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25357. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25358. wc_PKCS7_Free(pkcs7);
  25359. #endif
  25360. return EXPECT_RESULT();
  25361. }
  25362. static int test_wc_PKCS7_DecodeCompressedData(void)
  25363. {
  25364. EXPECT_DECLS;
  25365. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25366. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25367. PKCS7* pkcs7 = NULL;
  25368. void* heap = NULL;
  25369. byte out[4096];
  25370. byte* decompressed = NULL;
  25371. int outSz;
  25372. int decompressedSz;
  25373. const char* cert = "./certs/client-cert.pem";
  25374. byte* cert_buf = NULL;
  25375. size_t cert_sz = 0;
  25376. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25377. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  25378. DYNAMIC_TYPE_TMP_BUFFER)));
  25379. decompressedSz = (int)cert_sz;
  25380. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25381. if (pkcs7 != NULL) {
  25382. pkcs7->content = (byte*)cert_buf;
  25383. pkcs7->contentSz = (word32)cert_sz;
  25384. pkcs7->contentOID = DATA;
  25385. }
  25386. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25387. sizeof(out))), 0);
  25388. wc_PKCS7_Free(pkcs7);
  25389. pkcs7 = NULL;
  25390. /* compressed key should be smaller than when started */
  25391. ExpectIntLT(outSz, cert_sz);
  25392. /* test decompression */
  25393. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25394. ExpectIntEQ(pkcs7->contentOID, 0);
  25395. /* fail case with out buffer too small */
  25396. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25397. decompressed, outSz), 0);
  25398. /* success case */
  25399. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25400. decompressed, decompressedSz), cert_sz);
  25401. ExpectIntEQ(pkcs7->contentOID, DATA);
  25402. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25403. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25404. decompressed = NULL;
  25405. /* test decompression function with different 'max' inputs */
  25406. outSz = sizeof(out);
  25407. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25408. 0);
  25409. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25410. out, outSz, 0, heap), 0);
  25411. ExpectNull(decompressed);
  25412. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25413. out, outSz, 0, heap), 0);
  25414. ExpectNotNull(decompressed);
  25415. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25416. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25417. decompressed = NULL;
  25418. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25419. out, outSz, 0, heap), 0);
  25420. ExpectNotNull(decompressed);
  25421. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25422. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25423. if (cert_buf != NULL)
  25424. free(cert_buf);
  25425. wc_PKCS7_Free(pkcs7);
  25426. #endif
  25427. return EXPECT_RESULT();
  25428. }
  25429. static int test_wc_i2d_PKCS12(void)
  25430. {
  25431. EXPECT_DECLS;
  25432. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25433. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25434. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25435. WC_PKCS12* pkcs12 = NULL;
  25436. unsigned char der[FOURK_BUF * 2];
  25437. unsigned char* pt;
  25438. int derSz = 0;
  25439. unsigned char out[FOURK_BUF * 2];
  25440. int outSz = FOURK_BUF * 2;
  25441. const char p12_f[] = "./certs/test-servercert.p12";
  25442. XFILE f = XBADFILE;
  25443. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  25444. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  25445. if (f != XBADFILE)
  25446. XFCLOSE(f);
  25447. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25448. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25449. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25450. ExpectIntEQ(outSz, derSz);
  25451. outSz = derSz - 1;
  25452. pt = out;
  25453. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25454. outSz = derSz;
  25455. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25456. ExpectIntEQ((pt == out), 0);
  25457. pt = NULL;
  25458. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25459. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25460. wc_PKCS12_free(pkcs12);
  25461. pkcs12 = NULL;
  25462. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25463. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25464. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25465. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25466. ExpectIntEQ(outSz, derSz);
  25467. wc_PKCS12_free(pkcs12);
  25468. pkcs12 = NULL;
  25469. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25470. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25471. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25472. ExpectIntEQ(outSz, derSz);
  25473. wc_PKCS12_free(pkcs12);
  25474. pkcs12 = NULL;
  25475. #endif
  25476. return EXPECT_RESULT();
  25477. }
  25478. /* Testing wc_SignatureGetSize() for signature type ECC */
  25479. static int test_wc_SignatureGetSize_ecc(void)
  25480. {
  25481. EXPECT_DECLS;
  25482. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  25483. enum wc_SignatureType sig_type;
  25484. word32 key_len;
  25485. ecc_key ecc;
  25486. const char* qx =
  25487. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25488. const char* qy =
  25489. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25490. const char* d =
  25491. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25492. XMEMSET(&ecc, 0, sizeof(ecc_key));
  25493. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  25494. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  25495. /* Input for signature type ECC */
  25496. sig_type = WC_SIGNATURE_TYPE_ECC;
  25497. key_len = sizeof(ecc_key);
  25498. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  25499. /* Test bad args */
  25500. sig_type = (enum wc_SignatureType) 100;
  25501. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25502. sig_type = WC_SIGNATURE_TYPE_ECC;
  25503. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25504. key_len = (word32)0;
  25505. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25506. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  25507. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  25508. return EXPECT_RESULT();
  25509. } /* END test_wc_SignatureGetSize_ecc() */
  25510. /* Testing wc_SignatureGetSize() for signature type rsa */
  25511. static int test_wc_SignatureGetSize_rsa(void)
  25512. {
  25513. EXPECT_DECLS;
  25514. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  25515. enum wc_SignatureType sig_type;
  25516. word32 key_len;
  25517. word32 idx = 0;
  25518. RsaKey rsa_key;
  25519. byte* tmp = NULL;
  25520. size_t bytes;
  25521. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  25522. #ifdef USE_CERT_BUFFERS_1024
  25523. bytes = (size_t)sizeof_client_key_der_1024;
  25524. if (bytes < (size_t)sizeof_client_key_der_1024)
  25525. bytes = (size_t)sizeof_client_cert_der_1024;
  25526. #elif defined(USE_CERT_BUFFERS_2048)
  25527. bytes = (size_t)sizeof_client_key_der_2048;
  25528. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25529. bytes = (size_t)sizeof_client_cert_der_2048;
  25530. #else
  25531. bytes = FOURK_BUF;
  25532. #endif
  25533. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  25534. DYNAMIC_TYPE_TMP_BUFFER));
  25535. if (tmp != NULL) {
  25536. #ifdef USE_CERT_BUFFERS_1024
  25537. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  25538. #elif defined(USE_CERT_BUFFERS_2048)
  25539. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  25540. #elif !defined(NO_FILESYSTEM)
  25541. XFILE file = XBADFILE;
  25542. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  25543. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  25544. if (file != XBADFILE)
  25545. XFCLOSE(file);
  25546. }
  25547. #else
  25548. ExpectFail();
  25549. #endif
  25550. }
  25551. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  25552. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  25553. /* Input for signature type RSA */
  25554. sig_type = WC_SIGNATURE_TYPE_RSA;
  25555. key_len = sizeof(RsaKey);
  25556. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  25557. /* Test bad args */
  25558. sig_type = (enum wc_SignatureType)100;
  25559. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25560. sig_type = WC_SIGNATURE_TYPE_RSA;
  25561. #ifndef HAVE_USER_RSA
  25562. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  25563. #else
  25564. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25565. #endif
  25566. key_len = (word32)0;
  25567. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25568. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  25569. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25570. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  25571. return EXPECT_RESULT();
  25572. } /* END test_wc_SignatureGetSize_rsa(void) */
  25573. /*----------------------------------------------------------------------------*
  25574. | hash.h Tests
  25575. *----------------------------------------------------------------------------*/
  25576. static int test_wc_HashInit(void)
  25577. {
  25578. EXPECT_DECLS;
  25579. int i; /* 0 indicates tests passed, 1 indicates failure */
  25580. wc_HashAlg hash;
  25581. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25582. enum wc_HashType enumArray[] = {
  25583. #ifndef NO_MD5
  25584. WC_HASH_TYPE_MD5,
  25585. #endif
  25586. #ifndef NO_SHA
  25587. WC_HASH_TYPE_SHA,
  25588. #endif
  25589. #ifdef WOLFSSL_SHA224
  25590. WC_HASH_TYPE_SHA224,
  25591. #endif
  25592. #ifndef NO_SHA256
  25593. WC_HASH_TYPE_SHA256,
  25594. #endif
  25595. #ifdef WOLFSSL_SHA384
  25596. WC_HASH_TYPE_SHA384,
  25597. #endif
  25598. #ifdef WOLFSSL_SHA512
  25599. WC_HASH_TYPE_SHA512,
  25600. #endif
  25601. };
  25602. /* dynamically finds the length */
  25603. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25604. /* For loop to test various arguments... */
  25605. for (i = 0; i < enumlen; i++) {
  25606. /* check for bad args */
  25607. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25608. wc_HashFree(&hash, enumArray[i]);
  25609. /* check for null ptr */
  25610. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  25611. } /* end of for loop */
  25612. return EXPECT_RESULT();
  25613. } /* end of test_wc_HashInit */
  25614. /*
  25615. * Unit test function for wc_HashSetFlags()
  25616. */
  25617. static int test_wc_HashSetFlags(void)
  25618. {
  25619. EXPECT_DECLS;
  25620. #ifdef WOLFSSL_HASH_FLAGS
  25621. wc_HashAlg hash;
  25622. word32 flags = 0;
  25623. int i, j;
  25624. int notSupportedLen;
  25625. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25626. enum wc_HashType enumArray[] = {
  25627. #ifndef NO_MD5
  25628. WC_HASH_TYPE_MD5,
  25629. #endif
  25630. #ifndef NO_SHA
  25631. WC_HASH_TYPE_SHA,
  25632. #endif
  25633. #ifdef WOLFSSL_SHA224
  25634. WC_HASH_TYPE_SHA224,
  25635. #endif
  25636. #ifndef NO_SHA256
  25637. WC_HASH_TYPE_SHA256,
  25638. #endif
  25639. #ifdef WOLFSSL_SHA384
  25640. WC_HASH_TYPE_SHA384,
  25641. #endif
  25642. #ifdef WOLFSSL_SHA512
  25643. WC_HASH_TYPE_SHA512,
  25644. #endif
  25645. #ifdef WOLFSSL_SHA3
  25646. WC_HASH_TYPE_SHA3_224,
  25647. #endif
  25648. };
  25649. enum wc_HashType notSupported[] = {
  25650. WC_HASH_TYPE_MD5_SHA,
  25651. WC_HASH_TYPE_MD2,
  25652. WC_HASH_TYPE_MD4,
  25653. WC_HASH_TYPE_BLAKE2B,
  25654. WC_HASH_TYPE_BLAKE2S,
  25655. WC_HASH_TYPE_NONE,
  25656. };
  25657. /* dynamically finds the length */
  25658. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25659. /* For loop to test various arguments... */
  25660. for (i = 0; i < enumlen; i++) {
  25661. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25662. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  25663. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25664. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  25665. wc_HashFree(&hash, enumArray[i]);
  25666. }
  25667. /* For loop to test not supported cases */
  25668. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25669. for (j = 0; j < notSupportedLen; j++) {
  25670. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25671. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  25672. BAD_FUNC_ARG);
  25673. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25674. }
  25675. #endif
  25676. return EXPECT_RESULT();
  25677. } /* END test_wc_HashSetFlags */
  25678. /*
  25679. * Unit test function for wc_HashGetFlags()
  25680. */
  25681. static int test_wc_HashGetFlags(void)
  25682. {
  25683. EXPECT_DECLS;
  25684. #ifdef WOLFSSL_HASH_FLAGS
  25685. wc_HashAlg hash;
  25686. word32 flags = 0;
  25687. int i, j;
  25688. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25689. enum wc_HashType enumArray[] = {
  25690. #ifndef NO_MD5
  25691. WC_HASH_TYPE_MD5,
  25692. #endif
  25693. #ifndef NO_SHA
  25694. WC_HASH_TYPE_SHA,
  25695. #endif
  25696. #ifdef WOLFSSL_SHA224
  25697. WC_HASH_TYPE_SHA224,
  25698. #endif
  25699. #ifndef NO_SHA256
  25700. WC_HASH_TYPE_SHA256,
  25701. #endif
  25702. #ifdef WOLFSSL_SHA384
  25703. WC_HASH_TYPE_SHA384,
  25704. #endif
  25705. #ifdef WOLFSSL_SHA512
  25706. WC_HASH_TYPE_SHA512,
  25707. #endif
  25708. #ifdef WOLFSSL_SHA3
  25709. WC_HASH_TYPE_SHA3_224,
  25710. #endif
  25711. };
  25712. enum wc_HashType notSupported[] = {
  25713. WC_HASH_TYPE_MD5_SHA,
  25714. WC_HASH_TYPE_MD2,
  25715. WC_HASH_TYPE_MD4,
  25716. WC_HASH_TYPE_BLAKE2B,
  25717. WC_HASH_TYPE_BLAKE2S,
  25718. WC_HASH_TYPE_NONE,
  25719. };
  25720. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25721. int notSupportedLen;
  25722. /* For loop to test various arguments... */
  25723. for (i = 0; i < enumlen; i++) {
  25724. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25725. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  25726. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25727. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  25728. wc_HashFree(&hash, enumArray[i]);
  25729. }
  25730. /* For loop to test not supported cases */
  25731. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25732. for (j = 0; j < notSupportedLen; j++) {
  25733. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25734. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  25735. BAD_FUNC_ARG);
  25736. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25737. }
  25738. #endif
  25739. return EXPECT_RESULT();
  25740. } /* END test_wc_HashGetFlags */
  25741. /*----------------------------------------------------------------------------*
  25742. | Compatibility Tests
  25743. *----------------------------------------------------------------------------*/
  25744. /*----------------------------------------------------------------------------*
  25745. | ASN.1 Tests
  25746. *----------------------------------------------------------------------------*/
  25747. static int test_wolfSSL_ASN1_BIT_STRING(void)
  25748. {
  25749. EXPECT_DECLS;
  25750. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  25751. ASN1_BIT_STRING* str = NULL;
  25752. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25753. /* Empty data testing. */
  25754. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  25755. ASN1_BIT_STRING_free(str);
  25756. str = NULL;
  25757. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25758. /* Invalid parameter testing. */
  25759. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  25760. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  25761. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  25762. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  25763. /* No bit string - bit is always 0. */
  25764. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  25765. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  25766. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25767. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  25768. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  25769. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  25770. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  25771. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25772. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  25773. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  25774. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  25775. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  25776. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  25777. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  25778. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  25779. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  25780. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  25781. ASN1_BIT_STRING_free(str);
  25782. ASN1_BIT_STRING_free(NULL);
  25783. #endif
  25784. return EXPECT_RESULT();
  25785. }
  25786. static int test_wolfSSL_ASN1_INTEGER(void)
  25787. {
  25788. EXPECT_DECLS;
  25789. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25790. ASN1_INTEGER* a = NULL;
  25791. ASN1_INTEGER* dup = NULL;
  25792. const unsigned char invalidLenDer[] = {
  25793. 0x02, 0x20, 0x00
  25794. };
  25795. const unsigned char longDer[] = {
  25796. 0x02, 0x20,
  25797. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25798. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25799. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25800. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  25801. };
  25802. const unsigned char* p;
  25803. /* Invalid parameter testing. */
  25804. ASN1_INTEGER_free(NULL);
  25805. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  25806. ExpectNotNull(a = ASN1_INTEGER_new());
  25807. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25808. ASN1_INTEGER_free(dup);
  25809. dup = NULL;
  25810. ASN1_INTEGER_free(a);
  25811. a = NULL;
  25812. p = longDer;
  25813. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  25814. p = longDer;
  25815. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  25816. ExpectPtrNE(p, longDer);
  25817. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25818. ASN1_INTEGER_free(dup);
  25819. ASN1_INTEGER_free(a);
  25820. #endif
  25821. return EXPECT_RESULT();
  25822. }
  25823. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  25824. {
  25825. EXPECT_DECLS;
  25826. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25827. ASN1_INTEGER* a = NULL;
  25828. ASN1_INTEGER* b = NULL;
  25829. ExpectNotNull(a = ASN1_INTEGER_new());
  25830. ExpectNotNull(b = ASN1_INTEGER_new());
  25831. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  25832. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25833. /* Invalid parameter testing. */
  25834. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  25835. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  25836. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  25837. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25838. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  25839. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25840. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  25841. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25842. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25843. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25844. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  25845. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  25846. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25847. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  25848. ASN1_INTEGER_free(b);
  25849. ASN1_INTEGER_free(a);
  25850. #endif
  25851. return EXPECT_RESULT();
  25852. }
  25853. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  25854. {
  25855. EXPECT_DECLS;
  25856. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25857. ASN1_INTEGER* ai = NULL;
  25858. ASN1_INTEGER* ai2 = NULL;
  25859. BIGNUM* bn = NULL;
  25860. BIGNUM* bn2 = NULL;
  25861. ExpectNotNull(ai = ASN1_INTEGER_new());
  25862. ExpectNotNull(bn2 = BN_new());
  25863. /* Invalid parameter testing. */
  25864. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  25865. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  25866. /* at the moment hard setting since no set function */
  25867. if (ai != NULL) {
  25868. ai->data[0] = 0xff; /* No DER encoding. */
  25869. ai->length = 1;
  25870. }
  25871. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25872. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25873. BN_free(bn);
  25874. bn = NULL;
  25875. #else
  25876. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25877. #endif
  25878. if (ai != NULL) {
  25879. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25880. ai->data[1] = 0x04; /* bad length of integer */
  25881. ai->data[2] = 0x03;
  25882. ai->length = 3;
  25883. }
  25884. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25885. /* Interpreted as a number 0x020403. */
  25886. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25887. BN_free(bn);
  25888. bn = NULL;
  25889. #else
  25890. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25891. #endif
  25892. if (ai != NULL) {
  25893. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25894. ai->data[1] = 0x01; /* length of integer */
  25895. ai->data[2] = 0x03;
  25896. ai->length = 3;
  25897. }
  25898. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25899. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  25900. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25901. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25902. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25903. if (ai != NULL) {
  25904. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25905. ai->data[1] = 0x02; /* length of integer */
  25906. ai->data[2] = 0x00; /* padding byte to ensure positive */
  25907. ai->data[3] = 0xff;
  25908. ai->length = 4;
  25909. }
  25910. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25911. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25912. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25913. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25914. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25915. if (ai != NULL) {
  25916. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25917. ai->data[1] = 0x01; /* length of integer */
  25918. ai->data[2] = 0x00;
  25919. ai->length = 3;
  25920. }
  25921. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25922. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25923. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25924. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25925. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25926. if (ai != NULL) {
  25927. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25928. ai->data[1] = 0x01; /* length of integer */
  25929. ai->data[2] = 0x01;
  25930. ai->length = 3;
  25931. ai->negative = 1;
  25932. }
  25933. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25934. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25935. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25936. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25937. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25938. BN_free(bn2);
  25939. BN_free(bn);
  25940. ASN1_INTEGER_free(ai2);
  25941. ASN1_INTEGER_free(ai);
  25942. #endif
  25943. return EXPECT_RESULT();
  25944. }
  25945. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  25946. {
  25947. EXPECT_DECLS;
  25948. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25949. ASN1_INTEGER *a = NULL;
  25950. long val;
  25951. ExpectNotNull(a = ASN1_INTEGER_new());
  25952. /* Invalid parameter testing. */
  25953. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  25954. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25955. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  25956. #else
  25957. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  25958. #endif
  25959. ASN1_INTEGER_free(a);
  25960. a = NULL;
  25961. ExpectNotNull(a = ASN1_INTEGER_new());
  25962. val = 0;
  25963. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  25964. ASN1_INTEGER_free(a);
  25965. a = NULL;
  25966. /* 0 */
  25967. ExpectNotNull(a = ASN1_INTEGER_new());
  25968. val = 0;
  25969. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25970. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25971. ASN1_INTEGER_free(a);
  25972. a = NULL;
  25973. /* 40 */
  25974. ExpectNotNull(a = ASN1_INTEGER_new());
  25975. val = 40;
  25976. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25977. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25978. ASN1_INTEGER_free(a);
  25979. a = NULL;
  25980. /* -40 */
  25981. ExpectNotNull(a = ASN1_INTEGER_new());
  25982. val = -40;
  25983. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25984. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25985. ASN1_INTEGER_free(a);
  25986. a = NULL;
  25987. /* 128 */
  25988. ExpectNotNull(a = ASN1_INTEGER_new());
  25989. val = 128;
  25990. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25991. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25992. ASN1_INTEGER_free(a);
  25993. a = NULL;
  25994. /* -128 */
  25995. ExpectNotNull(a = ASN1_INTEGER_new());
  25996. val = -128;
  25997. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25998. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25999. ASN1_INTEGER_free(a);
  26000. a = NULL;
  26001. /* 200 */
  26002. ExpectNotNull(a = ASN1_INTEGER_new());
  26003. val = 200;
  26004. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26005. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26006. ASN1_INTEGER_free(a);
  26007. a = NULL;
  26008. /* int max (2147483647) */
  26009. ExpectNotNull(a = ASN1_INTEGER_new());
  26010. val = 2147483647;
  26011. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26012. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26013. ASN1_INTEGER_free(a);
  26014. a = NULL;
  26015. /* int min (-2147483648) */
  26016. ExpectNotNull(a = ASN1_INTEGER_new());
  26017. val = -2147483647 - 1;
  26018. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26019. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26020. ASN1_INTEGER_free(a);
  26021. a = NULL;
  26022. /* long max positive */
  26023. ExpectNotNull(a = ASN1_INTEGER_new());
  26024. val = (long)(((unsigned long)-1) >> 1);
  26025. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26026. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26027. ASN1_INTEGER_free(a);
  26028. #endif
  26029. return EXPECT_RESULT();
  26030. }
  26031. #if defined(OPENSSL_EXTRA)
  26032. typedef struct ASN1IntTestVector {
  26033. const byte* der;
  26034. const size_t derSz;
  26035. const long value;
  26036. } ASN1IntTestVector;
  26037. #endif
  26038. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  26039. {
  26040. EXPECT_DECLS;
  26041. #if defined(OPENSSL_EXTRA)
  26042. size_t i;
  26043. WOLFSSL_ASN1_INTEGER* a = NULL;
  26044. WOLFSSL_ASN1_INTEGER* b = NULL;
  26045. WOLFSSL_ASN1_INTEGER* c = NULL;
  26046. const byte* p = NULL;
  26047. byte* p2 = NULL;
  26048. byte* reEncoded = NULL;
  26049. int reEncodedSz;
  26050. static const byte zeroDer[] = {
  26051. 0x02, 0x01, 0x00
  26052. };
  26053. static const byte oneDer[] = {
  26054. 0x02, 0x01, 0x01
  26055. };
  26056. static const byte negativeDer[] = {
  26057. 0x02, 0x03, 0xC1, 0x16, 0x0D
  26058. };
  26059. static const byte positiveDer[] = {
  26060. 0x02, 0x03, 0x01, 0x00, 0x01
  26061. };
  26062. static const byte primeDer[] = {
  26063. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  26064. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  26065. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  26066. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  26067. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  26068. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  26069. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  26070. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  26071. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  26072. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  26073. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  26074. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  26075. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  26076. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  26077. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  26078. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  26079. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  26080. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  26081. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  26082. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  26083. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  26084. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  26085. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  26086. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  26087. };
  26088. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  26089. static const ASN1IntTestVector testVectors[] = {
  26090. {zeroDer, sizeof(zeroDer), 0},
  26091. {oneDer, sizeof(oneDer), 1},
  26092. {negativeDer, sizeof(negativeDer), -4123123},
  26093. {positiveDer, sizeof(positiveDer), 65537},
  26094. {primeDer, sizeof(primeDer), 0}
  26095. };
  26096. static const size_t NUM_TEST_VECTORS =
  26097. sizeof(testVectors)/sizeof(testVectors[0]);
  26098. /* Check d2i error conditions */
  26099. /* NULL pointer to input. */
  26100. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  26101. ExpectNull(b);
  26102. /* NULL input. */
  26103. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  26104. ExpectNull(b);
  26105. /* 0 length. */
  26106. p = testVectors[0].der;
  26107. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  26108. ExpectNull(b);
  26109. /* Negative length. */
  26110. p = testVectors[0].der;
  26111. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  26112. ExpectNull(b);
  26113. /* Garbage DER input. */
  26114. p = garbageDer;
  26115. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  26116. ExpectNull(b);
  26117. /* Check i2d error conditions */
  26118. /* NULL input. */
  26119. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  26120. /* 0 length input data buffer (a->length == 0). */
  26121. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  26122. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26123. if (a != NULL)
  26124. a->data = NULL;
  26125. /* NULL input data buffer. */
  26126. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26127. if (a != NULL) {
  26128. /* Reset a->data. */
  26129. a->data = a->intData;
  26130. }
  26131. /* Set a to valid value. */
  26132. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  26133. /* NULL output buffer. */
  26134. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  26135. wolfSSL_ASN1_INTEGER_free(a);
  26136. a = NULL;
  26137. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  26138. p = testVectors[i].der;
  26139. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  26140. testVectors[i].derSz));
  26141. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26142. if (testVectors[i].derSz <= sizeof(long)) {
  26143. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  26144. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  26145. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  26146. wolfSSL_ASN1_INTEGER_free(c);
  26147. c = NULL;
  26148. }
  26149. /* Convert to DER without a pre-allocated output buffer. */
  26150. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  26151. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  26152. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  26153. /* Convert to DER with a pre-allocated output buffer. In this case, the
  26154. * output buffer pointer should be incremented just past the end of the
  26155. * encoded data. */
  26156. p2 = reEncoded;
  26157. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  26158. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  26159. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  26160. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  26161. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  26162. reEncoded = NULL;
  26163. wolfSSL_ASN1_INTEGER_free(a);
  26164. a = NULL;
  26165. }
  26166. #endif /* OPENSSL_EXTRA */
  26167. return EXPECT_RESULT();
  26168. }
  26169. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  26170. {
  26171. EXPECT_DECLS;
  26172. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  26173. BIO* bio = NULL;
  26174. BIO* out = NULL;
  26175. BIO* fixed = NULL;
  26176. ASN1_INTEGER* ai = NULL;
  26177. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  26178. char tmp[1024];
  26179. int tmpSz;
  26180. const char expected1[] = "123456";
  26181. const char expected2[] = "112345678912345678901234567890";
  26182. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  26183. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  26184. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  26185. ExpectNotNull(ai = ASN1_INTEGER_new());
  26186. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  26187. /* Invalid parameter testing. */
  26188. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  26189. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  26190. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  26191. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  26192. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  26193. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  26194. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  26195. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  26196. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  26197. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  26198. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  26199. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  26200. /* No data to read from BIO. */
  26201. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  26202. /* read first line */
  26203. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26204. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  26205. XMEMSET(tmp, 0, 1024);
  26206. tmpSz = BIO_read(out, tmp, 1024);
  26207. ExpectIntEQ(tmpSz, 6);
  26208. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  26209. /* fail on second line (not % 2) */
  26210. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26211. /* read 3rd long line */
  26212. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26213. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  26214. XMEMSET(tmp, 0, 1024);
  26215. tmpSz = BIO_read(out, tmp, 1024);
  26216. ExpectIntEQ(tmpSz, 30);
  26217. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  26218. /* fail on empty line */
  26219. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  26220. BIO_free(bio);
  26221. bio = NULL;
  26222. /* Make long integer, requiring dynamic memory, even longer. */
  26223. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  26224. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26225. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  26226. XMEMSET(tmp, 0, 1024);
  26227. tmpSz = BIO_read(out, tmp, 1024);
  26228. ExpectIntEQ(tmpSz, 48);
  26229. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26230. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  26231. XMEMSET(tmp, 0, 1024);
  26232. tmpSz = BIO_read(out, tmp, 1024);
  26233. ExpectIntEQ(tmpSz, 56);
  26234. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  26235. BIO_free(bio);
  26236. BIO_free(out);
  26237. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26238. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26239. /* Ensure there is 0 bytes available to write into. */
  26240. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  26241. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26242. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26243. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26244. BIO_free(fixed);
  26245. ASN1_INTEGER_free(ai);
  26246. #endif
  26247. return EXPECT_RESULT();
  26248. }
  26249. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  26250. {
  26251. EXPECT_DECLS;
  26252. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26253. ASN1_INTEGER *a = NULL;
  26254. unsigned char *pp,*tpp;
  26255. int ret;
  26256. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  26257. /* Invalid parameter testing. */
  26258. /* Set pp to an invalid value. */
  26259. pp = NULL;
  26260. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  26261. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  26262. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  26263. /* 40 */
  26264. if (a != NULL) {
  26265. a->intData[0] = ASN_INTEGER;
  26266. a->intData[1] = 1;
  26267. a->intData[2] = 40;
  26268. }
  26269. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26270. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26271. DYNAMIC_TYPE_TMP_BUFFER));
  26272. tpp = pp;
  26273. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26274. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26275. tpp--;
  26276. ExpectIntEQ(*tpp, 40);
  26277. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26278. pp = NULL;
  26279. /* 128 */
  26280. if (a != NULL) {
  26281. a->intData[0] = ASN_INTEGER;
  26282. a->intData[1] = 1;
  26283. a->intData[2] = 128;
  26284. }
  26285. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26286. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26287. DYNAMIC_TYPE_TMP_BUFFER));
  26288. tpp = pp;
  26289. if (tpp != NULL) {
  26290. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26291. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26292. tpp--;
  26293. ExpectIntEQ(*(tpp--), 128);
  26294. ExpectIntEQ(*tpp, 0);
  26295. }
  26296. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26297. pp = NULL;
  26298. /* -40 */
  26299. if (a != NULL) {
  26300. a->intData[0] = ASN_INTEGER;
  26301. a->intData[1] = 1;
  26302. a->intData[2] = 40;
  26303. a->negative = 1;
  26304. }
  26305. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26306. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26307. DYNAMIC_TYPE_TMP_BUFFER));
  26308. tpp = pp;
  26309. if (tpp != NULL) {
  26310. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26311. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26312. tpp--;
  26313. ExpectIntEQ(*tpp, 216);
  26314. }
  26315. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26316. pp = NULL;
  26317. /* -128 */
  26318. if (a != NULL) {
  26319. a->intData[0] = ASN_INTEGER;
  26320. a->intData[1] = 1;
  26321. a->intData[2] = 128;
  26322. a->negative = 1;
  26323. }
  26324. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26325. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26326. DYNAMIC_TYPE_TMP_BUFFER));
  26327. tpp = pp;
  26328. if (tpp != NULL) {
  26329. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26330. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26331. tpp--;
  26332. ExpectIntEQ(*tpp, 128);
  26333. }
  26334. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26335. pp = NULL;
  26336. /* -200 */
  26337. if (a != NULL) {
  26338. a->intData[0] = ASN_INTEGER;
  26339. a->intData[1] = 1;
  26340. a->intData[2] = 200;
  26341. a->negative = 1;
  26342. }
  26343. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26344. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26345. DYNAMIC_TYPE_TMP_BUFFER));
  26346. tpp = pp;
  26347. if (tpp != NULL) {
  26348. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26349. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26350. tpp--;
  26351. ExpectIntEQ(*(tpp--), 56);
  26352. ExpectIntEQ(*tpp, 255);
  26353. }
  26354. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26355. pp = NULL;
  26356. /* Empty */
  26357. if (a != NULL) {
  26358. a->intData[0] = ASN_INTEGER;
  26359. a->intData[1] = 0;
  26360. a->negative = 0;
  26361. }
  26362. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26363. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26364. DYNAMIC_TYPE_TMP_BUFFER));
  26365. tpp = pp;
  26366. if (tpp != NULL) {
  26367. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26368. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26369. tpp--;
  26370. ExpectIntEQ(*tpp, 0);
  26371. }
  26372. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26373. pp = NULL;
  26374. /* 0 */
  26375. if (a != NULL) {
  26376. a->intData[0] = ASN_INTEGER;
  26377. a->intData[1] = 1;
  26378. a->intData[2] = 0;
  26379. a->negative = 1;
  26380. }
  26381. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26382. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26383. DYNAMIC_TYPE_TMP_BUFFER));
  26384. if (tpp != NULL) {
  26385. tpp = pp;
  26386. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26387. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26388. tpp--;
  26389. ExpectIntEQ(*tpp, 0);
  26390. }
  26391. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26392. pp = NULL;
  26393. /* 0x100 */
  26394. if (a != NULL) {
  26395. a->intData[0] = ASN_INTEGER;
  26396. a->intData[1] = 2;
  26397. a->intData[2] = 0x01;
  26398. a->intData[3] = 0x00;
  26399. a->negative = 0;
  26400. }
  26401. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26402. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26403. DYNAMIC_TYPE_TMP_BUFFER));
  26404. if (tpp != NULL) {
  26405. tpp = pp;
  26406. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26407. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26408. tpp -= 2;
  26409. ExpectIntEQ(tpp[0], 0x01);
  26410. ExpectIntEQ(tpp[1], 0x00);
  26411. }
  26412. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26413. pp = NULL;
  26414. /* -0x8000 => 0x8000 */
  26415. if (a != NULL) {
  26416. a->intData[0] = ASN_INTEGER;
  26417. a->intData[1] = 2;
  26418. a->intData[2] = 0x80;
  26419. a->intData[3] = 0x00;
  26420. a->negative = 1;
  26421. }
  26422. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26423. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26424. DYNAMIC_TYPE_TMP_BUFFER));
  26425. tpp = pp;
  26426. if (tpp != NULL) {
  26427. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26428. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26429. tpp -= 2;
  26430. ExpectIntEQ(tpp[0], 0x80);
  26431. ExpectIntEQ(tpp[1], 0x00);
  26432. }
  26433. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26434. pp = NULL;
  26435. /* -0x8001 => 0xFF7FFF */
  26436. if (a != NULL) {
  26437. a->intData[0] = ASN_INTEGER;
  26438. a->intData[1] = 2;
  26439. a->intData[2] = 0x80;
  26440. a->intData[3] = 0x01;
  26441. a->negative = 1;
  26442. }
  26443. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  26444. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26445. DYNAMIC_TYPE_TMP_BUFFER));
  26446. tpp = pp;
  26447. if (tpp != NULL) {
  26448. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26449. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  26450. tpp -= 3;
  26451. ExpectIntEQ(tpp[0], 0xFF);
  26452. ExpectIntEQ(tpp[1], 0x7F);
  26453. ExpectIntEQ(tpp[2], 0xFF);
  26454. }
  26455. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26456. wolfSSL_ASN1_INTEGER_free(a);
  26457. #endif /* OPENSSL_EXTRA && !NO_ASN */
  26458. return EXPECT_RESULT();
  26459. }
  26460. static int test_wolfSSL_ASN1_OBJECT(void)
  26461. {
  26462. EXPECT_DECLS;
  26463. #if defined(OPENSSL_EXTRA)
  26464. ASN1_OBJECT* a = NULL;
  26465. ASN1_OBJECT s;
  26466. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  26467. /* Invalid parameter testing. */
  26468. ASN1_OBJECT_free(NULL);
  26469. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  26470. /* Test that a static ASN1_OBJECT can be freed. */
  26471. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26472. ASN1_OBJECT_free(&s);
  26473. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26474. ASN1_OBJECT_free(a);
  26475. a = NULL;
  26476. s.obj = der;
  26477. s.objSz = sizeof(der);
  26478. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26479. ASN1_OBJECT_free(a);
  26480. ASN1_OBJECT_free(&s);
  26481. #endif /* OPENSSL_EXTRA */
  26482. return EXPECT_RESULT();
  26483. }
  26484. static int test_wolfSSL_ASN1_get_object(void)
  26485. {
  26486. EXPECT_DECLS;
  26487. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  26488. const unsigned char* derBuf = cliecc_cert_der_256;
  26489. const unsigned char* nullPtr = NULL;
  26490. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  26491. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  26492. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  26493. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  26494. const unsigned char* p;
  26495. unsigned char objDer[8];
  26496. unsigned char* der;
  26497. unsigned char* derPtr;
  26498. int len = sizeof_cliecc_cert_der_256;
  26499. long asnLen = 0;
  26500. int tag = 0;
  26501. int cls = 0;
  26502. ASN1_OBJECT* a = NULL;
  26503. ASN1_OBJECT s;
  26504. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26505. /* Invalid encoding at length. */
  26506. p = objDerInvalidLen;
  26507. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26508. 0x80);
  26509. p = objDerBadLen;
  26510. /* Error = 0x80, Constructed = 0x20 */
  26511. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26512. 0x80 | 0x20);
  26513. /* Read a couple TLV triplets and make sure they match the expected values
  26514. */
  26515. /* SEQUENCE */
  26516. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  26517. ExpectIntEQ(asnLen, 862);
  26518. ExpectIntEQ(tag, 0x10);
  26519. ExpectIntEQ(cls, 0);
  26520. /* SEQUENCE */
  26521. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26522. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26523. ExpectIntEQ(asnLen, 772);
  26524. ExpectIntEQ(tag, 0x10);
  26525. ExpectIntEQ(cls, 0);
  26526. /* [0] */
  26527. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26528. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26529. ExpectIntEQ(asnLen, 3);
  26530. ExpectIntEQ(tag, 0);
  26531. ExpectIntEQ(cls, 0x80);
  26532. /* INTEGER */
  26533. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26534. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26535. ExpectIntEQ(asnLen, 1);
  26536. ExpectIntEQ(tag, 0x2);
  26537. ExpectIntEQ(cls, 0);
  26538. derBuf += asnLen;
  26539. /* INTEGER */
  26540. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26541. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26542. ExpectIntEQ(asnLen, 20);
  26543. ExpectIntEQ(tag, 0x2);
  26544. ExpectIntEQ(cls, 0);
  26545. derBuf += asnLen;
  26546. /* SEQUENCE */
  26547. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26548. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26549. ExpectIntEQ(asnLen, 10);
  26550. ExpectIntEQ(tag, 0x10);
  26551. ExpectIntEQ(cls, 0);
  26552. /* Found OBJECT_ID. */
  26553. /* Invalid parameter testing. */
  26554. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  26555. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  26556. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  26557. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  26558. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  26559. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  26560. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  26561. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  26562. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  26563. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  26564. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26565. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26566. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  26567. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  26568. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  26569. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  26570. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  26571. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26572. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26573. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  26574. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  26575. /* Invalid encoding at length. */
  26576. p = objDerInvalidLen;
  26577. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  26578. p = objDerBadLen;
  26579. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  26580. p = objDerNotObj;
  26581. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  26582. p = objDerNoData;
  26583. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  26584. /* Create an ASN OBJECT from content */
  26585. p = derBuf + 2;
  26586. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  26587. ASN1_OBJECT_free(a);
  26588. a = NULL;
  26589. /* Create an ASN OBJECT from DER */
  26590. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  26591. /* Invalid parameter testing. */
  26592. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  26593. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  26594. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  26595. der = NULL;
  26596. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  26597. derPtr = objDer;
  26598. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  26599. ExpectPtrNE(derPtr, objDer);
  26600. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  26601. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  26602. ASN1_OBJECT_free(a);
  26603. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26604. return EXPECT_RESULT();
  26605. }
  26606. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  26607. {
  26608. EXPECT_DECLS;
  26609. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26610. ASN1_OBJECT* obj = NULL;
  26611. ASN1_OBJECT* a = NULL;
  26612. BIO *bio = NULL;
  26613. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  26614. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  26615. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  26616. const unsigned char* p;
  26617. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  26618. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  26619. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  26620. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  26621. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  26622. /* No DER encoding in ASN1_OBJECT. */
  26623. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  26624. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26625. ASN1_OBJECT_free(a);
  26626. a = NULL;
  26627. /* DER encoding - not OBJECT_ID */
  26628. p = notObjDer;
  26629. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26630. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26631. ASN1_OBJECT_free(a);
  26632. a = NULL;
  26633. /* Bad length encoding. */
  26634. p = badLenDer;
  26635. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26636. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26637. ASN1_OBJECT_free(a);
  26638. a = NULL;
  26639. /* Good encoding - but unknown. */
  26640. p = goodDer;
  26641. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26642. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26643. ASN1_OBJECT_free(a);
  26644. BIO_free(bio);
  26645. ASN1_OBJECT_free(obj);
  26646. #endif
  26647. return EXPECT_RESULT();
  26648. }
  26649. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  26650. {
  26651. EXPECT_DECLS;
  26652. #if defined(OPENSSL_EXTRA) && \
  26653. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  26654. char buf[50] = {0};
  26655. ASN1_OBJECT* obj;
  26656. const char* oid = "2.5.29.19";
  26657. const char* ln = "X509v3 Basic Constraints";
  26658. obj = NULL;
  26659. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  26660. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  26661. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  26662. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  26663. XMEMSET(buf, 0, sizeof(buf));
  26664. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  26665. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  26666. ASN1_OBJECT_free(obj);
  26667. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  26668. return EXPECT_RESULT();
  26669. }
  26670. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  26671. {
  26672. EXPECT_DECLS;
  26673. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  26674. WOLFSSL_STACK* sk = NULL;
  26675. WOLFSSL_ASN1_OBJECT* obj;
  26676. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26677. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26678. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26679. sk = NULL;
  26680. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26681. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  26682. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  26683. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  26684. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26685. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  26686. sk = NULL;
  26687. /* obj freed in pop_free call. */
  26688. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26689. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26690. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26691. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  26692. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26693. wolfSSL_ASN1_OBJECT_free(obj);
  26694. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  26695. return EXPECT_RESULT();
  26696. }
  26697. static int test_wolfSSL_ASN1_STRING(void)
  26698. {
  26699. EXPECT_DECLS;
  26700. #if defined(OPENSSL_EXTRA)
  26701. ASN1_STRING* str = NULL;
  26702. ASN1_STRING* c = NULL;
  26703. const char data[] = "hello wolfSSL";
  26704. const char data2[] = "Same len data";
  26705. const char longData[] =
  26706. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26707. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26708. ASN1_STRING_free(str);
  26709. str = NULL;
  26710. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26711. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  26712. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  26713. /* Check setting to NULL works. */
  26714. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  26715. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  26716. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26717. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  26718. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  26719. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  26720. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  26721. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  26722. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  26723. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  26724. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  26725. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  26726. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  26727. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26728. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  26729. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  26730. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  26731. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  26732. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26733. /* Check setting back to smaller size frees dynamic data. */
  26734. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26735. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  26736. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  26737. ExpectNull(ASN1_STRING_get0_data(NULL));
  26738. ExpectNotNull(ASN1_STRING_get0_data(str));
  26739. ExpectNull(ASN1_STRING_data(NULL));
  26740. ExpectNotNull(ASN1_STRING_data(str));
  26741. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  26742. ExpectIntGT(ASN1_STRING_length(str), 0);
  26743. ASN1_STRING_free(c);
  26744. ASN1_STRING_free(str);
  26745. ASN1_STRING_free(NULL);
  26746. #ifndef NO_WOLFSSL_STUB
  26747. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  26748. #endif
  26749. #endif
  26750. return EXPECT_RESULT();
  26751. }
  26752. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  26753. {
  26754. EXPECT_DECLS;
  26755. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  26756. !defined(NO_FILESYSTEM)
  26757. WOLFSSL_X509* x509 = NULL;
  26758. WOLFSSL_X509_NAME* subject = NULL;
  26759. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  26760. WOLFSSL_ASN1_STRING* a = NULL;
  26761. FILE* file = XBADFILE;
  26762. int idx = 0;
  26763. char targetOutput[16] = "www.wolfssl.com";
  26764. unsigned char* actual_output = NULL;
  26765. int len = 0;
  26766. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  26767. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  26768. if (file != NULL)
  26769. fclose(file);
  26770. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  26771. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  26772. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  26773. NID_commonName, -1)), 5);
  26774. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  26775. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  26776. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  26777. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  26778. a = NULL;
  26779. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  26780. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  26781. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  26782. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  26783. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  26784. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  26785. wolfSSL_X509_free(x509);
  26786. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26787. ExpectNotNull(a = ASN1_STRING_new());
  26788. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  26789. ASN1_STRING_free(a);
  26790. #endif
  26791. return EXPECT_RESULT();
  26792. }
  26793. static int test_wolfSSL_i2s_ASN1_STRING(void)
  26794. {
  26795. EXPECT_DECLS;
  26796. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26797. WOLFSSL_ASN1_STRING* str = NULL;
  26798. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  26799. char* ret = NULL;
  26800. ExpectNotNull(str = ASN1_STRING_new());
  26801. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  26802. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26803. ret = NULL;
  26804. /* No data. */
  26805. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26806. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26807. ret = NULL;
  26808. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  26809. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26810. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26811. ret = NULL;
  26812. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  26813. /* No type. */
  26814. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26815. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26816. ASN1_STRING_free(str);
  26817. #endif
  26818. return EXPECT_RESULT();
  26819. }
  26820. static int test_wolfSSL_ASN1_STRING_canon(void)
  26821. {
  26822. EXPECT_DECLS;
  26823. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  26824. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  26825. defined(OPENSSL_EXTRA_X509_SMALL))
  26826. WOLFSSL_ASN1_STRING* orig = NULL;
  26827. WOLFSSL_ASN1_STRING* canon = NULL;
  26828. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  26829. const char* whitespaceOnly = "\t\r\n";
  26830. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  26831. const char* canonData = "\x01 \x02 \xff tt";
  26832. const char longData[] =
  26833. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26834. ExpectNotNull(orig = ASN1_STRING_new());
  26835. ExpectNotNull(canon = ASN1_STRING_new());
  26836. /* Invalid parameter testing. */
  26837. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  26838. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  26839. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  26840. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26841. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26842. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  26843. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26844. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26845. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  26846. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26847. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26848. ASN1_STRING_free(orig);
  26849. orig = NULL;
  26850. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26851. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  26852. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26853. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  26854. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26855. ASN1_STRING_free(orig);
  26856. orig = NULL;
  26857. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26858. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  26859. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26860. ASN1_STRING_free(orig);
  26861. orig = NULL;
  26862. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26863. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26864. ASN1_STRING_free(orig);
  26865. ASN1_STRING_free(canon);
  26866. #endif
  26867. #endif
  26868. return EXPECT_RESULT();
  26869. }
  26870. static int test_wolfSSL_ASN1_STRING_print(void)
  26871. {
  26872. EXPECT_DECLS;
  26873. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  26874. !defined(NO_BIO)
  26875. ASN1_STRING* asnStr = NULL;
  26876. const char HELLO_DATA[]= \
  26877. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  26878. #define MAX_UNPRINTABLE_CHAR 32
  26879. #define MAX_BUF 255
  26880. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  26881. unsigned char expected[sizeof(unprintableData)+1];
  26882. unsigned char rbuf[MAX_BUF];
  26883. BIO *bio = NULL;
  26884. int p_len;
  26885. int i;
  26886. /* setup */
  26887. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  26888. unprintableData[i] = HELLO_DATA[i];
  26889. expected[i] = HELLO_DATA[i];
  26890. }
  26891. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  26892. unprintableData[sizeof(HELLO_DATA)+i] = i;
  26893. if (i == (int)'\n' || i == (int)'\r')
  26894. expected[sizeof(HELLO_DATA)+i] = i;
  26895. else
  26896. expected[sizeof(HELLO_DATA)+i] = '.';
  26897. }
  26898. unprintableData[sizeof(unprintableData)-1] = '\0';
  26899. expected[sizeof(expected)-1] = '\0';
  26900. XMEMSET(rbuf, 0, MAX_BUF);
  26901. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26902. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  26903. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26904. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  26905. (int)sizeof(unprintableData)), 1);
  26906. /* test */
  26907. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  26908. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  26909. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  26910. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  26911. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  26912. ExpectStrEQ((char*)rbuf, (const char*)expected);
  26913. BIO_free(bio);
  26914. bio = NULL;
  26915. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26916. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26917. /* Ensure there is 0 bytes available to write into. */
  26918. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  26919. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26920. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26921. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26922. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  26923. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26924. BIO_free(bio);
  26925. ASN1_STRING_free(asnStr);
  26926. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  26927. return EXPECT_RESULT();
  26928. }
  26929. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  26930. {
  26931. EXPECT_DECLS;
  26932. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26933. ASN1_STRING* asn_str = NULL;
  26934. const char data[] = "Hello wolfSSL!";
  26935. ASN1_STRING* esc_str = NULL;
  26936. const char esc_data[] = "a+;<>";
  26937. ASN1_STRING* neg_int = NULL;
  26938. const char neg_int_data[] = "\xff";
  26939. ASN1_STRING* neg_enum = NULL;
  26940. const char neg_enum_data[] = "\xff";
  26941. BIO *bio = NULL;
  26942. BIO *fixed = NULL;
  26943. unsigned long flags;
  26944. int p_len;
  26945. unsigned char rbuf[255];
  26946. /* setup */
  26947. XMEMSET(rbuf, 0, 255);
  26948. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26949. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  26950. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26951. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26952. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  26953. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26954. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  26955. sizeof(esc_data)), 1);
  26956. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  26957. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  26958. sizeof(neg_int_data) - 1), 1);
  26959. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  26960. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  26961. sizeof(neg_enum_data) - 1), 1);
  26962. /* Invalid parameter testing. */
  26963. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  26964. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  26965. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  26966. /* no flags */
  26967. XMEMSET(rbuf, 0, 255);
  26968. flags = 0;
  26969. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  26970. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  26971. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  26972. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26973. /* Ensure there is 0 bytes available to write into. */
  26974. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26975. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26976. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26977. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26978. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  26979. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26980. /* RFC2253 Escape */
  26981. XMEMSET(rbuf, 0, 255);
  26982. flags = ASN1_STRFLGS_ESC_2253;
  26983. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  26984. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  26985. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  26986. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26987. /* Ensure there is 0 bytes available to write into. */
  26988. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26989. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26990. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26991. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26992. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  26993. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26994. /* Show type */
  26995. XMEMSET(rbuf, 0, 255);
  26996. flags = ASN1_STRFLGS_SHOW_TYPE;
  26997. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  26998. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  26999. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  27000. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27001. /* Ensure there is 0 bytes available to write into. */
  27002. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27003. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27004. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27005. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27006. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  27007. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27008. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  27009. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27010. /* Dump All */
  27011. XMEMSET(rbuf, 0, 255);
  27012. flags = ASN1_STRFLGS_DUMP_ALL;
  27013. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  27014. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  27015. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  27016. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27017. /* Ensure there is 0 bytes available to write into. */
  27018. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27019. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27020. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27021. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27022. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27023. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27024. /* Dump Der */
  27025. XMEMSET(rbuf, 0, 255);
  27026. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  27027. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  27028. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  27029. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  27030. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27031. /* Ensure there is 0 bytes available to write into. */
  27032. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27033. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27034. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27035. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27036. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  27037. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27038. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27039. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27040. /* Dump All + Show type */
  27041. XMEMSET(rbuf, 0, 255);
  27042. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27043. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  27044. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  27045. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  27046. /* Dump All + Show type - Negative Integer. */
  27047. XMEMSET(rbuf, 0, 255);
  27048. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27049. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  27050. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  27051. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  27052. /* Dump All + Show type - Negative Enumerated. */
  27053. XMEMSET(rbuf, 0, 255);
  27054. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27055. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  27056. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  27057. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  27058. BIO_free(fixed);
  27059. BIO_free(bio);
  27060. ASN1_STRING_free(asn_str);
  27061. ASN1_STRING_free(esc_str);
  27062. ASN1_STRING_free(neg_int);
  27063. ASN1_STRING_free(neg_enum);
  27064. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  27065. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  27066. #endif
  27067. return EXPECT_RESULT();
  27068. }
  27069. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  27070. {
  27071. EXPECT_DECLS;
  27072. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  27073. ASN1_STRING* asn1str_test = NULL;
  27074. ASN1_STRING* asn1str_answer = NULL;
  27075. /* Each character is encoded using 4 bytes */
  27076. char input[] = {
  27077. 0, 0, 0, 'T',
  27078. 0, 0, 0, 'e',
  27079. 0, 0, 0, 's',
  27080. 0, 0, 0, 't',
  27081. };
  27082. char output[] = "Test";
  27083. char badInput[] = {
  27084. 1, 0, 0, 'T',
  27085. 0, 1, 0, 'e',
  27086. 0, 0, 1, 's',
  27087. };
  27088. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  27089. /* Test wrong type. */
  27090. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27091. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27092. ASN1_STRING_free(asn1str_test);
  27093. asn1str_test = NULL;
  27094. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  27095. /* Test bad length. */
  27096. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  27097. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27098. /* Test bad input. */
  27099. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  27100. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27101. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  27102. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27103. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  27104. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27105. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  27106. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  27107. ExpectNotNull(
  27108. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27109. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  27110. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  27111. ASN1_STRING_free(asn1str_test);
  27112. ASN1_STRING_free(asn1str_answer);
  27113. #endif /* OPENSSL_ALL && !NO_ASN */
  27114. return EXPECT_RESULT();
  27115. }
  27116. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27117. {
  27118. EXPECT_DECLS;
  27119. #if defined(OPENSSL_EXTRA)
  27120. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  27121. unsigned char nullstr[32];
  27122. XMEMSET(nullstr, 0, 32);
  27123. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27124. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  27125. if (asn1_gtime != NULL) {
  27126. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27127. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27128. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27129. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27130. }
  27131. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  27132. #endif /* OPENSSL_EXTRA */
  27133. return EXPECT_RESULT();
  27134. }
  27135. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  27136. {
  27137. EXPECT_DECLS;
  27138. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27139. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  27140. BIO* bio = NULL;
  27141. unsigned char buf[24];
  27142. int i;
  27143. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27144. BIO_set_write_buf_size(bio, 24);
  27145. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  27146. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  27147. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  27148. /* Type not set. */
  27149. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27150. gtime.type = V_ASN1_GENERALIZEDTIME;
  27151. /* Invalid parameters testing. */
  27152. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  27153. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  27154. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  27155. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  27156. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  27157. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  27158. BIO_free(bio);
  27159. bio = NULL;
  27160. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27161. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27162. /* Ensure there is 0 bytes available to write into. */
  27163. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  27164. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27165. for (i = 1; i < 20; i++) {
  27166. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  27167. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  27168. }
  27169. BIO_free(bio);
  27170. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  27171. #endif /* OPENSSL_EXTRA */
  27172. return EXPECT_RESULT();
  27173. }
  27174. static int test_wolfSSL_ASN1_TIME(void)
  27175. {
  27176. EXPECT_DECLS;
  27177. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27178. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27179. unsigned char *data;
  27180. ExpectNotNull(asn_time = ASN1_TIME_new());
  27181. #ifndef NO_WOLFSSL_STUB
  27182. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  27183. #endif
  27184. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  27185. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  27186. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  27187. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  27188. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  27189. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  27190. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  27191. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  27192. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  27193. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  27194. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  27195. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  27196. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  27197. ASN1_TIME_free(asn_time);
  27198. ASN1_TIME_free(NULL);
  27199. #endif
  27200. return EXPECT_RESULT();
  27201. }
  27202. static int test_wolfSSL_ASN1_TIME_to_string(void)
  27203. {
  27204. EXPECT_DECLS;
  27205. #ifndef NO_ASN_TIME
  27206. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27207. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  27208. WOLFSSL_ASN1_TIME* t = NULL;
  27209. char buf[ASN_GENERALIZED_TIME_SIZE];
  27210. ExpectNotNull((t = ASN1_TIME_new()));
  27211. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  27212. /* Invalid parameter testing. */
  27213. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  27214. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  27215. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  27216. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  27217. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  27218. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  27219. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  27220. /* Buffer needs to be longer than minimum of 5 characters. */
  27221. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  27222. ASN1_TIME_free(t);
  27223. #endif
  27224. #endif /* NO_ASN_TIME */
  27225. return EXPECT_RESULT();
  27226. }
  27227. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27228. {
  27229. EXPECT_DECLS;
  27230. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27231. ASN1_TIME* fromTime = NULL;
  27232. ASN1_TIME* closeToTime = NULL;
  27233. ASN1_TIME* toTime = NULL;
  27234. ASN1_TIME* invalidTime = NULL;
  27235. int daysDiff;
  27236. int secsDiff;
  27237. ExpectNotNull((fromTime = ASN1_TIME_new()));
  27238. /* Feb 22, 2003, 21:15:15 */
  27239. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  27240. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  27241. /* Feb 22, 2003, 21:16:15 */
  27242. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  27243. ExpectNotNull((toTime = ASN1_TIME_new()));
  27244. /* Dec 19, 2010, 18:10:11 */
  27245. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  27246. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  27247. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  27248. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  27249. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  27250. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  27251. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27252. /* Error conditions. */
  27253. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  27254. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  27255. /* If both times are NULL, difference is 0. */
  27256. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  27257. ExpectIntEQ(daysDiff, 0);
  27258. ExpectIntEQ(secsDiff, 0);
  27259. /* If one time is NULL, it defaults to the current time. */
  27260. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  27261. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  27262. /* Normal operation. Both times non-NULL. */
  27263. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27264. ExpectIntEQ(daysDiff, 2856);
  27265. ExpectIntEQ(secsDiff, 75296);
  27266. /* Swapping the times should return negative values. */
  27267. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  27268. ExpectIntEQ(daysDiff, -2856);
  27269. ExpectIntEQ(secsDiff, -75296);
  27270. /* Compare with invalid time string. */
  27271. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  27272. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  27273. /* Compare with days difference of 0. */
  27274. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  27275. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  27276. /* Days and seconds differences not 0. */
  27277. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27278. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27279. /* Same time. */
  27280. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27281. /* Compare regression test: No seconds difference, just difference in days.
  27282. */
  27283. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27284. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27285. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27286. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27287. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27288. /* Edge case with Unix epoch. */
  27289. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27290. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27291. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27292. ExpectIntEQ(daysDiff, 3652);
  27293. ExpectIntEQ(secsDiff, 0);
  27294. /* Edge case with year > 2038 (year 2038 problem). */
  27295. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27296. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27297. ExpectIntEQ(daysDiff, 2932896);
  27298. ExpectIntEQ(secsDiff, 86399);
  27299. ASN1_TIME_free(fromTime);
  27300. ASN1_TIME_free(closeToTime);
  27301. ASN1_TIME_free(toTime);
  27302. ASN1_TIME_free(invalidTime);
  27303. #endif
  27304. return EXPECT_RESULT();
  27305. }
  27306. static int test_wolfSSL_ASN1_TIME_adj(void)
  27307. {
  27308. EXPECT_DECLS;
  27309. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  27310. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  27311. const int year = 365*24*60*60;
  27312. const int day = 24*60*60;
  27313. const int hour = 60*60;
  27314. const int mini = 60;
  27315. const byte asn_utc_time = ASN_UTC_TIME;
  27316. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27317. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  27318. #endif
  27319. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27320. WOLFSSL_ASN1_TIME* s = NULL;
  27321. int offset_day;
  27322. long offset_sec;
  27323. char date_str[CTC_DATE_SIZE + 1];
  27324. time_t t;
  27325. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  27326. /* UTC notation test */
  27327. /* 2000/2/15 20:30:00 */
  27328. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  27329. offset_day = 7;
  27330. offset_sec = 45 * mini;
  27331. /* offset_sec = -45 * min;*/
  27332. ExpectNotNull(asn_time =
  27333. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  27334. ExpectTrue(asn_time->type == asn_utc_time);
  27335. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27336. CTC_DATE_SIZE));
  27337. date_str[CTC_DATE_SIZE] = '\0';
  27338. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  27339. /* negative offset */
  27340. offset_sec = -45 * mini;
  27341. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  27342. ExpectNotNull(asn_time);
  27343. ExpectTrue(asn_time->type == asn_utc_time);
  27344. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27345. CTC_DATE_SIZE));
  27346. date_str[CTC_DATE_SIZE] = '\0';
  27347. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  27348. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27349. s = NULL;
  27350. XMEMSET(date_str, 0, sizeof(date_str));
  27351. /* Generalized time will overflow time_t if not long */
  27352. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27353. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  27354. DYNAMIC_TYPE_OPENSSL);
  27355. /* GeneralizedTime notation test */
  27356. /* 2055/03/01 09:00:00 */
  27357. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  27358. offset_day = 12;
  27359. offset_sec = 10 * mini;
  27360. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27361. offset_sec));
  27362. ExpectTrue(asn_time->type == asn_gen_time);
  27363. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27364. CTC_DATE_SIZE));
  27365. date_str[CTC_DATE_SIZE] = '\0';
  27366. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  27367. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27368. s = NULL;
  27369. XMEMSET(date_str, 0, sizeof(date_str));
  27370. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  27371. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  27372. s = NULL;
  27373. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  27374. offset_day = 7;
  27375. offset_sec = 45 * mini;
  27376. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27377. offset_sec));
  27378. ExpectTrue(asn_time->type == asn_utc_time);
  27379. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27380. CTC_DATE_SIZE));
  27381. date_str[CTC_DATE_SIZE] = '\0';
  27382. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27383. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27384. asn_time = NULL;
  27385. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  27386. offset_sec));
  27387. ExpectTrue(asn_time->type == asn_utc_time);
  27388. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27389. CTC_DATE_SIZE));
  27390. date_str[CTC_DATE_SIZE] = '\0';
  27391. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27392. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27393. #endif
  27394. return EXPECT_RESULT();
  27395. }
  27396. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  27397. {
  27398. EXPECT_DECLS;
  27399. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27400. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27401. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  27402. ASN1_TIME asnTime;
  27403. struct tm tm;
  27404. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  27405. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27406. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  27407. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  27408. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27409. ExpectIntEQ(tm.tm_sec, 15);
  27410. ExpectIntEQ(tm.tm_min, 15);
  27411. ExpectIntEQ(tm.tm_hour, 21);
  27412. ExpectIntEQ(tm.tm_mday, 22);
  27413. ExpectIntEQ(tm.tm_mon, 1);
  27414. ExpectIntEQ(tm.tm_year, 100);
  27415. ExpectIntEQ(tm.tm_isdst, 0);
  27416. #ifdef XMKTIME
  27417. ExpectIntEQ(tm.tm_wday, 2);
  27418. ExpectIntEQ(tm.tm_yday, 52);
  27419. #endif
  27420. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  27421. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27422. ExpectIntEQ(tm.tm_year, 50);
  27423. /* Get current time. */
  27424. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  27425. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  27426. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27427. /* 0 length. */
  27428. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27429. /* No type. */
  27430. asnTime.length = 1;
  27431. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27432. /* Not UTCTIME length. */
  27433. asnTime.type = V_ASN1_UTCTIME;
  27434. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27435. /* Not GENERALIZEDTIME length. */
  27436. asnTime.type = V_ASN1_GENERALIZEDTIME;
  27437. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27438. /* Not Zulu timezone. */
  27439. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  27440. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27441. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  27442. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27443. #ifdef XMKTIME
  27444. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  27445. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27446. ExpectIntEQ(tm.tm_sec, 47);
  27447. ExpectIntEQ(tm.tm_min, 22);
  27448. ExpectIntEQ(tm.tm_hour, 21);
  27449. ExpectIntEQ(tm.tm_mday, 12);
  27450. ExpectIntEQ(tm.tm_mon, 4);
  27451. ExpectIntEQ(tm.tm_year, 123);
  27452. ExpectIntEQ(tm.tm_wday, 5);
  27453. ExpectIntEQ(tm.tm_yday, 131);
  27454. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  27455. fields are zeroed out as expected. */
  27456. ExpectIntEQ(tm.tm_isdst, 0);
  27457. #endif
  27458. #endif
  27459. return EXPECT_RESULT();
  27460. }
  27461. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  27462. {
  27463. EXPECT_DECLS;
  27464. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27465. WOLFSSL_ASN1_TIME *t = NULL;
  27466. WOLFSSL_ASN1_TIME *out = NULL;
  27467. WOLFSSL_ASN1_TIME *gtime = NULL;
  27468. int tlen = 0;
  27469. unsigned char *data = NULL;
  27470. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27471. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  27472. /* type not set. */
  27473. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27474. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27475. t = NULL;
  27476. /* UTC Time test */
  27477. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27478. if (t != NULL) {
  27479. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  27480. t->type = ASN_UTC_TIME;
  27481. t->length = ASN_UTC_TIME_SIZE;
  27482. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27483. }
  27484. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  27485. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  27486. out = NULL;
  27487. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27488. wolfSSL_ASN1_TIME_free(gtime);
  27489. gtime = NULL;
  27490. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  27491. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27492. ExpectPtrEq(gtime, out);
  27493. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27494. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27495. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27496. /* Generalized Time test */
  27497. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27498. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27499. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27500. if (t != NULL) {
  27501. t->type = ASN_GENERALIZED_TIME;
  27502. t->length = ASN_GENERALIZED_TIME_SIZE;
  27503. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  27504. }
  27505. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  27506. ASN_GENERALIZED_TIME_SIZE);
  27507. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  27508. "20050727123456Z");
  27509. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27510. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27511. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27512. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27513. /* UTC Time to Generalized Time 1900's test */
  27514. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27515. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27516. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27517. if (t != NULL) {
  27518. t->type = ASN_UTC_TIME;
  27519. t->length = ASN_UTC_TIME_SIZE;
  27520. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  27521. }
  27522. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27523. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27524. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27525. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  27526. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27527. /* Null parameter test */
  27528. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27529. gtime = NULL;
  27530. out = NULL;
  27531. if (t != NULL) {
  27532. t->type = ASN_UTC_TIME;
  27533. t->length = ASN_UTC_TIME_SIZE;
  27534. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27535. }
  27536. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  27537. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27538. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27539. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27540. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27541. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27542. #endif
  27543. return EXPECT_RESULT();
  27544. }
  27545. static int test_wolfSSL_ASN1_TIME_print(void)
  27546. {
  27547. EXPECT_DECLS;
  27548. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  27549. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27550. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27551. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  27552. !defined(NO_ASN_TIME)
  27553. BIO* bio = NULL;
  27554. BIO* fixed = NULL;
  27555. X509* x509 = NULL;
  27556. const unsigned char* der = client_cert_der_2048;
  27557. ASN1_TIME* notAfter;
  27558. ASN1_TIME* notBefore;
  27559. unsigned char buf[25];
  27560. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27561. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27562. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27563. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27564. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  27565. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  27566. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  27567. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  27568. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  27569. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27570. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  27571. /* Test BIO_write fails. */
  27572. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27573. /* Ensure there is 0 bytes available to write into. */
  27574. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  27575. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27576. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27577. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27578. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  27579. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27580. /* create a bad time and test results */
  27581. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  27582. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  27583. if (EXPECT_SUCCESS()) {
  27584. notAfter->data[8] = 0;
  27585. notAfter->data[3] = 0;
  27586. }
  27587. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  27588. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27589. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27590. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  27591. BIO_free(bio);
  27592. BIO_free(fixed);
  27593. X509_free(x509);
  27594. #endif
  27595. return EXPECT_RESULT();
  27596. }
  27597. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27598. {
  27599. EXPECT_DECLS;
  27600. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27601. BIO* bio = NULL;
  27602. ASN1_UTCTIME* utc = NULL;
  27603. unsigned char buf[25];
  27604. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27605. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27606. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  27607. /* Valid date */
  27608. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27609. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27610. DYNAMIC_TYPE_ASN1));
  27611. if (utc != NULL) {
  27612. utc->type = ASN_UTC_TIME;
  27613. utc->length = ASN_UTC_TIME_SIZE;
  27614. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27615. }
  27616. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  27617. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  27618. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  27619. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27620. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27621. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27622. XMEMSET(buf, 0, sizeof(buf));
  27623. BIO_free(bio);
  27624. bio = NULL;
  27625. /* Invalid format */
  27626. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27627. if (utc != NULL) {
  27628. utc->type = ASN_UTC_TIME;
  27629. utc->length = ASN_UTC_TIME_SIZE;
  27630. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27631. }
  27632. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27633. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27634. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27635. /* Invalid type */
  27636. if (utc != NULL) {
  27637. utc->type = ASN_GENERALIZED_TIME;
  27638. utc->length = ASN_GENERALIZED_TIME_SIZE;
  27639. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  27640. }
  27641. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27642. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27643. BIO_free(bio);
  27644. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27645. return EXPECT_RESULT();
  27646. }
  27647. static int test_wolfSSL_ASN1_TYPE(void)
  27648. {
  27649. EXPECT_DECLS;
  27650. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  27651. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  27652. WOLFSSL_ASN1_TYPE* t = NULL;
  27653. WOLFSSL_ASN1_OBJECT* obj = NULL;
  27654. #ifndef NO_ASN_TIME
  27655. WOLFSSL_ASN1_TIME* time = NULL;
  27656. #endif
  27657. WOLFSSL_ASN1_STRING* str = NULL;
  27658. unsigned char data[] = { 0x00 };
  27659. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  27660. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27661. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  27662. wolfSSL_ASN1_TYPE_free(t);
  27663. t = NULL;
  27664. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27665. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  27666. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  27667. wolfSSL_ASN1_TYPE_free(t);
  27668. t = NULL;
  27669. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27670. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27671. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  27672. wolfSSL_ASN1_TYPE_free(t);
  27673. t = NULL;
  27674. #ifndef NO_ASN_TIME
  27675. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27676. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27677. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  27678. wolfSSL_ASN1_TYPE_free(t);
  27679. t = NULL;
  27680. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27681. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27682. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  27683. wolfSSL_ASN1_TYPE_free(t);
  27684. t = NULL;
  27685. #endif
  27686. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27687. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27688. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  27689. wolfSSL_ASN1_TYPE_free(t);
  27690. t = NULL;
  27691. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27692. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27693. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  27694. wolfSSL_ASN1_TYPE_free(t);
  27695. t = NULL;
  27696. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27697. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27698. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  27699. wolfSSL_ASN1_TYPE_free(t);
  27700. t = NULL;
  27701. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27702. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27703. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  27704. wolfSSL_ASN1_TYPE_free(t);
  27705. t = NULL;
  27706. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27707. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27708. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  27709. wolfSSL_ASN1_TYPE_free(t);
  27710. t = NULL;
  27711. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27712. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27713. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  27714. wolfSSL_ASN1_TYPE_free(t);
  27715. t = NULL;
  27716. #endif
  27717. return EXPECT_RESULT();
  27718. }
  27719. /* Testing code used in dpp.c in hostap */
  27720. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27721. typedef struct {
  27722. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  27723. * as an OID identifying the curve */
  27724. X509_ALGOR *alg;
  27725. /* Compressed format public key per ANSI X9.63 */
  27726. ASN1_BIT_STRING *pub_key;
  27727. } DPP_BOOTSTRAPPING_KEY;
  27728. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  27729. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  27730. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  27731. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  27732. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  27733. typedef struct {
  27734. ASN1_INTEGER *integer;
  27735. } TEST_ASN1;
  27736. ASN1_SEQUENCE(TEST_ASN1) = {
  27737. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  27738. } ASN1_SEQUENCE_END(TEST_ASN1)
  27739. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  27740. typedef struct {
  27741. ASN1_OCTET_STRING *octet_string;
  27742. } TEST_FAIL_ASN1;
  27743. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  27744. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  27745. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  27746. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  27747. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  27748. #endif
  27749. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  27750. {
  27751. EXPECT_DECLS;
  27752. /* Testing code used in dpp.c in hostap */
  27753. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27754. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27755. EC_KEY *eckey = NULL;
  27756. EVP_PKEY *key = NULL;
  27757. size_t len;
  27758. unsigned char *der = NULL;
  27759. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  27760. const unsigned char *in = ecc_clikey_der_256;
  27761. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  27762. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  27763. const EC_GROUP *group = NULL;
  27764. const EC_POINT *point = NULL;
  27765. int nid;
  27766. TEST_ASN1 *test_asn1 = NULL;
  27767. TEST_FAIL_ASN1 test_fail_asn1;
  27768. const unsigned char badObjDer[] = { 0x06, 0x00 };
  27769. const unsigned char goodObjDer[] = {
  27770. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  27771. };
  27772. WOLFSSL_ASN1_ITEM emptyTemplate;
  27773. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  27774. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27775. der = NULL;
  27776. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  27777. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  27778. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27779. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  27780. (long)sizeof_ecc_clikey_der_256));
  27781. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  27782. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  27783. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  27784. nid = EC_GROUP_get_curve_name(group);
  27785. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  27786. group_obj = OBJ_nid2obj(nid);
  27787. if ((ec_obj != NULL) && (group_obj != NULL)) {
  27788. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  27789. group_obj), 1);
  27790. if (EXPECT_SUCCESS()) {
  27791. ec_obj = NULL;
  27792. group_obj = NULL;
  27793. }
  27794. }
  27795. wolfSSL_ASN1_OBJECT_free(group_obj);
  27796. wolfSSL_ASN1_OBJECT_free(ec_obj);
  27797. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  27798. #ifdef HAVE_COMP_KEY
  27799. ExpectIntGT((len = EC_POINT_point2oct(
  27800. group, point, POINT_CONVERSION_COMPRESSED,
  27801. NULL, 0, NULL)), 0);
  27802. #else
  27803. ExpectIntGT((len = EC_POINT_point2oct(
  27804. group, point, POINT_CONVERSION_UNCOMPRESSED,
  27805. NULL, 0, NULL)), 0);
  27806. #endif
  27807. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  27808. #ifdef HAVE_COMP_KEY
  27809. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27810. der, len-1, NULL), 0);
  27811. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27812. der, len, NULL), len);
  27813. #else
  27814. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27815. der, len-1, NULL), 0);
  27816. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27817. der, len, NULL), len);
  27818. #endif
  27819. if (EXPECT_SUCCESS()) {
  27820. bootstrap->pub_key->data = der;
  27821. bootstrap->pub_key->length = (int)len;
  27822. /* Not actually used */
  27823. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27824. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27825. }
  27826. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  27827. der = NULL;
  27828. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27829. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27830. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27831. EVP_PKEY_free(key);
  27832. EC_KEY_free(eckey);
  27833. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27834. bootstrap = NULL;
  27835. DPP_BOOTSTRAPPING_KEY_free(NULL);
  27836. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  27837. * a NULL and an empty BIT_STRING. */
  27838. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27839. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  27840. if (EXPECT_SUCCESS()) {
  27841. bootstrap->alg->algorithm->obj = badObjDer;
  27842. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  27843. }
  27844. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  27845. if (EXPECT_SUCCESS()) {
  27846. bootstrap->alg->parameter->type = V_ASN1_NULL;
  27847. bootstrap->alg->parameter->value.ptr = NULL;
  27848. bootstrap->pub_key->data = NULL;
  27849. bootstrap->pub_key->length = 0;
  27850. /* Not actually used */
  27851. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27852. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27853. }
  27854. /* Encode with bad OBJECT_ID. */
  27855. der = NULL;
  27856. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27857. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  27858. if (EXPECT_SUCCESS()) {
  27859. bootstrap->alg->algorithm->obj = goodObjDer;
  27860. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  27861. bootstrap->alg->algorithm->grp = 2;
  27862. }
  27863. der = NULL;
  27864. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  27865. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  27866. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27867. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27868. /* Test integer */
  27869. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  27870. der = NULL;
  27871. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  27872. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  27873. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27874. TEST_ASN1_free(test_asn1);
  27875. /* Test integer cases. */
  27876. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27877. TEST_ASN1_free(NULL);
  27878. /* Test error cases. */
  27879. ExpectNull(TEST_FAIL_ASN1_new());
  27880. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27881. TEST_FAIL_ASN1_free(NULL);
  27882. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  27883. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  27884. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27885. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27886. return EXPECT_RESULT();
  27887. }
  27888. static int test_wolfSSL_lhash(void)
  27889. {
  27890. EXPECT_DECLS;
  27891. #ifdef OPENSSL_ALL
  27892. const char testStr[] = "Like a true nature's child\n"
  27893. "We were born\n"
  27894. "Born to be wild";
  27895. #ifdef NO_SHA
  27896. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  27897. #else
  27898. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  27899. #endif
  27900. #endif
  27901. return EXPECT_RESULT();
  27902. }
  27903. static int test_wolfSSL_X509_NAME(void)
  27904. {
  27905. EXPECT_DECLS;
  27906. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  27907. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27908. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  27909. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  27910. defined(OPENSSL_EXTRA))
  27911. X509* x509 = NULL;
  27912. const unsigned char* c;
  27913. unsigned char buf[4096];
  27914. int bytes;
  27915. XFILE f = XBADFILE;
  27916. const X509_NAME* a = NULL;
  27917. const X509_NAME* b = NULL;
  27918. X509_NAME* d2i_name = NULL;
  27919. int sz = 0;
  27920. unsigned char* tmp;
  27921. char file[] = "./certs/ca-cert.der";
  27922. #ifndef OPENSSL_EXTRA_X509_SMALL
  27923. byte empty[] = { /* CN=empty emailAddress= */
  27924. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  27925. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  27926. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  27927. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27928. 0x01, 0x16, 0x00
  27929. };
  27930. #endif
  27931. #ifndef OPENSSL_EXTRA_X509_SMALL
  27932. /* test compile of deprecated function, returns 0 */
  27933. ExpectIntEQ(CRYPTO_thread_id(), 0);
  27934. #endif
  27935. ExpectNotNull(a = X509_NAME_new());
  27936. X509_NAME_free((X509_NAME*)a);
  27937. a = NULL;
  27938. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  27939. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  27940. if (f != XBADFILE)
  27941. XFCLOSE(f);
  27942. c = buf;
  27943. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  27944. /* test cmp function */
  27945. ExpectNotNull(a = X509_get_issuer_name(x509));
  27946. ExpectNotNull(b = X509_get_subject_name(x509));
  27947. #ifndef OPENSSL_EXTRA_X509_SMALL
  27948. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  27949. #endif
  27950. tmp = buf;
  27951. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  27952. if (sz > 0 && tmp == buf) {
  27953. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  27954. __LINE__);
  27955. fprintf(stderr, " Expected pointer to be incremented\n");
  27956. abort();
  27957. }
  27958. #ifndef OPENSSL_EXTRA_X509_SMALL
  27959. tmp = buf;
  27960. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27961. #endif
  27962. /* if output parameter is NULL, should still return required size. */
  27963. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  27964. /* retry but with the function creating a buffer */
  27965. tmp = NULL;
  27966. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27967. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27968. tmp = NULL;
  27969. #ifdef WOLFSSL_CERT_NAME_ALL
  27970. /* test for givenName and name */
  27971. {
  27972. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  27973. const byte gName[] = "test-given-name";
  27974. const byte name[] = "test-name";
  27975. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27976. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  27977. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27978. 1);
  27979. wolfSSL_X509_NAME_ENTRY_free(entry);
  27980. entry = NULL;
  27981. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27982. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  27983. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27984. 1);
  27985. wolfSSL_X509_NAME_ENTRY_free(entry);
  27986. tmp = NULL;
  27987. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27988. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27989. }
  27990. #endif
  27991. b = NULL;
  27992. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  27993. #ifndef OPENSSL_EXTRA_X509_SMALL
  27994. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  27995. #endif
  27996. X509_NAME_free((X509_NAME*)b);
  27997. X509_NAME_free(d2i_name);
  27998. d2i_name = NULL;
  27999. X509_free(x509);
  28000. #ifndef OPENSSL_EXTRA_X509_SMALL
  28001. /* test with an empty domain component */
  28002. tmp = empty;
  28003. sz = sizeof(empty);
  28004. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28005. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  28006. /* size of empty emailAddress will be 0 */
  28007. tmp = buf;
  28008. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  28009. (char*)tmp, sizeof(buf)), 0);
  28010. /* should contain no organization name */
  28011. tmp = buf;
  28012. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  28013. (char*)tmp, sizeof(buf)), -1);
  28014. X509_NAME_free(d2i_name);
  28015. #endif
  28016. #endif
  28017. return EXPECT_RESULT();
  28018. }
  28019. static int test_wolfSSL_X509_NAME_hash(void)
  28020. {
  28021. EXPECT_DECLS;
  28022. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28023. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  28024. BIO* bio = NULL;
  28025. X509* x509 = NULL;
  28026. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28027. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28028. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28029. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  28030. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  28031. X509_free(x509);
  28032. BIO_free(bio);
  28033. #endif
  28034. return EXPECT_RESULT();
  28035. }
  28036. static int test_wolfSSL_X509_NAME_print_ex(void)
  28037. {
  28038. EXPECT_DECLS;
  28039. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  28040. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  28041. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  28042. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  28043. !defined(NO_BIO) && !defined(NO_RSA)
  28044. int memSz;
  28045. byte* mem = NULL;
  28046. BIO* bio = NULL;
  28047. BIO* membio = NULL;
  28048. X509* x509 = NULL;
  28049. X509_NAME* name = NULL;
  28050. const char* expNormal = "C=US, CN=wolfssl.com";
  28051. const char* expReverse = "CN=wolfssl.com, C=US";
  28052. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  28053. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  28054. const char* expRFC5523 =
  28055. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  28056. /* Test with real cert (svrCertFile) first */
  28057. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28058. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28059. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28060. ExpectNotNull(name = X509_get_subject_name(x509));
  28061. /* Test without flags */
  28062. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28063. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28064. BIO_free(membio);
  28065. membio = NULL;
  28066. /* Test flag: XN_FLAG_RFC2253 */
  28067. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28068. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28069. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28070. BIO_free(membio);
  28071. membio = NULL;
  28072. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  28073. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28074. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28075. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28076. BIO_free(membio);
  28077. membio = NULL;
  28078. X509_free(x509);
  28079. BIO_free(bio);
  28080. name = NULL;
  28081. /* Test normal case without escaped characters */
  28082. {
  28083. /* Create name: "/C=US/CN=wolfssl.com" */
  28084. ExpectNotNull(name = X509_NAME_new());
  28085. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28086. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  28087. WOLFSSL_SUCCESS);
  28088. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28089. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  28090. WOLFSSL_SUCCESS);
  28091. /* Test without flags */
  28092. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28093. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28094. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28095. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  28096. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  28097. BIO_free(membio);
  28098. membio = NULL;
  28099. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  28100. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28101. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28102. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28103. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28104. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28105. BIO_free(membio);
  28106. membio = NULL;
  28107. /* Test flags: XN_FLAG_DN_REV - reversed */
  28108. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28109. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28110. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28111. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28112. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28113. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  28114. BIO_free(membio);
  28115. membio = NULL;
  28116. X509_NAME_free(name);
  28117. name = NULL;
  28118. }
  28119. /* Test RFC2253 characters are escaped with backslashes */
  28120. {
  28121. ExpectNotNull(name = X509_NAME_new());
  28122. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28123. /* space at beginning and end, and: ,+"\ */
  28124. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  28125. WOLFSSL_SUCCESS);
  28126. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28127. /* # at beginning, and: <>;*/
  28128. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  28129. WOLFSSL_SUCCESS);
  28130. /* Test without flags */
  28131. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28132. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28133. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28134. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  28135. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  28136. XSTRLEN(expNotEscaped)), 0);
  28137. BIO_free(membio);
  28138. membio = NULL;
  28139. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  28140. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28141. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28142. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28143. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28144. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  28145. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  28146. BIO_free(membio);
  28147. membio = NULL;
  28148. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  28149. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28150. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28151. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28152. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28153. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  28154. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  28155. XSTRLEN(expNotEscapedRev)), 0);
  28156. BIO_free(membio);
  28157. X509_NAME_free(name);
  28158. }
  28159. #endif
  28160. return EXPECT_RESULT();
  28161. }
  28162. #ifndef NO_BIO
  28163. static int test_wolfSSL_X509_INFO_multiple_info(void)
  28164. {
  28165. EXPECT_DECLS;
  28166. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28167. STACK_OF(X509_INFO) *info_stack = NULL;
  28168. X509_INFO *info = NULL;
  28169. int len;
  28170. int i;
  28171. const char* files[] = {
  28172. cliCertFile,
  28173. cliKeyFile,
  28174. /* This needs to be the order as svrCertFile contains the
  28175. * intermediate cert as well. */
  28176. svrKeyFile,
  28177. svrCertFile,
  28178. NULL,
  28179. };
  28180. const char** curFile;
  28181. BIO *fileBIO = NULL;
  28182. BIO *concatBIO = NULL;
  28183. byte tmp[FOURK_BUF];
  28184. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  28185. * to group objects together. */
  28186. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  28187. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  28188. int fileLen;
  28189. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  28190. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  28191. if (EXPECT_SUCCESS()) {
  28192. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  28193. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  28194. fileLen -= len;
  28195. if (EXPECT_FAIL())
  28196. break;
  28197. }
  28198. /* Make sure we read the entire file */
  28199. ExpectIntEQ(fileLen, 0);
  28200. }
  28201. BIO_free(fileBIO);
  28202. fileBIO = NULL;
  28203. }
  28204. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  28205. NULL));
  28206. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  28207. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28208. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28209. ExpectNotNull(info->x509);
  28210. ExpectNull(info->crl);
  28211. if (i != 0) {
  28212. ExpectNotNull(info->x_pkey);
  28213. ExpectIntEQ(X509_check_private_key(info->x509,
  28214. info->x_pkey->dec_pkey), 1);
  28215. }
  28216. else {
  28217. ExpectNull(info->x_pkey);
  28218. }
  28219. }
  28220. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28221. BIO_free(concatBIO);
  28222. #endif
  28223. return EXPECT_RESULT();
  28224. }
  28225. #endif
  28226. #ifndef NO_BIO
  28227. static int test_wolfSSL_X509_INFO(void)
  28228. {
  28229. EXPECT_DECLS;
  28230. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28231. STACK_OF(X509_INFO) *info_stack = NULL;
  28232. X509_INFO *info = NULL;
  28233. BIO *cert = NULL;
  28234. int i;
  28235. /* PEM in hex format to avoid null terminator */
  28236. byte data[] = {
  28237. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  28238. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  28239. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  28240. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  28241. 0x2d, 0x2d, 0x2d
  28242. };
  28243. /* PEM in hex format to avoid null terminator */
  28244. byte data2[] = {
  28245. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  28246. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  28247. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  28248. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  28249. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  28250. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  28251. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  28252. };
  28253. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28254. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28255. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28256. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28257. ExpectNotNull(info->x509);
  28258. ExpectNull(info->crl);
  28259. ExpectNull(info->x_pkey);
  28260. }
  28261. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28262. info_stack = NULL;
  28263. BIO_free(cert);
  28264. cert = NULL;
  28265. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28266. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28267. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28268. info_stack = NULL;
  28269. BIO_free(cert);
  28270. cert = NULL;
  28271. /* This case should fail due to invalid input. */
  28272. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28273. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  28274. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28275. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28276. info_stack = NULL;
  28277. BIO_free(cert);
  28278. cert = NULL;
  28279. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28280. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  28281. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28282. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28283. BIO_free(cert);
  28284. #endif
  28285. return EXPECT_RESULT();
  28286. }
  28287. #endif
  28288. static int test_wolfSSL_X509_subject_name_hash(void)
  28289. {
  28290. EXPECT_DECLS;
  28291. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28292. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28293. X509* x509 = NULL;
  28294. X509_NAME* subjectName = NULL;
  28295. unsigned long ret1 = 0;
  28296. unsigned long ret2 = 0;
  28297. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28298. SSL_FILETYPE_PEM));
  28299. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  28300. /* These two
  28301. * - X509_subject_name_hash(x509)
  28302. * - X509_NAME_hash(X509_get_subject_name(x509))
  28303. * should give the same hash, if !defined(NO_SHA) is true. */
  28304. ret1 = X509_subject_name_hash(x509);
  28305. ExpectIntNE(ret1, 0);
  28306. #if !defined(NO_SHA)
  28307. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  28308. ExpectIntNE(ret2, 0);
  28309. ExpectIntEQ(ret1, ret2);
  28310. #else
  28311. (void) ret2;
  28312. #endif
  28313. X509_free(x509);
  28314. #endif
  28315. return EXPECT_RESULT();
  28316. }
  28317. static int test_wolfSSL_X509_issuer_name_hash(void)
  28318. {
  28319. EXPECT_DECLS;
  28320. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28321. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28322. X509* x509 = NULL;
  28323. X509_NAME* issuertName = NULL;
  28324. unsigned long ret1 = 0;
  28325. unsigned long ret2 = 0;
  28326. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28327. SSL_FILETYPE_PEM));
  28328. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  28329. /* These two
  28330. * - X509_issuer_name_hash(x509)
  28331. * - X509_NAME_hash(X509_get_issuer_name(x509))
  28332. * should give the same hash, if !defined(NO_SHA) is true. */
  28333. ret1 = X509_issuer_name_hash(x509);
  28334. ExpectIntNE(ret1, 0);
  28335. #if !defined(NO_SHA)
  28336. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  28337. ExpectIntNE(ret2, 0);
  28338. ExpectIntEQ(ret1, ret2);
  28339. #else
  28340. (void) ret2;
  28341. #endif
  28342. X509_free(x509);
  28343. #endif
  28344. return EXPECT_RESULT();
  28345. }
  28346. static int test_wolfSSL_X509_check_host(void)
  28347. {
  28348. EXPECT_DECLS;
  28349. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28350. && !defined(NO_SHA) && !defined(NO_RSA)
  28351. X509* x509 = NULL;
  28352. const char altName[] = "example.com";
  28353. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28354. SSL_FILETYPE_PEM));
  28355. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  28356. WOLFSSL_SUCCESS);
  28357. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  28358. WOLFSSL_FAILURE);
  28359. X509_free(x509);
  28360. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  28361. WOLFSSL_FAILURE);
  28362. #endif
  28363. return EXPECT_RESULT();
  28364. }
  28365. static int test_wolfSSL_X509_check_email(void)
  28366. {
  28367. EXPECT_DECLS;
  28368. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  28369. X509* x509 = NULL;
  28370. const char goodEmail[] = "info@wolfssl.com";
  28371. const char badEmail[] = "disinfo@wolfssl.com";
  28372. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28373. SSL_FILETYPE_PEM));
  28374. /* Should fail on non-matching email address */
  28375. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  28376. WOLFSSL_FAILURE);
  28377. /* Should succeed on matching email address */
  28378. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  28379. WOLFSSL_SUCCESS);
  28380. /* Should compute length internally when not provided */
  28381. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  28382. WOLFSSL_SUCCESS);
  28383. /* Should fail when email address is NULL */
  28384. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  28385. WOLFSSL_FAILURE);
  28386. X509_free(x509);
  28387. /* Should fail when x509 is NULL */
  28388. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  28389. WOLFSSL_FAILURE);
  28390. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  28391. return EXPECT_RESULT();
  28392. }
  28393. static int test_wc_PemToDer(void)
  28394. {
  28395. EXPECT_DECLS;
  28396. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28397. int ret;
  28398. DerBuffer* pDer = NULL;
  28399. const char* ca_cert = "./certs/server-cert.pem";
  28400. byte* cert_buf = NULL;
  28401. size_t cert_sz = 0;
  28402. int eccKey = 0;
  28403. EncryptedInfo info;
  28404. XMEMSET(&info, 0, sizeof(info));
  28405. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28406. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  28407. &info, &eccKey), 0);
  28408. wc_FreeDer(&pDer);
  28409. pDer = NULL;
  28410. if (cert_buf != NULL) {
  28411. free(cert_buf);
  28412. cert_buf = NULL;
  28413. }
  28414. #ifdef HAVE_ECC
  28415. {
  28416. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  28417. byte key_buf[256] = {0};
  28418. /* Test fail of loading a key with cert type */
  28419. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  28420. key_buf[0] = '\n';
  28421. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  28422. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  28423. &pDer, NULL, &info, &eccKey)), 0);
  28424. #ifdef OPENSSL_EXTRA
  28425. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  28426. &pDer, NULL, &info, &eccKey)), 0);
  28427. #endif
  28428. wc_FreeDer(&pDer);
  28429. if (cert_buf != NULL)
  28430. free(cert_buf);
  28431. }
  28432. #endif
  28433. #endif
  28434. return EXPECT_RESULT();
  28435. }
  28436. static int test_wc_AllocDer(void)
  28437. {
  28438. EXPECT_DECLS;
  28439. #if !defined(NO_CERTS)
  28440. DerBuffer* pDer = NULL;
  28441. word32 testSize = 1024;
  28442. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  28443. BAD_FUNC_ARG);
  28444. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  28445. ExpectNotNull(pDer);
  28446. wc_FreeDer(&pDer);
  28447. #endif
  28448. return EXPECT_RESULT();
  28449. }
  28450. static int test_wc_CertPemToDer(void)
  28451. {
  28452. EXPECT_DECLS;
  28453. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28454. const char* ca_cert = "./certs/ca-cert.pem";
  28455. byte* cert_buf = NULL;
  28456. size_t cert_sz = 0;
  28457. size_t cert_dersz = 0;
  28458. byte* cert_der = NULL;
  28459. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28460. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28461. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28462. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28463. (int)cert_dersz, CERT_TYPE), 0);
  28464. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  28465. BAD_FUNC_ARG);
  28466. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  28467. BAD_FUNC_ARG);
  28468. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  28469. BAD_FUNC_ARG);
  28470. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  28471. CERT_TYPE), BAD_FUNC_ARG);
  28472. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  28473. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28474. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  28475. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28476. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  28477. CERT_TYPE), BAD_FUNC_ARG);
  28478. if (cert_der != NULL)
  28479. free(cert_der);
  28480. if (cert_buf != NULL)
  28481. free(cert_buf);
  28482. #endif
  28483. return EXPECT_RESULT();
  28484. }
  28485. static int test_wc_KeyPemToDer(void)
  28486. {
  28487. EXPECT_DECLS;
  28488. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28489. int ret;
  28490. const byte cert_buf[] = \
  28491. "-----BEGIN PRIVATE KEY-----\n"
  28492. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  28493. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  28494. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  28495. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  28496. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  28497. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  28498. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  28499. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  28500. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  28501. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  28502. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  28503. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  28504. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  28505. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  28506. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  28507. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  28508. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  28509. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  28510. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  28511. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  28512. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  28513. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  28514. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  28515. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  28516. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  28517. "AQue5o6oVfhgLiJzMdo/77gw\n"
  28518. "-----END PRIVATE KEY-----\n";
  28519. const int cert_sz = sizeof(cert_buf);
  28520. const char cert_pw[] = "password";
  28521. int cert_dersz = 0;
  28522. byte* cert_der = NULL;
  28523. /* Bad arg: Cert buffer is NULL */
  28524. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  28525. BAD_FUNC_ARG);
  28526. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  28527. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  28528. BAD_FUNC_ARG);
  28529. /* Test normal operation */
  28530. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28531. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28532. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28533. cert_pw), 0);
  28534. ExpectIntLE(ret, cert_sz);
  28535. if (cert_der != NULL) {
  28536. free(cert_der);
  28537. cert_der = NULL;
  28538. }
  28539. /* Test NULL for DER buffer to return needed DER buffer size */
  28540. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  28541. ExpectIntLE(ret, cert_sz);
  28542. if (EXPECT_SUCCESS())
  28543. cert_dersz = ret;
  28544. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28545. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28546. cert_pw), 0);
  28547. ExpectIntLE(ret, cert_sz);
  28548. if (cert_der != NULL)
  28549. free(cert_der);
  28550. #endif
  28551. return EXPECT_RESULT();
  28552. }
  28553. static int test_wc_PubKeyPemToDer(void)
  28554. {
  28555. EXPECT_DECLS;
  28556. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  28557. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28558. int ret = 0;
  28559. const char* key = "./certs/ecc-client-keyPub.pem";
  28560. byte* cert_buf = NULL;
  28561. size_t cert_sz = 0, cert_dersz = 0;
  28562. byte* cert_der = NULL;
  28563. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  28564. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  28565. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  28566. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28567. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28568. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28569. (int)cert_dersz), 0);
  28570. if (cert_der != NULL) {
  28571. free(cert_der);
  28572. cert_der = NULL;
  28573. }
  28574. /* Test NULL for DER buffer to return needed DER buffer size */
  28575. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  28576. ExpectIntLE(ret, cert_sz);
  28577. cert_dersz = ret;
  28578. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28579. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28580. (int)cert_dersz), 0);
  28581. if (cert_der != NULL) {
  28582. free(cert_der);
  28583. }
  28584. if (cert_buf != NULL) {
  28585. free(cert_buf);
  28586. }
  28587. #endif
  28588. return EXPECT_RESULT();
  28589. }
  28590. static int test_wc_PemPubKeyToDer(void)
  28591. {
  28592. EXPECT_DECLS;
  28593. #if !defined(NO_FILESYSTEM) && \
  28594. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28595. const char* key = "./certs/ecc-client-keyPub.pem";
  28596. size_t cert_dersz = 1024;
  28597. byte* cert_der = NULL;
  28598. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  28599. BAD_FUNC_ARG);
  28600. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28601. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  28602. if (cert_der != NULL) {
  28603. free(cert_der);
  28604. }
  28605. #endif
  28606. return EXPECT_RESULT();
  28607. }
  28608. static int test_wc_GetPubKeyDerFromCert(void)
  28609. {
  28610. EXPECT_DECLS;
  28611. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28612. int ret;
  28613. word32 idx = 0;
  28614. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28615. word32 keyDerSz = (word32)sizeof(keyDer);
  28616. DecodedCert decoded;
  28617. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28618. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  28619. word32 certBufSz = sizeof(certBuf);
  28620. #endif
  28621. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  28622. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28623. XFILE fp = XBADFILE;
  28624. #endif
  28625. #ifndef NO_RSA
  28626. RsaKey rsaKey;
  28627. #if defined(USE_CERT_BUFFERS_2048)
  28628. byte* rsaCertDer = (byte*)client_cert_der_2048;
  28629. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  28630. #elif defined(USE_CERT_BUFFERS_1024)
  28631. byte* rsaCertDer = (byte*)client_cert_der_1024;
  28632. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  28633. #else
  28634. unsigned char rsaCertDer[TWOK_BUF];
  28635. word32 rsaCertDerSz;
  28636. #endif
  28637. #endif
  28638. #ifdef HAVE_ECC
  28639. ecc_key eccKey;
  28640. #if defined(USE_CERT_BUFFERS_256)
  28641. byte* eccCert = (byte*)cliecc_cert_der_256;
  28642. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  28643. #else
  28644. unsigned char eccCert[ONEK_BUF];
  28645. word32 eccCertSz;
  28646. XFILE fp2 = XBADFILE;
  28647. #endif
  28648. #endif
  28649. #ifndef NO_RSA
  28650. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  28651. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  28652. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  28653. fp), 0);
  28654. if (fp != XBADFILE) {
  28655. XFCLOSE(fp);
  28656. fp = XBADFILE;
  28657. }
  28658. #endif
  28659. /* good test case - RSA DER cert */
  28660. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  28661. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28662. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28663. ExpectIntGT(keyDerSz, 0);
  28664. /* sanity check, verify we can import DER public key */
  28665. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28666. ExpectIntEQ(ret, 0);
  28667. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28668. if (ret == 0) {
  28669. wc_FreeRsaKey(&rsaKey);
  28670. }
  28671. /* test LENGTH_ONLY_E case */
  28672. keyDerSz = 0;
  28673. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28674. LENGTH_ONLY_E);
  28675. ExpectIntGT(keyDerSz, 0);
  28676. /* bad args: DecodedCert NULL */
  28677. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  28678. /* bad args: output key buff size */
  28679. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  28680. /* bad args: zero size output key buffer */
  28681. keyDerSz = 0;
  28682. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28683. BAD_FUNC_ARG);
  28684. wc_FreeDecodedCert(&decoded);
  28685. /* Certificate Request Tests */
  28686. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28687. {
  28688. XMEMSET(certBuf, 0, sizeof(certBuf));
  28689. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  28690. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  28691. if (fp != XBADFILE) {
  28692. XFCLOSE(fp);
  28693. }
  28694. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  28695. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  28696. /* good test case - RSA DER certificate request */
  28697. keyDerSz = sizeof(keyDer);
  28698. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28699. 0);
  28700. ExpectIntGT(keyDerSz, 0);
  28701. /* sanity check, verify we can import DER public key */
  28702. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28703. ExpectIntEQ(ret, 0);
  28704. idx = 0;
  28705. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28706. if (ret == 0) {
  28707. wc_FreeRsaKey(&rsaKey);
  28708. }
  28709. wc_FreeDecodedCert(&decoded);
  28710. }
  28711. #endif /* WOLFSSL_CERT_REQ */
  28712. #endif /* NO_RSA */
  28713. #ifdef HAVE_ECC
  28714. #ifndef USE_CERT_BUFFERS_256
  28715. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  28716. XBADFILE);
  28717. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  28718. if (fp2 != XBADFILE) {
  28719. XFCLOSE(fp2);
  28720. }
  28721. #endif
  28722. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  28723. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28724. /* good test case - ECC */
  28725. XMEMSET(keyDer, 0, sizeof(keyDer));
  28726. keyDerSz = sizeof(keyDer);
  28727. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28728. ExpectIntGT(keyDerSz, 0);
  28729. /* sanity check, verify we can import DER public key */
  28730. ret = wc_ecc_init(&eccKey);
  28731. ExpectIntEQ(ret, 0);
  28732. idx = 0; /* reset idx to 0, used above in RSA case */
  28733. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  28734. if (ret == 0) {
  28735. wc_ecc_free(&eccKey);
  28736. }
  28737. /* test LENGTH_ONLY_E case */
  28738. keyDerSz = 0;
  28739. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28740. LENGTH_ONLY_E);
  28741. ExpectIntGT(keyDerSz, 0);
  28742. wc_FreeDecodedCert(&decoded);
  28743. #endif
  28744. #endif /* !NO_RSA || HAVE_ECC */
  28745. return EXPECT_RESULT();
  28746. }
  28747. static int test_wc_CheckCertSigPubKey(void)
  28748. {
  28749. EXPECT_DECLS;
  28750. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28751. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  28752. int ret;
  28753. const char* ca_cert = "./certs/ca-cert.pem";
  28754. byte* cert_buf = NULL;
  28755. size_t cert_sz = 0;
  28756. byte* cert_der = NULL;
  28757. word32 cert_dersz = 0;
  28758. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28759. word32 keyDerSz = (word32)sizeof(keyDer);
  28760. DecodedCert decoded;
  28761. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28762. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  28763. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28764. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28765. (int)cert_dersz, CERT_TYPE), 0);
  28766. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  28767. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28768. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28769. ExpectIntGT(keyDerSz, 0);
  28770. /* Good test case. */
  28771. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28772. keyDerSz, RSAk), 0);
  28773. /* No certificate. */
  28774. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  28775. ECDSAk), BAD_FUNC_ARG);
  28776. /* Bad cert size. */
  28777. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  28778. RSAk), 0);
  28779. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  28780. /* No public key. */
  28781. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  28782. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  28783. /* Bad public key size. */
  28784. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  28785. RSAk), BAD_FUNC_ARG);
  28786. /* Wrong aglo. */
  28787. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28788. keyDerSz, ECDSAk), ASN_PARSE_E);
  28789. wc_FreeDecodedCert(&decoded);
  28790. if (cert_der != NULL)
  28791. free(cert_der);
  28792. if (cert_buf != NULL)
  28793. free(cert_buf);
  28794. #endif
  28795. return EXPECT_RESULT();
  28796. }
  28797. static int test_wolfSSL_certs(void)
  28798. {
  28799. EXPECT_DECLS;
  28800. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28801. !defined(NO_RSA)
  28802. X509* x509ext = NULL;
  28803. #ifdef OPENSSL_ALL
  28804. X509* x509 = NULL;
  28805. WOLFSSL_X509_EXTENSION* ext = NULL;
  28806. ASN1_OBJECT* obj = NULL;
  28807. #endif
  28808. WOLFSSL* ssl = NULL;
  28809. WOLFSSL_CTX* ctx = NULL;
  28810. STACK_OF(ASN1_OBJECT)* sk = NULL;
  28811. ASN1_STRING* asn1_str = NULL;
  28812. AUTHORITY_KEYID* akey = NULL;
  28813. BASIC_CONSTRAINTS* bc = NULL;
  28814. int crit;
  28815. #ifndef NO_WOLFSSL_SERVER
  28816. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  28817. #else
  28818. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  28819. #endif
  28820. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28821. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28822. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  28823. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28824. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  28825. #endif
  28826. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28827. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28828. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  28829. #endif
  28830. ExpectNotNull(ssl = SSL_new(ctx));
  28831. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28832. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28833. #endif
  28834. #ifdef HAVE_PK_CALLBACKS
  28835. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  28836. #endif /* HAVE_PK_CALLBACKS */
  28837. /* create and use x509 */
  28838. #ifdef OPENSSL_ALL
  28839. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28840. WOLFSSL_FILETYPE_PEM));
  28841. #endif
  28842. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  28843. WOLFSSL_FILETYPE_PEM));
  28844. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  28845. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28846. /* with loading in a new cert the check on private key should now fail */
  28847. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28848. #endif
  28849. #if defined(USE_CERT_BUFFERS_2048)
  28850. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  28851. (unsigned char*)server_cert_der_2048,
  28852. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  28853. #endif
  28854. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28855. /************* Get Digest of Certificate ******************/
  28856. {
  28857. byte digest[64]; /* max digest size */
  28858. word32 digestSz;
  28859. XMEMSET(digest, 0, sizeof(digest));
  28860. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  28861. WOLFSSL_SUCCESS);
  28862. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  28863. WOLFSSL_SUCCESS);
  28864. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  28865. WOLFSSL_FAILURE);
  28866. }
  28867. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  28868. /* test and checkout X509 extensions */
  28869. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  28870. NID_basic_constraints, &crit, NULL));
  28871. ExpectIntEQ(crit, 0);
  28872. #ifdef OPENSSL_ALL
  28873. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  28874. X509_EXTENSION_free(ext);
  28875. ext = NULL;
  28876. ExpectNotNull(ext = X509_EXTENSION_new());
  28877. X509_EXTENSION_set_critical(ext, 1);
  28878. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  28879. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  28880. ASN1_OBJECT_free(obj);
  28881. obj = NULL;
  28882. X509_EXTENSION_free(ext);
  28883. ext = NULL;
  28884. ExpectNotNull(ext = X509_EXTENSION_new());
  28885. X509_EXTENSION_set_critical(ext, 0);
  28886. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  28887. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  28888. NULL);
  28889. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  28890. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  28891. * and X509_get_ext_d2i has created new */
  28892. asn1_str = NULL;
  28893. X509_EXTENSION_free(ext);
  28894. ext = NULL;
  28895. #endif
  28896. BASIC_CONSTRAINTS_free(bc);
  28897. bc = NULL;
  28898. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  28899. NID_key_usage, &crit, NULL));
  28900. ExpectIntEQ(crit, 1);
  28901. ExpectIntEQ(asn1_str->type, NID_key_usage);
  28902. #ifdef OPENSSL_ALL
  28903. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  28904. X509_EXTENSION_free(ext);
  28905. ext = NULL;
  28906. #endif
  28907. ASN1_STRING_free(asn1_str);
  28908. asn1_str = NULL;
  28909. #ifdef OPENSSL_ALL
  28910. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  28911. NID_ext_key_usage, &crit, NULL));
  28912. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  28913. X509_EXTENSION_free(ext);
  28914. ext = NULL;
  28915. EXTENDED_KEY_USAGE_free(sk);
  28916. sk = NULL;
  28917. #else
  28918. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  28919. &crit, NULL);
  28920. ExpectNull(sk);
  28921. #endif
  28922. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  28923. NID_authority_key_identifier, &crit, NULL));
  28924. #ifdef OPENSSL_ALL
  28925. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  28926. akey));
  28927. X509_EXTENSION_free(ext);
  28928. ext = NULL;
  28929. #endif
  28930. wolfSSL_AUTHORITY_KEYID_free(akey);
  28931. akey = NULL;
  28932. /* NID not yet supported */
  28933. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28934. NID_private_key_usage_period, &crit, NULL));
  28935. ExpectIntEQ(crit, -1);
  28936. sk_ASN1_OBJECT_free(sk);
  28937. sk = NULL;
  28938. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  28939. NID_subject_alt_name, &crit, NULL));
  28940. {
  28941. int i;
  28942. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  28943. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  28944. ExpectIntEQ(gen->type, GEN_DNS);
  28945. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  28946. }
  28947. }
  28948. sk_GENERAL_NAME_free(sk);
  28949. sk = NULL;
  28950. /* NID not yet supported */
  28951. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28952. NID_issuer_alt_name, &crit, NULL));
  28953. ExpectIntEQ(crit, -1);
  28954. sk_ASN1_OBJECT_free(sk);
  28955. sk = NULL;
  28956. /* NID not yet supported */
  28957. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28958. NID_info_access, &crit, NULL));
  28959. sk_ASN1_OBJECT_free(sk);
  28960. sk = NULL;
  28961. /* NID not yet supported */
  28962. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28963. NID_sinfo_access, &crit, NULL));
  28964. ExpectIntEQ(crit, -1);
  28965. sk_ASN1_OBJECT_free(sk);
  28966. sk = NULL;
  28967. /* NID not yet supported */
  28968. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28969. NID_name_constraints, &crit, NULL));
  28970. ExpectIntEQ(crit, -1);
  28971. sk_ASN1_OBJECT_free(sk);
  28972. sk = NULL;
  28973. /* no cert policy set */
  28974. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28975. NID_certificate_policies, &crit, NULL));
  28976. sk_ASN1_OBJECT_free(sk);
  28977. sk = NULL;
  28978. /* NID not yet supported */
  28979. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28980. NID_policy_mappings, &crit, NULL));
  28981. ExpectIntEQ(crit, -1);
  28982. sk_ASN1_OBJECT_free(sk);
  28983. sk = NULL;
  28984. /* NID not yet supported */
  28985. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28986. NID_policy_constraints, &crit, NULL));
  28987. ExpectIntEQ(crit, -1);
  28988. sk_ASN1_OBJECT_free(sk);
  28989. sk = NULL;
  28990. /* NID not yet supported */
  28991. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28992. NID_inhibit_any_policy, &crit, NULL));
  28993. ExpectIntEQ(crit, -1);
  28994. sk_ASN1_OBJECT_free(sk);
  28995. sk = NULL;
  28996. /* NID not yet supported */
  28997. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28998. NID_tlsfeature, &crit, NULL));
  28999. ExpectIntEQ(crit, -1);
  29000. sk_ASN1_OBJECT_free(sk);
  29001. sk = NULL;
  29002. /* test invalid cases */
  29003. crit = 0;
  29004. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  29005. NULL));
  29006. ExpectIntEQ(crit, -1);
  29007. /* NULL passed for criticality. */
  29008. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  29009. NID_tlsfeature, NULL, NULL));
  29010. ExpectIntEQ(SSL_get_hit(ssl), 0);
  29011. #ifdef OPENSSL_ALL
  29012. X509_free(x509);
  29013. #endif
  29014. X509_free(x509ext);
  29015. SSL_free(ssl);
  29016. SSL_CTX_free(ctx);
  29017. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  29018. return EXPECT_RESULT();
  29019. }
  29020. static int test_wolfSSL_X509_check_private_key(void)
  29021. {
  29022. EXPECT_DECLS;
  29023. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29024. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  29025. X509* x509 = NULL;
  29026. EVP_PKEY* pkey = NULL;
  29027. const byte* key;
  29028. /* Check with correct key */
  29029. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  29030. SSL_FILETYPE_PEM)));
  29031. key = client_key_der_2048;
  29032. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29033. (long)sizeof_client_key_der_2048));
  29034. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  29035. EVP_PKEY_free(pkey);
  29036. pkey = NULL;
  29037. /* Check with wrong key */
  29038. key = server_key_der_2048;
  29039. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29040. (long)sizeof_server_key_der_2048));
  29041. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  29042. /* test for incorrect parameter */
  29043. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  29044. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  29045. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  29046. EVP_PKEY_free(pkey);
  29047. X509_free(x509);
  29048. #endif
  29049. return EXPECT_RESULT();
  29050. }
  29051. static int test_wolfSSL_private_keys(void)
  29052. {
  29053. EXPECT_DECLS;
  29054. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29055. !defined(NO_FILESYSTEM)
  29056. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29057. WOLFSSL* ssl = NULL;
  29058. WOLFSSL_CTX* ctx = NULL;
  29059. EVP_PKEY* pkey = NULL;
  29060. OpenSSL_add_all_digests();
  29061. OpenSSL_add_all_algorithms();
  29062. #ifndef NO_RSA
  29063. #ifndef NO_WOLFSSL_SERVER
  29064. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29065. #else
  29066. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29067. #endif
  29068. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  29069. /* Have to load a cert before you can check the private key against that
  29070. * certificates public key! */
  29071. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29072. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  29073. #endif
  29074. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  29075. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29076. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29077. #endif
  29078. ExpectNotNull(ssl = SSL_new(ctx));
  29079. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29080. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29081. #endif
  29082. #ifdef USE_CERT_BUFFERS_2048
  29083. {
  29084. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29085. unsigned char buf[FOURK_BUF];
  29086. word32 bufSz;
  29087. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  29088. (unsigned char*)client_key_der_2048,
  29089. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29090. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29091. /* Should mismatch now that a different private key loaded */
  29092. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29093. #endif
  29094. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  29095. (unsigned char*)server_key,
  29096. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29097. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29098. /* After loading back in DER format of original key, should match */
  29099. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29100. #endif
  29101. /* test loading private key to the WOLFSSL_CTX */
  29102. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29103. (unsigned char*)client_key_der_2048,
  29104. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29105. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29106. /* Should mismatch now that a different private key loaded */
  29107. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29108. #endif
  29109. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29110. (unsigned char*)server_key,
  29111. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29112. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29113. /* After loading back in DER format of original key, should match */
  29114. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29115. #endif
  29116. /* pkey not set yet, expecting to fail */
  29117. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  29118. /* set PKEY and test again */
  29119. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29120. &server_key, (long)sizeof_server_key_der_2048));
  29121. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29122. /* reuse PKEY structure and test
  29123. * this should be checked with a memory management sanity checker */
  29124. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  29125. server_key = (const unsigned char*)server_key_der_2048;
  29126. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29127. &server_key, (long)sizeof_server_key_der_2048));
  29128. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29129. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  29130. bufSz = FOURK_BUF;
  29131. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  29132. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  29133. RSAk, NULL, 0)), 0);
  29134. server_key = (const unsigned char*)buf;
  29135. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  29136. (long)bufSz));
  29137. }
  29138. #endif
  29139. EVP_PKEY_free(pkey);
  29140. pkey = NULL;
  29141. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  29142. ssl = NULL;
  29143. SSL_CTX_free(ctx);
  29144. ctx = NULL;
  29145. #endif /* end of RSA private key match tests */
  29146. #ifdef HAVE_ECC
  29147. #ifndef NO_WOLFSSL_SERVER
  29148. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29149. #else
  29150. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29151. #endif
  29152. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  29153. WOLFSSL_FILETYPE_PEM));
  29154. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  29155. WOLFSSL_FILETYPE_PEM));
  29156. ExpectNotNull(ssl = SSL_new(ctx));
  29157. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29158. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29159. #endif
  29160. SSL_free(ssl);
  29161. ssl = NULL;
  29162. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  29163. WOLFSSL_FILETYPE_PEM));
  29164. ExpectNotNull(ssl = SSL_new(ctx));
  29165. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  29166. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29167. #endif
  29168. SSL_free(ssl);
  29169. ssl = NULL;
  29170. SSL_CTX_free(ctx);
  29171. ctx = NULL;
  29172. #endif /* end of ECC private key match tests */
  29173. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  29174. #ifndef NO_WOLFSSL_SERVER
  29175. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29176. #else
  29177. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29178. #endif
  29179. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  29180. WOLFSSL_FILETYPE_PEM));
  29181. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  29182. WOLFSSL_FILETYPE_PEM));
  29183. ExpectNotNull(ssl = SSL_new(ctx));
  29184. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29185. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29186. #endif
  29187. SSL_free(ssl);
  29188. ssl = NULL;
  29189. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  29190. WOLFSSL_FILETYPE_PEM));
  29191. ExpectNotNull(ssl = SSL_new(ctx));
  29192. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29193. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29194. #endif
  29195. SSL_free(ssl);
  29196. ssl = NULL;
  29197. SSL_CTX_free(ctx);
  29198. ctx = NULL;
  29199. #endif /* end of Ed25519 private key match tests */
  29200. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  29201. #ifndef NO_WOLFSSL_SERVER
  29202. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29203. #else
  29204. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29205. #endif
  29206. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  29207. WOLFSSL_FILETYPE_PEM));
  29208. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  29209. WOLFSSL_FILETYPE_PEM));
  29210. ExpectNotNull(ssl = SSL_new(ctx));
  29211. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29212. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29213. #endif
  29214. SSL_free(ssl);
  29215. ssl = NULL;
  29216. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  29217. WOLFSSL_FILETYPE_PEM));
  29218. ExpectNotNull(ssl = SSL_new(ctx));
  29219. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29220. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29221. #endif
  29222. SSL_free(ssl);
  29223. ssl = NULL;
  29224. SSL_CTX_free(ctx);
  29225. ctx = NULL;
  29226. #endif /* end of Ed448 private key match tests */
  29227. EVP_cleanup();
  29228. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  29229. CONF_modules_free();
  29230. ENGINE_cleanup();
  29231. CONF_modules_unload();
  29232. (void)ssl;
  29233. (void)ctx;
  29234. (void)pkey;
  29235. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29236. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29237. return EXPECT_RESULT();
  29238. }
  29239. static int test_wolfSSL_PEM_read_PrivateKey(void)
  29240. {
  29241. EXPECT_DECLS;
  29242. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29243. && !defined(NO_FILESYSTEM)
  29244. XFILE file = XBADFILE;
  29245. const char* fname = "./certs/server-key.pem";
  29246. EVP_PKEY* pkey = NULL;
  29247. RSA* rsa = NULL;
  29248. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  29249. unsigned char* sig = NULL;
  29250. size_t sigLen;
  29251. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  29252. size_t tbsLen = sizeof(tbs);
  29253. /* Check error case. */
  29254. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  29255. /* Read in an RSA key. */
  29256. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29257. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  29258. if (file != XBADFILE)
  29259. XFCLOSE(file);
  29260. /* Make sure the key is usable by signing some data with it. */
  29261. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  29262. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  29263. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  29264. DYNAMIC_TYPE_TMP_BUFFER));
  29265. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  29266. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  29267. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  29268. WOLFSSL_SUCCESS);
  29269. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29270. EVP_PKEY_CTX_free(ctx);
  29271. EVP_PKEY_free(pkey);
  29272. #endif
  29273. return EXPECT_RESULT();
  29274. }
  29275. static int test_wolfSSL_PEM_read_PUBKEY(void)
  29276. {
  29277. EXPECT_DECLS;
  29278. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29279. && !defined(NO_FILESYSTEM)
  29280. XFILE file = XBADFILE;
  29281. const char* fname = "./certs/client-keyPub.pem";
  29282. EVP_PKEY* pkey = NULL;
  29283. /* Check error case. */
  29284. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  29285. /* Read in an RSA key. */
  29286. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29287. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  29288. EVP_PKEY_free(pkey);
  29289. if (file != XBADFILE)
  29290. XFCLOSE(file);
  29291. #endif
  29292. return EXPECT_RESULT();
  29293. }
  29294. static int test_wolfSSL_PEM_PrivateKey(void)
  29295. {
  29296. EXPECT_DECLS;
  29297. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29298. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  29299. #ifndef NO_BIO
  29300. BIO* bio = NULL;
  29301. #endif
  29302. EVP_PKEY* pkey = NULL;
  29303. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29304. #ifndef NO_BIO
  29305. /* test creating new EVP_PKEY with bad arg */
  29306. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  29307. /* test loading RSA key using BIO */
  29308. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29309. {
  29310. XFILE file = XBADFILE;
  29311. const char* fname = "./certs/server-key.pem";
  29312. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  29313. size_t sz;
  29314. byte* buf = NULL;
  29315. EVP_PKEY* pkey2 = NULL;
  29316. EVP_PKEY* pkey3 = NULL;
  29317. RSA* rsa_key = NULL;
  29318. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29319. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29320. ExpectIntGT(sz = XFTELL(file), 0);
  29321. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29322. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29323. if (buf != NULL) {
  29324. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29325. }
  29326. if (file != XBADFILE) {
  29327. XFCLOSE(file);
  29328. file = XBADFILE;
  29329. }
  29330. /* Test using BIO new mem and loading PEM private key */
  29331. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29332. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29333. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29334. buf = NULL;
  29335. BIO_free(bio);
  29336. bio = NULL;
  29337. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29338. if (pkey2 != NULL) {
  29339. pkey2->type = EVP_PKEY_RSA;
  29340. }
  29341. /* Test parameter copy */
  29342. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  29343. EVP_PKEY_free(pkey2);
  29344. EVP_PKEY_free(pkey);
  29345. pkey = NULL;
  29346. /* Qt unit test case : rsa pkcs8 key */
  29347. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  29348. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29349. ExpectIntGT(sz = XFTELL(file), 0);
  29350. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29351. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29352. if (buf) {
  29353. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29354. }
  29355. if (file != XBADFILE) {
  29356. XFCLOSE(file);
  29357. file = XBADFILE;
  29358. }
  29359. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29360. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29361. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29362. buf = NULL;
  29363. BIO_free(bio);
  29364. bio = NULL;
  29365. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29366. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  29367. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  29368. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29369. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29370. #else
  29371. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29372. #endif
  29373. RSA_free(rsa_key);
  29374. EVP_PKEY_free(pkey3);
  29375. EVP_PKEY_free(pkey);
  29376. pkey = NULL;
  29377. }
  29378. #endif
  29379. /* test loading ECC key using BIO */
  29380. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29381. {
  29382. XFILE file = XBADFILE;
  29383. const char* fname = "./certs/ecc-key.pem";
  29384. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  29385. size_t sz = 0;
  29386. byte* buf = NULL;
  29387. EVP_PKEY* pkey2 = NULL;
  29388. EVP_PKEY* pkey3 = NULL;
  29389. EC_KEY* ec_key = NULL;
  29390. int nid = 0;
  29391. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29392. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29393. ExpectIntGT(sz = XFTELL(file), 0);
  29394. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29395. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29396. if (buf) {
  29397. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29398. }
  29399. if (file != XBADFILE) {
  29400. XFCLOSE(file);
  29401. file = XBADFILE;
  29402. }
  29403. /* Test using BIO new mem and loading PEM private key */
  29404. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29405. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29406. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29407. buf = NULL;
  29408. BIO_free(bio);
  29409. bio = NULL;
  29410. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29411. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29412. if (pkey2 != NULL) {
  29413. pkey2->type = EVP_PKEY_EC;
  29414. }
  29415. /* Test parameter copy */
  29416. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  29417. /* Qt unit test case 1*/
  29418. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29419. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29420. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29421. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29422. #else
  29423. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29424. #endif
  29425. /* Test default digest */
  29426. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  29427. ExpectIntEQ(nid, NID_sha256);
  29428. EC_KEY_free(ec_key);
  29429. ec_key = NULL;
  29430. EVP_PKEY_free(pkey3);
  29431. pkey3 = NULL;
  29432. EVP_PKEY_free(pkey2);
  29433. pkey2 = NULL;
  29434. EVP_PKEY_free(pkey);
  29435. pkey = NULL;
  29436. /* Qt unit test case ec pkcs8 key */
  29437. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  29438. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29439. ExpectIntGT(sz = XFTELL(file), 0);
  29440. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29441. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29442. if (buf) {
  29443. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29444. }
  29445. if (file != XBADFILE) {
  29446. XFCLOSE(file);
  29447. file = XBADFILE;
  29448. }
  29449. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29450. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29451. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29452. buf = NULL;
  29453. BIO_free(bio);
  29454. bio = NULL;
  29455. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29456. /* Qt unit test case */
  29457. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29458. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29459. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29460. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29461. #else
  29462. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29463. #endif
  29464. EC_KEY_free(ec_key);
  29465. EVP_PKEY_free(pkey3);
  29466. EVP_PKEY_free(pkey);
  29467. pkey = NULL;
  29468. }
  29469. #endif
  29470. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  29471. defined(WOLFSSL_CERT_GEN))
  29472. {
  29473. #define BIO_PEM_TEST_CHAR 'a'
  29474. EVP_PKEY* pkey2 = NULL;
  29475. unsigned char extra[10];
  29476. int i;
  29477. BIO* pub_bio = NULL;
  29478. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  29479. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29480. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  29481. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29482. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  29483. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  29484. &server_key, (long)sizeof_server_key_der_2048));
  29485. ExpectNull(pkey);
  29486. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29487. &server_key, (long)sizeof_server_key_der_2048));
  29488. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  29489. NULL), WOLFSSL_FAILURE);
  29490. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29491. NULL), WOLFSSL_FAILURE);
  29492. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29493. NULL), WOLFSSL_SUCCESS);
  29494. ExpectIntGT(BIO_pending(bio), 0);
  29495. ExpectIntEQ(BIO_pending(bio), 1679);
  29496. /* Check if the pubkey API writes only the public key */
  29497. #ifdef WOLFSSL_KEY_GEN
  29498. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  29499. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  29500. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  29501. ExpectIntGT(BIO_pending(pub_bio), 0);
  29502. /* Previously both the private key and the pubkey calls would write
  29503. * out the private key and the PEM header was the only difference.
  29504. * The public PEM should be significantly shorter than the
  29505. * private key versison. */
  29506. ExpectIntEQ(BIO_pending(pub_bio), 451);
  29507. #endif
  29508. /* test creating new EVP_PKEY with good args */
  29509. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29510. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  29511. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  29512. /* test of reuse of EVP_PKEY */
  29513. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29514. ExpectIntEQ(BIO_pending(bio), 0);
  29515. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  29516. SSL_SUCCESS);
  29517. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  29518. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29519. ExpectNotNull(pkey);
  29520. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  29521. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  29522. }
  29523. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  29524. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  29525. for (i = 0; i < 10; i++) {
  29526. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  29527. }
  29528. BIO_free(pub_bio);
  29529. BIO_free(bio);
  29530. bio = NULL;
  29531. EVP_PKEY_free(pkey);
  29532. pkey = NULL;
  29533. EVP_PKEY_free(pkey2);
  29534. }
  29535. #endif
  29536. /* key is DES encrypted */
  29537. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  29538. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  29539. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  29540. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  29541. {
  29542. XFILE f = XBADFILE;
  29543. wc_pem_password_cb* passwd_cb = NULL;
  29544. void* passwd_cb_userdata;
  29545. SSL_CTX* ctx = NULL;
  29546. char passwd[] = "bad password";
  29547. #ifndef WOLFSSL_NO_TLS12
  29548. #ifndef NO_WOLFSSL_SERVER
  29549. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29550. #else
  29551. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29552. #endif
  29553. #else
  29554. #ifndef NO_WOLFSSL_SERVER
  29555. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29556. #else
  29557. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29558. #endif
  29559. #endif
  29560. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29561. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  29562. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  29563. ExpectNull(passwd_cb_userdata =
  29564. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29565. /* fail case with password call back */
  29566. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  29567. (void*)passwd));
  29568. BIO_free(bio);
  29569. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29570. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29571. (void*)passwd));
  29572. BIO_free(bio);
  29573. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  29574. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  29575. if ((bio == NULL) && (f != XBADFILE)) {
  29576. XFCLOSE(f);
  29577. }
  29578. /* use callback that works */
  29579. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29580. (void*)"yassl123"));
  29581. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29582. EVP_PKEY_free(pkey);
  29583. pkey = NULL;
  29584. BIO_free(bio);
  29585. bio = NULL;
  29586. SSL_CTX_free(ctx);
  29587. }
  29588. #endif /* !defined(NO_DES3) */
  29589. #endif /* !NO_BIO */
  29590. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29591. {
  29592. unsigned char buf[2048];
  29593. size_t bytes = 0;
  29594. XFILE f = XBADFILE;
  29595. SSL_CTX* ctx = NULL;
  29596. #ifndef WOLFSSL_NO_TLS12
  29597. #ifndef NO_WOLFSSL_SERVER
  29598. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29599. #else
  29600. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29601. #endif
  29602. #else
  29603. #ifndef NO_WOLFSSL_SERVER
  29604. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29605. #else
  29606. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29607. #endif
  29608. #endif
  29609. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  29610. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  29611. if (f != XBADFILE)
  29612. XFCLOSE(f);
  29613. server_key = buf;
  29614. pkey = NULL;
  29615. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  29616. ExpectNull(pkey);
  29617. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  29618. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29619. EVP_PKEY_free(pkey);
  29620. pkey = NULL;
  29621. SSL_CTX_free(ctx);
  29622. server_key = NULL;
  29623. }
  29624. #endif
  29625. #ifndef NO_BIO
  29626. (void)bio;
  29627. #endif
  29628. (void)pkey;
  29629. (void)server_key;
  29630. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  29631. return EXPECT_RESULT();
  29632. }
  29633. static int test_wolfSSL_PEM_file_RSAKey(void)
  29634. {
  29635. EXPECT_DECLS;
  29636. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29637. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29638. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29639. RSA* rsa = NULL;
  29640. XFILE fp = XBADFILE;
  29641. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  29642. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  29643. if (fp != XBADFILE)
  29644. XFCLOSE(fp);
  29645. ExpectIntEQ(RSA_size(rsa), 256);
  29646. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  29647. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  29648. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  29649. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  29650. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29651. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  29652. RSA_free(rsa);
  29653. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29654. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29655. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29656. return EXPECT_RESULT();
  29657. }
  29658. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  29659. {
  29660. EXPECT_DECLS;
  29661. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  29662. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  29663. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  29664. RSA* rsa = NULL;
  29665. XFILE f = NULL;
  29666. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  29667. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29668. ExpectIntEQ(RSA_size(rsa), 256);
  29669. if (f != XBADFILE) {
  29670. XFCLOSE(f);
  29671. f = XBADFILE;
  29672. }
  29673. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  29674. NULL), WOLFSSL_FAILURE);
  29675. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  29676. NULL), WOLFSSL_FAILURE);
  29677. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  29678. WOLFSSL_SUCCESS);
  29679. RSA_free(rsa);
  29680. #ifdef HAVE_ECC
  29681. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  29682. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29683. if (f != XBADFILE)
  29684. XFCLOSE(f);
  29685. #endif /* HAVE_ECC */
  29686. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29687. return EXPECT_RESULT();
  29688. }
  29689. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  29690. {
  29691. EXPECT_DECLS;
  29692. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29693. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29694. XFILE file = XBADFILE;
  29695. const char* fname = "./certs/client-keyPub.pem";
  29696. RSA *rsa = NULL;
  29697. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  29698. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29699. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  29700. ExpectIntEQ(RSA_size(rsa), 256);
  29701. RSA_free(rsa);
  29702. if (file != XBADFILE)
  29703. XFCLOSE(file);
  29704. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29705. return EXPECT_RESULT();
  29706. }
  29707. #ifndef NO_BIO
  29708. static int test_wolfSSL_PEM_bio_RSAKey(void)
  29709. {
  29710. EXPECT_DECLS;
  29711. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29712. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29713. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29714. RSA* rsa = NULL;
  29715. BIO* bio = NULL;
  29716. /* PrivateKey */
  29717. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29718. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  29719. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  29720. ExpectNotNull(rsa);
  29721. ExpectIntEQ(RSA_size(rsa), 256);
  29722. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  29723. NULL), WOLFSSL_FAILURE);
  29724. BIO_free(bio);
  29725. bio = NULL;
  29726. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29727. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  29728. NULL), WOLFSSL_SUCCESS);
  29729. BIO_free(bio);
  29730. bio = NULL;
  29731. RSA_free(rsa);
  29732. rsa = NULL;
  29733. /* PUBKEY */
  29734. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  29735. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29736. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29737. ExpectIntEQ(RSA_size(rsa), 256);
  29738. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29739. BIO_free(bio);
  29740. bio = NULL;
  29741. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29742. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  29743. BIO_free(bio);
  29744. bio = NULL;
  29745. RSA_free(rsa);
  29746. rsa = NULL;
  29747. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  29748. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  29749. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29750. BIO_free(bio);
  29751. bio = NULL;
  29752. RSA_free(rsa);
  29753. rsa = NULL;
  29754. #ifdef HAVE_ECC
  29755. /* ensure that non-rsa keys do not work */
  29756. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29757. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29758. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29759. BIO_free(bio);
  29760. bio = NULL;
  29761. RSA_free(rsa);
  29762. rsa = NULL;
  29763. #endif /* HAVE_ECC */
  29764. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29765. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29766. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29767. return EXPECT_RESULT();
  29768. }
  29769. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  29770. {
  29771. EXPECT_DECLS;
  29772. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29773. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29774. RSA* rsa = NULL;
  29775. RSA* rsa_dup = NULL;
  29776. BIO* bio = NULL;
  29777. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29778. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29779. ExpectIntEQ(RSA_size(rsa), 256);
  29780. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  29781. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  29782. /* Test duplicating empty key. */
  29783. ExpectNotNull(rsa_dup = RSA_new());
  29784. ExpectNull(RSAPublicKey_dup(rsa_dup));
  29785. RSA_free(rsa_dup);
  29786. rsa_dup = NULL;
  29787. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  29788. ExpectPtrNE(rsa_dup, rsa);
  29789. #endif
  29790. /* test if valgrind complains about unreleased memory */
  29791. RSA_up_ref(rsa);
  29792. RSA_free(rsa);
  29793. BIO_free(bio);
  29794. bio = NULL;
  29795. RSA_free(rsa);
  29796. rsa = NULL;
  29797. RSA_free(rsa_dup);
  29798. rsa_dup = NULL;
  29799. #ifdef HAVE_ECC
  29800. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  29801. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29802. BIO_free(bio);
  29803. #endif /* HAVE_ECC */
  29804. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29805. return EXPECT_RESULT();
  29806. }
  29807. static int test_wolfSSL_PEM_bio_DSAKey(void)
  29808. {
  29809. EXPECT_DECLS;
  29810. #ifndef HAVE_SELFTEST
  29811. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  29812. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  29813. DSA* dsa = NULL;
  29814. BIO* bio = NULL;
  29815. /* PrivateKey */
  29816. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  29817. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  29818. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29819. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29820. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29821. NULL), WOLFSSL_FAILURE);
  29822. BIO_free(bio);
  29823. bio = NULL;
  29824. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29825. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  29826. NULL), WOLFSSL_SUCCESS);
  29827. BIO_free(bio);
  29828. bio = NULL;
  29829. DSA_free(dsa);
  29830. dsa = NULL;
  29831. /* PUBKEY */
  29832. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  29833. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29834. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29835. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29836. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29837. BIO_free(bio);
  29838. bio = NULL;
  29839. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29840. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  29841. BIO_free(bio);
  29842. bio = NULL;
  29843. DSA_free(dsa);
  29844. dsa = NULL;
  29845. #ifdef HAVE_ECC
  29846. /* ensure that non-dsa keys do not work */
  29847. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29848. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29849. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29850. BIO_free(bio);
  29851. bio = NULL;
  29852. DSA_free(dsa);
  29853. dsa = NULL;
  29854. #endif /* HAVE_ECC */
  29855. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  29856. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  29857. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  29858. #endif /* HAVE_SELFTEST */
  29859. return EXPECT_RESULT();
  29860. }
  29861. static int test_wolfSSL_PEM_bio_ECKey(void)
  29862. {
  29863. EXPECT_DECLS;
  29864. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29865. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  29866. EC_KEY* ec = NULL;
  29867. EC_KEY* ec2;
  29868. BIO* bio = NULL;
  29869. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29870. unsigned char* pem = NULL;
  29871. int pLen;
  29872. #endif
  29873. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  29874. "MAA=\n"
  29875. "-----END PUBLIC KEY-----";
  29876. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  29877. "MAA=\n"
  29878. "-----END EC PRIVATE KEY-----";
  29879. /* PrivateKey */
  29880. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29881. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  29882. ec2 = NULL;
  29883. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  29884. ExpectIntEQ(ec == ec2, 1);
  29885. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29886. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29887. NULL), WOLFSSL_FAILURE);
  29888. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29889. NULL), WOLFSSL_FAILURE);
  29890. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  29891. WOLFSSL_FAILURE);
  29892. BIO_free(bio);
  29893. bio = NULL;
  29894. /* Public key data - fail. */
  29895. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29896. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29897. BIO_free(bio);
  29898. bio = NULL;
  29899. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29900. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  29901. NULL), WOLFSSL_SUCCESS);
  29902. BIO_free(bio);
  29903. bio = NULL;
  29904. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  29905. NULL),WOLFSSL_FAILURE);
  29906. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  29907. WOLFSSL_FAILURE);
  29908. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  29909. WOLFSSL_FAILURE);
  29910. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  29911. WOLFSSL_SUCCESS);
  29912. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29913. NULL), 0);
  29914. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29915. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29916. NULL), 0);
  29917. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29918. NULL), 0);
  29919. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29920. &pLen), 0);
  29921. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29922. &pLen), 0);
  29923. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29924. &pLen), 0);
  29925. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29926. NULL), 0);
  29927. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29928. &pLen), 1);
  29929. ExpectIntGT(pLen, 0);
  29930. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29931. #endif
  29932. EC_KEY_free(ec);
  29933. ec = NULL;
  29934. /* PUBKEY */
  29935. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29936. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  29937. ec2 = NULL;
  29938. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  29939. ExpectIntEQ(ec == ec2, 1);
  29940. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29941. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29942. BIO_free(bio);
  29943. bio = NULL;
  29944. /* Test 0x30, 0x00 fails. */
  29945. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  29946. sizeof(ec_key_bad_1)));
  29947. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29948. BIO_free(bio);
  29949. bio = NULL;
  29950. /* Private key data - fail. */
  29951. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29952. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29953. BIO_free(bio);
  29954. bio = NULL;
  29955. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29956. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  29957. BIO_free(bio);
  29958. bio = NULL;
  29959. /* Same test as above, but with a file pointer rather than a BIO. */
  29960. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29961. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  29962. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29963. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  29964. EC_KEY_free(ec);
  29965. ec = NULL;
  29966. #ifndef NO_RSA
  29967. /* ensure that non-ec keys do not work */
  29968. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  29969. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  29970. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  29971. BIO_free(bio);
  29972. bio = NULL;
  29973. EC_KEY_free(ec);
  29974. ec = NULL;
  29975. #endif /* !NO_RSA */
  29976. /* Test 0x30, 0x00 fails. */
  29977. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  29978. sizeof(ec_priv_key_bad_1)));
  29979. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29980. BIO_free(bio);
  29981. bio = NULL;
  29982. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29983. return EXPECT_RESULT();
  29984. }
  29985. static int test_wolfSSL_PEM_PUBKEY(void)
  29986. {
  29987. EXPECT_DECLS;
  29988. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  29989. BIO* bio = NULL;
  29990. EVP_PKEY* pkey = NULL;
  29991. /* test creating new EVP_PKEY with bad arg */
  29992. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  29993. /* test loading ECC key using BIO */
  29994. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29995. {
  29996. XFILE file = XBADFILE;
  29997. const char* fname = "./certs/ecc-client-keyPub.pem";
  29998. size_t sz;
  29999. byte* buf = NULL;
  30000. EVP_PKEY* pkey2 = NULL;
  30001. EC_KEY* ec_key = NULL;
  30002. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30003. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  30004. ExpectIntGT(sz = XFTELL(file), 0);
  30005. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  30006. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30007. if (buf != NULL) {
  30008. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30009. }
  30010. if (file != XBADFILE) {
  30011. XFCLOSE(file);
  30012. }
  30013. /* Test using BIO new mem and loading PEM private key */
  30014. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30015. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  30016. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30017. BIO_free(bio);
  30018. bio = NULL;
  30019. /* Qt unit test case*/
  30020. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30021. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30022. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  30023. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30024. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  30025. #else
  30026. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  30027. #endif
  30028. EC_KEY_free(ec_key);
  30029. EVP_PKEY_free(pkey2);
  30030. EVP_PKEY_free(pkey);
  30031. pkey = NULL;
  30032. }
  30033. #endif
  30034. (void)bio;
  30035. (void)pkey;
  30036. #endif
  30037. return EXPECT_RESULT();
  30038. }
  30039. #endif /* !NO_BIO */
  30040. static int test_DSA_do_sign_verify(void)
  30041. {
  30042. EXPECT_DECLS;
  30043. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  30044. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30045. !defined(NO_DSA)
  30046. unsigned char digest[WC_SHA_DIGEST_SIZE];
  30047. DSA_SIG* sig = NULL;
  30048. DSA* dsa = NULL;
  30049. word32 bytes;
  30050. byte sigBin[DSA_SIG_SIZE];
  30051. int dsacheck;
  30052. #ifdef USE_CERT_BUFFERS_1024
  30053. byte tmp[ONEK_BUF];
  30054. XMEMSET(tmp, 0, sizeof(tmp));
  30055. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  30056. bytes = sizeof_dsa_key_der_1024;
  30057. #elif defined(USE_CERT_BUFFERS_2048)
  30058. byte tmp[TWOK_BUF];
  30059. XMEMSET(tmp, 0, sizeof(tmp));
  30060. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  30061. bytes = sizeof_dsa_key_der_2048;
  30062. #else
  30063. byte tmp[TWOK_BUF];
  30064. XFILE fp = XBADFILE;
  30065. XMEMSET(tmp, 0, sizeof(tmp));
  30066. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  30067. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  30068. if (fp != XBADFILE)
  30069. XFCLOSE(fp);
  30070. #endif /* END USE_CERT_BUFFERS_1024 */
  30071. XMEMSET(digest, 202, sizeof(digest));
  30072. ExpectNotNull(dsa = DSA_new());
  30073. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  30074. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  30075. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  30076. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  30077. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  30078. DSA_SIG_free(sig);
  30079. DSA_free(dsa);
  30080. #endif
  30081. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  30082. return EXPECT_RESULT();
  30083. }
  30084. static int test_wolfSSL_tmp_dh(void)
  30085. {
  30086. EXPECT_DECLS;
  30087. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30088. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  30089. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30090. byte buff[6000];
  30091. char file[] = "./certs/dsaparams.pem";
  30092. XFILE f = XBADFILE;
  30093. int bytes = 0;
  30094. DSA* dsa = NULL;
  30095. DH* dh = NULL;
  30096. #if defined(WOLFSSL_DH_EXTRA) && \
  30097. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30098. DH* dh2 = NULL;
  30099. #endif
  30100. BIO* bio = NULL;
  30101. SSL* ssl = NULL;
  30102. SSL_CTX* ctx = NULL;
  30103. #ifndef NO_WOLFSSL_SERVER
  30104. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30105. #else
  30106. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30107. #endif
  30108. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30109. WOLFSSL_FILETYPE_PEM));
  30110. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30111. WOLFSSL_FILETYPE_PEM));
  30112. ExpectNotNull(ssl = SSL_new(ctx));
  30113. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30114. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  30115. if (f != XBADFILE)
  30116. XFCLOSE(f);
  30117. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30118. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  30119. ExpectNotNull(dsa);
  30120. dh = wolfSSL_DSA_dup_DH(dsa);
  30121. ExpectNotNull(dh);
  30122. #if defined(WOLFSSL_DH_EXTRA) && \
  30123. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30124. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  30125. #endif
  30126. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  30127. #ifndef NO_WOLFSSL_SERVER
  30128. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  30129. #else
  30130. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  30131. #endif
  30132. BIO_free(bio);
  30133. DSA_free(dsa);
  30134. DH_free(dh);
  30135. #if defined(WOLFSSL_DH_EXTRA) && \
  30136. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30137. DH_free(dh2);
  30138. #endif
  30139. SSL_free(ssl);
  30140. SSL_CTX_free(ctx);
  30141. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30142. #endif
  30143. return EXPECT_RESULT();
  30144. }
  30145. static int test_wolfSSL_ctrl(void)
  30146. {
  30147. EXPECT_DECLS;
  30148. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  30149. byte buff[6000];
  30150. BIO* bio = NULL;
  30151. int bytes;
  30152. BUF_MEM* ptr = NULL;
  30153. XMEMSET(buff, 0, sizeof(buff));
  30154. bytes = sizeof(buff);
  30155. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30156. ExpectNotNull(BIO_s_socket());
  30157. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  30158. /* needs tested after stubs filled out @TODO
  30159. SSL_ctrl
  30160. SSL_CTX_ctrl
  30161. */
  30162. BIO_free(bio);
  30163. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  30164. return EXPECT_RESULT();
  30165. }
  30166. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  30167. {
  30168. EXPECT_DECLS;
  30169. #ifdef OPENSSL_EXTRA
  30170. static const unsigned char pw[] = "password";
  30171. static const int pwSz = sizeof(pw) - 1;
  30172. size_t checkPwSz = 0;
  30173. const unsigned char* checkPw = NULL;
  30174. WOLFSSL_EVP_PKEY* key = NULL;
  30175. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  30176. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  30177. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30178. pwSz));
  30179. if (key != NULL) {
  30180. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  30181. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  30182. ExpectIntEQ(key->pkey_sz, pwSz);
  30183. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  30184. }
  30185. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  30186. ExpectIntEQ((int)checkPwSz, pwSz);
  30187. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  30188. wolfSSL_EVP_PKEY_free(key);
  30189. key = NULL;
  30190. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30191. 0));
  30192. ExpectIntEQ(key->pkey_sz, 0);
  30193. if (EXPECT_SUCCESS()) {
  30194. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30195. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30196. }
  30197. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30198. ExpectIntEQ((int)checkPwSz, 0);
  30199. wolfSSL_EVP_PKEY_free(key);
  30200. key = NULL;
  30201. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  30202. 0));
  30203. ExpectIntEQ(key->pkey_sz, 0);
  30204. if (EXPECT_SUCCESS()) {
  30205. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30206. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30207. }
  30208. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30209. ExpectIntEQ((int)checkPwSz, 0);
  30210. wolfSSL_EVP_PKEY_free(key);
  30211. key = NULL;
  30212. #endif /* OPENSSL_EXTRA */
  30213. return EXPECT_RESULT();
  30214. }
  30215. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  30216. {
  30217. EXPECT_DECLS;
  30218. #ifdef OPENSSL_EXTRA
  30219. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  30220. const char *priv = "ABCDEFGHIJKLMNOP";
  30221. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  30222. WOLFSSL_EVP_PKEY* key = NULL;
  30223. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30224. NULL, NULL, AES_128_KEY_SIZE, cipher));
  30225. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30226. NULL, (const unsigned char *)priv, 0, cipher));
  30227. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30228. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  30229. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30230. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  30231. wolfSSL_EVP_PKEY_free(key);
  30232. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  30233. #endif /* OPENSSL_EXTRA */
  30234. return EXPECT_RESULT();
  30235. }
  30236. static int test_wolfSSL_EVP_Digest(void)
  30237. {
  30238. EXPECT_DECLS;
  30239. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30240. const char* in = "abc";
  30241. int inLen = (int)XSTRLEN(in);
  30242. byte out[WC_SHA256_DIGEST_SIZE];
  30243. unsigned int outLen;
  30244. const char* expOut =
  30245. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  30246. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  30247. "\x15\xAD";
  30248. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  30249. "SHA256", NULL), 1);
  30250. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  30251. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  30252. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  30253. return EXPECT_RESULT();
  30254. }
  30255. static int test_wolfSSL_EVP_Digest_all(void)
  30256. {
  30257. EXPECT_DECLS;
  30258. #ifdef OPENSSL_EXTRA
  30259. const char* digests[] = {
  30260. #ifndef NO_MD5
  30261. "MD5",
  30262. #endif
  30263. #ifndef NO_SHA
  30264. "SHA",
  30265. #endif
  30266. #ifdef WOLFSSL_SHA224
  30267. "SHA224",
  30268. #endif
  30269. #ifndef NO_SHA256
  30270. "SHA256",
  30271. #endif
  30272. #ifdef WOLFSSL_SHA384
  30273. "SHA384",
  30274. #endif
  30275. #ifdef WOLFSSL_SHA512
  30276. "SHA512",
  30277. #endif
  30278. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  30279. "SHA512_224",
  30280. #endif
  30281. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  30282. "SHA512_256",
  30283. #endif
  30284. #ifdef WOLFSSL_SHA3
  30285. #ifndef WOLFSSL_NOSHA3_224
  30286. "SHA3_224",
  30287. #endif
  30288. #ifndef WOLFSSL_NOSHA3_256
  30289. "SHA3_256",
  30290. #endif
  30291. "SHA3_384",
  30292. #ifndef WOLFSSL_NOSHA3_512
  30293. "SHA3_512",
  30294. #endif
  30295. #endif /* WOLFSSL_SHA3 */
  30296. NULL
  30297. };
  30298. const char** d;
  30299. const unsigned char in[] = "abc";
  30300. int inLen = XSTR_SIZEOF(in);
  30301. byte out[WC_MAX_DIGEST_SIZE];
  30302. unsigned int outLen;
  30303. for (d = digests; *d != NULL; d++) {
  30304. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  30305. ExpectIntGT(outLen, 0);
  30306. ExpectIntEQ(EVP_MD_size(*d), outLen);
  30307. }
  30308. #endif
  30309. return EXPECT_RESULT();
  30310. }
  30311. static int test_wolfSSL_EVP_MD_size(void)
  30312. {
  30313. EXPECT_DECLS;
  30314. #ifdef OPENSSL_EXTRA
  30315. WOLFSSL_EVP_MD_CTX mdCtx;
  30316. #ifdef WOLFSSL_SHA3
  30317. #ifndef WOLFSSL_NOSHA3_224
  30318. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30319. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  30320. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  30321. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  30322. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30323. #endif
  30324. #ifndef WOLFSSL_NOSHA3_256
  30325. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30326. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  30327. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  30328. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  30329. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30330. #endif
  30331. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30332. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  30333. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  30334. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  30335. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30336. #ifndef WOLFSSL_NOSHA3_512
  30337. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30338. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  30339. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  30340. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  30341. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30342. #endif
  30343. #endif /* WOLFSSL_SHA3 */
  30344. #ifndef NO_SHA256
  30345. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30346. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  30347. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30348. WC_SHA256_DIGEST_SIZE);
  30349. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30350. WC_SHA256_BLOCK_SIZE);
  30351. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  30352. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  30353. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30354. #endif
  30355. #ifndef NO_MD5
  30356. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30357. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  30358. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30359. WC_MD5_DIGEST_SIZE);
  30360. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30361. WC_MD5_BLOCK_SIZE);
  30362. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  30363. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  30364. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30365. #endif
  30366. #ifdef WOLFSSL_SHA224
  30367. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30368. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  30369. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30370. WC_SHA224_DIGEST_SIZE);
  30371. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30372. WC_SHA224_BLOCK_SIZE);
  30373. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  30374. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  30375. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30376. #endif
  30377. #ifdef WOLFSSL_SHA384
  30378. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30379. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  30380. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30381. WC_SHA384_DIGEST_SIZE);
  30382. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30383. WC_SHA384_BLOCK_SIZE);
  30384. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  30385. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  30386. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30387. #endif
  30388. #ifdef WOLFSSL_SHA512
  30389. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30390. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  30391. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30392. WC_SHA512_DIGEST_SIZE);
  30393. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30394. WC_SHA512_BLOCK_SIZE);
  30395. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  30396. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  30397. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30398. #endif
  30399. #ifndef NO_SHA
  30400. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30401. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  30402. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30403. WC_SHA_DIGEST_SIZE);
  30404. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30405. WC_SHA_BLOCK_SIZE);
  30406. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30407. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30408. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30409. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30410. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  30411. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30412. WC_SHA_DIGEST_SIZE);
  30413. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30414. WC_SHA_BLOCK_SIZE);
  30415. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30416. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30417. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30418. #endif
  30419. /* error case */
  30420. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30421. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  30422. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30423. BAD_FUNC_ARG);
  30424. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  30425. /* Cleanup is valid on uninit'ed struct */
  30426. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30427. #endif /* OPENSSL_EXTRA */
  30428. return EXPECT_RESULT();
  30429. }
  30430. static int test_wolfSSL_EVP_MD_pkey_type(void)
  30431. {
  30432. EXPECT_DECLS;
  30433. #ifdef OPENSSL_EXTRA
  30434. const WOLFSSL_EVP_MD* md;
  30435. #ifndef NO_MD5
  30436. ExpectNotNull(md = EVP_md5());
  30437. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  30438. #endif
  30439. #ifndef NO_SHA
  30440. ExpectNotNull(md = EVP_sha1());
  30441. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  30442. #endif
  30443. #ifdef WOLFSSL_SHA224
  30444. ExpectNotNull(md = EVP_sha224());
  30445. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  30446. #endif
  30447. ExpectNotNull(md = EVP_sha256());
  30448. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  30449. #ifdef WOLFSSL_SHA384
  30450. ExpectNotNull(md = EVP_sha384());
  30451. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  30452. #endif
  30453. #ifdef WOLFSSL_SHA512
  30454. ExpectNotNull(md = EVP_sha512());
  30455. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  30456. #endif
  30457. #endif
  30458. return EXPECT_RESULT();
  30459. }
  30460. #ifdef OPENSSL_EXTRA
  30461. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  30462. size_t testKeySz, const char* testData, size_t testDataSz,
  30463. const byte* testResult, size_t testResultSz)
  30464. {
  30465. EXPECT_DECLS;
  30466. unsigned char check[WC_MAX_DIGEST_SIZE];
  30467. size_t checkSz = -1;
  30468. WOLFSSL_EVP_PKEY* key = NULL;
  30469. WOLFSSL_EVP_MD_CTX mdCtx;
  30470. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  30471. testKey, (int)testKeySz));
  30472. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30473. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30474. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30475. (unsigned int)testDataSz), 1);
  30476. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30477. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30478. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30479. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30480. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30481. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30482. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30483. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30484. (unsigned int)testDataSz), 1);
  30485. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30486. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30487. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30488. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30489. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30490. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30491. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30492. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30493. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30494. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30495. (unsigned int)testDataSz - 4), 1);
  30496. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30497. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30498. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30499. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30500. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30501. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30502. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30503. (unsigned int)testDataSz - 4), 1);
  30504. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30505. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30506. wolfSSL_EVP_PKEY_free(key);
  30507. return EXPECT_RESULT();
  30508. }
  30509. #endif
  30510. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  30511. {
  30512. EXPECT_DECLS;
  30513. #ifdef OPENSSL_EXTRA
  30514. static const unsigned char testKey[] =
  30515. {
  30516. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30517. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30518. 0x0b, 0x0b, 0x0b, 0x0b
  30519. };
  30520. static const char testData[] = "Hi There";
  30521. #ifdef WOLFSSL_SHA224
  30522. static const unsigned char testResultSha224[] =
  30523. {
  30524. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  30525. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  30526. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  30527. 0x53, 0x68, 0x4b, 0x22
  30528. };
  30529. #endif
  30530. #ifndef NO_SHA256
  30531. static const unsigned char testResultSha256[] =
  30532. {
  30533. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  30534. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  30535. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  30536. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  30537. };
  30538. #endif
  30539. #ifdef WOLFSSL_SHA384
  30540. static const unsigned char testResultSha384[] =
  30541. {
  30542. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  30543. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  30544. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  30545. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  30546. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  30547. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  30548. };
  30549. #endif
  30550. #ifdef WOLFSSL_SHA512
  30551. static const unsigned char testResultSha512[] =
  30552. {
  30553. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  30554. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  30555. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  30556. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  30557. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  30558. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  30559. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  30560. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  30561. };
  30562. #endif
  30563. #ifdef WOLFSSL_SHA3
  30564. #ifndef WOLFSSL_NOSHA3_224
  30565. static const unsigned char testResultSha3_224[] =
  30566. {
  30567. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  30568. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  30569. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  30570. 0xf3, 0xc8, 0x60, 0xf7
  30571. };
  30572. #endif
  30573. #ifndef WOLFSSL_NOSHA3_256
  30574. static const unsigned char testResultSha3_256[] =
  30575. {
  30576. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  30577. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  30578. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  30579. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  30580. };
  30581. #endif
  30582. #ifndef WOLFSSL_NOSHA3_384
  30583. static const unsigned char testResultSha3_384[] =
  30584. {
  30585. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  30586. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  30587. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  30588. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  30589. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  30590. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  30591. };
  30592. #endif
  30593. #ifndef WOLFSSL_NOSHA3_512
  30594. static const unsigned char testResultSha3_512[] =
  30595. {
  30596. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  30597. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  30598. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  30599. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  30600. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  30601. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  30602. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  30603. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  30604. };
  30605. #endif
  30606. #endif
  30607. #ifndef NO_SHA256
  30608. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  30609. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  30610. sizeof(testResultSha256)), TEST_SUCCESS);
  30611. #endif
  30612. #ifdef WOLFSSL_SHA224
  30613. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  30614. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  30615. sizeof(testResultSha224)), TEST_SUCCESS);
  30616. #endif
  30617. #ifdef WOLFSSL_SHA384
  30618. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  30619. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  30620. sizeof(testResultSha384)), TEST_SUCCESS);
  30621. #endif
  30622. #ifdef WOLFSSL_SHA512
  30623. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  30624. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  30625. sizeof(testResultSha512)), TEST_SUCCESS);
  30626. #endif
  30627. #ifdef WOLFSSL_SHA3
  30628. #ifndef WOLFSSL_NOSHA3_224
  30629. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  30630. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  30631. sizeof(testResultSha3_224)), TEST_SUCCESS);
  30632. #endif
  30633. #ifndef WOLFSSL_NOSHA3_256
  30634. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  30635. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  30636. sizeof(testResultSha3_256)), TEST_SUCCESS);
  30637. #endif
  30638. #ifndef WOLFSSL_NOSHA3_384
  30639. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  30640. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  30641. sizeof(testResultSha3_384)), TEST_SUCCESS);
  30642. #endif
  30643. #ifndef WOLFSSL_NOSHA3_512
  30644. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  30645. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  30646. sizeof(testResultSha3_512)), TEST_SUCCESS);
  30647. #endif
  30648. #endif
  30649. #endif /* OPENSSL_EXTRA */
  30650. return EXPECT_RESULT();
  30651. }
  30652. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  30653. {
  30654. EXPECT_DECLS;
  30655. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  30656. defined(USE_CERT_BUFFERS_2048)
  30657. WOLFSSL_EVP_PKEY* privKey = NULL;
  30658. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30659. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  30660. const char testData[] = "Hi There";
  30661. WOLFSSL_EVP_MD_CTX mdCtx;
  30662. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  30663. int ret;
  30664. size_t checkSz = -1;
  30665. int sz = 2048 / 8;
  30666. const unsigned char* cp;
  30667. const unsigned char* p;
  30668. unsigned char check[2048/8];
  30669. size_t i;
  30670. int paddings[] = {
  30671. RSA_PKCS1_PADDING,
  30672. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  30673. RSA_PKCS1_PSS_PADDING,
  30674. #endif
  30675. };
  30676. cp = client_key_der_2048;
  30677. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  30678. sizeof_client_key_der_2048)));
  30679. p = client_keypub_der_2048;
  30680. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30681. sizeof_client_keypub_der_2048)));
  30682. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30683. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  30684. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30685. NULL, privKey), 1);
  30686. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30687. (unsigned int)XSTRLEN(testData)), 1);
  30688. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30689. ExpectIntEQ((int)checkSz, sz);
  30690. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30691. ExpectIntEQ((int)checkSz,sz);
  30692. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30693. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30694. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  30695. ExpectIntEQ(ret, 1);
  30696. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30697. ExpectIntEQ(ret, 1);
  30698. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30699. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30700. NULL, pubKey), 1);
  30701. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30702. (unsigned int)XSTRLEN(testData)),
  30703. 1);
  30704. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30705. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30706. ExpectIntEQ(ret, 1);
  30707. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30708. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30709. NULL, privKey), 1);
  30710. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30711. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30712. ExpectIntEQ((int)checkSz, sz);
  30713. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30714. ExpectIntEQ((int)checkSz, sz);
  30715. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30716. (unsigned int)XSTRLEN(testData) - 4), 1);
  30717. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30718. ExpectIntEQ((int)checkSz, sz);
  30719. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30720. ExpectIntEQ(ret, 1);
  30721. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30722. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30723. NULL, pubKey), 1);
  30724. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30725. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30726. (unsigned int)XSTRLEN(testData) - 4),
  30727. 1);
  30728. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30729. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30730. ExpectIntEQ(ret, 1);
  30731. /* Check all signing padding types */
  30732. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  30733. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30734. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  30735. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  30736. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30737. paddings[i]), 1);
  30738. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30739. (unsigned int)XSTRLEN(testData)), 1);
  30740. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30741. ExpectIntEQ((int)checkSz, sz);
  30742. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30743. ExpectIntEQ((int)checkSz,sz);
  30744. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30745. ExpectIntEQ(ret, 1);
  30746. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30747. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  30748. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  30749. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30750. paddings[i]), 1);
  30751. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30752. (unsigned int)XSTRLEN(testData)), 1);
  30753. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30754. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30755. ExpectIntEQ(ret, 1);
  30756. }
  30757. wolfSSL_EVP_PKEY_free(pubKey);
  30758. wolfSSL_EVP_PKEY_free(privKey);
  30759. #endif
  30760. return EXPECT_RESULT();
  30761. }
  30762. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  30763. {
  30764. EXPECT_DECLS;
  30765. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30766. WOLFSSL_EVP_PKEY* privKey = NULL;
  30767. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30768. const char testData[] = "Hi There";
  30769. WOLFSSL_EVP_MD_CTX mdCtx;
  30770. int ret;
  30771. size_t checkSz = -1;
  30772. const unsigned char* cp;
  30773. const unsigned char* p;
  30774. unsigned char check[2048/8];
  30775. cp = ecc_clikey_der_256;
  30776. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  30777. sizeof_ecc_clikey_der_256));
  30778. p = ecc_clikeypub_der_256;
  30779. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30780. sizeof_ecc_clikeypub_der_256)));
  30781. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30782. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30783. NULL, privKey), 1);
  30784. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30785. (unsigned int)XSTRLEN(testData)), 1);
  30786. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30787. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30788. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30789. ExpectIntEQ(ret, 1);
  30790. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30791. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30792. NULL, pubKey), 1);
  30793. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30794. (unsigned int)XSTRLEN(testData)),
  30795. 1);
  30796. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30797. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30798. ExpectIntEQ(ret, 1);
  30799. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30800. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30801. NULL, privKey), 1);
  30802. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30803. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30804. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30805. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30806. (unsigned int)XSTRLEN(testData) - 4), 1);
  30807. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30808. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30809. ExpectIntEQ(ret, 1);
  30810. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30811. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30812. NULL, pubKey), 1);
  30813. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30814. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30815. (unsigned int)XSTRLEN(testData) - 4),
  30816. 1);
  30817. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30818. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30819. ExpectIntEQ(ret, 1);
  30820. wolfSSL_EVP_PKEY_free(pubKey);
  30821. wolfSSL_EVP_PKEY_free(privKey);
  30822. #endif
  30823. return EXPECT_RESULT();
  30824. }
  30825. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  30826. {
  30827. EXPECT_DECLS;
  30828. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30829. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  30830. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30831. char caFile[] = "./certs/client-ca.pem";
  30832. char clientFile[] = "./certs/client-cert.pem";
  30833. SSL_CTX* ctx = NULL;
  30834. X509* x509 = NULL;
  30835. BIO *bio = NULL;
  30836. X509 *cert = NULL;
  30837. X509 *ca = NULL;
  30838. STACK_OF(X509) *chain = NULL;
  30839. STACK_OF(X509) *chain2 = NULL;
  30840. #ifndef NO_WOLFSSL_SERVER
  30841. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30842. #else
  30843. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30844. #endif
  30845. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  30846. WOLFSSL_FILETYPE_PEM));
  30847. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  30848. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  30849. WOLFSSL_FILETYPE_PEM));
  30850. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  30851. /* additional test of getting EVP_PKEY key size from X509
  30852. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  30853. * allowed with user RSA */
  30854. {
  30855. EVP_PKEY* pkey = NULL;
  30856. #if defined(HAVE_ECC)
  30857. X509* ecX509 = NULL;
  30858. #endif /* HAVE_ECC */
  30859. ExpectNotNull(pkey = X509_get_pubkey(x509));
  30860. /* current RSA key is 2048 bit (256 bytes) */
  30861. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  30862. EVP_PKEY_free(pkey);
  30863. pkey = NULL;
  30864. #if defined(HAVE_ECC)
  30865. #if defined(USE_CERT_BUFFERS_256)
  30866. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  30867. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  30868. SSL_FILETYPE_ASN1));
  30869. #else
  30870. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  30871. cliEccCertFile, SSL_FILETYPE_PEM));
  30872. #endif
  30873. pkey = X509_get_pubkey(ecX509);
  30874. ExpectNotNull(pkey);
  30875. /* current ECC key is 256 bit (32 bytes) */
  30876. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  30877. X509_free(ecX509);
  30878. ecX509 = NULL;
  30879. EVP_PKEY_free(pkey);
  30880. pkey = NULL;
  30881. #endif /* HAVE_ECC */
  30882. }
  30883. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  30884. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  30885. if (EXPECT_SUCCESS()) {
  30886. x509 = NULL;
  30887. }
  30888. #ifdef WOLFSSL_ENCRYPTED_KEYS
  30889. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  30890. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  30891. #endif
  30892. SSL_CTX_free(ctx);
  30893. ctx = NULL;
  30894. #ifndef NO_WOLFSSL_SERVER
  30895. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30896. #else
  30897. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30898. #endif
  30899. /* Test haproxy use case */
  30900. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  30901. /* Read Certificate */
  30902. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  30903. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  30904. ExpectNotNull(chain = sk_X509_new_null());
  30905. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  30906. if (EXPECT_SUCCESS()) {
  30907. ca = NULL;
  30908. }
  30909. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  30910. ExpectNotNull(ca = sk_X509_shift(chain2));
  30911. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  30912. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  30913. if (EXPECT_SUCCESS()) {
  30914. ca = NULL;
  30915. }
  30916. BIO_free(bio);
  30917. X509_free(cert);
  30918. X509_free(ca);
  30919. X509_free(x509);
  30920. sk_X509_pop_free(chain, X509_free);
  30921. sk_X509_pop_free(chain2, X509_free);
  30922. SSL_CTX_free(ctx);
  30923. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30924. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30925. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  30926. return EXPECT_RESULT();
  30927. }
  30928. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30929. static int test_wolfSSL_ERR_peek_last_error_line(void)
  30930. {
  30931. EXPECT_DECLS;
  30932. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30933. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30934. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  30935. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  30936. callback_functions client_cb;
  30937. callback_functions server_cb;
  30938. int line = 0;
  30939. int flag = ERR_TXT_STRING;
  30940. const char* file = NULL;
  30941. const char* data = NULL;
  30942. /* create a failed connection and inspect the error */
  30943. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30944. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30945. client_cb.method = wolfTLSv1_1_client_method;
  30946. server_cb.method = wolfTLSv1_2_server_method;
  30947. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30948. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  30949. ExpectNotNull(data);
  30950. /* check clearing error state */
  30951. ERR_remove_state(0);
  30952. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30953. ERR_peek_last_error_line(NULL, &line);
  30954. ExpectIntEQ(line, 0);
  30955. ERR_peek_last_error_line(&file, NULL);
  30956. ExpectNull(file);
  30957. /* retry connection to fill error queue */
  30958. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30959. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30960. client_cb.method = wolfTLSv1_1_client_method;
  30961. server_cb.method = wolfTLSv1_2_server_method;
  30962. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30963. /* check that error code was stored */
  30964. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30965. ERR_peek_last_error_line(NULL, &line);
  30966. ExpectIntNE(line, 0);
  30967. ERR_peek_last_error_line(&file, NULL);
  30968. ExpectNotNull(file);
  30969. fprintf(stderr, "\nTesting error print out\n");
  30970. ERR_print_errors_fp(stderr);
  30971. fprintf(stderr, "Done testing print out\n\n");
  30972. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  30973. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  30974. return EXPECT_RESULT();
  30975. }
  30976. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  30977. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30978. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30979. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  30980. {
  30981. (void) ok;
  30982. (void) ctx;
  30983. fprintf(stderr, "ENTER verify_cb\n");
  30984. return SSL_SUCCESS;
  30985. }
  30986. #endif
  30987. static int test_wolfSSL_X509_Name_canon(void)
  30988. {
  30989. EXPECT_DECLS;
  30990. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30991. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  30992. defined(WOLFSSL_CERT_GEN) && \
  30993. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  30994. const long ex_hash1 = 0x0fdb2da4;
  30995. const long ex_hash2 = 0x9f3e8c9e;
  30996. X509_NAME *name = NULL;
  30997. X509 *x509 = NULL;
  30998. XFILE file = XBADFILE;
  30999. unsigned long hash = 0;
  31000. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  31001. byte *pbuf = NULL;
  31002. word32 len = 0;
  31003. (void) ex_hash2;
  31004. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  31005. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31006. ExpectNotNull(name = X509_get_issuer_name(x509));
  31007. /* When output buffer is NULL, should return necessary output buffer
  31008. * length.*/
  31009. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  31010. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31011. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31012. hash = (((unsigned long)digest[3] << 24) |
  31013. ((unsigned long)digest[2] << 16) |
  31014. ((unsigned long)digest[1] << 8) |
  31015. ((unsigned long)digest[0]));
  31016. ExpectIntEQ(hash, ex_hash1);
  31017. if (file != XBADFILE) {
  31018. XFCLOSE(file);
  31019. file = XBADFILE;
  31020. }
  31021. X509_free(x509);
  31022. x509 = NULL;
  31023. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31024. pbuf = NULL;
  31025. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  31026. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31027. ExpectNotNull(name = X509_get_issuer_name(x509));
  31028. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31029. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31030. hash = (((unsigned long)digest[3] << 24) |
  31031. ((unsigned long)digest[2] << 16) |
  31032. ((unsigned long)digest[1] << 8) |
  31033. ((unsigned long)digest[0]));
  31034. ExpectIntEQ(hash, ex_hash2);
  31035. if (file != XBADFILE)
  31036. XFCLOSE(file);
  31037. X509_free(x509);
  31038. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31039. #endif
  31040. return EXPECT_RESULT();
  31041. }
  31042. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  31043. {
  31044. EXPECT_DECLS;
  31045. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  31046. const int MAX_DIR = 4;
  31047. const char paths[][32] = {
  31048. "./certs/ed25519",
  31049. "./certs/ecc",
  31050. "./certs/crl",
  31051. "./certs/",
  31052. };
  31053. char CertCrl_path[MAX_FILENAME_SZ];
  31054. char *p;
  31055. X509_STORE* str = NULL;
  31056. X509_LOOKUP* lookup = NULL;
  31057. WOLFSSL_STACK* sk = NULL;
  31058. int len, total_len, i;
  31059. (void)sk;
  31060. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  31061. /* illegal string */
  31062. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31063. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31064. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  31065. SSL_FILETYPE_PEM,NULL), 0);
  31066. /* free store */
  31067. X509_STORE_free(str);
  31068. str = NULL;
  31069. /* short folder string */
  31070. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31071. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31072. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  31073. SSL_FILETYPE_PEM,NULL), 1);
  31074. #if defined(WOLFSSL_INT_H)
  31075. /* only available when including internal.h */
  31076. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31077. #endif
  31078. /* free store */
  31079. X509_STORE_free(str);
  31080. str = NULL;
  31081. /* typical function check */
  31082. p = &CertCrl_path[0];
  31083. total_len = 0;
  31084. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  31085. len = (int)XSTRLEN((const char*)&paths[i]);
  31086. total_len += len;
  31087. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  31088. p += len;
  31089. if (i != 0) *(p++) = SEPARATOR_CHAR;
  31090. }
  31091. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31092. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31093. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  31094. SSL_FILETYPE_PEM,NULL), 1);
  31095. #if defined(WOLFSSL_INT_H)
  31096. /* only available when including internal.h */
  31097. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31098. #endif
  31099. X509_STORE_free(str);
  31100. #endif
  31101. return EXPECT_RESULT();
  31102. }
  31103. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  31104. {
  31105. EXPECT_DECLS;
  31106. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31107. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31108. defined(WOLFSSL_SIGNER_DER_CERT)
  31109. X509_STORE_CTX* ctx = NULL;
  31110. X509_STORE* str = NULL;
  31111. X509_LOOKUP* lookup = NULL;
  31112. X509* cert1 = NULL;
  31113. X509* x509Ca = NULL;
  31114. X509* x509Svr = NULL;
  31115. X509* issuer = NULL;
  31116. WOLFSSL_STACK* sk = NULL;
  31117. X509_NAME* caName = NULL;
  31118. X509_NAME* issuerName = NULL;
  31119. XFILE file1 = XBADFILE;
  31120. int i;
  31121. int cert_count = 0;
  31122. int cmp;
  31123. char der[] = "certs/ca-cert.der";
  31124. #ifdef HAVE_CRL
  31125. char pem[][100] = {
  31126. "./certs/crl/crl.pem",
  31127. "./certs/crl/crl2.pem",
  31128. "./certs/crl/caEccCrl.pem",
  31129. "./certs/crl/eccCliCRL.pem",
  31130. "./certs/crl/eccSrvCRL.pem",
  31131. ""
  31132. };
  31133. #endif
  31134. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  31135. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  31136. if (file1 != XBADFILE)
  31137. XFCLOSE(file1);
  31138. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31139. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31140. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31141. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31142. SSL_FILETYPE_PEM,NULL), 1);
  31143. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31144. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31145. /* check if CA cert is loaded into the store */
  31146. for (i = 0; i < cert_count; i++) {
  31147. x509Ca = sk_X509_value(sk, i);
  31148. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31149. }
  31150. ExpectNotNull((x509Svr =
  31151. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31152. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31153. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31154. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  31155. ExpectNotNull(issuer);
  31156. caName = X509_get_subject_name(x509Ca);
  31157. ExpectNotNull(caName);
  31158. issuerName = X509_get_subject_name(issuer);
  31159. ExpectNotNull(issuerName);
  31160. cmp = X509_NAME_cmp(caName, issuerName);
  31161. ExpectIntEQ(cmp, 0);
  31162. /* load der format */
  31163. X509_free(issuer);
  31164. issuer = NULL;
  31165. X509_STORE_CTX_free(ctx);
  31166. ctx = NULL;
  31167. X509_STORE_free(str);
  31168. str = NULL;
  31169. sk_X509_pop_free(sk, NULL);
  31170. sk = NULL;
  31171. X509_free(x509Svr);
  31172. x509Svr = NULL;
  31173. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31174. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31175. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  31176. SSL_FILETYPE_ASN1,NULL), 1);
  31177. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31178. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31179. /* check if CA cert is loaded into the store */
  31180. for (i = 0; i < cert_count; i++) {
  31181. x509Ca = sk_X509_value(sk, i);
  31182. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31183. }
  31184. X509_STORE_free(str);
  31185. str = NULL;
  31186. sk_X509_pop_free(sk, NULL);
  31187. sk = NULL;
  31188. X509_free(cert1);
  31189. cert1 = NULL;
  31190. #ifdef HAVE_CRL
  31191. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  31192. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31193. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31194. SSL_FILETYPE_PEM,NULL), 1);
  31195. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  31196. "certs/server-revoked-cert.pem",
  31197. SSL_FILETYPE_PEM,NULL), 1);
  31198. if (str) {
  31199. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  31200. WOLFSSL_FILETYPE_PEM), 1);
  31201. /* since store hasn't yet known the revoked cert*/
  31202. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31203. "certs/server-revoked-cert.pem",
  31204. WOLFSSL_FILETYPE_PEM), 1);
  31205. }
  31206. for (i = 0; pem[i][0] != '\0'; i++)
  31207. {
  31208. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  31209. SSL_FILETYPE_PEM, NULL), 1);
  31210. }
  31211. if (str) {
  31212. /* since store knows crl list */
  31213. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31214. "certs/server-revoked-cert.pem",
  31215. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  31216. }
  31217. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  31218. X509_STORE_free(str);
  31219. #endif
  31220. #endif
  31221. return EXPECT_RESULT();
  31222. }
  31223. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  31224. {
  31225. int res = TEST_SKIPPED;
  31226. #if defined(OPENSSL_EXTRA)
  31227. X509_STORE_CTX_cleanup(NULL);
  31228. X509_STORE_CTX_trusted_stack(NULL, NULL);
  31229. res = TEST_SUCCESS;
  31230. #endif
  31231. return res;
  31232. }
  31233. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  31234. {
  31235. EXPECT_DECLS;
  31236. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31237. X509_STORE_CTX* ctx = NULL;
  31238. X509_STORE* str = NULL;
  31239. X509* x509Ca = NULL;
  31240. X509* x509Svr = NULL;
  31241. X509* issuer = NULL;
  31242. X509_NAME* caName = NULL;
  31243. X509_NAME* issuerName = NULL;
  31244. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31245. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31246. ExpectNotNull((x509Ca =
  31247. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  31248. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  31249. ExpectNotNull((x509Svr =
  31250. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31251. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31252. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31253. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  31254. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  31255. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  31256. #ifdef WOLFSSL_SIGNER_DER_CERT
  31257. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  31258. #endif
  31259. X509_free(issuer);
  31260. X509_STORE_CTX_free(ctx);
  31261. X509_free(x509Svr);
  31262. X509_STORE_free(str);
  31263. X509_free(x509Ca);
  31264. #endif
  31265. return EXPECT_RESULT();
  31266. }
  31267. static int test_wolfSSL_PKCS7_certs(void)
  31268. {
  31269. EXPECT_DECLS;
  31270. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  31271. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  31272. STACK_OF(X509)* sk = NULL;
  31273. STACK_OF(X509_INFO)* info_sk = NULL;
  31274. PKCS7 *p7 = NULL;
  31275. BIO* bio = NULL;
  31276. const byte* p = NULL;
  31277. int buflen = 0;
  31278. int i;
  31279. /* Test twice. Once with d2i and once without to test
  31280. * that everything is free'd correctly. */
  31281. for (i = 0; i < 2; i++) {
  31282. ExpectNotNull(p7 = PKCS7_new());
  31283. if (p7 != NULL) {
  31284. p7->version = 1;
  31285. #ifdef NO_SHA
  31286. p7->hashOID = SHA256h;
  31287. #else
  31288. p7->hashOID = SHAh;
  31289. #endif
  31290. }
  31291. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  31292. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  31293. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  31294. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  31295. ExpectNotNull(sk = sk_X509_new_null());
  31296. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  31297. X509_INFO* info = NULL;
  31298. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  31299. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  31300. if (EXPECT_SUCCESS() && (info != NULL)) {
  31301. info->x509 = NULL;
  31302. }
  31303. X509_INFO_free(info);
  31304. }
  31305. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  31306. info_sk = NULL;
  31307. BIO_free(bio);
  31308. bio = NULL;
  31309. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  31310. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  31311. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  31312. sk_X509_pop_free(sk, X509_free);
  31313. }
  31314. sk = NULL;
  31315. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  31316. if (i == 0) {
  31317. PKCS7_free(p7);
  31318. p7 = NULL;
  31319. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  31320. if (p7 != NULL) {
  31321. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  31322. * them */
  31323. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  31324. }
  31325. /* PKCS7_free free's the certs */
  31326. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  31327. }
  31328. BIO_free(bio);
  31329. bio = NULL;
  31330. PKCS7_free(p7);
  31331. p7 = NULL;
  31332. }
  31333. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31334. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  31335. return EXPECT_RESULT();
  31336. }
  31337. static int test_wolfSSL_X509_STORE_CTX(void)
  31338. {
  31339. EXPECT_DECLS;
  31340. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31341. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31342. X509_STORE_CTX* ctx = NULL;
  31343. X509_STORE* str = NULL;
  31344. X509* x509 = NULL;
  31345. #ifdef OPENSSL_ALL
  31346. X509* x5092 = NULL;
  31347. STACK_OF(X509) *sk = NULL;
  31348. STACK_OF(X509) *sk2 = NULL;
  31349. STACK_OF(X509) *sk3 = NULL;
  31350. #endif
  31351. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31352. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31353. ExpectNotNull((x509 =
  31354. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31355. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  31356. #ifdef OPENSSL_ALL
  31357. /* sk_X509_new only in OPENSSL_ALL */
  31358. sk = sk_X509_new_null();
  31359. ExpectNotNull(sk);
  31360. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  31361. #else
  31362. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  31363. #endif
  31364. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  31365. X509_STORE_CTX_set_error(ctx, -5);
  31366. X509_STORE_CTX_set_error(NULL, -5);
  31367. X509_STORE_CTX_free(ctx);
  31368. ctx = NULL;
  31369. #ifdef OPENSSL_ALL
  31370. sk_X509_pop_free(sk, NULL);
  31371. sk = NULL;
  31372. #endif
  31373. X509_STORE_free(str);
  31374. str = NULL;
  31375. X509_free(x509);
  31376. x509 = NULL;
  31377. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31378. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  31379. X509_STORE_CTX_free(ctx);
  31380. ctx = NULL;
  31381. #ifdef OPENSSL_ALL
  31382. /* test X509_STORE_CTX_get(1)_chain */
  31383. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  31384. SSL_FILETYPE_PEM)));
  31385. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  31386. SSL_FILETYPE_PEM)));
  31387. ExpectNotNull((sk = sk_X509_new_null()));
  31388. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  31389. if (EXPECT_FAIL()) {
  31390. X509_free(x509);
  31391. x509 = NULL;
  31392. }
  31393. ExpectNotNull((str = X509_STORE_new()));
  31394. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  31395. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  31396. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  31397. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  31398. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  31399. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  31400. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  31401. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  31402. X509_STORE_CTX_free(ctx);
  31403. ctx = NULL;
  31404. X509_STORE_free(str);
  31405. str = NULL;
  31406. /* CTX certs not freed yet */
  31407. X509_free(x5092);
  31408. x5092 = NULL;
  31409. sk_X509_pop_free(sk, NULL);
  31410. sk = NULL;
  31411. /* sk3 is dup so free here */
  31412. sk_X509_pop_free(sk3, NULL);
  31413. sk3 = NULL;
  31414. #endif
  31415. /* test X509_STORE_CTX_get/set_ex_data */
  31416. {
  31417. int i = 0, tmpData = 5;
  31418. void* tmpDataRet;
  31419. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31420. #ifdef HAVE_EX_DATA
  31421. for (i = 0; i < MAX_EX_DATA; i++) {
  31422. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31423. WOLFSSL_SUCCESS);
  31424. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31425. ExpectNotNull(tmpDataRet);
  31426. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31427. }
  31428. #else
  31429. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31430. WOLFSSL_FAILURE);
  31431. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31432. ExpectNull(tmpDataRet);
  31433. #endif
  31434. X509_STORE_CTX_free(ctx);
  31435. ctx = NULL;
  31436. }
  31437. /* test X509_STORE_get/set_ex_data */
  31438. {
  31439. int i = 0, tmpData = 99;
  31440. void* tmpDataRet;
  31441. ExpectNotNull(str = X509_STORE_new());
  31442. #ifdef HAVE_EX_DATA
  31443. for (i = 0; i < MAX_EX_DATA; i++) {
  31444. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31445. WOLFSSL_SUCCESS);
  31446. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31447. ExpectNotNull(tmpDataRet);
  31448. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31449. }
  31450. #else
  31451. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31452. WOLFSSL_FAILURE);
  31453. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31454. ExpectNull(tmpDataRet);
  31455. #endif
  31456. X509_STORE_free(str);
  31457. str = NULL;
  31458. }
  31459. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31460. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31461. return EXPECT_RESULT();
  31462. }
  31463. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31464. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  31465. STACK_OF(X509)* chain)
  31466. {
  31467. EXPECT_DECLS;
  31468. XFILE fp = XBADFILE;
  31469. X509* cert = NULL;
  31470. ExpectTrue((fp = XFOPEN(filename, "rb"))
  31471. != XBADFILE);
  31472. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31473. if (fp != XBADFILE) {
  31474. XFCLOSE(fp);
  31475. fp = XBADFILE;
  31476. }
  31477. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  31478. if (EXPECT_FAIL())
  31479. X509_free(cert);
  31480. return EXPECT_RESULT();
  31481. }
  31482. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  31483. int err, int loadCA)
  31484. {
  31485. EXPECT_DECLS;
  31486. X509_STORE_CTX* ctx = NULL;
  31487. X509_STORE* str = NULL;
  31488. XFILE fp = XBADFILE;
  31489. X509* cert = NULL;
  31490. STACK_OF(X509)* untrusted = NULL;
  31491. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  31492. != XBADFILE);
  31493. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31494. if (fp != XBADFILE) {
  31495. XFCLOSE(fp);
  31496. fp = XBADFILE;
  31497. }
  31498. ExpectNotNull(str = X509_STORE_new());
  31499. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31500. ExpectNotNull(untrusted = sk_X509_new_null());
  31501. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  31502. if (loadCA) {
  31503. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  31504. 1);
  31505. }
  31506. for (; *filenames; filenames++) {
  31507. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  31508. untrusted), TEST_SUCCESS);
  31509. }
  31510. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  31511. ExpectIntEQ(X509_verify_cert(ctx), ret);
  31512. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  31513. X509_free(cert);
  31514. X509_STORE_free(str);
  31515. X509_STORE_CTX_free(ctx);
  31516. sk_X509_pop_free(untrusted, NULL);
  31517. return EXPECT_RESULT();
  31518. }
  31519. #endif
  31520. static int test_X509_STORE_untrusted(void)
  31521. {
  31522. EXPECT_DECLS;
  31523. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31524. const char* untrusted1[] = {
  31525. "./certs/intermediate/ca-int2-cert.pem",
  31526. NULL
  31527. };
  31528. const char* untrusted2[] = {
  31529. "./certs/intermediate/ca-int-cert.pem",
  31530. "./certs/intermediate/ca-int2-cert.pem",
  31531. NULL
  31532. };
  31533. const char* untrusted3[] = {
  31534. "./certs/intermediate/ca-int-cert.pem",
  31535. "./certs/intermediate/ca-int2-cert.pem",
  31536. "./certs/ca-cert.pem",
  31537. NULL
  31538. };
  31539. /* Adding unrelated certs that should be ignored */
  31540. const char* untrusted4[] = {
  31541. "./certs/client-ca.pem",
  31542. "./certs/intermediate/ca-int-cert.pem",
  31543. "./certs/server-cert.pem",
  31544. "./certs/intermediate/ca-int2-cert.pem",
  31545. NULL
  31546. };
  31547. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  31548. * to loaded CA. */
  31549. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  31550. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  31551. /* Succeeds because path to loaded CA is available. */
  31552. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  31553. TEST_SUCCESS);
  31554. /* Fails because root CA is in the untrusted stack */
  31555. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  31556. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  31557. /* Succeeds because path to loaded CA is available. */
  31558. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  31559. TEST_SUCCESS);
  31560. #endif
  31561. return EXPECT_RESULT();
  31562. }
  31563. static int test_wolfSSL_X509_STORE_set_flags(void)
  31564. {
  31565. EXPECT_DECLS;
  31566. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31567. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31568. X509_STORE* store = NULL;
  31569. X509* x509 = NULL;
  31570. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  31571. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31572. WOLFSSL_FILETYPE_PEM)));
  31573. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  31574. #ifdef HAVE_CRL
  31575. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31576. WOLFSSL_SUCCESS);
  31577. #else
  31578. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31579. NOT_COMPILED_IN);
  31580. #endif
  31581. wolfSSL_X509_free(x509);
  31582. wolfSSL_X509_STORE_free(store);
  31583. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31584. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31585. return EXPECT_RESULT();
  31586. }
  31587. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  31588. {
  31589. EXPECT_DECLS;
  31590. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  31591. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31592. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  31593. WOLFSSL_X509_STORE* store = NULL;
  31594. WOLFSSL_X509_LOOKUP* lookup = NULL;
  31595. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  31596. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  31597. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  31598. X509_FILETYPE_PEM), 1);
  31599. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  31600. X509_FILETYPE_PEM), 1);
  31601. if (store != NULL) {
  31602. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  31603. WOLFSSL_FILETYPE_PEM), 1);
  31604. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31605. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  31606. }
  31607. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  31608. X509_FILETYPE_PEM), 1);
  31609. if (store != NULL) {
  31610. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31611. WOLFSSL_FILETYPE_PEM), 1);
  31612. }
  31613. wolfSSL_X509_STORE_free(store);
  31614. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  31615. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31616. return EXPECT_RESULT();
  31617. }
  31618. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  31619. {
  31620. EXPECT_DECLS;
  31621. #if defined(OPENSSL_EXTRA)
  31622. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  31623. time_t c_time;
  31624. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  31625. c_time = 365*24*60*60;
  31626. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  31627. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  31628. WOLFSSL_USE_CHECK_TIME);
  31629. ExpectTrue(ctx->param->check_time == c_time);
  31630. wolfSSL_X509_STORE_CTX_free(ctx);
  31631. #endif /* OPENSSL_EXTRA */
  31632. return EXPECT_RESULT();
  31633. }
  31634. static int test_wolfSSL_CTX_get0_set1_param(void)
  31635. {
  31636. EXPECT_DECLS;
  31637. #if defined(OPENSSL_EXTRA)
  31638. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31639. SSL_CTX* ctx = NULL;
  31640. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31641. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  31642. char testIPv4[] = "127.0.0.1";
  31643. char testhostName[] = "foo.hoge.com";
  31644. #ifndef NO_WOLFSSL_SERVER
  31645. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31646. #else
  31647. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31648. #endif
  31649. ExpectNull(SSL_CTX_get0_param(NULL));
  31650. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  31651. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  31652. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  31653. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  31654. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  31655. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  31656. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  31657. WOLFSSL_SUCCESS);
  31658. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  31659. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  31660. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  31661. (int)XSTRLEN(testhostName)));
  31662. ExpectIntEQ(0x01, pParam->hostFlags);
  31663. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31664. /* test for incorrect parameter */
  31665. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  31666. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  31667. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  31668. SSL_CTX_free(ctx);
  31669. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  31670. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31671. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31672. return EXPECT_RESULT();
  31673. }
  31674. static int test_wolfSSL_get0_param(void)
  31675. {
  31676. EXPECT_DECLS;
  31677. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31678. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31679. SSL_CTX* ctx = NULL;
  31680. SSL* ssl = NULL;
  31681. #ifndef NO_WOLFSSL_SERVER
  31682. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31683. #else
  31684. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31685. #endif
  31686. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31687. SSL_FILETYPE_PEM));
  31688. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31689. ExpectNotNull(ssl = SSL_new(ctx));
  31690. ExpectNotNull(SSL_get0_param(ssl));
  31691. SSL_free(ssl);
  31692. SSL_CTX_free(ctx);
  31693. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31694. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31695. return EXPECT_RESULT();
  31696. }
  31697. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  31698. {
  31699. EXPECT_DECLS;
  31700. #if defined(OPENSSL_EXTRA)
  31701. const char host[] = "www.example.com";
  31702. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31703. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  31704. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31705. if (pParam != NULL) {
  31706. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31707. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  31708. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31709. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31710. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31711. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31712. }
  31713. #endif /* OPENSSL_EXTRA */
  31714. return EXPECT_RESULT();
  31715. }
  31716. static int test_wolfSSL_set1_host(void)
  31717. {
  31718. EXPECT_DECLS;
  31719. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31720. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31721. const char host[] = "www.test_wolfSSL_set1_host.com";
  31722. const char emptyStr[] = "";
  31723. SSL_CTX* ctx = NULL;
  31724. SSL* ssl = NULL;
  31725. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31726. #ifndef NO_WOLFSSL_SERVER
  31727. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31728. #else
  31729. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31730. #endif
  31731. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31732. SSL_FILETYPE_PEM));
  31733. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31734. ExpectNotNull(ssl = SSL_new(ctx));
  31735. pParam = SSL_get0_param(ssl);
  31736. /* we should get back host string */
  31737. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31738. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31739. /* we should get back empty string */
  31740. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  31741. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31742. /* we should get back host string */
  31743. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31744. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31745. /* we should get back empty string */
  31746. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  31747. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31748. SSL_free(ssl);
  31749. SSL_CTX_free(ctx);
  31750. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31751. #endif /* OPENSSL_EXTRA */
  31752. return EXPECT_RESULT();
  31753. }
  31754. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  31755. {
  31756. EXPECT_DECLS;
  31757. #if defined(OPENSSL_EXTRA)
  31758. unsigned char buf[16] = {0};
  31759. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  31760. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  31761. /* test 127.0.0.1 */
  31762. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  31763. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  31764. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  31765. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  31766. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31767. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  31768. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  31769. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  31770. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31771. ExpectIntEQ(XSTRNCMP(param->ipasc,
  31772. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  31773. /* test 2001:db8:: */
  31774. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31775. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31776. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31777. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  31778. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31779. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  31780. /* test ::1234:5678 */
  31781. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  31782. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31783. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31784. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31785. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31786. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  31787. /* test 2001:db8::1234:5678 */
  31788. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31789. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31790. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31791. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31792. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31793. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  31794. sizeof(param->ipasc)), 0);
  31795. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  31796. /* 2001:db8:1::ab9:c0a8:102 */
  31797. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31798. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  31799. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  31800. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  31801. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31802. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  31803. sizeof(param->ipasc)), 0);
  31804. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31805. #endif /* OPENSSL_EXTRA */
  31806. return EXPECT_RESULT();
  31807. }
  31808. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  31809. {
  31810. EXPECT_DECLS;
  31811. #if defined(OPENSSL_EXTRA)
  31812. X509_STORE* store = NULL;
  31813. X509_STORE_CTX* ctx = NULL;
  31814. X509_STORE_CTX* ctx_no_init = NULL;
  31815. ExpectNotNull((store = X509_STORE_new()));
  31816. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31817. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  31818. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  31819. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  31820. /* should return NULL if ctx has not bee initialized */
  31821. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  31822. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  31823. wolfSSL_X509_STORE_CTX_free(ctx);
  31824. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  31825. X509_STORE_free(store);
  31826. #endif /* OPENSSL_EXTRA */
  31827. return EXPECT_RESULT();
  31828. }
  31829. static int test_wolfSSL_CTX_set_client_CA_list(void)
  31830. {
  31831. EXPECT_DECLS;
  31832. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31833. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  31834. WOLFSSL_CTX* ctx = NULL;
  31835. WOLFSSL* ssl = NULL;
  31836. X509_NAME* name = NULL;
  31837. STACK_OF(X509_NAME)* names = NULL;
  31838. STACK_OF(X509_NAME)* ca_list = NULL;
  31839. int names_len = 0;
  31840. int i;
  31841. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31842. /* Send two X501 names in cert request */
  31843. names = SSL_load_client_CA_file(cliCertFile);
  31844. ExpectNotNull(names);
  31845. ca_list = SSL_load_client_CA_file(caCertFile);
  31846. ExpectNotNull(ca_list);
  31847. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  31848. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  31849. if (EXPECT_FAIL()) {
  31850. wolfSSL_X509_NAME_free(name);
  31851. name = NULL;
  31852. }
  31853. SSL_CTX_set_client_CA_list(ctx, names);
  31854. /* This should only free the stack structure */
  31855. sk_X509_NAME_free(ca_list);
  31856. ca_list = NULL;
  31857. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31858. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31859. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31860. for (i = 0; i < names_len; i++) {
  31861. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31862. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31863. }
  31864. /* Needed to be able to create ssl object */
  31865. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31866. SSL_FILETYPE_PEM));
  31867. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31868. ExpectNotNull(ssl = wolfSSL_new(ctx));
  31869. /* load again as old names are responsibility of ctx to free*/
  31870. names = SSL_load_client_CA_file(cliCertFile);
  31871. ExpectNotNull(names);
  31872. SSL_set_client_CA_list(ssl, names);
  31873. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  31874. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31875. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31876. for (i = 0; i < names_len; i++) {
  31877. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31878. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31879. }
  31880. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  31881. {
  31882. tcp_ready ready;
  31883. func_args server_args;
  31884. callback_functions server_cb;
  31885. THREAD_TYPE serverThread;
  31886. WOLFSSL* ssl_client = NULL;
  31887. WOLFSSL_CTX* ctx_client = NULL;
  31888. SOCKET_T sockfd = 0;
  31889. /* wolfSSL_get_client_CA_list() with handshake */
  31890. StartTCP();
  31891. InitTcpReady(&ready);
  31892. XMEMSET(&server_args, 0, sizeof(func_args));
  31893. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31894. server_args.signal = &ready;
  31895. server_args.callbacks = &server_cb;
  31896. /* we are responsible for free'ing WOLFSSL_CTX */
  31897. server_cb.ctx = ctx;
  31898. server_cb.isSharedCtx = 1;
  31899. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  31900. cliCertFile, 0));
  31901. start_thread(test_server_nofail, &server_args, &serverThread);
  31902. wait_tcp_ready(&server_args);
  31903. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  31904. ExpectNotNull(ctx_client =
  31905. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  31906. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  31907. ctx_client, caCertFile, 0));
  31908. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  31909. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  31910. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  31911. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  31912. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  31913. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  31914. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  31915. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  31916. /* We are expecting two cert names to be sent */
  31917. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  31918. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  31919. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  31920. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  31921. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  31922. }
  31923. wolfSSL_shutdown(ssl_client);
  31924. wolfSSL_free(ssl_client);
  31925. wolfSSL_CTX_free(ctx_client);
  31926. CloseSocket(sockfd);
  31927. join_thread(serverThread);
  31928. FreeTcpReady(&ready);
  31929. }
  31930. #endif
  31931. wolfSSL_free(ssl);
  31932. wolfSSL_CTX_free(ctx);
  31933. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  31934. * !NO_BIO */
  31935. return EXPECT_RESULT();
  31936. }
  31937. static int test_wolfSSL_CTX_add_client_CA(void)
  31938. {
  31939. EXPECT_DECLS;
  31940. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31941. !defined(NO_WOLFSSL_CLIENT)
  31942. WOLFSSL_CTX* ctx = NULL;
  31943. WOLFSSL_X509* x509 = NULL;
  31944. WOLFSSL_X509* x509_a = NULL;
  31945. STACK_OF(X509_NAME)* ca_list = NULL;
  31946. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31947. /* Add client cert */
  31948. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  31949. SSL_FILETYPE_PEM));
  31950. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  31951. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31952. /* Add another client cert */
  31953. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  31954. SSL_FILETYPE_PEM));
  31955. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  31956. /* test for incorrect parameter */
  31957. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  31958. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  31959. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  31960. X509_free(x509);
  31961. X509_free(x509_a);
  31962. SSL_CTX_free(ctx);
  31963. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  31964. return EXPECT_RESULT();
  31965. }
  31966. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  31967. defined(HAVE_IO_TESTS_DEPENDENCIES)
  31968. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  31969. {
  31970. callback_functions* callbacks = ((func_args*)args)->callbacks;
  31971. WOLFSSL_CTX* ctx = callbacks->ctx;
  31972. WOLFSSL* ssl = NULL;
  31973. SOCKET_T sfd = 0;
  31974. SOCKET_T cfd = 0;
  31975. word16 port;
  31976. char input[1024];
  31977. int idx;
  31978. int ret, err = 0;
  31979. const char* privateName = "ech-private-name.com";
  31980. int privateNameLen = (int)XSTRLEN(privateName);
  31981. ((func_args*)args)->return_code = TEST_FAIL;
  31982. port = ((func_args*)args)->signal->port;
  31983. AssertIntEQ(WOLFSSL_SUCCESS,
  31984. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  31985. AssertIntEQ(WOLFSSL_SUCCESS,
  31986. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31987. WOLFSSL_FILETYPE_PEM));
  31988. AssertIntEQ(WOLFSSL_SUCCESS,
  31989. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31990. WOLFSSL_FILETYPE_PEM));
  31991. if (callbacks->ctx_ready)
  31992. callbacks->ctx_ready(ctx);
  31993. ssl = wolfSSL_new(ctx);
  31994. /* set the sni for the server */
  31995. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  31996. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  31997. CloseSocket(sfd);
  31998. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  31999. if (callbacks->ssl_ready)
  32000. callbacks->ssl_ready(ssl);
  32001. do {
  32002. err = 0; /* Reset error */
  32003. ret = wolfSSL_accept(ssl);
  32004. if (ret != WOLFSSL_SUCCESS) {
  32005. err = wolfSSL_get_error(ssl, 0);
  32006. }
  32007. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  32008. if (ret != WOLFSSL_SUCCESS) {
  32009. char buff[WOLFSSL_MAX_ERROR_SZ];
  32010. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  32011. }
  32012. else {
  32013. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  32014. input[idx] = 0;
  32015. fprintf(stderr, "Client message: %s\n", input);
  32016. }
  32017. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  32018. privateNameLen));
  32019. ((func_args*)args)->return_code = TEST_SUCCESS;
  32020. }
  32021. if (callbacks->on_result)
  32022. callbacks->on_result(ssl);
  32023. wolfSSL_shutdown(ssl);
  32024. wolfSSL_free(ssl);
  32025. wolfSSL_CTX_free(ctx);
  32026. CloseSocket(cfd);
  32027. #ifdef FP_ECC
  32028. wc_ecc_fp_free();
  32029. #endif
  32030. WOLFSSL_RETURN_FROM_THREAD(0);
  32031. }
  32032. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32033. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32034. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  32035. {
  32036. AssertNotNull(ssl);
  32037. AssertNotNull(line);
  32038. XFILE fp;
  32039. const byte lf = '\n';
  32040. fp = XFOPEN("./MyKeyLog.txt", "a");
  32041. XFWRITE( line, 1, strlen(line),fp);
  32042. XFWRITE( (void*)&lf,1,1,fp);
  32043. XFFLUSH(fp);
  32044. XFCLOSE(fp);
  32045. }
  32046. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32047. static int test_wolfSSL_CTX_set_keylog_callback(void)
  32048. {
  32049. EXPECT_DECLS;
  32050. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32051. !defined(NO_WOLFSSL_CLIENT)
  32052. SSL_CTX* ctx = NULL;
  32053. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32054. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32055. SSL_CTX_free(ctx);
  32056. SSL_CTX_set_keylog_callback(NULL, NULL);
  32057. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32058. return EXPECT_RESULT();
  32059. }
  32060. static int test_wolfSSL_CTX_get_keylog_callback(void)
  32061. {
  32062. EXPECT_DECLS;
  32063. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32064. !defined(NO_WOLFSSL_CLIENT)
  32065. SSL_CTX* ctx = NULL;
  32066. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32067. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32068. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32069. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  32070. SSL_CTX_set_keylog_callback(ctx, NULL );
  32071. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32072. SSL_CTX_free(ctx);
  32073. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32074. return EXPECT_RESULT();
  32075. }
  32076. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32077. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32078. {
  32079. /* set keylog callback */
  32080. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32081. return TEST_SUCCESS;
  32082. }
  32083. #endif
  32084. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  32085. {
  32086. EXPECT_DECLS;
  32087. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32088. /* This test is intended for checking whether keylog callback is called
  32089. * in client during TLS handshake between the client and a server.
  32090. */
  32091. test_ssl_cbf server_cbf;
  32092. test_ssl_cbf client_cbf;
  32093. XFILE fp = XBADFILE;
  32094. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32095. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32096. server_cbf.method = wolfTLSv1_2_server_method;
  32097. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  32098. /* clean up keylog file */
  32099. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32100. if (fp != XBADFILE) {
  32101. XFCLOSE(fp);
  32102. fp = XBADFILE;
  32103. }
  32104. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32105. &server_cbf, NULL), TEST_SUCCESS);
  32106. /* check if the keylog file exists */
  32107. char buff[300] = {0};
  32108. int found = 0;
  32109. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32110. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32111. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  32112. found = 1;
  32113. break;
  32114. }
  32115. }
  32116. if (fp != XBADFILE) {
  32117. XFCLOSE(fp);
  32118. }
  32119. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  32120. ExpectIntEQ(found, 1);
  32121. /* clean up */
  32122. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  32123. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32124. return EXPECT_RESULT();
  32125. }
  32126. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  32127. defined(HAVE_SECRET_CALLBACK)
  32128. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32129. {
  32130. /* set keylog callback */
  32131. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32132. return TEST_SUCCESS;
  32133. }
  32134. #endif
  32135. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  32136. {
  32137. EXPECT_DECLS;
  32138. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  32139. defined(HAVE_SECRET_CALLBACK)
  32140. /* This test is intended for checking whether keylog callback is called
  32141. * in client during TLS handshake between the client and a server.
  32142. */
  32143. test_ssl_cbf server_cbf;
  32144. test_ssl_cbf client_cbf;
  32145. XFILE fp = XBADFILE;
  32146. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32147. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32148. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32149. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  32150. /* clean up keylog file */
  32151. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32152. if (fp != XBADFILE) {
  32153. XFCLOSE(fp);
  32154. fp = XBADFILE;
  32155. }
  32156. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32157. &server_cbf, NULL), TEST_SUCCESS);
  32158. /* check if the keylog file exists */
  32159. {
  32160. char buff[300] = {0};
  32161. int found[4] = {0};
  32162. int numfnd = 0;
  32163. int i;
  32164. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32165. while (EXPECT_SUCCESS() &&
  32166. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32167. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  32168. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  32169. found[0] = 1;
  32170. continue;
  32171. }
  32172. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  32173. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  32174. found[1] = 1;
  32175. continue;
  32176. }
  32177. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  32178. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  32179. found[2] = 1;
  32180. continue;
  32181. }
  32182. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  32183. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  32184. found[3] = 1;
  32185. continue;
  32186. }
  32187. }
  32188. if (fp != XBADFILE)
  32189. XFCLOSE(fp);
  32190. for (i = 0; i < 4; i++) {
  32191. if (found[i] != 0)
  32192. numfnd++;
  32193. }
  32194. ExpectIntEQ(numfnd, 4);
  32195. }
  32196. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  32197. return EXPECT_RESULT();
  32198. }
  32199. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32200. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32201. static int test_wolfSSL_Tls13_ECH_params(void)
  32202. {
  32203. EXPECT_DECLS;
  32204. #if !defined(NO_WOLFSSL_CLIENT)
  32205. word32 outputLen = 0;
  32206. byte testBuf[72];
  32207. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  32208. WOLFSSL *ssl = wolfSSL_new(ctx);
  32209. ExpectNotNull(ctx);
  32210. ExpectNotNull(ssl);
  32211. /* invalid ctx */
  32212. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  32213. "ech-public-name.com", 0, 0, 0));
  32214. /* invalid public name */
  32215. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  32216. 0, 0));
  32217. /* invalid algorithms */
  32218. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  32219. "ech-public-name.com", 1000, 1000, 1000));
  32220. /* invalid ctx */
  32221. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  32222. &outputLen));
  32223. /* invalid output len */
  32224. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  32225. /* invalid ssl */
  32226. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  32227. (char*)testBuf, sizeof(testBuf)));
  32228. /* invalid configs64 */
  32229. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  32230. sizeof(testBuf)));
  32231. /* invalid size */
  32232. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  32233. (char*)testBuf, 0));
  32234. /* invalid ssl */
  32235. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  32236. sizeof(testBuf)));
  32237. /* invalid configs */
  32238. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  32239. sizeof(testBuf)));
  32240. /* invalid size */
  32241. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  32242. /* invalid ssl */
  32243. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  32244. /* invalid size */
  32245. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  32246. wolfSSL_free(ssl);
  32247. wolfSSL_CTX_free(ctx);
  32248. #endif /* !NO_WOLFSSL_CLIENT */
  32249. return EXPECT_RESULT();
  32250. }
  32251. static int test_wolfSSL_Tls13_ECH(void)
  32252. {
  32253. EXPECT_DECLS;
  32254. tcp_ready ready;
  32255. func_args client_args;
  32256. func_args server_args;
  32257. THREAD_TYPE serverThread;
  32258. callback_functions server_cbf;
  32259. callback_functions client_cbf;
  32260. SOCKET_T sockfd = 0;
  32261. WOLFSSL_CTX* ctx = NULL;
  32262. WOLFSSL* ssl = NULL;
  32263. const char* publicName = "ech-public-name.com";
  32264. const char* privateName = "ech-private-name.com";
  32265. int privateNameLen = 20;
  32266. char reply[1024];
  32267. int replyLen = 0;
  32268. byte rawEchConfig[128];
  32269. word32 rawEchConfigLen = sizeof(rawEchConfig);
  32270. InitTcpReady(&ready);
  32271. ready.port = 22222;
  32272. XMEMSET(&client_args, 0, sizeof(func_args));
  32273. XMEMSET(&server_args, 0, sizeof(func_args));
  32274. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  32275. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  32276. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32277. /* create the server context here so we can get the ech config */
  32278. ExpectNotNull(server_cbf.ctx =
  32279. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  32280. /* generate ech config */
  32281. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  32282. publicName, 0, 0, 0));
  32283. /* get the config for the client to use */
  32284. ExpectIntEQ(WOLFSSL_SUCCESS,
  32285. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  32286. &rawEchConfigLen));
  32287. server_args.callbacks = &server_cbf;
  32288. server_args.signal = &ready;
  32289. /* start server task */
  32290. start_thread(server_task_ech, &server_args, &serverThread);
  32291. wait_tcp_ready(&server_args);
  32292. /* run as a TLS1.3 client */
  32293. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  32294. ExpectIntEQ(WOLFSSL_SUCCESS,
  32295. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32296. ExpectIntEQ(WOLFSSL_SUCCESS,
  32297. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32298. ExpectIntEQ(WOLFSSL_SUCCESS,
  32299. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32300. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32301. /* get connected the server task */
  32302. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32303. /* set the ech configs for the client */
  32304. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  32305. rawEchConfigLen));
  32306. /* set the sni for the client */
  32307. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  32308. privateName, privateNameLen));
  32309. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  32310. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32311. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  32312. privateNameLen);
  32313. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  32314. /* add th null terminator for string compare */
  32315. reply[replyLen] = 0;
  32316. /* check that the server replied with the private name */
  32317. ExpectStrEQ(privateName, reply);
  32318. wolfSSL_free(ssl);
  32319. wolfSSL_CTX_free(ctx);
  32320. CloseSocket(sockfd);
  32321. join_thread(serverThread);
  32322. FreeTcpReady(&ready);
  32323. return EXPECT_RESULT();
  32324. }
  32325. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32326. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32327. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32328. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32329. static int post_auth_version_cb(WOLFSSL* ssl)
  32330. {
  32331. EXPECT_DECLS;
  32332. /* do handshake and then test version error */
  32333. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32334. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32335. return EXPECT_RESULT();
  32336. }
  32337. static int post_auth_version_client_cb(WOLFSSL* ssl)
  32338. {
  32339. EXPECT_DECLS;
  32340. /* do handshake and then test version error */
  32341. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32342. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32343. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  32344. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  32345. /* check was added to error queue */
  32346. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  32347. /* check the string matches expected string */
  32348. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  32349. "WRONG_SSL_VERSION");
  32350. #endif
  32351. return EXPECT_RESULT();
  32352. }
  32353. static int post_auth_cb(WOLFSSL* ssl)
  32354. {
  32355. EXPECT_DECLS;
  32356. WOLFSSL_X509* x509 = NULL;
  32357. /* do handshake and then test version error */
  32358. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32359. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  32360. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  32361. wolfSSL_X509_free(x509);
  32362. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  32363. return EXPECT_RESULT();
  32364. }
  32365. static int set_post_auth_cb(WOLFSSL* ssl)
  32366. {
  32367. if (!wolfSSL_is_server(ssl)) {
  32368. EXPECT_DECLS;
  32369. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  32370. return EXPECT_RESULT();
  32371. }
  32372. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  32373. return TEST_SUCCESS;
  32374. }
  32375. #endif
  32376. static int test_wolfSSL_Tls13_postauth(void)
  32377. {
  32378. EXPECT_DECLS;
  32379. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32380. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32381. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32382. test_ssl_cbf server_cbf;
  32383. test_ssl_cbf client_cbf;
  32384. /* test version failure doing post auth with TLS 1.2 connection */
  32385. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32386. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32387. server_cbf.method = wolfTLSv1_2_server_method;
  32388. server_cbf.ssl_ready = set_post_auth_cb;
  32389. server_cbf.on_result = post_auth_version_cb;
  32390. client_cbf.ssl_ready = set_post_auth_cb;
  32391. client_cbf.on_result = post_auth_version_client_cb;
  32392. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32393. &server_cbf, NULL), TEST_SUCCESS);
  32394. /* tests on post auth with TLS 1.3 */
  32395. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32396. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32397. server_cbf.method = wolfTLSv1_3_server_method;
  32398. server_cbf.ssl_ready = set_post_auth_cb;
  32399. client_cbf.ssl_ready = set_post_auth_cb;
  32400. server_cbf.on_result = post_auth_cb;
  32401. client_cbf.on_result = NULL;
  32402. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32403. &server_cbf, NULL), TEST_SUCCESS);
  32404. #endif
  32405. return EXPECT_RESULT();
  32406. }
  32407. static int test_wolfSSL_X509_NID(void)
  32408. {
  32409. EXPECT_DECLS;
  32410. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  32411. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  32412. int sigType;
  32413. int nameSz;
  32414. X509* cert = NULL;
  32415. EVP_PKEY* pubKeyTmp = NULL;
  32416. X509_NAME* name = NULL;
  32417. char commonName[80];
  32418. char countryName[80];
  32419. char localityName[80];
  32420. char stateName[80];
  32421. char orgName[80];
  32422. char orgUnit[80];
  32423. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  32424. /* convert cert from DER to internal WOLFSSL_X509 struct */
  32425. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  32426. sizeof_client_cert_der_2048));
  32427. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  32428. /* extract PUBLIC KEY from cert */
  32429. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  32430. /* extract signatureType */
  32431. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  32432. /* extract subjectName info */
  32433. ExpectNotNull(name = X509_get_subject_name(cert));
  32434. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  32435. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32436. NULL, 0)), 0);
  32437. ExpectIntEQ(nameSz, 15);
  32438. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32439. commonName, sizeof(commonName))), 0);
  32440. ExpectIntEQ(nameSz, 15);
  32441. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  32442. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32443. commonName, 9)), 0);
  32444. ExpectIntEQ(nameSz, 8);
  32445. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  32446. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  32447. countryName, sizeof(countryName))), 0);
  32448. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  32449. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  32450. localityName, sizeof(localityName))), 0);
  32451. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  32452. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32453. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  32454. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  32455. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  32456. orgName, sizeof(orgName))), 0);
  32457. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  32458. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32459. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  32460. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  32461. EVP_PKEY_free(pubKeyTmp);
  32462. X509_free(cert);
  32463. #endif
  32464. return EXPECT_RESULT();
  32465. }
  32466. static int test_wolfSSL_CTX_set_srp_username(void)
  32467. {
  32468. EXPECT_DECLS;
  32469. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  32470. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32471. WOLFSSL_CTX* ctx = NULL;
  32472. WOLFSSL* ssl = NULL;
  32473. const char *username = "TESTUSER";
  32474. const char *password = "TESTPASSWORD";
  32475. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32476. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32477. SSL_SUCCESS);
  32478. wolfSSL_CTX_free(ctx);
  32479. ctx = NULL;
  32480. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32481. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32482. SSL_SUCCESS);
  32483. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32484. SSL_SUCCESS);
  32485. ExpectNotNull(ssl = SSL_new(ctx));
  32486. ExpectNotNull(SSL_get_srp_username(ssl));
  32487. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  32488. wolfSSL_free(ssl);
  32489. wolfSSL_CTX_free(ctx);
  32490. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32491. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32492. return EXPECT_RESULT();
  32493. }
  32494. static int test_wolfSSL_CTX_set_srp_password(void)
  32495. {
  32496. EXPECT_DECLS;
  32497. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  32498. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32499. WOLFSSL_CTX* ctx = NULL;
  32500. const char *username = "TESTUSER";
  32501. const char *password = "TESTPASSWORD";
  32502. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32503. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32504. SSL_SUCCESS);
  32505. wolfSSL_CTX_free(ctx);
  32506. ctx = NULL;
  32507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32508. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32509. SSL_SUCCESS);
  32510. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32511. SSL_SUCCESS);
  32512. wolfSSL_CTX_free(ctx);
  32513. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32514. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32515. return EXPECT_RESULT();
  32516. }
  32517. static int test_wolfSSL_X509_STORE(void)
  32518. {
  32519. EXPECT_DECLS;
  32520. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32521. X509_STORE *store = NULL;
  32522. #ifdef HAVE_CRL
  32523. X509_STORE_CTX *storeCtx = NULL;
  32524. X509_CRL *crl = NULL;
  32525. X509 *ca = NULL;
  32526. X509 *cert = NULL;
  32527. const char crlPem[] = "./certs/crl/crl.revoked";
  32528. const char srvCert[] = "./certs/server-revoked-cert.pem";
  32529. const char caCert[] = "./certs/ca-cert.pem";
  32530. XFILE fp = XBADFILE;
  32531. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32532. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32533. SSL_FILETYPE_PEM)));
  32534. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32535. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32536. SSL_FILETYPE_PEM)));
  32537. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32538. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32539. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32540. X509_STORE_free(store);
  32541. store = NULL;
  32542. X509_STORE_CTX_free(storeCtx);
  32543. storeCtx = NULL;
  32544. X509_free(cert);
  32545. cert = NULL;
  32546. X509_free(ca);
  32547. ca = NULL;
  32548. /* should fail to verify now after adding in CRL */
  32549. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32550. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32551. SSL_FILETYPE_PEM)));
  32552. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32553. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  32554. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  32555. NULL, NULL));
  32556. if (fp != XBADFILE)
  32557. XFCLOSE(fp);
  32558. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  32559. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  32560. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32561. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32562. SSL_FILETYPE_PEM)));
  32563. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32564. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32565. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  32566. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  32567. X509_CRL_free(crl);
  32568. crl = NULL;
  32569. X509_STORE_free(store);
  32570. store = NULL;
  32571. X509_STORE_CTX_free(storeCtx);
  32572. storeCtx = NULL;
  32573. X509_free(cert);
  32574. cert = NULL;
  32575. X509_free(ca);
  32576. ca = NULL;
  32577. #endif /* HAVE_CRL */
  32578. #ifndef WOLFCRYPT_ONLY
  32579. {
  32580. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32581. SSL_CTX* ctx = NULL;
  32582. SSL* ssl = NULL;
  32583. int i;
  32584. for (i = 0; i < 2; i++) {
  32585. #ifndef NO_WOLFSSL_SERVER
  32586. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32587. #else
  32588. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32589. #endif
  32590. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32591. SSL_CTX_set_cert_store(ctx, store);
  32592. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32593. SSL_CTX_set_cert_store(ctx, store);
  32594. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32595. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32596. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32597. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32598. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32599. ExpectNotNull(ssl = SSL_new(ctx));
  32600. if (i == 0) {
  32601. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  32602. SSL_SUCCESS);
  32603. }
  32604. else {
  32605. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  32606. #ifdef OPENSSL_ALL
  32607. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  32608. #endif
  32609. }
  32610. if (EXPECT_FAIL() || (i == 1)) {
  32611. X509_STORE_free(store);
  32612. store = NULL;
  32613. }
  32614. SSL_free(ssl);
  32615. ssl = NULL;
  32616. SSL_CTX_free(ctx);
  32617. ctx = NULL;
  32618. }
  32619. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32620. }
  32621. #endif
  32622. #endif
  32623. return EXPECT_RESULT();
  32624. }
  32625. static int test_wolfSSL_X509_STORE_load_locations(void)
  32626. {
  32627. EXPECT_DECLS;
  32628. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32629. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32630. SSL_CTX *ctx = NULL;
  32631. X509_STORE *store = NULL;
  32632. const char ca_file[] = "./certs/ca-cert.pem";
  32633. const char client_pem_file[] = "./certs/client-cert.pem";
  32634. const char client_der_file[] = "./certs/client-cert.der";
  32635. const char ecc_file[] = "./certs/ecc-key.pem";
  32636. const char certs_path[] = "./certs/";
  32637. const char bad_path[] = "./bad-path/";
  32638. #ifdef HAVE_CRL
  32639. const char crl_path[] = "./certs/crl/";
  32640. const char crl_file[] = "./certs/crl/crl.pem";
  32641. #endif
  32642. #ifndef NO_WOLFSSL_SERVER
  32643. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32644. #else
  32645. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32646. #endif
  32647. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32648. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  32649. WOLFSSL_SUCCESS);
  32650. /* Test bad arguments */
  32651. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  32652. WOLFSSL_FAILURE);
  32653. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  32654. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  32655. WOLFSSL_FAILURE);
  32656. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  32657. WOLFSSL_FAILURE);
  32658. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  32659. WOLFSSL_FAILURE);
  32660. #ifdef HAVE_CRL
  32661. /* Test with CRL */
  32662. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  32663. WOLFSSL_SUCCESS);
  32664. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  32665. WOLFSSL_SUCCESS);
  32666. #endif
  32667. /* Test with CA */
  32668. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  32669. WOLFSSL_SUCCESS);
  32670. /* Test with client_cert and certs path */
  32671. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  32672. WOLFSSL_SUCCESS);
  32673. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  32674. WOLFSSL_SUCCESS);
  32675. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  32676. /* Clear nodes */
  32677. ERR_clear_error();
  32678. #endif
  32679. SSL_CTX_free(ctx);
  32680. #endif
  32681. return EXPECT_RESULT();
  32682. }
  32683. static int test_X509_STORE_get0_objects(void)
  32684. {
  32685. EXPECT_DECLS;
  32686. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  32687. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32688. X509_STORE *store = NULL;
  32689. X509_STORE *store_cpy = NULL;
  32690. SSL_CTX *ctx = NULL;
  32691. X509_OBJECT *obj = NULL;
  32692. STACK_OF(X509_OBJECT) *objs = NULL;
  32693. int i;
  32694. /* Setup store */
  32695. #ifndef NO_WOLFSSL_SERVER
  32696. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32697. #else
  32698. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32699. #endif
  32700. ExpectNotNull(store_cpy = X509_STORE_new());
  32701. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32702. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  32703. WOLFSSL_SUCCESS);
  32704. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  32705. WOLFSSL_SUCCESS);
  32706. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  32707. WOLFSSL_SUCCESS);
  32708. #ifdef HAVE_CRL
  32709. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  32710. WOLFSSL_SUCCESS);
  32711. #endif
  32712. /* Store ready */
  32713. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  32714. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  32715. #ifdef HAVE_CRL
  32716. #ifdef WOLFSSL_SIGNER_DER_CERT
  32717. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  32718. #else
  32719. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  32720. #endif
  32721. #else
  32722. #ifdef WOLFSSL_SIGNER_DER_CERT
  32723. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  32724. #else
  32725. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  32726. #endif
  32727. #endif
  32728. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  32729. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  32730. switch (X509_OBJECT_get_type(obj)) {
  32731. case X509_LU_X509:
  32732. {
  32733. WOLFSSL_X509* x509;
  32734. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  32735. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  32736. break;
  32737. }
  32738. case X509_LU_CRL:
  32739. #ifdef HAVE_CRL
  32740. {
  32741. WOLFSSL_CRL* crl = NULL;
  32742. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  32743. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  32744. break;
  32745. }
  32746. #endif
  32747. case X509_LU_NONE:
  32748. default:
  32749. Fail(("X509_OBJECT_get_type should return x509 or crl "
  32750. "(when built with crl support)"),
  32751. ("Unrecognized X509_OBJECT type or none"));
  32752. }
  32753. }
  32754. X509_STORE_free(store_cpy);
  32755. SSL_CTX_free(ctx);
  32756. #endif
  32757. return EXPECT_RESULT();
  32758. }
  32759. static int test_wolfSSL_BN_CTX(void)
  32760. {
  32761. EXPECT_DECLS;
  32762. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32763. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32764. WOLFSSL_BN_CTX* bn_ctx = NULL;
  32765. WOLFSSL_BIGNUM* t = NULL;
  32766. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  32767. /* No implementation. */
  32768. BN_CTX_init(NULL);
  32769. ExpectNotNull(t = BN_CTX_get(NULL));
  32770. BN_free(t);
  32771. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  32772. BN_free(t);
  32773. #ifndef NO_WOLFSSL_STUB
  32774. /* No implementation. */
  32775. BN_CTX_start(NULL);
  32776. BN_CTX_start(bn_ctx);
  32777. #endif
  32778. BN_CTX_free(NULL);
  32779. BN_CTX_free(bn_ctx);
  32780. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32781. return EXPECT_RESULT();
  32782. }
  32783. static int test_wolfSSL_BN(void)
  32784. {
  32785. EXPECT_DECLS;
  32786. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32787. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32788. BIGNUM* a = NULL;
  32789. BIGNUM* b = NULL;
  32790. BIGNUM* c = NULL;
  32791. BIGNUM* d = NULL;
  32792. BIGNUM emptyBN;
  32793. /* Setup */
  32794. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32795. /* internal not set emptyBN. */
  32796. ExpectNotNull(a = BN_new());
  32797. ExpectNotNull(b = BN_new());
  32798. ExpectNotNull(c = BN_dup(b));
  32799. ExpectNotNull(d = BN_new());
  32800. /* Invalid parameter testing. */
  32801. BN_free(NULL);
  32802. ExpectNull(BN_dup(NULL));
  32803. ExpectNull(BN_dup(&emptyBN));
  32804. ExpectNull(BN_copy(NULL, NULL));
  32805. ExpectNull(BN_copy(b, NULL));
  32806. ExpectNull(BN_copy(NULL, c));
  32807. ExpectNull(BN_copy(b, &emptyBN));
  32808. ExpectNull(BN_copy(&emptyBN, c));
  32809. BN_clear(NULL);
  32810. BN_clear(&emptyBN);
  32811. ExpectIntEQ(BN_num_bytes(NULL), 0);
  32812. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  32813. ExpectIntEQ(BN_num_bits(NULL), 0);
  32814. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  32815. ExpectIntEQ(BN_is_negative(NULL), 0);
  32816. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  32817. /* END Invalid Parameters */
  32818. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  32819. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  32820. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  32821. ExpectIntEQ(BN_num_bits(a), 2);
  32822. ExpectIntEQ(BN_num_bytes(a), 1);
  32823. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  32824. defined(WOLFSSL_SP_INT_NEGATIVE))
  32825. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  32826. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  32827. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  32828. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  32829. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  32830. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32831. {
  32832. /* Do additional tests on negative BN conversions. */
  32833. char* ret = NULL;
  32834. ASN1_INTEGER* asn1 = NULL;
  32835. BIGNUM* tmp = NULL;
  32836. /* Sanity check we have a negative BN. */
  32837. ExpectIntEQ(BN_is_negative(c), 1);
  32838. ExpectNotNull(ret = BN_bn2dec(c));
  32839. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32840. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32841. ret = NULL;
  32842. /* Convert to ASN1_INTEGER and back to BN. */
  32843. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  32844. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  32845. /* After converting back BN should be negative and correct. */
  32846. ExpectIntEQ(BN_is_negative(tmp), 1);
  32847. ExpectNotNull(ret = BN_bn2dec(tmp));
  32848. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32849. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32850. ASN1_INTEGER_free(asn1);
  32851. BN_free(tmp);
  32852. }
  32853. #endif
  32854. ExpectIntEQ(BN_get_word(c), 4);
  32855. #endif
  32856. ExpectIntEQ(BN_set_word(a, 3), 1);
  32857. ExpectIntEQ(BN_set_word(b, 3), 1);
  32858. ExpectIntEQ(BN_set_word(c, 4), 1);
  32859. /* NULL == NULL, NULL < num, num > NULL */
  32860. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  32861. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  32862. ExpectIntLT(BN_cmp(NULL, b), 0);
  32863. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  32864. ExpectIntGT(BN_cmp(a, NULL), 0);
  32865. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  32866. ExpectIntEQ(BN_cmp(a, b), 0);
  32867. ExpectIntLT(BN_cmp(a, c), 0);
  32868. ExpectIntGT(BN_cmp(c, b), 0);
  32869. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  32870. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  32871. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  32872. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  32873. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  32874. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  32875. BN_clear(a);
  32876. BN_free(a);
  32877. BN_free(b);
  32878. BN_free(c);
  32879. BN_clear_free(d);
  32880. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32881. return EXPECT_RESULT();
  32882. }
  32883. static int test_wolfSSL_BN_init(void)
  32884. {
  32885. EXPECT_DECLS;
  32886. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32887. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32888. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  32889. BIGNUM* ap = NULL;
  32890. BIGNUM bv;
  32891. BIGNUM cv;
  32892. BIGNUM dv;
  32893. ExpectNotNull(ap = BN_new());
  32894. BN_init(NULL);
  32895. XMEMSET(&bv, 0, sizeof(bv));
  32896. ExpectNull(BN_dup(&bv));
  32897. BN_init(&bv);
  32898. BN_init(&cv);
  32899. BN_init(&dv);
  32900. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  32901. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  32902. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  32903. /* a^b mod c = */
  32904. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  32905. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  32906. /* check result 3^2 mod 5 */
  32907. ExpectIntEQ(BN_get_word(&dv), 4);
  32908. /* a*b mod c = */
  32909. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  32910. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  32911. /* check result 3*2 mod 5 */
  32912. ExpectIntEQ(BN_get_word(&dv), 1);
  32913. BN_free(ap);
  32914. #endif
  32915. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32916. return EXPECT_RESULT();
  32917. }
  32918. static int test_wolfSSL_BN_enc_dec(void)
  32919. {
  32920. EXPECT_DECLS;
  32921. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32922. BIGNUM* a = NULL;
  32923. BIGNUM* b = NULL;
  32924. BIGNUM* c = NULL;
  32925. BIGNUM emptyBN;
  32926. char* str = NULL;
  32927. const char* emptyStr = "";
  32928. const char* numberStr = "12345";
  32929. const char* badStr = "g12345";
  32930. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32931. const char* twoStr = "2";
  32932. #endif
  32933. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  32934. unsigned char outNum[5];
  32935. /* Setup */
  32936. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32937. ExpectNotNull(a = BN_new());
  32938. ExpectNotNull(b = BN_new());
  32939. ExpectIntEQ(BN_set_word(a, 2), 1);
  32940. /* Invalid parameters */
  32941. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  32942. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  32943. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  32944. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  32945. ExpectNull(BN_bn2hex(NULL));
  32946. ExpectNull(BN_bn2hex(&emptyBN));
  32947. ExpectNull(BN_bn2dec(NULL));
  32948. ExpectNull(BN_bn2dec(&emptyBN));
  32949. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  32950. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  32951. ExpectNull(BN_bin2bn(binNum, -1, a));
  32952. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  32953. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  32954. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  32955. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  32956. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  32957. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  32958. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  32959. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  32960. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  32961. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  32962. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  32963. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  32964. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  32965. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  32966. ExpectIntEQ(BN_set_word(a, 2), 1);
  32967. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  32968. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  32969. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  32970. ExpectIntEQ(BN_cmp(a, b), 0);
  32971. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  32972. ExpectIntEQ(BN_cmp(a, b), -1);
  32973. ExpectNotNull(str = BN_bn2hex(a));
  32974. ExpectNotNull(BN_hex2bn(&b, str));
  32975. ExpectIntEQ(BN_cmp(a, b), 0);
  32976. ExpectNotNull(BN_hex2bn(&b, numberStr));
  32977. ExpectIntEQ(BN_cmp(a, b), -1);
  32978. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32979. str = NULL;
  32980. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32981. ExpectNotNull(str = BN_bn2dec(a));
  32982. ExpectStrEQ(str, twoStr);
  32983. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32984. str = NULL;
  32985. #ifndef NO_RSA
  32986. ExpectNotNull(str = BN_bn2dec(a));
  32987. ExpectNotNull(BN_dec2bn(&b, str));
  32988. ExpectIntEQ(BN_cmp(a, b), 0);
  32989. ExpectNotNull(BN_dec2bn(&b, numberStr));
  32990. ExpectIntEQ(BN_cmp(a, b), -1);
  32991. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32992. str = NULL;
  32993. #else
  32994. /* No implementation - fail with good parameters. */
  32995. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  32996. #endif
  32997. #endif
  32998. BN_free(b);
  32999. BN_free(a);
  33000. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33001. return EXPECT_RESULT();
  33002. }
  33003. static int test_wolfSSL_BN_word(void)
  33004. {
  33005. EXPECT_DECLS;
  33006. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33007. BIGNUM* a = NULL;
  33008. BIGNUM* b = NULL;
  33009. BIGNUM* c = NULL;
  33010. BIGNUM av;
  33011. ExpectNotNull(a = BN_new());
  33012. ExpectNotNull(b = BN_new());
  33013. ExpectNotNull(c = BN_new());
  33014. XMEMSET(&av, 0, sizeof(av));
  33015. /* Invalid parameter. */
  33016. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  33017. ExpectIntEQ(BN_add_word(&av, 3), 0);
  33018. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  33019. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  33020. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  33021. ExpectIntEQ(BN_set_word(&av, 3), 0);
  33022. ExpectIntEQ(BN_get_word(NULL), 0);
  33023. ExpectIntEQ(BN_get_word(&av), 0);
  33024. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  33025. ExpectIntEQ(BN_is_word(&av, 3), 0);
  33026. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33027. !defined(NO_DSA))
  33028. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  33029. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  33030. #endif
  33031. ExpectIntEQ(BN_one(NULL), 0);
  33032. ExpectIntEQ(BN_one(&av), 0);
  33033. BN_zero(NULL);
  33034. BN_zero(&av);
  33035. ExpectIntEQ(BN_is_one(NULL), 0);
  33036. ExpectIntEQ(BN_is_one(&av), 0);
  33037. ExpectIntEQ(BN_is_zero(NULL), 0);
  33038. ExpectIntEQ(BN_is_zero(&av), 0);
  33039. ExpectIntEQ(BN_set_word(a, 3), 1);
  33040. ExpectIntEQ(BN_set_word(b, 2), 1);
  33041. ExpectIntEQ(BN_set_word(c, 5), 1);
  33042. /* a + 3 = */
  33043. ExpectIntEQ(BN_add_word(a, 3), 1);
  33044. /* check result 3 + 3*/
  33045. ExpectIntEQ(BN_get_word(a), 6);
  33046. ExpectIntEQ(BN_is_word(a, 6), 1);
  33047. ExpectIntEQ(BN_is_word(a, 5), 0);
  33048. /* set a back to 3 */
  33049. ExpectIntEQ(BN_set_word(a, 3), 1);
  33050. /* a - 3 = */
  33051. ExpectIntEQ(BN_sub_word(a, 3), 1);
  33052. /* check result 3 - 3*/
  33053. ExpectIntEQ(BN_get_word(a), 0);
  33054. ExpectIntEQ(BN_one(a), 1);
  33055. ExpectIntEQ(BN_is_word(a, 1), 1);
  33056. ExpectIntEQ(BN_is_word(a, 0), 0);
  33057. ExpectIntEQ(BN_is_one(a), 1);
  33058. ExpectIntEQ(BN_is_zero(a), 0);
  33059. BN_zero(a);
  33060. ExpectIntEQ(BN_is_word(a, 0), 1);
  33061. ExpectIntEQ(BN_is_word(a, 1), 0);
  33062. ExpectIntEQ(BN_is_zero(a), 1);
  33063. ExpectIntEQ(BN_is_one(a), 0);
  33064. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33065. !defined(NO_DSA))
  33066. ExpectIntEQ(BN_set_word(a, 5), 1);
  33067. ExpectIntEQ(BN_mod_word(a, 3), 2);
  33068. ExpectIntEQ(BN_mod_word(a, 0), -1);
  33069. #endif
  33070. BN_free(c);
  33071. BN_free(b);
  33072. BN_free(a);
  33073. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33074. return EXPECT_RESULT();
  33075. }
  33076. static int test_wolfSSL_BN_bits(void)
  33077. {
  33078. EXPECT_DECLS;
  33079. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33080. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33081. BIGNUM* a = NULL;
  33082. BIGNUM emptyBN;
  33083. /* Setup */
  33084. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33085. ExpectNotNull(a = BN_new());
  33086. /* Invalid parameters. */
  33087. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  33088. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  33089. ExpectIntEQ(BN_set_bit(a, -1), 0);
  33090. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  33091. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  33092. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  33093. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  33094. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  33095. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  33096. ExpectIntEQ(BN_is_odd(NULL), 0);
  33097. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  33098. ExpectIntEQ(BN_set_word(a, 0), 1);
  33099. ExpectIntEQ(BN_is_zero(a), 1);
  33100. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  33101. ExpectIntEQ(BN_is_zero(a), 0);
  33102. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  33103. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  33104. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  33105. ExpectIntEQ(BN_is_zero(a), 1);
  33106. ExpectIntEQ(BN_set_bit(a, 0), 1);
  33107. ExpectIntEQ(BN_is_odd(a), 1);
  33108. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33109. ExpectIntEQ(BN_is_odd(a), 0);
  33110. ExpectIntEQ(BN_set_bit(a, 1), 1);
  33111. ExpectIntEQ(BN_is_odd(a), 0);
  33112. ExpectIntEQ(BN_set_bit(a, 129), 1);
  33113. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  33114. #ifndef NO_WOLFSSL_STUB
  33115. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  33116. #endif
  33117. BN_free(a);
  33118. #endif
  33119. return EXPECT_RESULT();
  33120. }
  33121. static int test_wolfSSL_BN_shift(void)
  33122. {
  33123. EXPECT_DECLS;
  33124. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33125. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33126. BIGNUM* a = NULL;
  33127. BIGNUM* b = NULL;
  33128. BIGNUM emptyBN;
  33129. /* Setup */
  33130. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33131. ExpectNotNull(a = BN_new());
  33132. ExpectNotNull(b = BN_new());
  33133. /* Invalid parameters. */
  33134. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  33135. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  33136. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  33137. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  33138. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  33139. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  33140. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  33141. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  33142. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  33143. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  33144. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  33145. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  33146. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  33147. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  33148. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  33149. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  33150. ExpectIntEQ(BN_set_word(a, 1), 1);
  33151. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  33152. ExpectIntEQ(BN_is_word(b, 2), 1);
  33153. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  33154. ExpectIntEQ(BN_is_word(a, 2), 1);
  33155. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  33156. ExpectIntEQ(BN_is_word(b, 1), 1);
  33157. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  33158. ExpectIntEQ(BN_is_word(a, 1), 1);
  33159. BN_free(b);
  33160. BN_free(a);
  33161. #endif
  33162. return EXPECT_RESULT();
  33163. }
  33164. static int test_wolfSSL_BN_math(void)
  33165. {
  33166. EXPECT_DECLS;
  33167. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33168. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33169. BIGNUM* a = NULL;
  33170. BIGNUM* b = NULL;
  33171. BIGNUM* r = NULL;
  33172. BIGNUM* rem = NULL;
  33173. BIGNUM emptyBN;
  33174. BN_ULONG val1;
  33175. BN_ULONG val2;
  33176. /* Setup */
  33177. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33178. ExpectNotNull(a = BN_new());
  33179. ExpectNotNull(b = BN_new());
  33180. ExpectNotNull(r = BN_new());
  33181. ExpectNotNull(rem = BN_new());
  33182. /* Invalid parameters. */
  33183. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  33184. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  33185. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  33186. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  33187. ExpectIntEQ(BN_add(r, a, NULL), 0);
  33188. ExpectIntEQ(BN_add(r, NULL, b), 0);
  33189. ExpectIntEQ(BN_add(NULL, a, b), 0);
  33190. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  33191. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  33192. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  33193. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  33194. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  33195. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  33196. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  33197. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  33198. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  33199. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  33200. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  33201. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  33202. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  33203. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  33204. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  33205. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  33206. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  33207. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  33208. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  33209. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  33210. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  33211. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  33212. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  33213. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  33214. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  33215. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  33216. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  33217. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  33218. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33219. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  33220. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  33221. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  33222. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  33223. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  33224. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  33225. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  33226. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  33227. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  33228. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  33229. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  33230. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  33231. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  33232. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  33233. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  33234. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33235. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33236. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  33237. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  33238. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  33239. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  33240. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  33241. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  33242. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  33243. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  33244. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  33245. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  33246. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  33247. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  33248. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  33249. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  33250. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33251. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  33252. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  33253. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  33254. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  33255. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  33256. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  33257. /* END Invalid parameters. */
  33258. val1 = 8;
  33259. val2 = 3;
  33260. ExpectIntEQ(BN_set_word(a, val1), 1);
  33261. ExpectIntEQ(BN_set_word(b, val2), 1);
  33262. ExpectIntEQ(BN_add(r, a, b), 1);
  33263. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  33264. ExpectIntEQ(BN_sub(r, a, b), 1);
  33265. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  33266. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  33267. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  33268. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  33269. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  33270. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  33271. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  33272. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  33273. BN_free(rem);
  33274. BN_free(r);
  33275. BN_free(b);
  33276. BN_free(a);
  33277. #endif
  33278. return EXPECT_RESULT();
  33279. }
  33280. static int test_wolfSSL_BN_math_mod(void)
  33281. {
  33282. EXPECT_DECLS;
  33283. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33284. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33285. BIGNUM* a = NULL;
  33286. BIGNUM* b = NULL;
  33287. BIGNUM* m = NULL;
  33288. BIGNUM* r = NULL;
  33289. BIGNUM* t = NULL;
  33290. BIGNUM emptyBN;
  33291. BN_ULONG val1;
  33292. BN_ULONG val2;
  33293. BN_ULONG val3;
  33294. /* Setup */
  33295. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33296. ExpectNotNull(a = BN_new());
  33297. ExpectNotNull(b = BN_new());
  33298. ExpectNotNull(m = BN_new());
  33299. ExpectNotNull(r = BN_new());
  33300. /* Invalid parameters. */
  33301. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  33302. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  33303. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  33304. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  33305. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  33306. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  33307. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  33308. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  33309. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  33310. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33311. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33312. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33313. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33314. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33315. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  33316. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  33317. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  33318. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  33319. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  33320. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  33321. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  33322. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  33323. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  33324. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  33325. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  33326. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  33327. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  33328. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33329. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33330. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33331. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33332. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33333. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  33334. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  33335. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  33336. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  33337. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  33338. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  33339. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  33340. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  33341. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  33342. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  33343. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  33344. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  33345. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  33346. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33347. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33348. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33349. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33350. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33351. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  33352. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  33353. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  33354. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  33355. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  33356. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33357. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33358. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33359. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33360. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  33361. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  33362. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  33363. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  33364. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  33365. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  33366. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  33367. /* END Invalid parameters. */
  33368. val1 = 9;
  33369. val2 = 13;
  33370. val3 = 5;
  33371. ExpectIntEQ(BN_set_word(a, val1), 1);
  33372. ExpectIntEQ(BN_set_word(b, val2), 1);
  33373. ExpectIntEQ(BN_set_word(m, val3), 1);
  33374. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  33375. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  33376. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  33377. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  33378. ExpectIntEQ(BN_set_word(a, 2), 1);
  33379. ExpectIntEQ(BN_set_word(b, 3), 1);
  33380. ExpectIntEQ(BN_set_word(m, 5), 1);
  33381. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  33382. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  33383. ExpectIntEQ(BN_is_word(r, 3), 1);
  33384. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  33385. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  33386. ExpectIntEQ(BN_is_word(r, 3), 1);
  33387. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  33388. ExpectIntEQ(BN_is_word(t, 3), 1);
  33389. BN_free(t);
  33390. /* No inverse case. No inverse when a divides b. */
  33391. ExpectIntEQ(BN_set_word(a, 3), 1);
  33392. ExpectIntEQ(BN_set_word(m, 9), 1);
  33393. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  33394. BN_free(r);
  33395. BN_free(m);
  33396. BN_free(b);
  33397. BN_free(a);
  33398. #endif
  33399. return EXPECT_RESULT();
  33400. }
  33401. static int test_wolfSSL_BN_math_other(void)
  33402. {
  33403. EXPECT_DECLS;
  33404. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33405. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33406. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  33407. BIGNUM* a = NULL;
  33408. BIGNUM* b = NULL;
  33409. BIGNUM* r = NULL;
  33410. BIGNUM emptyBN;
  33411. /* Setup */
  33412. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33413. ExpectNotNull(a = BN_new());
  33414. ExpectNotNull(b = BN_new());
  33415. ExpectNotNull(r = BN_new());
  33416. /* Invalid parameters. */
  33417. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  33418. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  33419. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  33420. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  33421. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  33422. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  33423. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  33424. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33425. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  33426. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  33427. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  33428. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  33429. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  33430. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  33431. /* END Invalid parameters. */
  33432. /* No common factors between 2 and 3. */
  33433. ExpectIntEQ(BN_set_word(a, 2), 1);
  33434. ExpectIntEQ(BN_set_word(b, 3), 1);
  33435. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33436. ExpectIntEQ(BN_is_word(r, 1), 1);
  33437. /* 3 is largest value that divides both 6 and 9. */
  33438. ExpectIntEQ(BN_set_word(a, 6), 1);
  33439. ExpectIntEQ(BN_set_word(b, 9), 1);
  33440. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33441. ExpectIntEQ(BN_is_word(r, 3), 1);
  33442. /* GCD of 0 and 0 is undefined. */
  33443. ExpectIntEQ(BN_set_word(a, 0), 1);
  33444. ExpectIntEQ(BN_set_word(b, 0), 1);
  33445. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  33446. /* Teardown */
  33447. BN_free(r);
  33448. BN_free(b);
  33449. BN_free(a);
  33450. #endif
  33451. #endif
  33452. return EXPECT_RESULT();
  33453. }
  33454. static int test_wolfSSL_BN_rand(void)
  33455. {
  33456. EXPECT_DECLS;
  33457. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  33458. BIGNUM* bn = NULL;
  33459. BIGNUM* range = NULL;
  33460. BIGNUM emptyBN;
  33461. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33462. ExpectNotNull(bn = BN_new());
  33463. ExpectNotNull(range = BN_new());
  33464. /* Invalid parameters. */
  33465. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  33466. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33467. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  33468. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  33469. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33470. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  33471. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  33472. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33473. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  33474. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  33475. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33476. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  33477. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  33478. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  33479. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  33480. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  33481. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  33482. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  33483. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  33484. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33485. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33486. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33487. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33488. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33489. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33490. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33491. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33492. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33493. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33494. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33495. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33496. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33497. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33498. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33499. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33500. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33501. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33502. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33503. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33504. /* 1 bit random value must have no more than one top bit set. */
  33505. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33506. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33507. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33508. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33509. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33510. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33511. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33512. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33513. /* END Invalid parameters. */
  33514. /* 0 bit random: 0. */
  33515. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33516. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33517. ExpectIntEQ(BN_is_zero(bn), 1);
  33518. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  33519. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33520. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33521. ExpectIntEQ(BN_is_zero(bn), 1);
  33522. /* 1 bit random: 0 or 1. */
  33523. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33524. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33525. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33526. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33527. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33528. ExpectIntEQ(BN_get_word(bn), 1);
  33529. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33530. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33531. ExpectIntEQ(BN_get_word(bn), 1);
  33532. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33533. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33534. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33535. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33536. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33537. ExpectIntEQ(BN_get_word(bn), 1);
  33538. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33539. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33540. ExpectIntEQ(BN_get_word(bn), 1);
  33541. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33542. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33543. ExpectIntEQ(BN_num_bits(bn), 8);
  33544. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33545. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33546. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33547. ExpectIntEQ(BN_num_bits(bn), 8);
  33548. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33549. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33550. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33551. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33552. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33553. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33554. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33555. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33556. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33557. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33558. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33559. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33560. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33561. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33562. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33563. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33564. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33565. * case, requesting a 13-bit random number would actually return a 16-bit
  33566. * random number. */
  33567. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  33568. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33569. ExpectIntEQ(BN_num_bits(bn), 13);
  33570. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  33571. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33572. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33573. ExpectIntEQ(BN_set_word(range, 0), 1);
  33574. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33575. ExpectIntEQ(BN_set_word(range, 1), 1);
  33576. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33577. BN_free(bn);
  33578. BN_free(range);
  33579. #endif
  33580. return EXPECT_RESULT();
  33581. }
  33582. static int test_wolfSSL_BN_prime(void)
  33583. {
  33584. EXPECT_DECLS;
  33585. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33586. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33587. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  33588. BIGNUM* a = NULL;
  33589. BIGNUM* add = NULL;
  33590. BIGNUM* rem = NULL;
  33591. BIGNUM emptyBN;
  33592. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33593. ExpectNotNull(a = BN_new());
  33594. ExpectNotNull(add = BN_new());
  33595. ExpectNotNull(rem = BN_new());
  33596. /* Invalid parameters. */
  33597. /* BN_generate_prime_ex()
  33598. * prime - must have valid BIGNUM
  33599. * bits - Greater then 0
  33600. * safe - not supported, must be 0
  33601. * add - not supported, must be NULL
  33602. * rem - not supported, must be NULL
  33603. * cb - anything
  33604. */
  33605. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  33606. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  33607. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  33608. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  33609. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  33610. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  33611. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  33612. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  33613. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  33614. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  33615. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  33616. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  33617. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  33618. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  33619. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  33620. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  33621. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  33622. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  33623. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  33624. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  33625. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  33626. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  33627. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  33628. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  33629. /* END Invalid parameters. */
  33630. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  33631. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  33632. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33633. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  33634. BN_free(rem);
  33635. BN_free(add);
  33636. BN_free(a);
  33637. #endif
  33638. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33639. return EXPECT_RESULT();
  33640. }
  33641. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33642. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33643. #define TEST_ARG 0x1234
  33644. static void msg_cb(int write_p, int version, int content_type,
  33645. const void *buf, size_t len, SSL *ssl, void *arg)
  33646. {
  33647. (void)write_p;
  33648. (void)version;
  33649. (void)content_type;
  33650. (void)buf;
  33651. (void)len;
  33652. (void)ssl;
  33653. AssertTrue(arg == (void*)TEST_ARG);
  33654. }
  33655. #endif
  33656. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33657. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33658. #if defined(SESSION_CERTS)
  33659. #include "wolfssl/internal.h"
  33660. #endif
  33661. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  33662. {
  33663. EXPECT_DECLS;
  33664. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33665. STACK_OF(X509)* sk = NULL;
  33666. X509* x509 = NULL;
  33667. int i, num;
  33668. BIO* bio = NULL;
  33669. #endif
  33670. ExpectNotNull(ctx);
  33671. ExpectNotNull(ssl);
  33672. fprintf(stderr, "\n===== msgcb called ====\n");
  33673. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  33674. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  33675. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  33676. ExpectNotNull(SSL_get0_verified_chain(ssl));
  33677. #endif
  33678. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33679. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  33680. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  33681. if (sk == NULL) {
  33682. BIO_free(bio);
  33683. return TEST_FAIL;
  33684. }
  33685. num = sk_X509_num(sk);
  33686. ExpectTrue(num > 0);
  33687. for (i = 0; i < num; i++) {
  33688. ExpectNotNull(x509 = sk_X509_value(sk,i));
  33689. if (x509 == NULL)
  33690. break;
  33691. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  33692. X509_print(bio,x509);
  33693. fprintf(stderr, "\n\n");
  33694. }
  33695. BIO_free(bio);
  33696. #endif
  33697. return EXPECT_RESULT();
  33698. }
  33699. #endif
  33700. static int test_wolfSSL_msgCb(void)
  33701. {
  33702. EXPECT_DECLS;
  33703. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33704. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33705. test_ssl_cbf client_cb;
  33706. test_ssl_cbf server_cb;
  33707. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33708. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33709. #ifndef WOLFSSL_NO_TLS12
  33710. client_cb.method = wolfTLSv1_2_client_method;
  33711. server_cb.method = wolfTLSv1_2_server_method;
  33712. #else
  33713. client_cb.method = wolfTLSv1_3_client_method;
  33714. server_cb.method = wolfTLSv1_3_server_method;
  33715. #endif
  33716. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33717. &server_cb, msgCb), TEST_SUCCESS);
  33718. #endif
  33719. return EXPECT_RESULT();
  33720. }
  33721. static int test_wolfSSL_either_side(void)
  33722. {
  33723. EXPECT_DECLS;
  33724. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33725. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33726. test_ssl_cbf client_cb;
  33727. test_ssl_cbf server_cb;
  33728. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33729. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33730. /* Use different CTX for client and server */
  33731. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33732. ExpectNotNull(client_cb.ctx);
  33733. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33734. ExpectNotNull(server_cb.ctx);
  33735. /* we are responsible for free'ing WOLFSSL_CTX */
  33736. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33737. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33738. &server_cb, NULL), TEST_SUCCESS);
  33739. wolfSSL_CTX_free(client_cb.ctx);
  33740. wolfSSL_CTX_free(server_cb.ctx);
  33741. #endif
  33742. return EXPECT_RESULT();
  33743. }
  33744. static int test_wolfSSL_DTLS_either_side(void)
  33745. {
  33746. EXPECT_DECLS;
  33747. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33748. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  33749. test_ssl_cbf client_cb;
  33750. test_ssl_cbf server_cb;
  33751. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33752. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33753. /* Use different CTX for client and server */
  33754. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33755. ExpectNotNull(client_cb.ctx);
  33756. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33757. ExpectNotNull(server_cb.ctx);
  33758. /* we are responsible for free'ing WOLFSSL_CTX */
  33759. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33760. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33761. &server_cb, NULL), TEST_SUCCESS);
  33762. wolfSSL_CTX_free(client_cb.ctx);
  33763. wolfSSL_CTX_free(server_cb.ctx);
  33764. #endif
  33765. return EXPECT_RESULT();
  33766. }
  33767. static int test_generate_cookie(void)
  33768. {
  33769. EXPECT_DECLS;
  33770. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  33771. SSL_CTX* ctx = NULL;
  33772. SSL* ssl = NULL;
  33773. byte buf[FOURK_BUF] = {0};
  33774. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  33775. ExpectNotNull(ssl = SSL_new(ctx));
  33776. /* Test unconnected */
  33777. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  33778. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  33779. wolfSSL_SetCookieCtx(ssl, ctx);
  33780. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  33781. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  33782. SSL_free(ssl);
  33783. SSL_CTX_free(ctx);
  33784. #endif
  33785. return EXPECT_RESULT();
  33786. }
  33787. static int test_wolfSSL_set_options(void)
  33788. {
  33789. EXPECT_DECLS;
  33790. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33791. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33792. WOLFSSL* ssl = NULL;
  33793. WOLFSSL_CTX* ctx = NULL;
  33794. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33795. char appData[] = "extra msg";
  33796. #endif
  33797. #ifdef OPENSSL_EXTRA
  33798. unsigned char protos[] = {
  33799. 7, 't', 'l', 's', '/', '1', '.', '2',
  33800. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  33801. };
  33802. unsigned int len = sizeof(protos);
  33803. void *arg = (void *)TEST_ARG;
  33804. #endif
  33805. #ifndef NO_WOLFSSL_SERVER
  33806. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33807. #else
  33808. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33809. #endif
  33810. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33811. WOLFSSL_FILETYPE_PEM));
  33812. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33813. WOLFSSL_FILETYPE_PEM));
  33814. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  33815. == WOLFSSL_OP_NO_TLSv1);
  33816. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  33817. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33818. WOLFSSL_OP_NO_SSLv2)), 0);
  33819. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33820. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33821. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  33822. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33823. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33824. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33825. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33826. WOLFSSL_OP_NO_COMPRESSION));
  33827. wolfSSL_CTX_free(ctx);
  33828. ctx = NULL;
  33829. #ifndef NO_WOLFSSL_SERVER
  33830. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33831. #else
  33832. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33833. #endif
  33834. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33835. WOLFSSL_FILETYPE_PEM));
  33836. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33837. WOLFSSL_FILETYPE_PEM));
  33838. #ifdef OPENSSL_EXTRA
  33839. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  33840. #endif
  33841. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33842. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33843. #ifdef HAVE_EX_DATA
  33844. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  33845. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33846. if (ssl != NULL) {
  33847. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  33848. appData, sizeof(appData)), 0);
  33849. }
  33850. #else
  33851. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  33852. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33853. #endif
  33854. #endif
  33855. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  33856. WOLFSSL_OP_NO_TLSv1);
  33857. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  33858. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33859. WOLFSSL_OP_NO_SSLv2)), 0);
  33860. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33861. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33862. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  33863. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33864. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33865. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33866. #ifdef OPENSSL_EXTRA
  33867. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33868. WOLFSSL_OP_NO_COMPRESSION));
  33869. #endif
  33870. #ifdef OPENSSL_EXTRA
  33871. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  33872. wolfSSL_set_msg_callback_arg(ssl, arg);
  33873. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33874. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  33875. #else
  33876. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  33877. #endif
  33878. #endif
  33879. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  33880. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  33881. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  33882. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  33883. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33884. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  33885. #else
  33886. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  33887. #endif
  33888. #endif /* HAVE_ALPN && !NO_BIO */
  33889. #endif
  33890. wolfSSL_free(ssl);
  33891. wolfSSL_CTX_free(ctx);
  33892. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33893. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33894. return EXPECT_RESULT();
  33895. }
  33896. static int test_wolfSSL_sk_SSL_CIPHER(void)
  33897. {
  33898. EXPECT_DECLS;
  33899. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33900. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33901. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33902. SSL* ssl = NULL;
  33903. SSL_CTX* ctx = NULL;
  33904. STACK_OF(SSL_CIPHER) *sk = NULL;
  33905. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  33906. #ifndef NO_WOLFSSL_SERVER
  33907. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33908. #else
  33909. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33910. #endif
  33911. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33912. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33913. ExpectNotNull(ssl = SSL_new(ctx));
  33914. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  33915. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  33916. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  33917. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  33918. /* error case because connection has not been established yet */
  33919. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  33920. sk_SSL_CIPHER_free(dupSk);
  33921. /* sk is pointer to internal struct that should be free'd in SSL_free */
  33922. SSL_free(ssl);
  33923. SSL_CTX_free(ctx);
  33924. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33925. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33926. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33927. return EXPECT_RESULT();
  33928. }
  33929. static int test_wolfSSL_set1_curves_list(void)
  33930. {
  33931. EXPECT_DECLS;
  33932. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  33933. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33934. SSL* ssl = NULL;
  33935. SSL_CTX* ctx = NULL;
  33936. #ifndef NO_WOLFSSL_SERVER
  33937. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33938. #else
  33939. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33940. #endif
  33941. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  33942. SSL_FILETYPE_PEM));
  33943. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  33944. ExpectNotNull(ssl = SSL_new(ctx));
  33945. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  33946. #ifdef HAVE_ECC
  33947. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  33948. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  33949. #endif
  33950. #ifdef HAVE_CURVE25519
  33951. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  33952. #else
  33953. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  33954. #endif
  33955. #ifdef HAVE_CURVE448
  33956. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  33957. #else
  33958. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  33959. #endif
  33960. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  33961. #ifdef HAVE_ECC
  33962. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  33963. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  33964. #endif
  33965. #ifdef HAVE_CURVE25519
  33966. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  33967. #else
  33968. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  33969. #endif
  33970. #ifdef HAVE_CURVE448
  33971. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  33972. #else
  33973. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  33974. #endif
  33975. SSL_free(ssl);
  33976. SSL_CTX_free(ctx);
  33977. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33978. #endif
  33979. return EXPECT_RESULT();
  33980. }
  33981. static int test_wolfSSL_set1_sigalgs_list(void)
  33982. {
  33983. EXPECT_DECLS;
  33984. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  33985. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33986. SSL* ssl = NULL;
  33987. SSL_CTX* ctx = NULL;
  33988. #ifndef NO_WOLFSSL_SERVER
  33989. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33990. #else
  33991. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33992. #endif
  33993. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33994. SSL_FILETYPE_PEM));
  33995. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33996. ExpectNotNull(ssl = SSL_new(ctx));
  33997. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33998. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  33999. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34000. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  34001. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  34002. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  34003. #ifndef NO_RSA
  34004. #ifndef NO_SHA256
  34005. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34006. WOLFSSL_FAILURE);
  34007. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34008. WOLFSSL_FAILURE);
  34009. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  34010. WOLFSSL_SUCCESS);
  34011. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  34012. WOLFSSL_SUCCESS);
  34013. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  34014. WOLFSSL_FAILURE);
  34015. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  34016. WOLFSSL_FAILURE);
  34017. #ifdef WC_RSA_PSS
  34018. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  34019. WOLFSSL_SUCCESS);
  34020. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  34021. WOLFSSL_SUCCESS);
  34022. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  34023. WOLFSSL_SUCCESS);
  34024. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  34025. WOLFSSL_SUCCESS);
  34026. #endif
  34027. #ifdef WOLFSSL_SHA512
  34028. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34029. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34030. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34031. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34032. #elif defined(WOLFSSL_SHA384)
  34033. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34034. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34035. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34036. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34037. #endif
  34038. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  34039. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  34040. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  34041. WOLFSSL_FAILURE);
  34042. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  34043. WOLFSSL_FAILURE);
  34044. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  34045. WOLFSSL_FAILURE);
  34046. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  34047. WOLFSSL_FAILURE);
  34048. #endif
  34049. #endif
  34050. #ifdef HAVE_ECC
  34051. #ifndef NO_SHA256
  34052. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  34053. WOLFSSL_SUCCESS);
  34054. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  34055. WOLFSSL_SUCCESS);
  34056. #ifdef WOLFSSL_SHA512
  34057. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34058. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34059. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34060. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34061. #elif defined(WOLFSSL_SHA384)
  34062. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34063. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34064. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34065. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34066. #endif
  34067. #endif
  34068. #endif
  34069. #ifdef HAVE_ED25519
  34070. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  34071. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  34072. #endif
  34073. #ifdef HAVE_ED448
  34074. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  34075. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  34076. #endif
  34077. #ifndef NO_DSA
  34078. #ifndef NO_SHA256
  34079. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  34080. WOLFSSL_SUCCESS);
  34081. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  34082. WOLFSSL_SUCCESS);
  34083. #endif
  34084. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  34085. defined(WOLFSSL_ALLOW_TLS_SHA1))
  34086. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  34087. WOLFSSL_SUCCESS);
  34088. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  34089. WOLFSSL_SUCCESS);
  34090. #endif
  34091. #endif
  34092. SSL_free(ssl);
  34093. SSL_CTX_free(ctx);
  34094. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34095. #endif
  34096. return EXPECT_RESULT();
  34097. }
  34098. /* Testing wolfSSL_set_tlsext_status_type function.
  34099. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  34100. */
  34101. static int test_wolfSSL_set_tlsext_status_type(void)
  34102. {
  34103. EXPECT_DECLS;
  34104. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  34105. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  34106. SSL* ssl = NULL;
  34107. SSL_CTX* ctx = NULL;
  34108. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34109. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34110. SSL_FILETYPE_PEM));
  34111. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34112. ExpectNotNull(ssl = SSL_new(ctx));
  34113. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  34114. SSL_SUCCESS);
  34115. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  34116. SSL_free(ssl);
  34117. SSL_CTX_free(ctx);
  34118. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  34119. return EXPECT_RESULT();
  34120. }
  34121. #ifndef NO_BIO
  34122. static int test_wolfSSL_PEM_read_bio(void)
  34123. {
  34124. EXPECT_DECLS;
  34125. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34126. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34127. byte buff[6000];
  34128. XFILE f = XBADFILE;
  34129. int bytes;
  34130. X509* x509 = NULL;
  34131. BIO* bio = NULL;
  34132. BUF_MEM* buf = NULL;
  34133. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  34134. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  34135. if (f != XBADFILE)
  34136. XFCLOSE(f);
  34137. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34138. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  34139. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  34140. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34141. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  34142. /* BIO should return the set EOF value */
  34143. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  34144. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  34145. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  34146. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  34147. BIO_free(bio);
  34148. BUF_MEM_free(buf);
  34149. X509_free(x509);
  34150. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34151. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34152. return EXPECT_RESULT();
  34153. }
  34154. #if defined(OPENSSL_EXTRA)
  34155. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  34156. long argl, long ret)
  34157. {
  34158. (void)bio;
  34159. (void)cmd;
  34160. (void)argp;
  34161. (void)argi;
  34162. (void)argl;
  34163. return ret;
  34164. }
  34165. #endif
  34166. static int test_wolfSSL_BIO(void)
  34167. {
  34168. EXPECT_DECLS;
  34169. #if defined(OPENSSL_EXTRA)
  34170. const unsigned char* p = NULL;
  34171. byte buff[20];
  34172. BIO* bio1 = NULL;
  34173. BIO* bio2 = NULL;
  34174. BIO* bio3 = NULL;
  34175. char* bufPt = NULL;
  34176. int i;
  34177. for (i = 0; i < 20; i++) {
  34178. buff[i] = i;
  34179. }
  34180. /* test BIO_free with NULL */
  34181. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  34182. /* Creating and testing type BIO_s_bio */
  34183. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34184. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  34185. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  34186. /* read/write before set up */
  34187. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34188. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34189. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  34190. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  34191. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  34192. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  34193. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  34194. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  34195. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  34196. /* write buffer full */
  34197. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  34198. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  34199. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  34200. /* write the other direction with pair */
  34201. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  34202. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  34203. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  34204. /* try read */
  34205. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  34206. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  34207. /* try read using ctrl function */
  34208. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  34209. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  34210. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  34211. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  34212. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  34213. for (i = 0; i < 20; i++) {
  34214. ExpectIntEQ((int)bufPt[i], i);
  34215. }
  34216. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34217. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  34218. for (i = 0; i < 8; i++) {
  34219. ExpectIntEQ((int)bufPt[i], i);
  34220. }
  34221. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34222. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  34223. /* new pair */
  34224. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  34225. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  34226. bio2 = NULL;
  34227. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  34228. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34229. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  34230. /* test wrap around... */
  34231. ExpectIntEQ(BIO_reset(bio1), 0);
  34232. ExpectIntEQ(BIO_reset(bio3), 0);
  34233. /* fill write buffer, read only small amount then write again */
  34234. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34235. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34236. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  34237. for (i = 0; i < 4; i++) {
  34238. ExpectIntEQ(bufPt[i], i);
  34239. }
  34240. /* try writing over read index */
  34241. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  34242. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  34243. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  34244. /* read and write 0 bytes */
  34245. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34246. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  34247. /* should read only to end of write buffer then need to read again */
  34248. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  34249. for (i = 0; i < 16; i++) {
  34250. ExpectIntEQ(bufPt[i], buff[4 + i]);
  34251. }
  34252. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  34253. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  34254. for (i = 0; i < 4; i++) {
  34255. ExpectIntEQ(bufPt[i], 0);
  34256. }
  34257. /* read index should not have advanced with nread0 */
  34258. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  34259. for (i = 0; i < 4; i++) {
  34260. ExpectIntEQ(bufPt[i], 0);
  34261. }
  34262. /* write and fill up buffer checking reset of index state */
  34263. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34264. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34265. /* test reset on data in bio1 write buffer */
  34266. ExpectIntEQ(BIO_reset(bio1), 0);
  34267. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34268. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  34269. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34270. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  34271. ExpectNotNull(p);
  34272. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34273. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  34274. for (i = 0; i < 6; i++) {
  34275. ExpectIntEQ(bufPt[i], i);
  34276. }
  34277. /* test case of writing twice with offset read index */
  34278. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  34279. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  34280. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34281. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34282. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34283. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  34284. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  34285. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34286. BIO_free(bio1);
  34287. bio1 = NULL;
  34288. BIO_free(bio3);
  34289. bio3 = NULL;
  34290. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  34291. {
  34292. BIO* bioA = NULL;
  34293. BIO* bioB = NULL;
  34294. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  34295. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  34296. BIO_free(bioA);
  34297. bioA = NULL;
  34298. BIO_free(bioB);
  34299. bioB = NULL;
  34300. }
  34301. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  34302. /* BIOs with file pointers */
  34303. #if !defined(NO_FILESYSTEM)
  34304. {
  34305. XFILE f1 = XBADFILE;
  34306. XFILE f2 = XBADFILE;
  34307. BIO* f_bio1 = NULL;
  34308. BIO* f_bio2 = NULL;
  34309. unsigned char cert[300];
  34310. char testFile[] = "tests/bio_write_test.txt";
  34311. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  34312. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  34313. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  34314. /* Failure due to wrong BIO type */
  34315. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34316. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  34317. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  34318. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  34319. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  34320. WOLFSSL_SUCCESS);
  34321. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34322. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  34323. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  34324. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  34325. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  34326. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  34327. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  34328. ExpectIntEQ(BIO_reset(f_bio2), 0);
  34329. ExpectIntEQ(BIO_tell(NULL),-1);
  34330. ExpectIntEQ(BIO_tell(f_bio2),0);
  34331. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  34332. ExpectIntEQ(BIO_tell(f_bio2),4);
  34333. BIO_free(f_bio1);
  34334. f_bio1 = NULL;
  34335. BIO_free(f_bio2);
  34336. f_bio2 = NULL;
  34337. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  34338. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34339. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34340. BIO_free(f_bio1);
  34341. f_bio1 = NULL;
  34342. }
  34343. #endif /* !defined(NO_FILESYSTEM) */
  34344. /* BIO info callback */
  34345. {
  34346. const char* testArg = "test";
  34347. BIO* cb_bio = NULL;
  34348. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  34349. BIO_set_callback(cb_bio, bioCallback);
  34350. ExpectNotNull(BIO_get_callback(cb_bio));
  34351. BIO_set_callback(cb_bio, NULL);
  34352. ExpectNull(BIO_get_callback(cb_bio));
  34353. BIO_set_callback_arg(cb_bio, (char*)testArg);
  34354. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  34355. ExpectNull(BIO_get_callback_arg(NULL));
  34356. BIO_free(cb_bio);
  34357. cb_bio = NULL;
  34358. }
  34359. /* BIO_vfree */
  34360. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34361. BIO_vfree(NULL);
  34362. BIO_vfree(bio1);
  34363. #endif
  34364. return EXPECT_RESULT();
  34365. }
  34366. #endif /* !NO_BIO */
  34367. static int test_wolfSSL_a2i_IPADDRESS(void)
  34368. {
  34369. EXPECT_DECLS;
  34370. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  34371. const unsigned char* data = NULL;
  34372. int dataSz = 0;
  34373. ASN1_OCTET_STRING *st = NULL;
  34374. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  34375. const unsigned char ipv6_exp[] = {
  34376. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  34377. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  34378. };
  34379. const unsigned char ipv6_home[] = {
  34380. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34381. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  34382. };
  34383. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  34384. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  34385. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34386. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  34387. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  34388. ASN1_STRING_free(st);
  34389. st = NULL;
  34390. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  34391. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34392. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34393. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  34394. ASN1_STRING_free(st);
  34395. st = NULL;
  34396. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  34397. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34398. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34399. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  34400. ASN1_STRING_free(st);
  34401. #endif
  34402. return EXPECT_RESULT();
  34403. }
  34404. static int test_wolfSSL_X509_cmp_time(void)
  34405. {
  34406. EXPECT_DECLS;
  34407. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  34408. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  34409. WOLFSSL_ASN1_TIME asn_time;
  34410. time_t t;
  34411. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  34412. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  34413. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  34414. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  34415. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  34416. #endif
  34417. return EXPECT_RESULT();
  34418. }
  34419. static int test_wolfSSL_X509_time_adj(void)
  34420. {
  34421. EXPECT_DECLS;
  34422. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  34423. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  34424. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  34425. !defined(NO_ASN_TIME)
  34426. X509* x509 = NULL;
  34427. time_t t;
  34428. time_t not_before;
  34429. time_t not_after;
  34430. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  34431. client_cert_der_2048, sizeof_client_cert_der_2048,
  34432. WOLFSSL_FILETYPE_ASN1));
  34433. t = 0;
  34434. not_before = wc_Time(0);
  34435. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  34436. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  34437. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  34438. /* Check X509_gmtime_adj, too. */
  34439. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  34440. X509_free(x509);
  34441. #endif
  34442. return EXPECT_RESULT();
  34443. }
  34444. static int test_wolfSSL_X509(void)
  34445. {
  34446. EXPECT_DECLS;
  34447. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34448. !defined(NO_RSA)
  34449. X509* x509 = NULL;
  34450. #ifndef NO_BIO
  34451. BIO* bio = NULL;
  34452. X509_STORE_CTX* ctx = NULL;
  34453. X509_STORE* store = NULL;
  34454. #endif
  34455. char der[] = "certs/ca-cert.der";
  34456. XFILE fp = XBADFILE;
  34457. ExpectNotNull(x509 = X509_new());
  34458. X509_free(x509);
  34459. x509 = NULL;
  34460. #ifndef NO_BIO
  34461. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34462. SSL_FILETYPE_PEM));
  34463. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34464. #ifdef WOLFSSL_CERT_GEN
  34465. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  34466. #endif
  34467. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34468. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  34469. ExpectNotNull(store = X509_STORE_new());
  34470. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  34471. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  34472. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  34473. X509_STORE_CTX_free(ctx);
  34474. X509_STORE_free(store);
  34475. X509_free(x509);
  34476. x509 = NULL;
  34477. BIO_free(bio);
  34478. #endif
  34479. /** d2i_X509_fp test **/
  34480. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34481. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  34482. ExpectNotNull(x509);
  34483. X509_free(x509);
  34484. x509 = NULL;
  34485. if (fp != XBADFILE) {
  34486. XFCLOSE(fp);
  34487. fp = XBADFILE;
  34488. }
  34489. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34490. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  34491. ExpectNotNull(x509);
  34492. X509_free(x509);
  34493. if (fp != XBADFILE)
  34494. XFCLOSE(fp);
  34495. /* X509_up_ref test */
  34496. ExpectIntEQ(X509_up_ref(NULL), 0);
  34497. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  34498. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  34499. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  34500. X509_free(x509); /* refCount = 2 */
  34501. X509_free(x509); /* refCount = 1 */
  34502. X509_free(x509); /* refCount = 0, free */
  34503. #endif
  34504. return EXPECT_RESULT();
  34505. }
  34506. static int test_wolfSSL_X509_get_ext_count(void)
  34507. {
  34508. EXPECT_DECLS;
  34509. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34510. !defined(NO_RSA)
  34511. int ret = 0;
  34512. WOLFSSL_X509* x509 = NULL;
  34513. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  34514. XFILE f = XBADFILE;
  34515. /* NULL parameter check */
  34516. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  34517. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34518. SSL_FILETYPE_PEM));
  34519. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34520. wolfSSL_X509_free(x509);
  34521. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  34522. SSL_FILETYPE_PEM));
  34523. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34524. wolfSSL_X509_free(x509);
  34525. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  34526. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  34527. if (f != XBADFILE)
  34528. XFCLOSE(f);
  34529. /* wolfSSL_X509_get_ext_count() valid input */
  34530. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  34531. /* wolfSSL_X509_get_ext_count() NULL argument */
  34532. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  34533. wolfSSL_X509_free(x509);
  34534. #endif
  34535. return EXPECT_RESULT();
  34536. }
  34537. static int test_wolfSSL_X509_sign2(void)
  34538. {
  34539. EXPECT_DECLS;
  34540. /* test requires WOLFSSL_AKID_NAME to match expected output */
  34541. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34542. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  34543. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  34544. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  34545. defined(WOLFSSL_IP_ALT_NAME))
  34546. WOLFSSL_X509 *x509 = NULL;
  34547. WOLFSSL_X509 *ca = NULL;
  34548. const unsigned char *der = NULL;
  34549. const unsigned char *pt = NULL;
  34550. WOLFSSL_EVP_PKEY *priv = NULL;
  34551. WOLFSSL_X509_NAME *name = NULL;
  34552. int derSz;
  34553. #ifndef NO_ASN_TIME
  34554. WOLFSSL_ASN1_TIME *notBefore = NULL;
  34555. WOLFSSL_ASN1_TIME *notAfter = NULL;
  34556. const int year = 365*24*60*60;
  34557. const int day = 24*60*60;
  34558. const int hour = 60*60;
  34559. const int mini = 60;
  34560. time_t t;
  34561. #endif
  34562. const unsigned char expected[] = {
  34563. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  34564. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  34565. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  34566. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  34567. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  34568. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  34569. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  34570. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  34571. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  34572. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  34573. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  34574. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  34575. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  34576. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  34577. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34578. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  34579. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  34580. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  34581. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  34582. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  34583. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  34584. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  34585. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  34586. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  34587. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  34588. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  34589. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  34590. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  34591. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  34592. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  34593. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  34594. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  34595. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  34596. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34597. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  34598. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  34599. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  34600. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  34601. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  34602. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  34603. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  34604. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  34605. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  34606. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  34607. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  34608. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  34609. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  34610. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  34611. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  34612. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  34613. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  34614. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  34615. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  34616. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  34617. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  34618. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  34619. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  34620. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  34621. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  34622. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  34623. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  34624. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  34625. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34626. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34627. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  34628. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34629. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34630. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  34631. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  34632. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  34633. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  34634. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  34635. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  34636. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  34637. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  34638. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  34639. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  34640. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34641. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  34642. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  34643. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34644. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  34645. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  34646. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  34647. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  34648. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  34649. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  34650. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  34651. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  34652. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  34653. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  34654. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  34655. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  34656. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  34657. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  34658. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  34659. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  34660. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  34661. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  34662. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  34663. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  34664. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  34665. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  34666. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  34667. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  34668. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  34669. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  34670. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  34671. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  34672. };
  34673. pt = ca_key_der_2048;
  34674. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  34675. sizeof_ca_key_der_2048));
  34676. pt = client_cert_der_2048;
  34677. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  34678. sizeof_client_cert_der_2048));
  34679. pt = ca_cert_der_2048;
  34680. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  34681. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  34682. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34683. #ifndef NO_ASN_TIME
  34684. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  34685. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  34686. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  34687. ExpectIntEQ(notAfter->length, 13);
  34688. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  34689. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  34690. #endif
  34691. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  34692. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  34693. ExpectIntEQ(derSz, sizeof(expected));
  34694. #ifndef NO_ASN_TIME
  34695. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  34696. #endif
  34697. wolfSSL_X509_free(ca);
  34698. wolfSSL_X509_free(x509);
  34699. wolfSSL_EVP_PKEY_free(priv);
  34700. #ifndef NO_ASN_TIME
  34701. wolfSSL_ASN1_TIME_free(notBefore);
  34702. wolfSSL_ASN1_TIME_free(notAfter);
  34703. #endif
  34704. #endif
  34705. return EXPECT_RESULT();
  34706. }
  34707. static int test_wolfSSL_X509_sign(void)
  34708. {
  34709. EXPECT_DECLS;
  34710. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  34711. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  34712. int ret;
  34713. char *cn = NULL;
  34714. word32 cnSz;
  34715. X509_NAME *name = NULL;
  34716. X509 *x509 = NULL;
  34717. X509 *ca = NULL;
  34718. DecodedCert dCert;
  34719. EVP_PKEY *pub = NULL;
  34720. EVP_PKEY *priv = NULL;
  34721. EVP_MD_CTX *mctx = NULL;
  34722. #if defined(USE_CERT_BUFFERS_1024)
  34723. const unsigned char* rsaPriv = client_key_der_1024;
  34724. const unsigned char* rsaPub = client_keypub_der_1024;
  34725. const unsigned char* certIssuer = client_cert_der_1024;
  34726. long clientKeySz = (long)sizeof_client_key_der_1024;
  34727. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  34728. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  34729. #elif defined(USE_CERT_BUFFERS_2048)
  34730. const unsigned char* rsaPriv = client_key_der_2048;
  34731. const unsigned char* rsaPub = client_keypub_der_2048;
  34732. const unsigned char* certIssuer = client_cert_der_2048;
  34733. long clientKeySz = (long)sizeof_client_key_der_2048;
  34734. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  34735. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  34736. #endif
  34737. byte sn[16];
  34738. int snSz = sizeof(sn);
  34739. /* Set X509_NAME fields */
  34740. ExpectNotNull(name = X509_NAME_new());
  34741. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  34742. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  34743. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34744. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  34745. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34746. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  34747. /* Get private and public keys */
  34748. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  34749. clientKeySz));
  34750. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  34751. ExpectNotNull(x509 = X509_new());
  34752. /* Set version 3 */
  34753. ExpectIntNE(X509_set_version(x509, 2L), 0);
  34754. /* Set subject name, add pubkey, and sign certificate */
  34755. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  34756. X509_NAME_free(name);
  34757. name = NULL;
  34758. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  34759. #ifdef WOLFSSL_ALT_NAMES
  34760. /* Add some subject alt names */
  34761. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  34762. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  34763. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34764. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  34765. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34766. "sphygmomanometer",
  34767. ASN_DNS_TYPE), SSL_SUCCESS);
  34768. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34769. "supercalifragilisticexpialidocious",
  34770. ASN_DNS_TYPE), SSL_SUCCESS);
  34771. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34772. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  34773. ASN_DNS_TYPE), SSL_SUCCESS);
  34774. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34775. {
  34776. unsigned char ip4_type[] = {127,128,0,255};
  34777. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  34778. 0xff, 0xee, 0x99, 0x88,
  34779. 0x77, 0x66, 0x55, 0x44,
  34780. 0x00, 0x33, 0x22, 0x11};
  34781. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  34782. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  34783. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  34784. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  34785. }
  34786. #endif
  34787. #endif /* WOLFSSL_ALT_NAMES */
  34788. /* test valid sign case */
  34789. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  34790. /* test valid X509_sign_ctx case */
  34791. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34792. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  34793. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  34794. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  34795. ExpectIntEQ(X509_get_ext_count(x509), 1);
  34796. #endif
  34797. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  34798. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  34799. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  34800. #endif
  34801. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  34802. WOLFSSL_SUCCESS);
  34803. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  34804. /* Variation in size depends on ASN.1 encoding when MSB is set.
  34805. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  34806. * with the MSB set. See GenerateInteger in asn.c */
  34807. #ifndef USE_CERT_BUFFERS_1024
  34808. #ifndef WOLFSSL_ALT_NAMES
  34809. /* Valid case - size should be 781-786 with 16 byte serial number */
  34810. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  34811. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34812. /* Valid case - size should be 955-960 with 16 byte serial number */
  34813. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  34814. #else
  34815. /* Valid case - size should be 926-931 with 16 byte serial number */
  34816. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  34817. #endif
  34818. #else
  34819. #ifndef WOLFSSL_ALT_NAMES
  34820. /* Valid case - size should be 537-542 with 16 byte serial number */
  34821. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  34822. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34823. /* Valid case - size should be 695-670 with 16 byte serial number */
  34824. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  34825. #else
  34826. /* Valid case - size should be 666-671 with 16 byte serial number */
  34827. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  34828. #endif
  34829. #endif
  34830. /* check that issuer name is as expected after signature */
  34831. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  34832. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  34833. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  34834. ExpectNotNull(name = X509_get_subject_name(ca));
  34835. cnSz = X509_NAME_get_sz(name);
  34836. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34837. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34838. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  34839. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34840. cn = NULL;
  34841. #ifdef WOLFSSL_MULTI_ATTRIB
  34842. /* test adding multiple OU's to the signer */
  34843. ExpectNotNull(name = X509_get_subject_name(ca));
  34844. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34845. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  34846. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34847. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  34848. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  34849. #endif
  34850. ExpectNotNull(name = X509_get_subject_name(ca));
  34851. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  34852. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34853. ExpectNotNull(name = X509_get_issuer_name(x509));
  34854. cnSz = X509_NAME_get_sz(name);
  34855. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34856. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34857. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  34858. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  34859. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34860. cn = NULL;
  34861. FreeDecodedCert(&dCert);
  34862. /* Test invalid parameters */
  34863. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  34864. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  34865. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  34866. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  34867. EVP_MD_CTX_free(mctx);
  34868. mctx = NULL;
  34869. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34870. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  34871. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  34872. /* test invalid version number */
  34873. #if defined(OPENSSL_ALL)
  34874. ExpectIntNE(X509_set_version(x509, 6L), 0);
  34875. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34876. /* uses ParseCert which fails on bad version number */
  34877. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  34878. #endif
  34879. EVP_MD_CTX_free(mctx);
  34880. EVP_PKEY_free(priv);
  34881. EVP_PKEY_free(pub);
  34882. X509_free(x509);
  34883. X509_free(ca);
  34884. #endif
  34885. return EXPECT_RESULT();
  34886. }
  34887. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  34888. {
  34889. EXPECT_DECLS;
  34890. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34891. X509* x509 = NULL;
  34892. const X509_ALGOR* alg;
  34893. ExpectNotNull(x509 = X509_new());
  34894. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  34895. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34896. X509_free(x509);
  34897. #endif
  34898. return EXPECT_RESULT();
  34899. }
  34900. static int test_wolfSSL_X509_ALGOR_get0(void)
  34901. {
  34902. EXPECT_DECLS;
  34903. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34904. !defined(NO_SHA256) && !defined(NO_RSA)
  34905. X509* x509 = NULL;
  34906. const ASN1_OBJECT* obj = NULL;
  34907. const X509_ALGOR* alg = NULL;
  34908. int pptype = 0;
  34909. const void *ppval = NULL;
  34910. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34911. SSL_FILETYPE_PEM));
  34912. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34913. /* Invalid case */
  34914. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  34915. ExpectNull(obj);
  34916. /* Valid case */
  34917. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  34918. ExpectNotNull(obj);
  34919. ExpectNull(ppval);
  34920. ExpectIntNE(pptype, 0);
  34921. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  34922. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  34923. X509_free(x509);
  34924. #endif
  34925. return EXPECT_RESULT();
  34926. }
  34927. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  34928. {
  34929. EXPECT_DECLS;
  34930. #if defined(OPENSSL_EXTRA)
  34931. X509_VERIFY_PARAM *paramTo = NULL;
  34932. X509_VERIFY_PARAM *paramFrom = NULL;
  34933. char testIPv4[] = "127.0.0.1";
  34934. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  34935. char testhostName1[] = "foo.hoge.com";
  34936. char testhostName2[] = "foobar.hoge.com";
  34937. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  34938. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  34939. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  34940. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  34941. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  34942. (int)XSTRLEN(testhostName1)), 1);
  34943. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  34944. (int)XSTRLEN(testhostName1)));
  34945. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  34946. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  34947. ExpectIntEQ(0x01, paramFrom->hostFlags);
  34948. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  34949. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  34950. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34951. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  34952. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  34953. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34954. /* null pointer */
  34955. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  34956. /* in the case of "from" null, returns success */
  34957. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  34958. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  34959. /* inherit flags test : VPARAM_DEFAULT */
  34960. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34961. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34962. (int)XSTRLEN(testhostName1)));
  34963. ExpectIntEQ(0x01, paramTo->hostFlags);
  34964. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34965. /* inherit flags test : VPARAM OVERWRITE */
  34966. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34967. (int)XSTRLEN(testhostName2)), 1);
  34968. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34969. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34970. if (paramTo != NULL) {
  34971. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  34972. }
  34973. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34974. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34975. (int)XSTRLEN(testhostName1)));
  34976. ExpectIntEQ(0x01, paramTo->hostFlags);
  34977. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34978. /* inherit flags test : VPARAM_RESET_FLAGS */
  34979. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34980. (int)XSTRLEN(testhostName2)), 1);
  34981. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34982. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  34983. if (paramTo != NULL) {
  34984. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  34985. }
  34986. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34987. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34988. (int)XSTRLEN(testhostName1)));
  34989. ExpectIntEQ(0x01, paramTo->hostFlags);
  34990. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34991. /* inherit flags test : VPARAM_LOCKED */
  34992. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34993. (int)XSTRLEN(testhostName2)), 1);
  34994. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34995. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34996. if (paramTo != NULL) {
  34997. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  34998. }
  34999. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35000. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  35001. (int)XSTRLEN(testhostName2)));
  35002. ExpectIntEQ(0x00, paramTo->hostFlags);
  35003. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35004. /* test for incorrect parameters */
  35005. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  35006. 0);
  35007. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  35008. /* inherit flags test : VPARAM_ONCE, not testable yet */
  35009. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  35010. 1);
  35011. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  35012. X509_V_FLAG_CRL_CHECK_ALL);
  35013. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  35014. X509_V_FLAG_CRL_CHECK_ALL), 1);
  35015. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  35016. X509_VERIFY_PARAM_free(paramTo);
  35017. X509_VERIFY_PARAM_free(paramFrom);
  35018. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  35019. #endif
  35020. return EXPECT_RESULT();
  35021. }
  35022. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35023. static int test_wolfSSL_check_domain_verify_count = 0;
  35024. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  35025. WOLFSSL_X509_STORE_CTX* store)
  35026. {
  35027. EXPECT_DECLS;
  35028. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  35029. ExpectIntEQ(preverify, 1);
  35030. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  35031. return EXPECT_SUCCESS();
  35032. }
  35033. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  35034. {
  35035. EXPECT_DECLS;
  35036. X509_VERIFY_PARAM *param = NULL;
  35037. ExpectNotNull(param = SSL_get0_param(ssl));
  35038. /* Domain check should only be done on the leaf cert */
  35039. X509_VERIFY_PARAM_set_hostflags(param,
  35040. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  35041. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  35042. "wolfSSL Server Chain", 0), 1);
  35043. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  35044. test_wolfSSL_check_domain_verify_cb);
  35045. return EXPECT_RESULT();
  35046. }
  35047. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  35048. {
  35049. EXPECT_DECLS;
  35050. /* Use a cert with different domains in chain */
  35051. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  35052. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  35053. return EXPECT_RESULT();
  35054. }
  35055. static int test_wolfSSL_check_domain(void)
  35056. {
  35057. EXPECT_DECLS;
  35058. test_ssl_cbf func_cb_client;
  35059. test_ssl_cbf func_cb_server;
  35060. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35061. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35062. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  35063. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  35064. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35065. &func_cb_server, NULL), TEST_SUCCESS);
  35066. /* Should have been called once for each cert in sent chain */
  35067. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  35068. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  35069. #else
  35070. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  35071. #endif
  35072. return EXPECT_RESULT();
  35073. }
  35074. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  35075. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  35076. {
  35077. EXPECT_DECLS;
  35078. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35079. X509* x509 = NULL;
  35080. X509_PUBKEY* pubKey;
  35081. ExpectNotNull(x509 = X509_new());
  35082. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  35083. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  35084. X509_free(x509);
  35085. #endif
  35086. return EXPECT_RESULT();
  35087. }
  35088. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  35089. {
  35090. EXPECT_DECLS;
  35091. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35092. !defined(NO_SHA256) && !defined(NO_RSA)
  35093. X509* x509 = NULL;
  35094. ASN1_OBJECT* obj = NULL;
  35095. const ASN1_OBJECT* pa_oid = NULL;
  35096. X509_PUBKEY* pubKey = NULL;
  35097. X509_PUBKEY* pubKey2 = NULL;
  35098. EVP_PKEY* evpKey = NULL;
  35099. const unsigned char *pk = NULL;
  35100. int ppklen;
  35101. int pptype;
  35102. X509_ALGOR *pa = NULL;
  35103. const void *pval;
  35104. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  35105. SSL_FILETYPE_PEM));
  35106. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  35107. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35108. ExpectNotNull(pk);
  35109. ExpectNotNull(pa);
  35110. ExpectNotNull(pubKey);
  35111. ExpectIntGT(ppklen, 0);
  35112. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  35113. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  35114. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  35115. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  35116. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  35117. ExpectNotNull(pk);
  35118. ExpectNotNull(pa);
  35119. ExpectIntGT(ppklen, 0);
  35120. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35121. ExpectNotNull(pa_oid);
  35122. ExpectNull(pval);
  35123. ExpectIntEQ(pptype, V_ASN1_NULL);
  35124. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  35125. X509_PUBKEY_free(pubKey2);
  35126. X509_free(x509);
  35127. EVP_PKEY_free(evpKey);
  35128. #endif
  35129. return EXPECT_RESULT();
  35130. }
  35131. static int test_wolfSSL_X509_PUBKEY_EC(void)
  35132. {
  35133. EXPECT_DECLS;
  35134. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  35135. X509* x509 = NULL;
  35136. ASN1_OBJECT* obj = NULL;
  35137. ASN1_OBJECT* poid = NULL;
  35138. const ASN1_OBJECT* pa_oid = NULL;
  35139. X509_PUBKEY* pubKey = NULL;
  35140. X509_PUBKEY* pubKey2 = NULL;
  35141. EVP_PKEY* evpKey = NULL;
  35142. const unsigned char *pk = NULL;
  35143. int ppklen;
  35144. int pptype;
  35145. X509_ALGOR *pa = NULL;
  35146. const void *pval;
  35147. char buf[50];
  35148. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  35149. SSL_FILETYPE_PEM));
  35150. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  35151. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  35152. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  35153. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  35154. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  35155. ExpectNotNull(pk);
  35156. ExpectNotNull(pa);
  35157. ExpectIntGT(ppklen, 0);
  35158. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35159. ExpectNotNull(pa_oid);
  35160. ExpectNotNull(pval);
  35161. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  35162. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  35163. poid = (ASN1_OBJECT *)pval;
  35164. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  35165. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  35166. X509_PUBKEY_free(pubKey2);
  35167. X509_free(x509);
  35168. EVP_PKEY_free(evpKey);
  35169. #endif
  35170. return EXPECT_RESULT();
  35171. }
  35172. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  35173. {
  35174. EXPECT_DECLS;
  35175. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  35176. word32 bytes;
  35177. #ifdef USE_CERT_BUFFERS_1024
  35178. byte tmp[ONEK_BUF];
  35179. #elif defined(USE_CERT_BUFFERS_2048)
  35180. byte tmp[TWOK_BUF];
  35181. #else
  35182. byte tmp[TWOK_BUF];
  35183. #endif /* END USE_CERT_BUFFERS_1024 */
  35184. const unsigned char* dsaKeyDer = tmp;
  35185. ASN1_OBJECT* obj = NULL;
  35186. ASN1_STRING* str;
  35187. const ASN1_OBJECT* pa_oid = NULL;
  35188. X509_PUBKEY* pubKey = NULL;
  35189. EVP_PKEY* evpKey = NULL;
  35190. const unsigned char *pk = NULL;
  35191. int ppklen, pptype;
  35192. X509_ALGOR *pa = NULL;
  35193. const void *pval;
  35194. #ifdef USE_CERT_BUFFERS_1024
  35195. XMEMSET(tmp, 0, sizeof(tmp));
  35196. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  35197. bytes = sizeof_dsa_key_der_1024;
  35198. #elif defined(USE_CERT_BUFFERS_2048)
  35199. XMEMSET(tmp, 0, sizeof(tmp));
  35200. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  35201. bytes = sizeof_dsa_key_der_2048;
  35202. #else
  35203. {
  35204. XFILE fp = XBADFILE;
  35205. XMEMSET(tmp, 0, sizeof(tmp));
  35206. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  35207. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  35208. if (fp != XBADFILE)
  35209. XFCLOSE(fp);
  35210. }
  35211. #endif
  35212. /* Initialize pkey with der format dsa key */
  35213. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  35214. ExpectNotNull(pubKey = X509_PUBKEY_new());
  35215. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  35216. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35217. ExpectNotNull(pk);
  35218. ExpectNotNull(pa);
  35219. ExpectIntGT(ppklen, 0);
  35220. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35221. ExpectNotNull(pa_oid);
  35222. ExpectNotNull(pval);
  35223. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  35224. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  35225. str = (ASN1_STRING *)pval;
  35226. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  35227. #ifdef USE_CERT_BUFFERS_1024
  35228. ExpectIntEQ(ASN1_STRING_length(str), 291);
  35229. #else
  35230. ExpectIntEQ(ASN1_STRING_length(str), 549);
  35231. #endif /* END USE_CERT_BUFFERS_1024 */
  35232. X509_PUBKEY_free(pubKey);
  35233. EVP_PKEY_free(evpKey);
  35234. #endif
  35235. return EXPECT_RESULT();
  35236. }
  35237. static int test_wolfSSL_BUF(void)
  35238. {
  35239. EXPECT_DECLS;
  35240. #if defined(OPENSSL_EXTRA)
  35241. BUF_MEM* buf = NULL;
  35242. ExpectNotNull(buf = BUF_MEM_new());
  35243. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  35244. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  35245. BUF_MEM_free(buf);
  35246. #endif
  35247. return EXPECT_RESULT();
  35248. }
  35249. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35250. static int stub_rand_seed(const void *buf, int num)
  35251. {
  35252. (void)buf;
  35253. (void)num;
  35254. return 123;
  35255. }
  35256. static int stub_rand_bytes(unsigned char *buf, int num)
  35257. {
  35258. (void)buf;
  35259. (void)num;
  35260. return 456;
  35261. }
  35262. static byte* was_stub_rand_cleanup_called(void)
  35263. {
  35264. static byte was_called = 0;
  35265. return &was_called;
  35266. }
  35267. static void stub_rand_cleanup(void)
  35268. {
  35269. byte* was_called = was_stub_rand_cleanup_called();
  35270. *was_called = 1;
  35271. return;
  35272. }
  35273. static byte* was_stub_rand_add_called(void)
  35274. {
  35275. static byte was_called = 0;
  35276. return &was_called;
  35277. }
  35278. static int stub_rand_add(const void *buf, int num, double entropy)
  35279. {
  35280. byte* was_called = was_stub_rand_add_called();
  35281. (void)buf;
  35282. (void)num;
  35283. (void)entropy;
  35284. *was_called = 1;
  35285. return 0;
  35286. }
  35287. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  35288. {
  35289. (void)buf;
  35290. (void)num;
  35291. return 9876;
  35292. }
  35293. static int stub_rand_status(void)
  35294. {
  35295. return 5432;
  35296. }
  35297. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35298. static int test_wolfSSL_RAND_set_rand_method(void)
  35299. {
  35300. EXPECT_DECLS;
  35301. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35302. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  35303. unsigned char* buf = NULL;
  35304. int num = 0;
  35305. double entropy = 0;
  35306. int ret;
  35307. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  35308. byte* was_add_called = was_stub_rand_add_called();
  35309. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  35310. DYNAMIC_TYPE_TMP_BUFFER));
  35311. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  35312. ExpectIntEQ(*was_cleanup_called, 0);
  35313. RAND_cleanup();
  35314. ExpectIntEQ(*was_cleanup_called, 0);
  35315. rand_methods.seed = &stub_rand_seed;
  35316. rand_methods.bytes = &stub_rand_bytes;
  35317. rand_methods.cleanup = &stub_rand_cleanup;
  35318. rand_methods.add = &stub_rand_add;
  35319. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  35320. rand_methods.status = &stub_rand_status;
  35321. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  35322. ExpectIntEQ(RAND_seed(buf, num), 123);
  35323. ExpectIntEQ(RAND_bytes(buf, num), 456);
  35324. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  35325. ExpectIntEQ(RAND_status(), 5432);
  35326. ExpectIntEQ(*was_add_called, 0);
  35327. /* The function pointer for RAND_add returns int, but RAND_add itself
  35328. * returns void. */
  35329. RAND_add(buf, num, entropy);
  35330. ExpectIntEQ(*was_add_called, 1);
  35331. was_add_called = 0;
  35332. ExpectIntEQ(*was_cleanup_called, 0);
  35333. RAND_cleanup();
  35334. ExpectIntEQ(*was_cleanup_called, 1);
  35335. *was_cleanup_called = 0;
  35336. ret = RAND_set_rand_method(NULL);
  35337. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  35338. ExpectIntNE(RAND_status(), 5432);
  35339. ExpectIntEQ(*was_cleanup_called, 0);
  35340. RAND_cleanup();
  35341. ExpectIntEQ(*was_cleanup_called, 0);
  35342. RAND_set_rand_method(NULL);
  35343. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35344. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35345. return EXPECT_RESULT();
  35346. }
  35347. static int test_wolfSSL_RAND_bytes(void)
  35348. {
  35349. EXPECT_DECLS;
  35350. #if defined(OPENSSL_EXTRA)
  35351. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  35352. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  35353. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  35354. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  35355. int max_bufsize;
  35356. byte *my_buf = NULL;
  35357. /* sanity check */
  35358. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  35359. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  35360. max_bufsize = size4;
  35361. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  35362. DYNAMIC_TYPE_TMP_BUFFER));
  35363. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  35364. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  35365. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  35366. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  35367. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  35368. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  35369. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  35370. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35371. #endif
  35372. return EXPECT_RESULT();
  35373. }
  35374. static int test_wolfSSL_RAND(void)
  35375. {
  35376. EXPECT_DECLS;
  35377. #if defined(OPENSSL_EXTRA)
  35378. byte seed[16];
  35379. XMEMSET(seed, 0, sizeof(seed));
  35380. /* No global methods set. */
  35381. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  35382. ExpectIntEQ(RAND_poll(), 1);
  35383. RAND_cleanup();
  35384. ExpectIntEQ(RAND_egd(NULL), -1);
  35385. #ifndef NO_FILESYSTEM
  35386. {
  35387. char fname[100];
  35388. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  35389. ExpectIntEQ(RAND_write_file(NULL), 0);
  35390. }
  35391. #endif
  35392. #endif
  35393. return EXPECT_RESULT();
  35394. }
  35395. static int test_wolfSSL_PKCS8_Compat(void)
  35396. {
  35397. EXPECT_DECLS;
  35398. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  35399. !defined(NO_BIO)
  35400. PKCS8_PRIV_KEY_INFO* pt = NULL;
  35401. BIO* bio = NULL;
  35402. XFILE f = XBADFILE;
  35403. int bytes;
  35404. char pkcs8_buffer[512];
  35405. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35406. EVP_PKEY *pkey = NULL;
  35407. #endif
  35408. /* file from wolfssl/certs/ directory */
  35409. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  35410. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  35411. 0);
  35412. if (f != XBADFILE)
  35413. XFCLOSE(f);
  35414. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35415. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  35416. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35417. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  35418. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  35419. /* gets PKCS8 pointer to pkey */
  35420. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  35421. EVP_PKEY_free(pkey);
  35422. #endif
  35423. BIO_free(bio);
  35424. PKCS8_PRIV_KEY_INFO_free(pt);
  35425. #endif
  35426. return EXPECT_RESULT();
  35427. }
  35428. static int test_wolfSSL_PKCS8_d2i(void)
  35429. {
  35430. EXPECT_DECLS;
  35431. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  35432. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  35433. * requires a 12 byte password in FIPS mode. This test ends up
  35434. * trying to use an 8 byte password. */
  35435. #ifndef NO_FILESYSTEM
  35436. unsigned char pkcs8_buffer[2048];
  35437. const unsigned char* p;
  35438. int bytes;
  35439. XFILE file = XBADFILE;
  35440. WOLFSSL_EVP_PKEY* pkey = NULL;
  35441. #ifndef NO_BIO
  35442. BIO* bio = NULL;
  35443. #if defined(OPENSSL_ALL) && \
  35444. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  35445. defined(HAVE_ECC)) && \
  35446. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35447. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  35448. #endif
  35449. #endif
  35450. #ifndef NO_RSA
  35451. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  35452. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  35453. #ifndef NO_DES3
  35454. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  35455. #endif
  35456. #endif /* NO_RSA */
  35457. #ifdef HAVE_ECC
  35458. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  35459. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  35460. #ifndef NO_DES3
  35461. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  35462. #endif
  35463. #endif /* HAVE_ECC */
  35464. #endif /* !NO_FILESYSTEM */
  35465. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  35466. #ifndef NO_RSA
  35467. #ifdef USE_CERT_BUFFERS_1024
  35468. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  35469. int rsaSz = sizeof_server_key_der_1024;
  35470. #else
  35471. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  35472. int rsaSz = sizeof_server_key_der_2048;
  35473. #endif
  35474. #endif
  35475. #ifdef HAVE_ECC
  35476. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  35477. int ecSz = sizeof_ecc_key_der_256;
  35478. #endif
  35479. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  35480. #ifndef NO_FILESYSTEM
  35481. (void)pkcs8_buffer;
  35482. (void)p;
  35483. (void)bytes;
  35484. (void)file;
  35485. #ifndef NO_BIO
  35486. (void)bio;
  35487. #endif
  35488. #endif
  35489. #ifdef OPENSSL_ALL
  35490. #ifndef NO_RSA
  35491. /* Try to auto-detect normal RSA private key */
  35492. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  35493. EVP_PKEY_free(pkey);
  35494. pkey = NULL;
  35495. #endif
  35496. #ifdef HAVE_ECC
  35497. /* Try to auto-detect normal EC private key */
  35498. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  35499. EVP_PKEY_free(pkey);
  35500. pkey = NULL;
  35501. #endif
  35502. #endif /* OPENSSL_ALL */
  35503. #ifndef NO_FILESYSTEM
  35504. #ifndef NO_RSA
  35505. /* Get DER encoded RSA PKCS#8 data. */
  35506. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  35507. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35508. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35509. file)), 0);
  35510. if (file != XBADFILE) {
  35511. XFCLOSE(file);
  35512. file = XBADFILE;
  35513. }
  35514. p = pkcs8_buffer;
  35515. #ifdef OPENSSL_ALL
  35516. /* Try to decode - auto-detect key type. */
  35517. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35518. #else
  35519. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  35520. #endif
  35521. /* Get PEM encoded RSA PKCS#8 data. */
  35522. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  35523. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35524. file)), 0);
  35525. if (file != XBADFILE) {
  35526. XFCLOSE(file);
  35527. file = XBADFILE;
  35528. }
  35529. #if defined(OPENSSL_ALL) && \
  35530. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35531. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35532. /* Write PKCS#8 PEM to BIO. */
  35533. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35534. NULL), bytes);
  35535. /* Compare file and written data */
  35536. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35537. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35538. BIO_free(bio);
  35539. bio = NULL;
  35540. #if !defined(NO_DES3) && !defined(NO_SHA)
  35541. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35542. /* Write Encrypted PKCS#8 PEM to BIO. */
  35543. bytes = 1834;
  35544. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  35545. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35546. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35547. (void*)"yassl123"));
  35548. EVP_PKEY_free(evpPkey);
  35549. evpPkey = NULL;
  35550. BIO_free(bio);
  35551. bio = NULL;
  35552. #endif /* !NO_DES3 && !NO_SHA */
  35553. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35554. EVP_PKEY_free(pkey);
  35555. pkey = NULL;
  35556. /* PKCS#8 encrypted RSA key */
  35557. #ifndef NO_DES3
  35558. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  35559. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35560. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35561. file)), 0);
  35562. if (file != XBADFILE) {
  35563. XFCLOSE(file);
  35564. file = XBADFILE;
  35565. }
  35566. #if defined(OPENSSL_ALL) && \
  35567. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35568. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35569. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35570. (void*)"yassl123"));
  35571. EVP_PKEY_free(pkey);
  35572. pkey = NULL;
  35573. BIO_free(bio);
  35574. bio = NULL;
  35575. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35576. #endif /* !NO_DES3 */
  35577. #endif /* NO_RSA */
  35578. #ifdef HAVE_ECC
  35579. /* PKCS#8 encode EC key */
  35580. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  35581. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35582. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35583. file)), 0);
  35584. if (file != XBADFILE) {
  35585. XFCLOSE(file);
  35586. file = XBADFILE;
  35587. }
  35588. p = pkcs8_buffer;
  35589. #ifdef OPENSSL_ALL
  35590. /* Try to decode - auto-detect key type. */
  35591. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35592. #else
  35593. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  35594. #endif
  35595. /* Get PEM encoded RSA PKCS#8 data. */
  35596. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  35597. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35598. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35599. file)), 0);
  35600. if (file != XBADFILE) {
  35601. XFCLOSE(file);
  35602. file = XBADFILE;
  35603. }
  35604. #if defined(OPENSSL_ALL) && \
  35605. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  35606. defined(HAVE_AES_CBC)
  35607. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35608. /* Write PKCS#8 PEM to BIO. */
  35609. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35610. NULL), bytes);
  35611. /* Compare file and written data */
  35612. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35613. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35614. BIO_free(bio);
  35615. bio = NULL;
  35616. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35617. /* Write Encrypted PKCS#8 PEM to BIO. */
  35618. bytes = 379;
  35619. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  35620. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35621. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35622. (void*)"yassl123"));
  35623. EVP_PKEY_free(evpPkey);
  35624. evpPkey = NULL;
  35625. BIO_free(bio);
  35626. bio = NULL;
  35627. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  35628. EVP_PKEY_free(pkey);
  35629. pkey = NULL;
  35630. /* PKCS#8 encrypted EC key */
  35631. #ifndef NO_DES3
  35632. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  35633. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35634. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35635. file)), 0);
  35636. if (file != XBADFILE) {
  35637. XFCLOSE(file);
  35638. file = XBADFILE;
  35639. }
  35640. #if defined(OPENSSL_ALL) && \
  35641. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35642. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35643. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35644. (void*)"yassl123"));
  35645. EVP_PKEY_free(pkey);
  35646. pkey = NULL;
  35647. BIO_free(bio);
  35648. bio = NULL;
  35649. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35650. #endif /* !NO_DES3 */
  35651. #endif /* HAVE_ECC */
  35652. #endif /* !NO_FILESYSTEM */
  35653. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  35654. return EXPECT_RESULT();
  35655. }
  35656. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35657. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35658. #define LOGGING_THREADS 5
  35659. #define ERROR_COUNT 10
  35660. /* copied from logging.c since this is not exposed otherwise */
  35661. #ifndef ERROR_QUEUE_MAX
  35662. #ifdef ERROR_QUEUE_PER_THREAD
  35663. #define ERROR_QUEUE_MAX 16
  35664. #else
  35665. /* this breaks from compat of unlimited error queue size */
  35666. #define ERROR_QUEUE_MAX 100
  35667. #endif
  35668. #endif
  35669. static volatile int loggingThreadsReady;
  35670. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  35671. {
  35672. const char* file;
  35673. int line;
  35674. unsigned long err;
  35675. int errorCount = 0;
  35676. int i;
  35677. (void)args;
  35678. while (!loggingThreadsReady);
  35679. for (i = 0; i < ERROR_COUNT; i++)
  35680. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35681. while ((err = ERR_get_error_line(&file, &line))) {
  35682. AssertIntEQ(err, 990 + errorCount);
  35683. errorCount++;
  35684. }
  35685. AssertIntEQ(errorCount, ERROR_COUNT);
  35686. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  35687. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  35688. errorCount = 0;
  35689. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  35690. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35691. while ((err = ERR_get_error_line(&file, &line))) {
  35692. AssertIntEQ(err, 990 + errorCount);
  35693. errorCount++;
  35694. }
  35695. /* test that the 3 errors over the max were dropped */
  35696. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  35697. WOLFSSL_RETURN_FROM_THREAD(0);
  35698. }
  35699. #endif
  35700. static int test_error_queue_per_thread(void)
  35701. {
  35702. int res = TEST_SKIPPED;
  35703. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35704. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35705. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  35706. int i;
  35707. ERR_clear_error(); /* clear out any error nodes */
  35708. loggingThreadsReady = 0;
  35709. for (i = 0; i < LOGGING_THREADS; i++)
  35710. start_thread(test_logging, NULL, &loggingThreads[i]);
  35711. loggingThreadsReady = 1;
  35712. for (i = 0; i < LOGGING_THREADS; i++)
  35713. join_thread(loggingThreads[i]);
  35714. res = TEST_SUCCESS;
  35715. #endif
  35716. return res;
  35717. }
  35718. static int test_wolfSSL_ERR_put_error(void)
  35719. {
  35720. EXPECT_DECLS;
  35721. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35722. defined(DEBUG_WOLFSSL)
  35723. const char* file;
  35724. int line;
  35725. ERR_clear_error(); /* clear out any error nodes */
  35726. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35727. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35728. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  35729. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  35730. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  35731. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  35732. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  35733. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  35734. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  35735. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  35736. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  35737. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  35738. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  35739. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  35740. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  35741. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  35742. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  35743. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  35744. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  35745. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  35746. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  35747. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  35748. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  35749. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  35750. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  35751. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  35752. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  35753. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  35754. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  35755. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  35756. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  35757. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  35758. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  35759. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  35760. "this file", 100);
  35761. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  35762. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35763. ExpectIntEQ(line, 100);
  35764. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  35765. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35766. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  35767. #endif
  35768. /* try reading past end of error queue */
  35769. file = NULL;
  35770. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35771. ExpectNull(file);
  35772. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  35773. PEMerr(4,4);
  35774. ExpectIntEQ(ERR_get_error(), 4);
  35775. /* Empty and free up all error nodes */
  35776. ERR_clear_error();
  35777. /* Verify all nodes are cleared */
  35778. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35779. ERR_clear_error();
  35780. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35781. #endif
  35782. return EXPECT_RESULT();
  35783. }
  35784. /*
  35785. * This is a regression test for a bug where the peek/get error functions were
  35786. * drawing from the end of the queue rather than the front.
  35787. */
  35788. static int test_wolfSSL_ERR_get_error_order(void)
  35789. {
  35790. EXPECT_DECLS;
  35791. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  35792. /* Empty the queue. */
  35793. wolfSSL_ERR_clear_error();
  35794. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  35795. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  35796. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  35797. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  35798. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  35799. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  35800. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  35801. return EXPECT_RESULT();
  35802. }
  35803. #ifndef NO_BIO
  35804. static int test_wolfSSL_ERR_print_errors(void)
  35805. {
  35806. EXPECT_DECLS;
  35807. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35808. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  35809. BIO* bio = NULL;
  35810. char buf[1024];
  35811. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35812. ERR_clear_error(); /* clear out any error nodes */
  35813. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35814. /* Choosing -600 as an unused errno. */
  35815. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  35816. ERR_print_errors(bio);
  35817. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  35818. ExpectIntEQ(XSTRNCMP(
  35819. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  35820. buf, 55), 0);
  35821. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  35822. ExpectIntEQ(XSTRNCMP(
  35823. "error:600:wolfSSL library:unknown error number:asn.c:100",
  35824. buf, 56), 0);
  35825. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  35826. ExpectIntEQ(buf[0], '\0');
  35827. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  35828. BIO_free(bio);
  35829. #endif
  35830. return EXPECT_RESULT();
  35831. }
  35832. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35833. defined(DEBUG_WOLFSSL)
  35834. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  35835. {
  35836. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  35837. return 0;
  35838. }
  35839. #endif
  35840. static int test_wolfSSL_ERR_print_errors_cb(void)
  35841. {
  35842. EXPECT_DECLS;
  35843. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35844. defined(DEBUG_WOLFSSL)
  35845. BIO* bio = NULL;
  35846. char buf[1024];
  35847. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35848. ERR_clear_error(); /* clear out any error nodes */
  35849. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35850. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  35851. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  35852. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  35853. ExpectIntEQ(XSTRNCMP(
  35854. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  35855. buf, 53), 0);
  35856. ExpectIntEQ(XSTRNCMP(
  35857. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  35858. buf + 53, 55), 0);
  35859. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  35860. BIO_free(bio);
  35861. #endif
  35862. return EXPECT_RESULT();
  35863. }
  35864. /*
  35865. * Testing WOLFSSL_ERROR_MSG
  35866. */
  35867. static int test_WOLFSSL_ERROR_MSG(void)
  35868. {
  35869. int res = TEST_SKIPPED;
  35870. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  35871. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  35872. const char* msg = TEST_STRING;
  35873. WOLFSSL_ERROR_MSG(msg);
  35874. res = TEST_SUCCESS;
  35875. #endif
  35876. return res;
  35877. } /* End test_WOLFSSL_ERROR_MSG */
  35878. /*
  35879. * Testing wc_ERR_remove_state
  35880. */
  35881. static int test_wc_ERR_remove_state(void)
  35882. {
  35883. int res = TEST_SKIPPED;
  35884. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35885. wc_ERR_remove_state();
  35886. res = TEST_SUCCESS;
  35887. #endif
  35888. return res;
  35889. } /* End test_wc_ERR_remove_state */
  35890. /*
  35891. * Testing wc_ERR_print_errors_fp
  35892. */
  35893. static int test_wc_ERR_print_errors_fp(void)
  35894. {
  35895. EXPECT_DECLS;
  35896. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  35897. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  35898. long sz;
  35899. XFILE fp = XBADFILE;
  35900. WOLFSSL_ERROR(BAD_FUNC_ARG);
  35901. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  35902. XBADFILE);
  35903. wc_ERR_print_errors_fp(fp);
  35904. #if defined(DEBUG_WOLFSSL)
  35905. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  35906. #ifdef NO_ERROR_QUEUE
  35907. ExpectIntEQ(sz = XFTELL(fp), 0);
  35908. #else
  35909. ExpectIntNE(sz = XFTELL(fp), 0);
  35910. #endif
  35911. #endif
  35912. if (fp != XBADFILE)
  35913. XFCLOSE(fp);
  35914. (void)sz;
  35915. #endif
  35916. return EXPECT_RESULT();
  35917. } /* End test_wc_ERR_print_errors_fp */
  35918. #ifdef DEBUG_WOLFSSL
  35919. static void Logging_cb(const int logLevel, const char *const logMessage)
  35920. {
  35921. (void)logLevel;
  35922. (void)logMessage;
  35923. }
  35924. #endif
  35925. /*
  35926. * Testing wolfSSL_GetLoggingCb
  35927. */
  35928. static int test_wolfSSL_GetLoggingCb(void)
  35929. {
  35930. EXPECT_DECLS;
  35931. #ifdef DEBUG_WOLFSSL
  35932. /* Testing without wolfSSL_SetLoggingCb() */
  35933. ExpectNull(wolfSSL_GetLoggingCb());
  35934. /* Testing with wolfSSL_SetLoggingCb() */
  35935. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  35936. ExpectNotNull(wolfSSL_GetLoggingCb());
  35937. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  35938. #endif
  35939. ExpectNull(wolfSSL_GetLoggingCb());
  35940. return EXPECT_RESULT();
  35941. } /* End test_wolfSSL_GetLoggingCb */
  35942. #endif /* !NO_BIO */
  35943. static int test_wolfSSL_MD4(void)
  35944. {
  35945. EXPECT_DECLS;
  35946. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  35947. MD4_CTX md4;
  35948. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  35949. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  35950. "789012345678901234567890";
  35951. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  35952. "\xcc\x05\x36";
  35953. int msgSz = (int)XSTRLEN(msg);
  35954. XMEMSET(out, 0, sizeof(out));
  35955. MD4_Init(&md4);
  35956. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  35957. MD4_Final(out, &md4);
  35958. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  35959. #endif
  35960. return EXPECT_RESULT();
  35961. }
  35962. static int test_wolfSSL_MD5(void)
  35963. {
  35964. EXPECT_DECLS;
  35965. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  35966. byte input1[] = "";
  35967. byte input2[] = "message digest";
  35968. byte hash[WC_MD5_DIGEST_SIZE];
  35969. unsigned char output1[] =
  35970. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  35971. unsigned char output2[] =
  35972. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  35973. WOLFSSL_MD5_CTX md5;
  35974. XMEMSET(&md5, 0, sizeof(md5));
  35975. /* Test cases for illegal parameters */
  35976. ExpectIntEQ(MD5_Init(NULL), 0);
  35977. ExpectIntEQ(MD5_Init(&md5), 1);
  35978. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  35979. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  35980. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  35981. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  35982. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  35983. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  35984. /* Init MD5 CTX */
  35985. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35986. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  35987. 1);
  35988. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35989. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  35990. /* Init MD5 CTX */
  35991. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  35992. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  35993. (int)XSTRLEN((const char*)input2)), 1);
  35994. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  35995. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  35996. #if !defined(NO_OLD_NAMES) && \
  35997. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  35998. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  35999. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  36000. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  36001. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  36002. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  36003. &hash);
  36004. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36005. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  36006. &hash);
  36007. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36008. {
  36009. byte data[] = "Data to be hashed.";
  36010. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  36011. ExpectNotNull(MD5(data, sizeof(data), NULL));
  36012. ExpectNotNull(MD5(data, sizeof(data), hash));
  36013. ExpectNotNull(MD5(NULL, 0, hash));
  36014. ExpectNull(MD5(NULL, sizeof(data), hash));
  36015. }
  36016. #endif
  36017. #endif
  36018. return EXPECT_RESULT();
  36019. }
  36020. static int test_wolfSSL_MD5_Transform(void)
  36021. {
  36022. EXPECT_DECLS;
  36023. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36024. byte input1[] = "";
  36025. byte input2[] = "abc";
  36026. byte local[WC_MD5_BLOCK_SIZE];
  36027. word32 sLen = 0;
  36028. #ifdef BIG_ENDIAN_ORDER
  36029. unsigned char output1[] =
  36030. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  36031. unsigned char output2[] =
  36032. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  36033. #else
  36034. unsigned char output1[] =
  36035. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  36036. unsigned char output2[] =
  36037. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  36038. #endif
  36039. union {
  36040. wc_Md5 native;
  36041. MD5_CTX compat;
  36042. } md5;
  36043. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  36044. XMEMSET(&local, 0, sizeof(local));
  36045. /* sanity check */
  36046. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  36047. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  36048. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  36049. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  36050. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36051. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  36052. /* Init MD5 CTX */
  36053. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  36054. /* Do Transform*/
  36055. sLen = (word32)XSTRLEN((char*)input1);
  36056. XMEMCPY(local, input1, sLen);
  36057. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36058. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  36059. /* Init MD5 CTX */
  36060. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  36061. sLen = (word32)XSTRLEN((char*)input2);
  36062. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  36063. XMEMCPY(local, input2, sLen);
  36064. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36065. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  36066. #endif
  36067. return EXPECT_RESULT();
  36068. }
  36069. static int test_wolfSSL_SHA(void)
  36070. {
  36071. EXPECT_DECLS;
  36072. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36073. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36074. (!defined(HAVE_FIPS) || \
  36075. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36076. {
  36077. const unsigned char in[] = "abc";
  36078. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36079. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36080. unsigned char out[WC_SHA_DIGEST_SIZE];
  36081. unsigned char* p;
  36082. WOLFSSL_SHA_CTX sha;
  36083. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36084. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36085. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36086. /* SHA interface test */
  36087. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36088. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  36089. ExpectNotNull(SHA(in, 0, out));
  36090. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36091. ExpectNotNull(SHA(NULL, 0, out));
  36092. ExpectNotNull(SHA(NULL, 0, NULL));
  36093. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  36094. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36095. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  36096. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  36097. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  36098. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36099. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  36100. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36101. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  36102. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36103. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  36104. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36105. }
  36106. #endif
  36107. #if !defined(NO_SHA256)
  36108. {
  36109. const unsigned char in[] = "abc";
  36110. unsigned char expected[] =
  36111. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  36112. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  36113. "\x15\xAD";
  36114. unsigned char out[WC_SHA256_DIGEST_SIZE];
  36115. unsigned char* p;
  36116. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  36117. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36118. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  36119. #else
  36120. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  36121. #endif
  36122. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  36123. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36124. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  36125. #else
  36126. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  36127. #endif
  36128. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  36129. }
  36130. #endif
  36131. #if defined(WOLFSSL_SHA384)
  36132. {
  36133. const unsigned char in[] = "abc";
  36134. unsigned char expected[] =
  36135. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  36136. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  36137. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  36138. "\xc8\x25\xa7";
  36139. unsigned char out[WC_SHA384_DIGEST_SIZE];
  36140. unsigned char* p;
  36141. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  36142. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36143. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  36144. #else
  36145. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  36146. #endif
  36147. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  36148. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36149. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  36150. #else
  36151. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  36152. #endif
  36153. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  36154. }
  36155. #endif
  36156. #if defined(WOLFSSL_SHA512)
  36157. {
  36158. const unsigned char in[] = "abc";
  36159. unsigned char expected[] =
  36160. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  36161. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  36162. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  36163. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  36164. "\xa5\x4c\xa4\x9f";
  36165. unsigned char out[WC_SHA512_DIGEST_SIZE];
  36166. unsigned char* p;
  36167. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  36168. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36169. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  36170. #else
  36171. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  36172. #endif
  36173. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  36174. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36175. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  36176. #else
  36177. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  36178. #endif
  36179. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  36180. }
  36181. #endif
  36182. #endif
  36183. return EXPECT_RESULT();
  36184. }
  36185. static int test_wolfSSL_SHA_Transform(void)
  36186. {
  36187. EXPECT_DECLS;
  36188. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  36189. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36190. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36191. byte input1[] = "";
  36192. byte input2[] = "abc";
  36193. byte local[WC_SHA_BLOCK_SIZE];
  36194. word32 sLen = 0;
  36195. #ifdef BIG_ENDIAN_ORDER
  36196. unsigned char output1[] =
  36197. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  36198. "\x09\xf7\x3a\x93";
  36199. unsigned char output2[] =
  36200. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  36201. "\x7c\x6e\x08\xb8";
  36202. #else
  36203. unsigned char output1[] =
  36204. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  36205. "\x93\x3a\xf7\x09";
  36206. unsigned char output2[] =
  36207. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  36208. "\xb8\x08\x6e\x7c";
  36209. #endif
  36210. union {
  36211. wc_Sha native;
  36212. SHA_CTX compat;
  36213. } sha;
  36214. union {
  36215. wc_Sha native;
  36216. SHA_CTX compat;
  36217. } sha1;
  36218. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  36219. XMEMSET(&local, 0, sizeof(local));
  36220. /* sanity check */
  36221. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  36222. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  36223. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  36224. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  36225. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  36226. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  36227. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  36228. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36229. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  36230. /* Init SHA CTX */
  36231. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36232. /* Do Transform*/
  36233. sLen = (word32)XSTRLEN((char*)input1);
  36234. XMEMCPY(local, input1, sLen);
  36235. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36236. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36237. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36238. /* Init SHA CTX */
  36239. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  36240. sLen = (word32)XSTRLEN((char*)input2);
  36241. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36242. XMEMCPY(local, input2, sLen);
  36243. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36244. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36245. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36246. /* SHA1 */
  36247. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36248. /* Init SHA CTX */
  36249. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36250. /* Do Transform*/
  36251. sLen = (word32)XSTRLEN((char*)input1);
  36252. XMEMCPY(local, input1, sLen);
  36253. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36254. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  36255. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  36256. /* Init SHA CTX */
  36257. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  36258. sLen = (word32)XSTRLEN((char*)input2);
  36259. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36260. XMEMCPY(local, input2, sLen);
  36261. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36262. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  36263. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36264. #endif
  36265. #endif
  36266. return EXPECT_RESULT();
  36267. }
  36268. static int test_wolfSSL_SHA224(void)
  36269. {
  36270. EXPECT_DECLS;
  36271. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  36272. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36273. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36274. unsigned char input[] =
  36275. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36276. unsigned char output[] =
  36277. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  36278. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  36279. size_t inLen;
  36280. byte hash[WC_SHA224_DIGEST_SIZE];
  36281. unsigned char* p;
  36282. inLen = XSTRLEN((char*)input);
  36283. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  36284. ExpectNull(SHA224(NULL, inLen, hash));
  36285. ExpectNotNull(SHA224(input, 0, hash));
  36286. ExpectNotNull(SHA224(input, inLen, NULL));
  36287. ExpectNotNull(SHA224(NULL, 0, hash));
  36288. ExpectNotNull(SHA224(NULL, 0, NULL));
  36289. ExpectNotNull(SHA224(input, inLen, hash));
  36290. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  36291. ExpectNotNull(p = SHA224(input, inLen, NULL));
  36292. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  36293. #endif
  36294. return EXPECT_RESULT();
  36295. }
  36296. static int test_wolfSSL_SHA256(void)
  36297. {
  36298. EXPECT_DECLS;
  36299. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  36300. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36301. unsigned char input[] =
  36302. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36303. unsigned char output[] =
  36304. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  36305. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  36306. "\x06\xC1";
  36307. size_t inLen;
  36308. byte hash[WC_SHA256_DIGEST_SIZE];
  36309. inLen = XSTRLEN((char*)input);
  36310. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  36311. ExpectNotNull(SHA256(input, inLen, hash));
  36312. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  36313. #endif
  36314. return EXPECT_RESULT();
  36315. }
  36316. static int test_wolfSSL_SHA256_Transform(void)
  36317. {
  36318. EXPECT_DECLS;
  36319. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36320. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36321. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36322. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  36323. !defined(WOLFSSL_KCAPI_HASH)
  36324. byte input1[] = "";
  36325. byte input2[] = "abc";
  36326. byte local[WC_SHA256_BLOCK_SIZE];
  36327. word32 sLen = 0;
  36328. #ifdef BIG_ENDIAN_ORDER
  36329. unsigned char output1[] =
  36330. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  36331. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  36332. unsigned char output2[] =
  36333. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  36334. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  36335. #else
  36336. unsigned char output1[] =
  36337. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  36338. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  36339. unsigned char output2[] =
  36340. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  36341. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  36342. #endif
  36343. union {
  36344. wc_Sha256 native;
  36345. SHA256_CTX compat;
  36346. } sha256;
  36347. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  36348. XMEMSET(&local, 0, sizeof(local));
  36349. /* sanity check */
  36350. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  36351. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  36352. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  36353. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  36354. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36355. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  36356. /* Init SHA256 CTX */
  36357. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36358. /* Do Transform*/
  36359. sLen = (word32)XSTRLEN((char*)input1);
  36360. XMEMCPY(local, input1, sLen);
  36361. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36362. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  36363. 0);
  36364. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36365. /* Init SHA256 CTX */
  36366. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  36367. sLen = (word32)XSTRLEN((char*)input2);
  36368. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  36369. XMEMCPY(local, input2, sLen);
  36370. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36371. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  36372. 0);
  36373. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36374. #endif
  36375. #endif
  36376. return EXPECT_RESULT();
  36377. }
  36378. static int test_wolfSSL_SHA512_Transform(void)
  36379. {
  36380. EXPECT_DECLS;
  36381. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  36382. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36383. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36384. !defined(WOLFSSL_KCAPI_HASH)
  36385. byte input1[] = "";
  36386. byte input2[] = "abc";
  36387. byte local[WC_SHA512_BLOCK_SIZE];
  36388. word32 sLen = 0;
  36389. #ifdef BIG_ENDIAN_ORDER
  36390. unsigned char output1[] =
  36391. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  36392. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  36393. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  36394. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  36395. unsigned char output2[] =
  36396. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  36397. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  36398. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  36399. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  36400. #else
  36401. unsigned char output1[] =
  36402. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  36403. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  36404. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  36405. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  36406. unsigned char output2[] =
  36407. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  36408. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  36409. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  36410. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  36411. #endif
  36412. union {
  36413. wc_Sha512 native;
  36414. SHA512_CTX compat;
  36415. } sha512;
  36416. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36417. XMEMSET(&local, 0, sizeof(local));
  36418. /* sanity check */
  36419. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  36420. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  36421. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  36422. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  36423. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36424. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36425. /* Init SHA512 CTX */
  36426. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  36427. /* Do Transform*/
  36428. sLen = (word32)XSTRLEN((char*)input1);
  36429. XMEMCPY(local, input1, sLen);
  36430. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36431. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36432. WC_SHA512_DIGEST_SIZE), 0);
  36433. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36434. /* Init SHA512 CTX */
  36435. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  36436. sLen = (word32)XSTRLEN((char*)input2);
  36437. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36438. XMEMCPY(local, input2, sLen);
  36439. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36440. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36441. WC_SHA512_DIGEST_SIZE), 0);
  36442. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36443. (void)input1;
  36444. #endif
  36445. #endif
  36446. return EXPECT_RESULT();
  36447. }
  36448. static int test_wolfSSL_SHA512_224_Transform(void)
  36449. {
  36450. EXPECT_DECLS;
  36451. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36452. !defined(WOLFSSL_NOSHA512_224)
  36453. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36454. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36455. !defined(WOLFSSL_KCAPI_HASH)
  36456. byte input1[] = "";
  36457. byte input2[] = "abc";
  36458. byte local[WC_SHA512_BLOCK_SIZE];
  36459. word32 sLen = 0;
  36460. unsigned char output1[] =
  36461. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  36462. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  36463. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  36464. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  36465. unsigned char output2[] =
  36466. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  36467. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  36468. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  36469. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  36470. union {
  36471. wc_Sha512 native;
  36472. SHA512_CTX compat;
  36473. } sha512;
  36474. #ifdef BIG_ENDIAN_ORDER
  36475. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36476. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36477. #endif
  36478. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36479. XMEMSET(&local, 0, sizeof(local));
  36480. /* sanity check */
  36481. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  36482. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  36483. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  36484. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  36485. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  36486. BAD_FUNC_ARG);
  36487. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36488. /* Init SHA512 CTX */
  36489. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  36490. /* Do Transform*/
  36491. sLen = (word32)XSTRLEN((char*)input1);
  36492. XMEMCPY(local, input1, sLen);
  36493. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36494. 1);
  36495. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36496. WC_SHA512_DIGEST_SIZE), 0);
  36497. /* frees resources */
  36498. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36499. /* Init SHA512 CTX */
  36500. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  36501. sLen = (word32)XSTRLEN((char*)input2);
  36502. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36503. XMEMCPY(local, input2, sLen);
  36504. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  36505. 1);
  36506. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36507. WC_SHA512_DIGEST_SIZE), 0);
  36508. /* frees resources */
  36509. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  36510. #endif
  36511. #endif
  36512. return EXPECT_RESULT();
  36513. }
  36514. static int test_wolfSSL_SHA512_256_Transform(void)
  36515. {
  36516. EXPECT_DECLS;
  36517. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  36518. !defined(WOLFSSL_NOSHA512_256)
  36519. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36520. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36521. !defined(WOLFSSL_KCAPI_HASH)
  36522. byte input1[] = "";
  36523. byte input2[] = "abc";
  36524. byte local[WC_SHA512_BLOCK_SIZE];
  36525. word32 sLen = 0;
  36526. unsigned char output1[] =
  36527. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  36528. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  36529. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  36530. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  36531. unsigned char output2[] =
  36532. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  36533. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  36534. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  36535. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  36536. union {
  36537. wc_Sha512 native;
  36538. SHA512_CTX compat;
  36539. } sha512;
  36540. #ifdef BIG_ENDIAN_ORDER
  36541. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  36542. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  36543. #endif
  36544. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36545. XMEMSET(&local, 0, sizeof(local));
  36546. /* sanity check */
  36547. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  36548. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  36549. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  36550. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  36551. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  36552. BAD_FUNC_ARG);
  36553. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36554. /* Init SHA512 CTX */
  36555. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  36556. /* Do Transform*/
  36557. sLen = (word32)XSTRLEN((char*)input1);
  36558. XMEMCPY(local, input1, sLen);
  36559. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36560. 1);
  36561. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  36562. WC_SHA512_DIGEST_SIZE), 0);
  36563. /* frees resources */
  36564. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36565. /* Init SHA512 CTX */
  36566. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  36567. sLen = (word32)XSTRLEN((char*)input2);
  36568. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36569. XMEMCPY(local, input2, sLen);
  36570. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  36571. 1);
  36572. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  36573. WC_SHA512_DIGEST_SIZE), 0);
  36574. /* frees resources */
  36575. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  36576. #endif
  36577. #endif
  36578. return EXPECT_RESULT();
  36579. }
  36580. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36581. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36582. * buffer of 64 bytes.
  36583. *
  36584. * returns the size of the digest buffer on success and a negative value on
  36585. * failure.
  36586. */
  36587. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  36588. int* sz)
  36589. {
  36590. EXPECT_DECLS;
  36591. HMAC_CTX ctx1;
  36592. HMAC_CTX ctx2;
  36593. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36594. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36595. unsigned char long_key[] =
  36596. "0123456789012345678901234567890123456789"
  36597. "0123456789012345678901234567890123456789"
  36598. "0123456789012345678901234567890123456789"
  36599. "0123456789012345678901234567890123456789";
  36600. unsigned char msg[] = "message to hash";
  36601. unsigned int digestSz = 64;
  36602. int keySz = sizeof(key);
  36603. int long_keySz = sizeof(long_key);
  36604. int msgSz = sizeof(msg);
  36605. unsigned char digest2[64];
  36606. unsigned int digestSz2 = 64;
  36607. HMAC_CTX_init(&ctx1);
  36608. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36609. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36610. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36611. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36612. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36613. HMAC_CTX_cleanup(&ctx1);
  36614. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36615. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36616. HMAC_CTX_cleanup(&ctx2);
  36617. ExpectIntEQ(digestSz, digestSz2);
  36618. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36619. /* test HMAC_Init with NULL key */
  36620. /* init after copy */
  36621. HMAC_CTX_init(&ctx1);
  36622. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36623. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36624. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36625. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36626. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36627. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36628. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36629. HMAC_CTX_cleanup(&ctx1);
  36630. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36631. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36632. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36633. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36634. HMAC_CTX_cleanup(&ctx2);
  36635. ExpectIntEQ(digestSz, digestSz2);
  36636. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36637. /* long key */
  36638. HMAC_CTX_init(&ctx1);
  36639. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  36640. SSL_SUCCESS);
  36641. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36642. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36643. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36644. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36645. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36646. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36647. HMAC_CTX_cleanup(&ctx1);
  36648. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36649. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36650. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36651. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36652. HMAC_CTX_cleanup(&ctx2);
  36653. ExpectIntEQ(digestSz, digestSz2);
  36654. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36655. /* init before copy */
  36656. HMAC_CTX_init(&ctx1);
  36657. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36658. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36659. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36660. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36661. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36662. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36663. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36664. HMAC_CTX_cleanup(&ctx1);
  36665. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36666. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36667. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36668. HMAC_CTX_cleanup(&ctx2);
  36669. ExpectIntEQ(digestSz, digestSz2);
  36670. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36671. *sz = digestSz;
  36672. return EXPECT_RESULT();
  36673. }
  36674. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36675. static int test_wolfSSL_HMAC_CTX(void)
  36676. {
  36677. EXPECT_DECLS;
  36678. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36679. unsigned char digest[64];
  36680. int digestSz;
  36681. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  36682. WOLFSSL_HMAC_CTX ctx1;
  36683. WOLFSSL_HMAC_CTX ctx2;
  36684. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  36685. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  36686. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  36687. wolfSSL_HMAC_CTX_free(NULL);
  36688. wolfSSL_HMAC_CTX_free(hmac_ctx);
  36689. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  36690. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  36691. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  36692. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  36693. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  36694. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  36695. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  36696. ((! defined(HAVE_FIPS_VERSION)) || \
  36697. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  36698. /* Copy object that hasn't had a digest set - MD5. */
  36699. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  36700. #else
  36701. /* Copy object that hasn't had a digest set. */
  36702. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  36703. #endif
  36704. HMAC_CTX_cleanup(NULL);
  36705. HMAC_CTX_cleanup(&ctx2);
  36706. ExpectNull(HMAC_CTX_get_md(NULL));
  36707. #ifndef NO_SHA
  36708. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  36709. TEST_SUCCESS);
  36710. ExpectIntEQ(digestSz, 20);
  36711. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36712. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36713. #endif /* !NO_SHA */
  36714. #ifdef WOLFSSL_SHA224
  36715. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  36716. TEST_SUCCESS);
  36717. ExpectIntEQ(digestSz, 28);
  36718. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36719. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36720. "\x02\x0E", digest, digestSz), 0);
  36721. #endif /* WOLFSSL_SHA224 */
  36722. #ifndef NO_SHA256
  36723. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  36724. TEST_SUCCESS);
  36725. ExpectIntEQ(digestSz, 32);
  36726. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  36727. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  36728. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  36729. #endif /* !NO_SHA256 */
  36730. #ifdef WOLFSSL_SHA384
  36731. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  36732. TEST_SUCCESS);
  36733. ExpectIntEQ(digestSz, 48);
  36734. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  36735. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  36736. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  36737. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  36738. digest, digestSz), 0);
  36739. #endif /* WOLFSSL_SHA384 */
  36740. #ifdef WOLFSSL_SHA512
  36741. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  36742. TEST_SUCCESS);
  36743. ExpectIntEQ(digestSz, 64);
  36744. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  36745. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  36746. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  36747. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  36748. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  36749. digest, digestSz), 0);
  36750. #endif /* WOLFSSL_SHA512 */
  36751. #ifdef WOLFSSL_SHA3
  36752. #ifndef WOLFSSL_NOSHA3_224
  36753. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  36754. TEST_SUCCESS);
  36755. ExpectIntEQ(digestSz, 28);
  36756. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  36757. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  36758. "\x29\x0f", digest, digestSz), 0);
  36759. #endif
  36760. #ifndef WOLFSSL_NOSHA3_256
  36761. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  36762. TEST_SUCCESS);
  36763. ExpectIntEQ(digestSz, 32);
  36764. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  36765. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  36766. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  36767. #endif
  36768. #ifndef WOLFSSL_NOSHA3_384
  36769. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  36770. TEST_SUCCESS);
  36771. ExpectIntEQ(digestSz, 48);
  36772. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  36773. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  36774. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  36775. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  36776. digest, digestSz), 0);
  36777. #endif
  36778. #ifndef WOLFSSL_NOSHA3_512
  36779. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  36780. TEST_SUCCESS);
  36781. ExpectIntEQ(digestSz, 64);
  36782. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  36783. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  36784. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  36785. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  36786. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  36787. digest, digestSz), 0);
  36788. #endif
  36789. #endif
  36790. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  36791. HAVE_FIPS_VERSION <= 2)
  36792. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  36793. TEST_SUCCESS);
  36794. ExpectIntEQ(digestSz, 16);
  36795. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  36796. "\xE4\x98\xDD", digest, digestSz), 0);
  36797. #endif /* !NO_MD5 */
  36798. #endif
  36799. return EXPECT_RESULT();
  36800. }
  36801. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  36802. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  36803. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  36804. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  36805. {
  36806. EXPECT_DECLS;
  36807. static const unsigned char key[] = "simple test key";
  36808. HMAC_CTX* hmac = NULL;
  36809. ENGINE* e = NULL;
  36810. unsigned char hash[WC_MAX_DIGEST_SIZE];
  36811. unsigned int len;
  36812. ExpectNotNull(hmac = HMAC_CTX_new());
  36813. HMAC_CTX_init(hmac);
  36814. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  36815. ((! defined(HAVE_FIPS_VERSION)) || \
  36816. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  36817. /* Get size on object that hasn't had a digest set - MD5. */
  36818. ExpectIntEQ(HMAC_size(hmac), 16);
  36819. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  36820. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  36821. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  36822. #else
  36823. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  36824. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  36825. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  36826. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  36827. #endif
  36828. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  36829. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  36830. /* re-using test key as data to hash */
  36831. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  36832. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  36833. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  36834. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  36835. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  36836. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  36837. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  36838. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  36839. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  36840. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  36841. ExpectIntEQ(len, md_len);
  36842. ExpectIntEQ(HMAC_size(NULL), 0);
  36843. ExpectIntEQ(HMAC_size(hmac), md_len);
  36844. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  36845. HMAC_cleanup(NULL);
  36846. HMAC_cleanup(hmac);
  36847. HMAC_CTX_free(hmac);
  36848. len = 0;
  36849. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  36850. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  36851. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  36852. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  36853. ExpectIntEQ(len, md_len);
  36854. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  36855. /* With data. */
  36856. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  36857. &len));
  36858. /* With NULL data. */
  36859. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  36860. &len));
  36861. /* With zero length data. */
  36862. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  36863. return EXPECT_RESULT();
  36864. }
  36865. #endif
  36866. static int test_wolfSSL_HMAC(void)
  36867. {
  36868. EXPECT_DECLS;
  36869. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  36870. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  36871. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  36872. #ifndef NO_SHA256
  36873. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  36874. TEST_SUCCESS);
  36875. #endif
  36876. #ifdef WOLFSSL_SHA224
  36877. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  36878. TEST_SUCCESS);
  36879. #endif
  36880. #ifdef WOLFSSL_SHA384
  36881. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  36882. TEST_SUCCESS);
  36883. #endif
  36884. #ifdef WOLFSSL_SHA512
  36885. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  36886. TEST_SUCCESS);
  36887. #endif
  36888. #ifdef WOLFSSL_SHA3
  36889. #ifndef WOLFSSL_NOSHA3_224
  36890. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  36891. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  36892. #endif
  36893. #ifndef WOLFSSL_NOSHA3_256
  36894. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  36895. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  36896. #endif
  36897. #ifndef WOLFSSL_NOSHA3_384
  36898. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  36899. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  36900. #endif
  36901. #ifndef WOLFSSL_NOSHA3_512
  36902. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  36903. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  36904. #endif
  36905. #endif
  36906. #ifndef NO_SHA
  36907. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  36908. TEST_SUCCESS);
  36909. #endif
  36910. #endif
  36911. return EXPECT_RESULT();
  36912. }
  36913. static int test_wolfSSL_CMAC(void)
  36914. {
  36915. EXPECT_DECLS;
  36916. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  36917. defined(WOLFSSL_AES_DIRECT)
  36918. int i;
  36919. byte key[AES_256_KEY_SIZE];
  36920. CMAC_CTX* cmacCtx = NULL;
  36921. byte out[AES_BLOCK_SIZE];
  36922. size_t outLen = AES_BLOCK_SIZE;
  36923. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  36924. key[i] = i;
  36925. }
  36926. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36927. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  36928. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  36929. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36930. NULL), 1);
  36931. /* re-using test key as data to hash */
  36932. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36933. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  36934. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  36935. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  36936. /* No Update works. */
  36937. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36938. NULL), 1);
  36939. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36940. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  36941. NULL), 1);
  36942. /* Test parameters with CMAC_Update. */
  36943. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  36944. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  36945. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  36946. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  36947. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  36948. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  36949. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  36950. /* Test parameters with CMAC_Final. */
  36951. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  36952. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  36953. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  36954. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  36955. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  36956. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  36957. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36958. CMAC_CTX_free(cmacCtx);
  36959. /* Test parameters with CMAC Init. */
  36960. cmacCtx = NULL;
  36961. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36962. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  36963. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  36964. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36965. NULL), 0);
  36966. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36967. NULL), 0);
  36968. /* give a key too small for the cipher, verify we get failure */
  36969. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  36970. NULL), 0);
  36971. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  36972. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  36973. /* Only AES-CBC supported. */
  36974. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  36975. NULL), 0);
  36976. #endif
  36977. CMAC_CTX_free(cmacCtx);
  36978. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  36979. cmacCtx = NULL;
  36980. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36981. /* No Init. */
  36982. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  36983. CMAC_CTX_free(cmacCtx);
  36984. /* Test AES-256-CBC */
  36985. cmacCtx = NULL;
  36986. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36987. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  36988. NULL), 1);
  36989. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36990. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36991. CMAC_CTX_free(cmacCtx);
  36992. /* Test AES-192-CBC */
  36993. cmacCtx = NULL;
  36994. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  36995. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  36996. NULL), 1);
  36997. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  36998. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  36999. CMAC_CTX_free(cmacCtx);
  37000. cmacCtx = NULL;
  37001. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37002. CMAC_CTX_free(cmacCtx);
  37003. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  37004. return EXPECT_RESULT();
  37005. }
  37006. static int test_wolfSSL_DES(void)
  37007. {
  37008. EXPECT_DECLS;
  37009. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37010. const_DES_cblock myDes;
  37011. DES_cblock iv;
  37012. DES_key_schedule key;
  37013. word32 i;
  37014. DES_LONG dl;
  37015. unsigned char msg[] = "hello wolfssl";
  37016. unsigned char weakKey[][8] = {
  37017. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  37018. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  37019. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  37020. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  37021. };
  37022. unsigned char semiWeakKey[][8] = {
  37023. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  37024. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  37025. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  37026. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  37027. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  37028. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  37029. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  37030. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  37031. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  37032. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  37033. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  37034. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  37035. };
  37036. DES_check_key(1);
  37037. DES_set_key(&myDes, &key);
  37038. /* check, check of odd parity */
  37039. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  37040. myDes[0] = 6; /* set even parity */
  37041. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37042. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  37043. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37044. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  37045. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  37046. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  37047. /* set odd parity for success case */
  37048. DES_set_odd_parity(&myDes);
  37049. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  37050. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  37051. myDes[3]);
  37052. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37053. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37054. ExpectIntEQ(key[i], myDes[i]);
  37055. }
  37056. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37057. /* check weak key */
  37058. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  37059. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37060. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  37061. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37062. DES_set_key_unchecked(NULL, NULL);
  37063. DES_set_key_unchecked(&myDes, NULL);
  37064. DES_set_key_unchecked(NULL, &key);
  37065. /* compare arrays, should be the same */
  37066. /* now do unchecked copy of a weak key over */
  37067. DES_set_key_unchecked(&myDes, &key);
  37068. /* compare arrays, should be the same */
  37069. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37070. ExpectIntEQ(key[i], myDes[i]);
  37071. }
  37072. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  37073. myDes[7] = 2;
  37074. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37075. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37076. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  37077. /* Test all weak keys. */
  37078. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  37079. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  37080. }
  37081. /* Test all semi-weak keys. */
  37082. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  37083. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  37084. }
  37085. /* check DES_key_sched API */
  37086. XMEMSET(key, 1, sizeof(DES_key_schedule));
  37087. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  37088. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  37089. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  37090. /* compare arrays, should be the same */
  37091. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37092. ExpectIntEQ(key[i], myDes[i]);
  37093. }
  37094. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  37095. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  37096. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  37097. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  37098. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  37099. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  37100. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  37101. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  37102. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  37103. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  37104. * DES_cbc_encrypt on the input */
  37105. XMEMSET(iv, 0, sizeof(DES_cblock));
  37106. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  37107. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  37108. ExpectIntEQ(dl, 480052723);
  37109. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  37110. return EXPECT_RESULT();
  37111. }
  37112. static int test_wolfSSL_DES_ncbc(void)
  37113. {
  37114. EXPECT_DECLS;
  37115. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37116. const_DES_cblock myDes;
  37117. DES_cblock iv = {1};
  37118. DES_key_schedule key = {0};
  37119. unsigned char msg[] = "hello wolfssl";
  37120. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37121. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37122. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37123. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37124. /* partial block test */
  37125. DES_set_key(&key, &myDes);
  37126. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37127. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37128. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37129. DES_set_key(&key, &myDes);
  37130. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37131. *((byte*)&iv) = 1;
  37132. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  37133. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  37134. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37135. /* full block test */
  37136. DES_set_key(&key, &myDes);
  37137. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  37138. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37139. *((byte*)&iv) = 1;
  37140. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  37141. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  37142. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37143. DES_set_key(&key, &myDes);
  37144. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37145. *((byte*)&iv) = 1;
  37146. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  37147. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  37148. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37149. #endif
  37150. return EXPECT_RESULT();
  37151. }
  37152. static int test_wolfSSL_DES_ecb_encrypt(void)
  37153. {
  37154. EXPECT_DECLS;
  37155. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  37156. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  37157. WOLFSSL_DES_key_schedule key;
  37158. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  37159. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  37160. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  37161. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  37162. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  37163. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  37164. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  37165. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  37166. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  37167. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  37168. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  37169. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  37170. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  37171. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  37172. /* Encrypt messages */
  37173. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  37174. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  37175. {
  37176. /* Decrypt messages */
  37177. int ret1 = 0;
  37178. int ret2 = 0;
  37179. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  37180. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  37181. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  37182. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  37183. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  37184. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  37185. }
  37186. #endif
  37187. return EXPECT_RESULT();
  37188. }
  37189. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  37190. {
  37191. EXPECT_DECLS;
  37192. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37193. unsigned char input1[8], input2[8];
  37194. unsigned char output1[8], output2[8];
  37195. unsigned char back1[8], back2[8];
  37196. WOLFSSL_DES_cblock iv1, iv2;
  37197. WOLFSSL_DES_key_schedule key1, key2, key3;
  37198. int i;
  37199. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  37200. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  37201. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  37202. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  37203. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  37204. XMEMSET(output1, 0, sizeof(output1));
  37205. XMEMSET(output2, 0, sizeof(output2));
  37206. XMEMSET(back1, 0, sizeof(back1));
  37207. XMEMSET(back2, 0, sizeof(back2));
  37208. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37209. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37210. /* Encrypt messages */
  37211. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  37212. DES_ENCRYPT);
  37213. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  37214. DES_ENCRYPT);
  37215. {
  37216. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37217. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37218. /* Decrypt messages */
  37219. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  37220. &iv1, DES_DECRYPT);
  37221. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  37222. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  37223. &iv2, DES_DECRYPT);
  37224. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  37225. }
  37226. for (i = 0; i < 8; i++) {
  37227. XMEMSET(output1, 0, sizeof(output1));
  37228. XMEMSET(output2, 0, sizeof(output2));
  37229. XMEMSET(back1, 0, sizeof(back1));
  37230. XMEMSET(back2, 0, sizeof(back2));
  37231. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37232. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37233. /* Encrypt partial messages */
  37234. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  37235. &iv1, DES_ENCRYPT);
  37236. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  37237. &iv2, DES_ENCRYPT);
  37238. {
  37239. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  37240. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  37241. /* Decrypt messages */
  37242. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  37243. &key3, &iv1, DES_DECRYPT);
  37244. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  37245. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  37246. &key3, &iv2, DES_DECRYPT);
  37247. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  37248. }
  37249. }
  37250. #endif
  37251. return EXPECT_RESULT();
  37252. }
  37253. static int test_wolfSSL_AES_encrypt(void)
  37254. {
  37255. EXPECT_DECLS;
  37256. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  37257. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37258. AES_KEY enc;
  37259. AES_KEY dec;
  37260. const byte msg[] = {
  37261. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37262. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37263. };
  37264. const byte exp[] = {
  37265. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  37266. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  37267. };
  37268. const byte key[] = {
  37269. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37270. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37271. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37272. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37273. };
  37274. byte eout[sizeof(msg)];
  37275. byte dout[sizeof(msg)];
  37276. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  37277. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  37278. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  37279. wolfSSL_AES_encrypt(msg, NULL, NULL);
  37280. wolfSSL_AES_encrypt(NULL, eout, NULL);
  37281. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  37282. wolfSSL_AES_encrypt(msg, eout, NULL);
  37283. wolfSSL_AES_encrypt(msg, NULL, &enc);
  37284. wolfSSL_AES_encrypt(NULL, eout, &enc);
  37285. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  37286. wolfSSL_AES_decrypt(eout, NULL, NULL);
  37287. wolfSSL_AES_decrypt(NULL, dout, NULL);
  37288. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  37289. wolfSSL_AES_decrypt(eout, dout, NULL);
  37290. wolfSSL_AES_decrypt(eout, NULL, &dec);
  37291. wolfSSL_AES_decrypt(NULL, dout, &dec);
  37292. wolfSSL_AES_encrypt(msg, eout, &enc);
  37293. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  37294. wolfSSL_AES_decrypt(eout, dout, &dec);
  37295. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  37296. #endif
  37297. return EXPECT_RESULT();
  37298. }
  37299. static int test_wolfSSL_AES_ecb_encrypt(void)
  37300. {
  37301. EXPECT_DECLS;
  37302. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  37303. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37304. AES_KEY aes;
  37305. const byte msg[] =
  37306. {
  37307. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37308. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37309. };
  37310. const byte verify[] =
  37311. {
  37312. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37313. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37314. };
  37315. const byte key[] =
  37316. {
  37317. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37318. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37319. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37320. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37321. };
  37322. byte out[AES_BLOCK_SIZE];
  37323. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37324. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37325. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37326. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37327. #ifdef HAVE_AES_DECRYPT
  37328. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37329. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37330. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37331. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37332. #endif
  37333. /* test bad arguments */
  37334. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37335. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37336. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37337. #endif
  37338. return EXPECT_RESULT();
  37339. }
  37340. static int test_wolfSSL_AES_cbc_encrypt(void)
  37341. {
  37342. EXPECT_DECLS;
  37343. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37344. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37345. AES_KEY aes;
  37346. AES_KEY* aesN = NULL;
  37347. size_t len = 0;
  37348. size_t lenB = 0;
  37349. int keySz0 = 0;
  37350. int keySzN = -1;
  37351. byte out[AES_BLOCK_SIZE] = {0};
  37352. byte* outN = NULL;
  37353. /* Test vectors retrieved from:
  37354. * <begin URL>
  37355. * https://csrc.nist.gov/
  37356. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37357. * documents/aes/KAT_AES.zip
  37358. * </end URL>
  37359. */
  37360. const byte* pt128N = NULL;
  37361. byte* key128N = NULL;
  37362. byte* iv128N = NULL;
  37363. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37364. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37365. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37366. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37367. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37368. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37369. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37370. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37371. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37372. len = sizeof(pt128);
  37373. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37374. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37375. ExpectIntNE(XMEMCMP(b, g, h), i)
  37376. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37377. /* Stressing wolfSSL_AES_cbc_encrypt() */
  37378. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37379. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37380. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37381. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37382. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37383. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37384. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37385. /* Stressing wolfSSL_AES_set_encrypt_key */
  37386. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37387. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37388. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37389. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37390. /* Stressing wolfSSL_AES_set_decrypt_key */
  37391. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37392. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37393. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37394. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37395. #ifdef WOLFSSL_AES_128
  37396. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  37397. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37398. RESET_IV(iv128tmp, iv128);
  37399. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37400. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37401. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37402. wc_AesFree((Aes*)&aes);
  37403. #ifdef HAVE_AES_DECRYPT
  37404. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  37405. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37406. RESET_IV(iv128tmp, iv128);
  37407. len = sizeof(ct128);
  37408. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37409. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37410. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37411. wc_AesFree((Aes*)&aes);
  37412. #endif
  37413. #endif /* WOLFSSL_AES_128 */
  37414. #ifdef WOLFSSL_AES_192
  37415. {
  37416. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37417. * Appendix F.2.3 */
  37418. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37419. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37420. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37421. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37422. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37423. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37424. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37425. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37426. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37427. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37428. len = sizeof(pt192);
  37429. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  37430. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37431. RESET_IV(iv192tmp, iv192);
  37432. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37433. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37434. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37435. wc_AesFree((Aes*)&aes);
  37436. #ifdef HAVE_AES_DECRYPT
  37437. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  37438. len = sizeof(ct192);
  37439. RESET_IV(iv192tmp, iv192);
  37440. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37441. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37442. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  37443. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  37444. wc_AesFree((Aes*)&aes);
  37445. #endif
  37446. }
  37447. #endif /* WOLFSSL_AES_192 */
  37448. #ifdef WOLFSSL_AES_256
  37449. {
  37450. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  37451. * Appendix F.2.5 */
  37452. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  37453. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37454. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37455. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  37456. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  37457. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37458. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37459. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37460. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37461. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37462. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  37463. len = sizeof(pt256);
  37464. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  37465. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37466. RESET_IV(iv256tmp, iv256);
  37467. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37468. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  37469. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  37470. wc_AesFree((Aes*)&aes);
  37471. #ifdef HAVE_AES_DECRYPT
  37472. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  37473. len = sizeof(ct256);
  37474. RESET_IV(iv256tmp, iv256);
  37475. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37476. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37477. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  37478. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  37479. wc_AesFree((Aes*)&aes);
  37480. #endif
  37481. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  37482. !defined(HAVE_SELFTEST)
  37483. {
  37484. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  37485. byte wrapPlain[sizeof(key256)] = { 0 };
  37486. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  37487. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  37488. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37489. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37490. 15), WOLFSSL_FAILURE);
  37491. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37492. sizeof(key256)), sizeof(wrapCipher));
  37493. wc_AesFree((Aes*)&aes);
  37494. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  37495. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37496. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37497. 23), WOLFSSL_FAILURE);
  37498. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37499. sizeof(wrapCipher)), sizeof(wrapPlain));
  37500. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37501. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  37502. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  37503. wc_AesFree((Aes*)&aes);
  37504. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  37505. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37506. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  37507. sizeof(key256)), sizeof(wrapCipher));
  37508. wc_AesFree((Aes*)&aes);
  37509. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  37510. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37511. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  37512. sizeof(wrapCipher)), sizeof(wrapPlain));
  37513. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37514. wc_AesFree((Aes*)&aes);
  37515. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37516. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37517. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37518. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  37519. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  37520. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  37521. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  37522. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  37523. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  37524. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  37525. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  37526. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  37527. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  37528. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  37529. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  37530. 0);
  37531. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  37532. 0);
  37533. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  37534. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  37535. }
  37536. #endif /* HAVE_AES_KEYWRAP */
  37537. }
  37538. #endif /* WOLFSSL_AES_256 */
  37539. #endif
  37540. return EXPECT_RESULT();
  37541. }
  37542. static int test_wolfSSL_AES_cfb128_encrypt(void)
  37543. {
  37544. EXPECT_DECLS;
  37545. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  37546. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37547. AES_KEY aesEnc;
  37548. AES_KEY aesDec;
  37549. const byte msg[] = {
  37550. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  37551. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  37552. };
  37553. const byte exp[] = {
  37554. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  37555. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  37556. };
  37557. const byte key[] = {
  37558. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  37559. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37560. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37561. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  37562. };
  37563. const byte ivData[] = {
  37564. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  37565. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  37566. };
  37567. byte out[AES_BLOCK_SIZE];
  37568. byte iv[AES_BLOCK_SIZE];
  37569. word32 i;
  37570. int num;
  37571. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37572. XMEMCPY(iv, ivData, sizeof(iv));
  37573. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37574. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  37575. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  37576. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37577. #ifdef HAVE_AES_DECRYPT
  37578. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37579. XMEMCPY(iv, ivData, sizeof(iv));
  37580. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37581. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  37582. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  37583. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37584. #endif
  37585. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  37586. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  37587. XMEMCPY(iv, ivData, sizeof(iv));
  37588. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37589. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  37590. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37591. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  37592. if (i == 0) {
  37593. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37594. }
  37595. else {
  37596. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37597. }
  37598. #ifdef HAVE_AES_DECRYPT
  37599. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  37600. XMEMCPY(iv, ivData, sizeof(iv));
  37601. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37602. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  37603. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  37604. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  37605. if (i == 0) {
  37606. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37607. }
  37608. else {
  37609. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  37610. }
  37611. #endif
  37612. }
  37613. if (EXPECT_SUCCESS()) {
  37614. /* test bad arguments */
  37615. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37616. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  37617. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  37618. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37619. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  37620. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37621. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  37622. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  37623. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  37624. }
  37625. #endif
  37626. return EXPECT_RESULT();
  37627. }
  37628. static int test_wolfSSL_CRYPTO_cts128(void)
  37629. {
  37630. EXPECT_DECLS;
  37631. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  37632. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  37633. byte tmp[64]; /* Largest vector size */
  37634. /* Test vectors taken form RFC3962 Appendix B */
  37635. const testVector vects[] = {
  37636. {
  37637. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37638. "\x20",
  37639. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  37640. "\x97",
  37641. 17, 17
  37642. },
  37643. {
  37644. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37645. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  37646. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  37647. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  37648. 31, 31
  37649. },
  37650. {
  37651. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37652. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  37653. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37654. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  37655. 32, 32
  37656. },
  37657. {
  37658. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37659. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37660. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  37661. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37662. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  37663. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  37664. 47, 47
  37665. },
  37666. {
  37667. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37668. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37669. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  37670. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37671. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  37672. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  37673. 48, 48
  37674. },
  37675. {
  37676. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37677. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37678. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  37679. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  37680. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37681. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37682. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  37683. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  37684. 64, 64
  37685. }
  37686. };
  37687. byte keyBytes[AES_128_KEY_SIZE] = {
  37688. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  37689. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  37690. };
  37691. size_t i;
  37692. AES_KEY encKey;
  37693. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  37694. XMEMSET(tmp, 0, sizeof(tmp));
  37695. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  37696. AES_KEY decKey;
  37697. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  37698. &encKey), 0);
  37699. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  37700. &decKey), 0);
  37701. XMEMSET(iv, 0, sizeof(iv));
  37702. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  37703. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  37704. vects[i].outLen);
  37705. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  37706. XMEMSET(iv, 0, sizeof(iv));
  37707. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  37708. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  37709. vects[i].inLen);
  37710. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  37711. }
  37712. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  37713. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  37714. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  37715. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  37716. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  37717. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  37718. (cbc128_f)AES_cbc_encrypt), 0);
  37719. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  37720. (cbc128_f)AES_cbc_encrypt), 0);
  37721. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  37722. (cbc128_f)AES_cbc_encrypt), 0);
  37723. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  37724. (cbc128_f)AES_cbc_encrypt), 0);
  37725. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  37726. (cbc128_f)AES_cbc_encrypt), 0);
  37727. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  37728. /* Length too small. */
  37729. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  37730. (cbc128_f)AES_cbc_encrypt), 0);
  37731. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  37732. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  37733. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  37734. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  37735. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  37736. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  37737. (cbc128_f)AES_cbc_encrypt), 0);
  37738. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  37739. (cbc128_f)AES_cbc_encrypt), 0);
  37740. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  37741. (cbc128_f)AES_cbc_encrypt), 0);
  37742. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  37743. (cbc128_f)AES_cbc_encrypt), 0);
  37744. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  37745. (cbc128_f)AES_cbc_encrypt), 0);
  37746. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  37747. /* Length too small. */
  37748. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  37749. (cbc128_f)AES_cbc_encrypt), 0);
  37750. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  37751. return EXPECT_RESULT();
  37752. }
  37753. static int test_wolfSSL_RC4(void)
  37754. {
  37755. EXPECT_DECLS;
  37756. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  37757. WOLFSSL_RC4_KEY rc4Key;
  37758. unsigned char key[] = {
  37759. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37760. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37761. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37762. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  37763. };
  37764. unsigned char data[] = {
  37765. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37766. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37767. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37768. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  37769. };
  37770. unsigned char enc[sizeof(data)];
  37771. unsigned char dec[sizeof(data)];
  37772. word32 i;
  37773. word32 j;
  37774. wolfSSL_RC4_set_key(NULL, -1, NULL);
  37775. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  37776. wolfSSL_RC4_set_key(NULL, 0, NULL);
  37777. wolfSSL_RC4_set_key(NULL, -1, key);
  37778. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  37779. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  37780. wolfSSL_RC4_set_key(NULL, 0, key);
  37781. wolfSSL_RC4(NULL, 0, NULL, NULL);
  37782. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  37783. wolfSSL_RC4(NULL, 0, data, NULL);
  37784. wolfSSL_RC4(NULL, 0, NULL, enc);
  37785. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  37786. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  37787. wolfSSL_RC4(NULL, 0, data, enc);
  37788. ExpectIntEQ(1, 1);
  37789. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  37790. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  37791. XMEMSET(enc, 0, sizeof(enc));
  37792. XMEMSET(dec, 0, sizeof(dec));
  37793. /* Encrypt */
  37794. wolfSSL_RC4_set_key(&rc4Key, i, key);
  37795. wolfSSL_RC4(&rc4Key, j, data, enc);
  37796. /* Decrypt */
  37797. wolfSSL_RC4_set_key(&rc4Key, i, key);
  37798. wolfSSL_RC4(&rc4Key, j, enc, dec);
  37799. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  37800. }
  37801. }
  37802. #endif
  37803. return EXPECT_RESULT();
  37804. }
  37805. static int test_wolfSSL_OBJ(void)
  37806. {
  37807. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  37808. * mode
  37809. */
  37810. EXPECT_DECLS;
  37811. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  37812. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  37813. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  37814. ASN1_OBJECT *obj = NULL;
  37815. ASN1_OBJECT *obj2 = NULL;
  37816. char buf[50];
  37817. XFILE fp = XBADFILE;
  37818. X509 *x509 = NULL;
  37819. X509_NAME *x509Name = NULL;
  37820. X509_NAME_ENTRY *x509NameEntry = NULL;
  37821. ASN1_OBJECT *asn1Name = NULL;
  37822. int numNames = 0;
  37823. BIO *bio = NULL;
  37824. int nid;
  37825. int i, j;
  37826. const char *f[] = {
  37827. #ifndef NO_RSA
  37828. "./certs/ca-cert.der",
  37829. #endif
  37830. #ifdef HAVE_ECC
  37831. "./certs/ca-ecc-cert.der",
  37832. "./certs/ca-ecc384-cert.der",
  37833. #endif
  37834. NULL};
  37835. ASN1_OBJECT *field_name_obj = NULL;
  37836. int lastpos = -1;
  37837. int tmp = -1;
  37838. ASN1_STRING *asn1 = NULL;
  37839. unsigned char *buf_dyn = NULL;
  37840. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  37841. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37842. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  37843. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  37844. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37845. ASN1_OBJECT_free(obj);
  37846. obj = NULL;
  37847. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  37848. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  37849. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  37850. #ifdef WOLFSSL_CERT_EXT
  37851. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  37852. #endif
  37853. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  37854. ExpectNotNull(obj2 = OBJ_dup(obj));
  37855. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  37856. ASN1_OBJECT_free(obj);
  37857. obj = NULL;
  37858. ASN1_OBJECT_free(obj2);
  37859. obj2 = NULL;
  37860. for (i = 0; f[i] != NULL; i++)
  37861. {
  37862. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  37863. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  37864. if (fp != XBADFILE) {
  37865. XFCLOSE(fp);
  37866. fp = XBADFILE;
  37867. }
  37868. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  37869. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37870. /* Get the Common Name by using OBJ_txt2obj */
  37871. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  37872. do
  37873. {
  37874. lastpos = tmp;
  37875. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  37876. } while (tmp > -1);
  37877. ExpectIntNE(lastpos, -1);
  37878. ASN1_OBJECT_free(field_name_obj);
  37879. field_name_obj = NULL;
  37880. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  37881. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  37882. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  37883. /*
  37884. * All Common Names should be www.wolfssl.com
  37885. * This makes testing easier as we can test for the expected value.
  37886. */
  37887. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  37888. OPENSSL_free(buf_dyn);
  37889. buf_dyn = NULL;
  37890. bio = BIO_new(BIO_s_mem());
  37891. ExpectTrue(bio != NULL);
  37892. for (j = 0; j < numNames; j++)
  37893. {
  37894. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37895. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  37896. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37897. }
  37898. BIO_free(bio);
  37899. bio = NULL;
  37900. X509_free(x509);
  37901. x509 = NULL;
  37902. }
  37903. #ifdef HAVE_PKCS12
  37904. {
  37905. PKCS12 *p12 = NULL;
  37906. int boolRet;
  37907. EVP_PKEY *pkey = NULL;
  37908. const char *p12_f[] = {
  37909. #if !defined(NO_DES3) && !defined(NO_RSA)
  37910. "./certs/test-servercert.p12",
  37911. #endif
  37912. NULL};
  37913. for (i = 0; p12_f[i] != NULL; i++)
  37914. {
  37915. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  37916. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  37917. if (fp != XBADFILE) {
  37918. XFCLOSE(fp);
  37919. fp = XBADFILE;
  37920. }
  37921. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  37922. &pkey, &x509, NULL)) > 0);
  37923. wc_PKCS12_free(p12);
  37924. p12 = NULL;
  37925. EVP_PKEY_free(pkey);
  37926. x509Name = X509_get_issuer_name(x509);
  37927. ExpectNotNull(x509Name);
  37928. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  37929. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  37930. for (j = 0; j < numNames; j++)
  37931. {
  37932. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  37933. ExpectNotNull(asn1Name =
  37934. X509_NAME_ENTRY_get_object(x509NameEntry));
  37935. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  37936. }
  37937. BIO_free(bio);
  37938. bio = NULL;
  37939. X509_free(x509);
  37940. x509 = NULL;
  37941. }
  37942. }
  37943. #endif /* HAVE_PKCS12 */
  37944. #endif
  37945. return EXPECT_RESULT();
  37946. }
  37947. static int test_wolfSSL_OBJ_cmp(void)
  37948. {
  37949. EXPECT_DECLS;
  37950. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37951. ASN1_OBJECT *obj = NULL;
  37952. ASN1_OBJECT *obj2 = NULL;
  37953. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  37954. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  37955. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  37956. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  37957. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  37958. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  37959. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  37960. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  37961. ASN1_OBJECT_free(obj);
  37962. ASN1_OBJECT_free(obj2);
  37963. #endif
  37964. return EXPECT_RESULT();
  37965. }
  37966. static int test_wolfSSL_OBJ_txt2nid(void)
  37967. {
  37968. EXPECT_DECLS;
  37969. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  37970. defined(WOLFSSL_APACHE_HTTPD)
  37971. int i;
  37972. static const struct {
  37973. const char* sn;
  37974. const char* ln;
  37975. const char* oid;
  37976. int nid;
  37977. } testVals[] = {
  37978. #ifdef WOLFSSL_APACHE_HTTPD
  37979. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  37980. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  37981. NID_id_on_dnsSRV },
  37982. { "msUPN", "Microsoft User Principal Name",
  37983. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  37984. #endif
  37985. { NULL, NULL, NULL, NID_undef }
  37986. };
  37987. /* Invalid cases */
  37988. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  37989. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  37990. /* Valid cases */
  37991. for (i = 0; testVals[i].sn != NULL; i++) {
  37992. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  37993. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  37994. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  37995. }
  37996. #endif
  37997. return EXPECT_RESULT();
  37998. }
  37999. static int test_wolfSSL_OBJ_txt2obj(void)
  38000. {
  38001. EXPECT_DECLS;
  38002. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  38003. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  38004. int i;
  38005. char buf[50];
  38006. ASN1_OBJECT* obj = NULL;
  38007. static const struct {
  38008. const char* oidStr;
  38009. const char* sn;
  38010. const char* ln;
  38011. } objs_list[] = {
  38012. #if defined(WOLFSSL_APACHE_HTTPD)
  38013. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  38014. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  38015. #endif
  38016. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  38017. { NULL, NULL, NULL }
  38018. };
  38019. static const struct {
  38020. const char* numeric;
  38021. const char* name;
  38022. } objs_named[] = {
  38023. /* In dictionary but not in normal list. */
  38024. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  38025. /* Made up OID. */
  38026. { "1.3.5.7", "1.3.5.7" },
  38027. { NULL, NULL }
  38028. };
  38029. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  38030. ASN1_OBJECT_free(obj);
  38031. obj = NULL;
  38032. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  38033. ASN1_OBJECT_free(obj);
  38034. obj = NULL;
  38035. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  38036. /* Test numerical value of oid (oidStr) */
  38037. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  38038. /* Convert object back to text to confirm oid is correct */
  38039. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38040. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38041. ASN1_OBJECT_free(obj);
  38042. obj = NULL;
  38043. XMEMSET(buf, 0, sizeof(buf));
  38044. /* Test short name (sn) */
  38045. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  38046. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  38047. /* Convert object back to text to confirm oid is correct */
  38048. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38049. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38050. ASN1_OBJECT_free(obj);
  38051. obj = NULL;
  38052. XMEMSET(buf, 0, sizeof(buf));
  38053. /* Test long name (ln) - should fail when no_name = 1 */
  38054. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  38055. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  38056. /* Convert object back to text to confirm oid is correct */
  38057. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38058. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38059. ASN1_OBJECT_free(obj);
  38060. obj = NULL;
  38061. XMEMSET(buf, 0, sizeof(buf));
  38062. }
  38063. for (i = 0; objs_named[i].numeric != NULL; i++) {
  38064. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  38065. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  38066. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  38067. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38068. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  38069. ASN1_OBJECT_free(obj);
  38070. obj = NULL;
  38071. }
  38072. #endif
  38073. return EXPECT_RESULT();
  38074. }
  38075. static int test_wolfSSL_PEM_write_bio_X509(void)
  38076. {
  38077. EXPECT_DECLS;
  38078. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  38079. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  38080. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  38081. !defined(NO_FILESYSTEM)
  38082. /* This test contains the hard coded expected
  38083. * lengths. Update if necessary */
  38084. XFILE fp = XBADFILE;
  38085. WOLFSSL_EVP_PKEY *priv = NULL;
  38086. BIO* input = NULL;
  38087. BIO* output = NULL;
  38088. X509* x509a = NULL;
  38089. X509* x509b = NULL;
  38090. ASN1_TIME* notBeforeA = NULL;
  38091. ASN1_TIME* notAfterA = NULL;
  38092. #ifndef NO_ASN_TIME
  38093. ASN1_TIME* notBeforeB = NULL;
  38094. ASN1_TIME* notAfterB = NULL;
  38095. #endif
  38096. int expectedLen;
  38097. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  38098. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  38099. if (fp != XBADFILE) {
  38100. XFCLOSE(fp);
  38101. fp = XBADFILE;
  38102. }
  38103. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  38104. "rb"));
  38105. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  38106. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  38107. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38108. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  38109. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  38110. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  38111. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38112. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38113. /* compare length against expected */
  38114. expectedLen = 2000;
  38115. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38116. #ifndef NO_ASN_TIME
  38117. /* read exported X509 PEM back into struct, sanity check on export,
  38118. * make sure notBefore/notAfter are the same and certs are identical. */
  38119. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38120. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  38121. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  38122. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  38123. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  38124. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38125. X509_free(x509b);
  38126. x509b = NULL;
  38127. #endif
  38128. /* Reset output buffer */
  38129. BIO_free(output);
  38130. output = NULL;
  38131. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38132. /* Test forcing the AKID to be generated just from KeyIdentifier */
  38133. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  38134. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  38135. x509a->authKeyId = x509a->authKeyIdSrc;
  38136. x509a->authKeyIdSrc = NULL;
  38137. x509a->authKeyIdSrcSz = 0;
  38138. }
  38139. /* Resign to re-generate the der */
  38140. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  38141. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38142. /* Check that we generate a smaller output since the AKID will
  38143. * only contain the KeyIdentifier without any additional
  38144. * information */
  38145. /* Here we copy the validity struct from the original */
  38146. expectedLen = 1688;
  38147. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  38148. /* Reset buffers and x509 */
  38149. BIO_free(input);
  38150. input = NULL;
  38151. BIO_free(output);
  38152. output = NULL;
  38153. X509_free(x509a);
  38154. x509a = NULL;
  38155. /* test CA and basicConstSet values are encoded when
  38156. * the cert is a CA */
  38157. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  38158. /* read PEM into X509 struct */
  38159. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38160. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38161. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38162. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38163. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  38164. * values are maintained and certs are identical.*/
  38165. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38166. ExpectIntEQ(x509b->isCa, 1);
  38167. ExpectIntEQ(x509b->basicConstSet, 1);
  38168. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38169. X509_free(x509a);
  38170. x509a = NULL;
  38171. X509_free(x509b);
  38172. x509b = NULL;
  38173. BIO_free(input);
  38174. input = NULL;
  38175. BIO_free(output);
  38176. output = NULL;
  38177. /* test CA and basicConstSet values are encoded when
  38178. * the cert is not CA */
  38179. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  38180. /* read PEM into X509 struct */
  38181. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  38182. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  38183. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  38184. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  38185. /* read exported X509 PEM back into struct, ensure isCa and
  38186. * basicConstSet values are maintained and certs are identical */
  38187. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  38188. ExpectIntEQ(x509b->isCa, 0);
  38189. ExpectIntEQ(x509b->basicConstSet, 1);
  38190. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  38191. wolfSSL_EVP_PKEY_free(priv);
  38192. X509_free(x509a);
  38193. X509_free(x509b);
  38194. BIO_free(input);
  38195. BIO_free(output);
  38196. #endif
  38197. return EXPECT_RESULT();
  38198. }
  38199. static int test_wolfSSL_X509_NAME_ENTRY(void)
  38200. {
  38201. EXPECT_DECLS;
  38202. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38203. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  38204. X509* x509 = NULL;
  38205. #ifndef NO_BIO
  38206. BIO* bio = NULL;
  38207. #endif
  38208. X509_NAME* nm = NULL;
  38209. X509_NAME_ENTRY* entry = NULL;
  38210. unsigned char cn[] = "another name to add";
  38211. #ifdef OPENSSL_ALL
  38212. int i;
  38213. int names_len = 0;
  38214. #endif
  38215. ExpectNotNull(x509 =
  38216. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38217. #ifndef NO_BIO
  38218. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38219. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  38220. #endif
  38221. #ifdef WOLFSSL_CERT_REQ
  38222. {
  38223. X509_REQ* req = NULL;
  38224. #ifndef NO_BIO
  38225. BIO* bReq = NULL;
  38226. #endif
  38227. ExpectNotNull(req =
  38228. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  38229. #ifndef NO_BIO
  38230. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  38231. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  38232. BIO_free(bReq);
  38233. #endif
  38234. X509_free(req);
  38235. }
  38236. #endif
  38237. ExpectNotNull(nm = X509_get_subject_name(x509));
  38238. /* Test add entry */
  38239. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  38240. 0x0c, cn, (int)sizeof(cn)));
  38241. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38242. #ifdef WOLFSSL_CERT_EXT
  38243. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  38244. (byte*)"support@wolfssl.com", 19, -1,
  38245. 1), WOLFSSL_SUCCESS);
  38246. #endif
  38247. X509_NAME_ENTRY_free(entry);
  38248. entry = NULL;
  38249. #ifdef WOLFSSL_CERT_REQ
  38250. {
  38251. unsigned char srv_pkcs9p[] = "Server";
  38252. unsigned char fvrtDrnk[] = "tequila";
  38253. unsigned char* der = NULL;
  38254. char* subject = NULL;
  38255. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  38256. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  38257. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  38258. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  38259. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  38260. ExpectNotNull(der);
  38261. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  38262. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  38263. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  38264. #ifdef DEBUG_WOLFSSL
  38265. if (subject != NULL) {
  38266. fprintf(stderr, "\n\t%s\n", subject);
  38267. }
  38268. #endif
  38269. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38270. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  38271. }
  38272. #endif
  38273. /* Test add entry by text */
  38274. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  38275. 0x0c, cn, (int)sizeof(cn)));
  38276. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  38277. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  38278. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  38279. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  38280. #endif
  38281. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  38282. X509_NAME_ENTRY_free(entry);
  38283. entry = NULL;
  38284. /* Test add entry by NID */
  38285. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  38286. cn, -1, -1, 0), SSL_SUCCESS);
  38287. #ifdef OPENSSL_ALL
  38288. /* stack of name entry */
  38289. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  38290. for (i = 0; i < names_len; i++) {
  38291. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  38292. }
  38293. #endif
  38294. #ifndef NO_BIO
  38295. BIO_free(bio);
  38296. #endif
  38297. X509_free(x509); /* free's nm */
  38298. #endif
  38299. return EXPECT_RESULT();
  38300. }
  38301. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38302. static int test_GENERAL_NAME_set0_othername(void) {
  38303. EXPECT_DECLS;
  38304. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38305. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38306. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38307. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38308. defined(WOLFSSL_FPKI)
  38309. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38310. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38311. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38312. const char * cert_fname = "./certs/server-cert.der";
  38313. const char * key_fname = "./certs/server-key.der";
  38314. X509* x509 = NULL;
  38315. GENERAL_NAME* gn = NULL;
  38316. GENERAL_NAMES* gns = NULL;
  38317. ASN1_OBJECT* upn_oid = NULL;
  38318. ASN1_UTF8STRING *utf8str = NULL;
  38319. ASN1_TYPE *value = NULL;
  38320. X509_EXTENSION * ext = NULL;
  38321. byte* pt = NULL;
  38322. byte der[4096];
  38323. int derSz = 0;
  38324. EVP_PKEY* priv = NULL;
  38325. XFILE f = XBADFILE;
  38326. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  38327. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  38328. if (f != XBADFILE) {
  38329. XFCLOSE(f);
  38330. f = XBADFILE;
  38331. }
  38332. ExpectNotNull(gn = GENERAL_NAME_new());
  38333. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38334. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38335. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38336. ExpectNotNull(value = ASN1_TYPE_new());
  38337. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38338. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  38339. wolfSSL_ASN1_STRING_free(utf8str);
  38340. }
  38341. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38342. if (EXPECT_FAIL()) {
  38343. ASN1_TYPE_free(value);
  38344. }
  38345. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38346. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38347. if (EXPECT_FAIL()) {
  38348. GENERAL_NAME_free(gn);
  38349. gn = NULL;
  38350. }
  38351. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38352. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  38353. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38354. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38355. if (f != XBADFILE) {
  38356. XFCLOSE(f);
  38357. f = XBADFILE;
  38358. }
  38359. pt = der;
  38360. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38361. (const unsigned char**)&pt, derSz));
  38362. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38363. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38364. gns = NULL;
  38365. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38366. NULL));
  38367. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  38368. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  38369. ExpectIntEQ(gn->type, 0);
  38370. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38371. ASN1_OBJECT_free(upn_oid);
  38372. X509_EXTENSION_free(ext);
  38373. X509_free(x509);
  38374. EVP_PKEY_free(priv);
  38375. #endif
  38376. return EXPECT_RESULT();
  38377. }
  38378. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  38379. static int test_othername_and_SID_ext(void) {
  38380. EXPECT_DECLS;
  38381. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38382. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  38383. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  38384. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  38385. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  38386. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  38387. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  38388. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  38389. const char* csr_fname = "./certs/csr.signed.der";
  38390. const char* key_fname = "./certs/server-key.der";
  38391. byte der[4096];
  38392. int derSz = 0;
  38393. X509_REQ* x509 = NULL;
  38394. STACK_OF(X509_EXTENSION) *exts = NULL;
  38395. X509_EXTENSION * san_ext = NULL;
  38396. X509_EXTENSION * ext = NULL;
  38397. GENERAL_NAME* gn = NULL;
  38398. GENERAL_NAMES* gns = NULL;
  38399. ASN1_OBJECT* upn_oid = NULL;
  38400. ASN1_UTF8STRING *utf8str = NULL;
  38401. ASN1_TYPE *value = NULL;
  38402. ASN1_STRING *extval = NULL;
  38403. /* SID extension. SID data format explained here:
  38404. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  38405. */
  38406. uint8_t SidExtension[] = {
  38407. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  38408. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  38409. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  38410. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  38411. uint8_t expectedAltName[] = {
  38412. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  38413. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  38414. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  38415. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  38416. X509_EXTENSION *sid_ext = NULL;
  38417. ASN1_OBJECT* sid_oid = NULL;
  38418. ASN1_OCTET_STRING *sid_data = NULL;
  38419. EVP_PKEY* priv = NULL;
  38420. XFILE f = XBADFILE;
  38421. byte* pt = NULL;
  38422. BIO* bio = NULL;
  38423. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  38424. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  38425. if (f != XBADFILE) {
  38426. XFCLOSE(f);
  38427. f = XBADFILE;
  38428. }
  38429. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  38430. ExpectNotNull(gn = GENERAL_NAME_new());
  38431. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  38432. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  38433. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  38434. ExpectNotNull(value = ASN1_TYPE_new());
  38435. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  38436. if (EXPECT_FAIL()) {
  38437. ASN1_UTF8STRING_free(utf8str);
  38438. }
  38439. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  38440. if (EXPECT_FAIL()) {
  38441. ASN1_TYPE_free(value);
  38442. GENERAL_NAME_free(gn);
  38443. gn = NULL;
  38444. }
  38445. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  38446. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  38447. if (EXPECT_FAIL()) {
  38448. GENERAL_NAME_free(gn);
  38449. }
  38450. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  38451. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  38452. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  38453. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  38454. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  38455. sid_data));
  38456. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  38457. /* Ensure an empty stack doesn't raise an error. */
  38458. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38459. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  38460. if (EXPECT_FAIL()) {
  38461. X509_EXTENSION_free(san_ext);
  38462. }
  38463. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  38464. if (EXPECT_FAIL()) {
  38465. X509_EXTENSION_free(sid_ext);
  38466. }
  38467. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  38468. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  38469. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  38470. if (f != XBADFILE)
  38471. XFCLOSE(f);
  38472. pt = der;
  38473. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  38474. (const unsigned char**)&pt, derSz));
  38475. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  38476. pt = der;
  38477. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  38478. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38479. gns = NULL;
  38480. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38481. exts = NULL;
  38482. ASN1_OBJECT_free(upn_oid);
  38483. ASN1_OBJECT_free(sid_oid);
  38484. ASN1_OCTET_STRING_free(sid_data);
  38485. X509_REQ_free(x509);
  38486. x509 = NULL;
  38487. EVP_PKEY_free(priv);
  38488. /* At this point everything used to generate what is in der is cleaned up.
  38489. * We now read back from der to confirm the extensions were inserted
  38490. * correctly. */
  38491. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  38492. ExpectNotNull(bio);
  38493. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  38494. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  38495. ExpectNotNull(x509);
  38496. BIO_free(bio);
  38497. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  38498. x509));
  38499. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  38500. /* Check the SID extension. */
  38501. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  38502. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38503. ExpectIntEQ(extval->length, sizeof(SidExtension));
  38504. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  38505. /* Check the AltNames extension. */
  38506. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  38507. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  38508. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  38509. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  38510. 0);
  38511. /* Cleanup */
  38512. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  38513. NULL));
  38514. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  38515. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  38516. ExpectIntEQ(gn->type, 0);
  38517. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  38518. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38519. X509_REQ_free(x509);
  38520. #endif
  38521. return EXPECT_RESULT();
  38522. }
  38523. static int test_wolfSSL_X509_set_name(void)
  38524. {
  38525. EXPECT_DECLS;
  38526. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38527. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38528. X509* x509 = NULL;
  38529. X509_NAME* name = NULL;
  38530. ExpectNotNull(name = X509_NAME_new());
  38531. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38532. (byte*)"wolfssl.com", 11, 0, 1),
  38533. WOLFSSL_SUCCESS);
  38534. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38535. (byte*)"support@wolfssl.com", 19, -1,
  38536. 1), WOLFSSL_SUCCESS);
  38537. ExpectNotNull(x509 = X509_new());
  38538. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  38539. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  38540. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  38541. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  38542. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  38543. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  38544. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  38545. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38546. X509_free(x509);
  38547. X509_NAME_free(name);
  38548. #endif /* OPENSSL_ALL && !NO_CERTS */
  38549. return EXPECT_RESULT();
  38550. }
  38551. static int test_wolfSSL_X509_set_notAfter(void)
  38552. {
  38553. EXPECT_DECLS;
  38554. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38555. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38556. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38557. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  38558. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  38559. /* Generalized time will overflow time_t if not long */
  38560. X509* x = NULL;
  38561. BIO* bio = NULL;
  38562. ASN1_TIME *asn_time = NULL;
  38563. ASN1_TIME *time_check = NULL;
  38564. const int year = 365*24*60*60;
  38565. const int day = 24*60*60;
  38566. const int hour = 60*60;
  38567. const int mini = 60;
  38568. int offset_day;
  38569. unsigned char buf[25];
  38570. time_t t;
  38571. /*
  38572. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38573. */
  38574. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  38575. offset_day = 7;
  38576. /*
  38577. * Free these.
  38578. */
  38579. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38580. ExpectNotNull(asn_time);
  38581. ExpectNotNull(x = X509_new());
  38582. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38583. /*
  38584. * Tests
  38585. */
  38586. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  38587. /* time_check is simply (ANS1_TIME*)x->notAfter */
  38588. ExpectNotNull(time_check = X509_get_notAfter(x));
  38589. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38590. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38591. /* Convert to human readable format and compare to intended date */
  38592. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38593. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38594. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  38595. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  38596. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  38597. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  38598. /*
  38599. * Cleanup
  38600. */
  38601. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38602. X509_free(x);
  38603. BIO_free(bio);
  38604. #endif
  38605. return EXPECT_RESULT();
  38606. }
  38607. static int test_wolfSSL_X509_set_notBefore(void)
  38608. {
  38609. EXPECT_DECLS;
  38610. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  38611. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  38612. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  38613. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  38614. X509* x = NULL;
  38615. BIO* bio = NULL;
  38616. ASN1_TIME *asn_time = NULL;
  38617. ASN1_TIME *time_check = NULL;
  38618. const int year = 365*24*60*60;
  38619. const int day = 24*60*60;
  38620. const int hour = 60*60;
  38621. const int mini = 60;
  38622. int offset_day;
  38623. unsigned char buf[25];
  38624. time_t t;
  38625. /*
  38626. * Setup asn_time. APACHE HTTPD uses time(NULL)
  38627. */
  38628. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  38629. offset_day = 7;
  38630. /*
  38631. * Free these.
  38632. */
  38633. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  38634. ExpectNotNull(asn_time);
  38635. ExpectNotNull(x = X509_new());
  38636. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38637. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  38638. /*
  38639. * Main Tests
  38640. */
  38641. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  38642. /* time_check == (ANS1_TIME*)x->notBefore */
  38643. ExpectNotNull(time_check = X509_get_notBefore(x));
  38644. /* ANS1_TIME_check validates by checking if argument can be parsed */
  38645. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  38646. /* Convert to human readable format and compare to intended date */
  38647. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  38648. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  38649. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  38650. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  38651. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  38652. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  38653. /*
  38654. * Cleanup
  38655. */
  38656. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  38657. X509_free(x);
  38658. BIO_free(bio);
  38659. #endif
  38660. return EXPECT_RESULT();
  38661. }
  38662. static int test_wolfSSL_X509_set_version(void)
  38663. {
  38664. EXPECT_DECLS;
  38665. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38666. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  38667. X509* x509 = NULL;
  38668. long v = 2L;
  38669. long maxInt = INT_MAX;
  38670. ExpectNotNull(x509 = X509_new());
  38671. /* These should pass. */
  38672. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  38673. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  38674. /* Fail Case: When v(long) is greater than x509->version(int). */
  38675. v = maxInt+1;
  38676. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  38677. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  38678. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  38679. /* Cleanup */
  38680. X509_free(x509);
  38681. #endif
  38682. return EXPECT_RESULT();
  38683. }
  38684. #ifndef NO_BIO
  38685. static int test_wolfSSL_BIO_gets(void)
  38686. {
  38687. EXPECT_DECLS;
  38688. #if defined(OPENSSL_EXTRA)
  38689. BIO* bio = NULL;
  38690. BIO* bio2 = NULL;
  38691. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  38692. char emp[] = "";
  38693. char bio_buffer[20];
  38694. int bufferSz = 20;
  38695. #ifdef OPENSSL_ALL
  38696. BUF_MEM* emp_bm = NULL;
  38697. BUF_MEM* msg_bm = NULL;
  38698. #endif
  38699. /* try with bad args */
  38700. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  38701. #ifdef OPENSSL_ALL
  38702. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  38703. #endif
  38704. /* try with real msg */
  38705. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38706. XMEMSET(bio_buffer, 0, bufferSz);
  38707. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  38708. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  38709. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  38710. ExpectFalse(bio2 != BIO_next(bio));
  38711. /* make buffer filled with no terminating characters */
  38712. XMEMSET(bio_buffer, 1, bufferSz);
  38713. /* BIO_gets reads a line of data */
  38714. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38715. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38716. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38717. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38718. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38719. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38720. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38721. #ifdef OPENSSL_ALL
  38722. /* test setting the mem_buf manually */
  38723. BIO_free(bio);
  38724. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  38725. ExpectNotNull(emp_bm = BUF_MEM_new());
  38726. ExpectNotNull(msg_bm = BUF_MEM_new());
  38727. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  38728. if (EXPECT_SUCCESS()) {
  38729. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  38730. msg_bm->data = NULL;
  38731. }
  38732. /* emp size is 1 for terminator */
  38733. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  38734. if (EXPECT_SUCCESS()) {
  38735. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  38736. emp_bm->data = emp;
  38737. msg_bm->data = msg;
  38738. }
  38739. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  38740. /* check reading an empty string */
  38741. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38742. ExpectStrEQ(emp, bio_buffer);
  38743. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38744. /* BIO_gets reads a line of data */
  38745. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  38746. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38747. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38748. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38749. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38750. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38751. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38752. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38753. if (EXPECT_SUCCESS())
  38754. emp_bm->data = NULL;
  38755. BUF_MEM_free(emp_bm);
  38756. if (EXPECT_SUCCESS())
  38757. msg_bm->data = NULL;
  38758. BUF_MEM_free(msg_bm);
  38759. #endif
  38760. /* check not null terminated string */
  38761. BIO_free(bio);
  38762. bio = NULL;
  38763. msg[0] = 0x33;
  38764. msg[1] = 0x33;
  38765. msg[2] = 0x33;
  38766. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38767. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  38768. ExpectIntEQ(bio_buffer[0], msg[0]);
  38769. ExpectIntEQ(bio_buffer[1], msg[1]);
  38770. ExpectIntNE(bio_buffer[2], msg[2]);
  38771. BIO_free(bio);
  38772. bio = NULL;
  38773. msg[3] = 0x33;
  38774. bio_buffer[3] = 0x33;
  38775. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  38776. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  38777. ExpectIntEQ(bio_buffer[0], msg[0]);
  38778. ExpectIntEQ(bio_buffer[1], msg[1]);
  38779. ExpectIntEQ(bio_buffer[2], msg[2]);
  38780. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  38781. /* check reading an empty string */
  38782. BIO_free(bio);
  38783. bio = NULL;
  38784. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  38785. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  38786. ExpectStrEQ(emp, bio_buffer);
  38787. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38788. /* check error cases */
  38789. BIO_free(bio);
  38790. bio = NULL;
  38791. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  38792. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38793. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38794. #if !defined(NO_FILESYSTEM)
  38795. {
  38796. BIO* f_bio = NULL;
  38797. XFILE f = XBADFILE;
  38798. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  38799. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38800. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  38801. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  38802. if (EXPECT_FAIL() && (f != XBADFILE)) {
  38803. XFCLOSE(f);
  38804. }
  38805. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  38806. BIO_free(f_bio);
  38807. f_bio = NULL;
  38808. }
  38809. #endif /* NO_FILESYSTEM */
  38810. BIO_free(bio);
  38811. bio = NULL;
  38812. BIO_free(bio2);
  38813. bio2 = NULL;
  38814. /* try with type BIO */
  38815. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  38816. sizeof(msg));
  38817. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  38818. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  38819. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  38820. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  38821. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  38822. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  38823. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  38824. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  38825. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  38826. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  38827. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  38828. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  38829. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  38830. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  38831. BIO_free(bio);
  38832. bio = NULL;
  38833. BIO_free(bio2);
  38834. bio2 = NULL;
  38835. /* check reading an empty string */
  38836. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  38837. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  38838. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  38839. ExpectStrEQ(emp, bio_buffer);
  38840. BIO_free(bio);
  38841. bio = NULL;
  38842. #endif
  38843. return EXPECT_RESULT();
  38844. }
  38845. static int test_wolfSSL_BIO_puts(void)
  38846. {
  38847. EXPECT_DECLS;
  38848. #if defined(OPENSSL_EXTRA)
  38849. BIO* bio = NULL;
  38850. char input[] = "hello\0world\n.....ok\n\0";
  38851. char output[128];
  38852. XMEMSET(output, 0, sizeof(output));
  38853. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38854. ExpectIntEQ(BIO_puts(bio, input), 5);
  38855. ExpectIntEQ(BIO_pending(bio), 5);
  38856. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  38857. ExpectIntEQ(BIO_pending(bio), 19);
  38858. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  38859. ExpectStrEQ(output, "helloworld\n");
  38860. ExpectIntEQ(BIO_pending(bio), 8);
  38861. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  38862. ExpectStrEQ(output, ".....ok\n");
  38863. ExpectIntEQ(BIO_pending(bio), 0);
  38864. ExpectIntEQ(BIO_puts(bio, ""), -1);
  38865. BIO_free(bio);
  38866. #endif
  38867. return EXPECT_RESULT();
  38868. }
  38869. static int test_wolfSSL_BIO_dump(void)
  38870. {
  38871. EXPECT_DECLS;
  38872. #if defined(OPENSSL_EXTRA)
  38873. BIO* bio;
  38874. static const unsigned char data[] = {
  38875. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  38876. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  38877. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  38878. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  38879. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  38880. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  38881. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  38882. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  38883. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  38884. 0xB4
  38885. };
  38886. /* Generated with OpenSSL. */
  38887. static const char expected[] =
  38888. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  38889. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  38890. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  38891. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  38892. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  38893. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  38894. static const char expectedAll[] =
  38895. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  38896. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  38897. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  38898. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  38899. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  38900. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  38901. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  38902. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  38903. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  38904. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  38905. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  38906. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  38907. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  38908. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  38909. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  38910. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  38911. char output[16 * 80];
  38912. int i;
  38913. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38914. /* Example key dumped. */
  38915. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  38916. sizeof(expected) - 1);
  38917. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  38918. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  38919. /* Try every possible value for a character. */
  38920. for (i = 0; i < 256; i++)
  38921. output[i] = i;
  38922. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  38923. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  38924. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  38925. BIO_free(bio);
  38926. #endif
  38927. return EXPECT_RESULT();
  38928. }
  38929. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38930. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38931. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38932. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  38933. {
  38934. (void)ssl;
  38935. (void)buf;
  38936. (void)sz;
  38937. (void)ctx;
  38938. return WOLFSSL_CBIO_ERR_WANT_READ;
  38939. }
  38940. #endif
  38941. static int test_wolfSSL_BIO_should_retry(void)
  38942. {
  38943. EXPECT_DECLS;
  38944. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38945. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  38946. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  38947. tcp_ready ready;
  38948. func_args server_args;
  38949. THREAD_TYPE serverThread;
  38950. SOCKET_T sockfd = 0;
  38951. WOLFSSL_CTX* ctx = NULL;
  38952. WOLFSSL* ssl = NULL;
  38953. char msg[64] = "hello wolfssl!";
  38954. char reply[1024];
  38955. int msgSz = (int)XSTRLEN(msg);
  38956. int ret;
  38957. BIO* bio = NULL;
  38958. XMEMSET(&server_args, 0, sizeof(func_args));
  38959. #ifdef WOLFSSL_TIRTOS
  38960. fdOpenSession(Task_self());
  38961. #endif
  38962. StartTCP();
  38963. InitTcpReady(&ready);
  38964. #if defined(USE_WINDOWS_API)
  38965. /* use RNG to get random port if using windows */
  38966. ready.port = GetRandomPort();
  38967. #endif
  38968. server_args.signal = &ready;
  38969. start_thread(test_server_nofail, &server_args, &serverThread);
  38970. wait_tcp_ready(&server_args);
  38971. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38972. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  38973. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  38974. #endif
  38975. ExpectIntEQ(WOLFSSL_SUCCESS,
  38976. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  38977. ExpectIntEQ(WOLFSSL_SUCCESS,
  38978. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  38979. ExpectIntEQ(WOLFSSL_SUCCESS,
  38980. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  38981. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  38982. /* force retry */
  38983. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  38984. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  38985. ExpectNotNull(ssl);
  38986. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  38987. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  38988. if (EXPECT_FAIL()) {
  38989. wolfSSL_free(ssl);
  38990. ssl = NULL;
  38991. }
  38992. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  38993. ExpectIntNE(BIO_should_retry(bio), 0);
  38994. ExpectIntEQ(BIO_should_read(bio), 0);
  38995. ExpectIntEQ(BIO_should_write(bio), 0);
  38996. /* now perform successful connection */
  38997. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  38998. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  38999. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  39000. ret = wolfSSL_get_error(ssl, -1);
  39001. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  39002. ExpectIntNE(BIO_should_retry(bio), 0);
  39003. if (ret == WOLFSSL_ERROR_WANT_READ)
  39004. ExpectIntEQ(BIO_should_read(bio), 1);
  39005. else
  39006. ExpectIntEQ(BIO_should_read(bio), 0);
  39007. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  39008. ExpectIntEQ(BIO_should_write(bio), 1);
  39009. else
  39010. ExpectIntEQ(BIO_should_write(bio), 0);
  39011. }
  39012. else {
  39013. ExpectIntEQ(BIO_should_retry(bio), 0);
  39014. ExpectIntEQ(BIO_should_read(bio), 0);
  39015. ExpectIntEQ(BIO_should_write(bio), 0);
  39016. }
  39017. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  39018. XSTRLEN("I hear you fa shizzle!")), 0);
  39019. BIO_free(bio);
  39020. wolfSSL_CTX_free(ctx);
  39021. CloseSocket(sockfd);
  39022. join_thread(serverThread);
  39023. FreeTcpReady(&ready);
  39024. #ifdef WOLFSSL_TIRTOS
  39025. fdOpenSession(Task_self());
  39026. #endif
  39027. #endif
  39028. return EXPECT_RESULT();
  39029. }
  39030. static int test_wolfSSL_BIO_connect(void)
  39031. {
  39032. EXPECT_DECLS;
  39033. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39034. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  39035. tcp_ready ready;
  39036. func_args server_args;
  39037. THREAD_TYPE serverThread;
  39038. BIO *tcpBio = NULL;
  39039. BIO *sslBio = NULL;
  39040. SSL_CTX* ctx = NULL;
  39041. SSL *ssl = NULL;
  39042. SSL *sslPtr;
  39043. char msg[] = "hello wolfssl!";
  39044. char reply[30];
  39045. char buff[10] = {0};
  39046. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39047. ExpectIntEQ(WOLFSSL_SUCCESS,
  39048. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39049. ExpectIntEQ(WOLFSSL_SUCCESS,
  39050. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39051. ExpectIntEQ(WOLFSSL_SUCCESS,
  39052. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39053. /* Setup server */
  39054. XMEMSET(&server_args, 0, sizeof(func_args));
  39055. StartTCP();
  39056. InitTcpReady(&ready);
  39057. #if defined(USE_WINDOWS_API)
  39058. /* use RNG to get random port if using windows */
  39059. ready.port = GetRandomPort();
  39060. #endif
  39061. server_args.signal = &ready;
  39062. start_thread(test_server_nofail, &server_args, &serverThread);
  39063. wait_tcp_ready(&server_args);
  39064. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39065. /* Start the test proper */
  39066. /* Setup the TCP BIO */
  39067. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  39068. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  39069. /* Setup the SSL object */
  39070. ExpectNotNull(ssl = SSL_new(ctx));
  39071. SSL_set_connect_state(ssl);
  39072. /* Setup the SSL BIO */
  39073. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  39074. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  39075. if (EXPECT_FAIL()) {
  39076. wolfSSL_free(ssl);
  39077. }
  39078. /* Verify that BIO_get_ssl works. */
  39079. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  39080. ExpectPtrEq(ssl, sslPtr);
  39081. /* Link BIO's so that sslBio uses tcpBio for IO */
  39082. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  39083. /* Do TCP connect */
  39084. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39085. /* Do TLS handshake */
  39086. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39087. /* Test writing */
  39088. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39089. /* Expect length of default wolfSSL reply */
  39090. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39091. /* Clean it all up */
  39092. BIO_free_all(sslBio);
  39093. /* Server clean up */
  39094. join_thread(serverThread);
  39095. FreeTcpReady(&ready);
  39096. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  39097. * after. */
  39098. XMEMSET(&server_args, 0, sizeof(func_args));
  39099. StartTCP();
  39100. InitTcpReady(&ready);
  39101. #if defined(USE_WINDOWS_API)
  39102. /* use RNG to get random port if using windows */
  39103. ready.port = GetRandomPort();
  39104. #endif
  39105. server_args.signal = &ready;
  39106. start_thread(test_server_nofail, &server_args, &serverThread);
  39107. wait_tcp_ready(&server_args);
  39108. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39109. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  39110. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  39111. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  39112. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39113. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39114. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39115. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39116. /* Attempt to close the TLS connection gracefully. */
  39117. BIO_ssl_shutdown(sslBio);
  39118. BIO_free_all(sslBio);
  39119. join_thread(serverThread);
  39120. FreeTcpReady(&ready);
  39121. SSL_CTX_free(ctx);
  39122. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39123. wc_ecc_fp_free(); /* free per thread cache */
  39124. #endif
  39125. #endif
  39126. return EXPECT_RESULT();
  39127. }
  39128. static int test_wolfSSL_BIO_tls(void)
  39129. {
  39130. EXPECT_DECLS;
  39131. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  39132. SSL_CTX* ctx = NULL;
  39133. SSL *ssl = NULL;
  39134. BIO *readBio = NULL;
  39135. BIO *writeBio = NULL;
  39136. int ret;
  39137. int err = 0;
  39138. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39139. ExpectNotNull(ssl = SSL_new(ctx));
  39140. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  39141. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  39142. /* Qt reads data from write-bio,
  39143. * then writes the read data into plain packet.
  39144. * Qt reads data from plain packet,
  39145. * then writes the read data into read-bio.
  39146. */
  39147. SSL_set_bio(ssl, readBio, writeBio);
  39148. do {
  39149. #ifdef WOLFSSL_ASYNC_CRYPT
  39150. if (err == WC_PENDING_E) {
  39151. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39152. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39153. }
  39154. #endif
  39155. ret = SSL_connect(ssl);
  39156. err = SSL_get_error(ssl, 0);
  39157. } while (err == WC_PENDING_E);
  39158. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  39159. /* in this use case, should return WANT READ
  39160. * so that Qt will read the data from plain packet for next state.
  39161. */
  39162. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  39163. SSL_free(ssl);
  39164. SSL_CTX_free(ctx);
  39165. #endif
  39166. return EXPECT_RESULT();
  39167. }
  39168. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39169. defined(HAVE_HTTP_CLIENT)
  39170. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  39171. {
  39172. BIO* clientBio;
  39173. SSL* sslClient;
  39174. SSL_CTX* ctx;
  39175. char connectAddr[20]; /* IP + port */;
  39176. (void)args;
  39177. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  39178. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  39179. AssertIntEQ(BIO_do_connect(clientBio), 1);
  39180. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39181. AssertNotNull(sslClient = SSL_new(ctx));
  39182. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  39183. SSL_set_bio(sslClient, clientBio, clientBio);
  39184. AssertIntEQ(SSL_connect(sslClient), 1);
  39185. SSL_free(sslClient);
  39186. SSL_CTX_free(ctx);
  39187. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39188. wc_ecc_fp_free(); /* free per thread cache */
  39189. #endif
  39190. WOLFSSL_RETURN_FROM_THREAD(0);
  39191. }
  39192. #endif
  39193. static int test_wolfSSL_BIO_accept(void)
  39194. {
  39195. EXPECT_DECLS;
  39196. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39197. defined(HAVE_HTTP_CLIENT)
  39198. BIO* serverBindBio = NULL;
  39199. BIO* serverAcceptBio = NULL;
  39200. SSL* sslServer = NULL;
  39201. SSL_CTX* ctx = NULL;
  39202. func_args args;
  39203. THREAD_TYPE thread;
  39204. char port[10]; /* 10 bytes should be enough to store the string
  39205. * representation of the port */
  39206. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  39207. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  39208. /* First BIO_do_accept binds the port */
  39209. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39210. XMEMSET(&args, 0, sizeof(func_args));
  39211. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  39212. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  39213. /* Let's plug it into SSL to test */
  39214. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  39215. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  39216. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39217. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  39218. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  39219. ExpectNotNull(sslServer = SSL_new(ctx));
  39220. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  39221. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  39222. ExpectIntEQ(SSL_accept(sslServer), 1);
  39223. join_thread(thread);
  39224. BIO_free(serverBindBio);
  39225. SSL_free(sslServer);
  39226. SSL_CTX_free(ctx);
  39227. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  39228. wc_ecc_fp_free(); /* free per thread cache */
  39229. #endif
  39230. #endif
  39231. return EXPECT_RESULT();
  39232. }
  39233. static int test_wolfSSL_BIO_write(void)
  39234. {
  39235. EXPECT_DECLS;
  39236. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  39237. BIO* bio = NULL;
  39238. BIO* bio64 = NULL;
  39239. BIO* bio_mem = NULL;
  39240. BIO* ptr = NULL;
  39241. int sz;
  39242. char msg[] = "conversion test";
  39243. char out[40];
  39244. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  39245. void* bufPtr = NULL;
  39246. BUF_MEM* buf = NULL;
  39247. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39248. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  39249. if (EXPECT_FAIL()) {
  39250. BIO_free(bio64);
  39251. }
  39252. /* now should convert to base64 then write to memory */
  39253. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39254. BIO_flush(bio);
  39255. /* test BIO chain */
  39256. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  39257. ExpectNotNull(buf);
  39258. ExpectIntEQ(buf->length, 25);
  39259. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  39260. ExpectPtrEq(buf->data, bufPtr);
  39261. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  39262. sz = sizeof(out);
  39263. XMEMSET(out, 0, sz);
  39264. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  39265. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39266. /* write then read should return the same message */
  39267. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39268. sz = sizeof(out);
  39269. XMEMSET(out, 0, sz);
  39270. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  39271. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39272. /* now try encoding with no line ending */
  39273. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  39274. #ifdef HAVE_EX_DATA
  39275. BIO_set_ex_data(bio64, 0, (void*) "data");
  39276. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  39277. #endif
  39278. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39279. BIO_flush(bio);
  39280. sz = sizeof(out);
  39281. XMEMSET(out, 0, sz);
  39282. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  39283. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  39284. BIO_free_all(bio); /* frees bio64 also */
  39285. bio = NULL;
  39286. /* test with more than one bio64 in list */
  39287. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  39288. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  39289. if (EXPECT_FAIL()) {
  39290. BIO_free(bio64);
  39291. bio64 = NULL;
  39292. }
  39293. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  39294. ExpectNotNull(BIO_push(bio64, bio_mem));
  39295. if (EXPECT_FAIL()) {
  39296. BIO_free(bio_mem);
  39297. }
  39298. /* now should convert to base64 when stored and then decode with read */
  39299. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  39300. BIO_flush(bio);
  39301. sz = sizeof(out);
  39302. XMEMSET(out, 0, sz);
  39303. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  39304. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  39305. BIO_clear_flags(bio64, ~0);
  39306. BIO_set_retry_read(bio);
  39307. BIO_free_all(bio); /* frees bio64s also */
  39308. bio = NULL;
  39309. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  39310. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  39311. BIO_free(bio);
  39312. #endif
  39313. return EXPECT_RESULT();
  39314. }
  39315. static int test_wolfSSL_BIO_printf(void)
  39316. {
  39317. EXPECT_DECLS;
  39318. #if defined(OPENSSL_ALL)
  39319. BIO* bio = NULL;
  39320. int sz = 7;
  39321. char msg[] = "TLS 1.3 for the world";
  39322. char out[60];
  39323. char expected[] = "TLS 1.3 for the world : sz = 7";
  39324. XMEMSET(out, 0, sizeof(out));
  39325. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39326. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  39327. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  39328. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  39329. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  39330. BIO_free(bio);
  39331. #endif
  39332. return EXPECT_RESULT();
  39333. }
  39334. static int test_wolfSSL_BIO_f_md(void)
  39335. {
  39336. EXPECT_DECLS;
  39337. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  39338. BIO* bio = NULL;
  39339. BIO* mem = NULL;
  39340. char msg[] = "message to hash";
  39341. char out[60];
  39342. EVP_MD_CTX* ctx = NULL;
  39343. const unsigned char testKey[] =
  39344. {
  39345. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39346. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  39347. 0x0b, 0x0b, 0x0b, 0x0b
  39348. };
  39349. const char testData[] = "Hi There";
  39350. const unsigned char testResult[] =
  39351. {
  39352. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  39353. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  39354. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  39355. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  39356. };
  39357. const unsigned char expectedHash[] =
  39358. {
  39359. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  39360. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  39361. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  39362. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  39363. };
  39364. const unsigned char emptyHash[] =
  39365. {
  39366. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  39367. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  39368. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  39369. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  39370. };
  39371. unsigned char check[sizeof(testResult) + 1];
  39372. size_t checkSz = -1;
  39373. EVP_PKEY* key = NULL;
  39374. XMEMSET(out, 0, sizeof(out));
  39375. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39376. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39377. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  39378. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  39379. /* should not be able to write/read yet since just digest wrapper and no
  39380. * data is passing through the bio */
  39381. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  39382. ExpectIntEQ(BIO_pending(bio), 0);
  39383. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  39384. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  39385. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39386. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  39387. BIO_reset(bio);
  39388. /* append BIO mem to bio in order to read/write */
  39389. ExpectNotNull(bio = BIO_push(bio, mem));
  39390. XMEMSET(out, 0, sizeof(out));
  39391. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  39392. ExpectIntEQ(BIO_pending(bio), 16);
  39393. /* this just reads the message and does not hash it (gets calls final) */
  39394. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  39395. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39396. /* create a message digest using BIO */
  39397. XMEMSET(out, 0, sizeof(out));
  39398. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  39399. ExpectIntEQ(BIO_pending(mem), 16);
  39400. ExpectIntEQ(BIO_pending(bio), 16);
  39401. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  39402. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  39403. BIO_free(bio);
  39404. bio = NULL;
  39405. BIO_free(mem);
  39406. mem = NULL;
  39407. /* test with HMAC */
  39408. XMEMSET(out, 0, sizeof(out));
  39409. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39410. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  39411. BIO_get_md_ctx(bio, &ctx);
  39412. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  39413. (int)sizeof(testKey)));
  39414. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  39415. ExpectNotNull(bio = BIO_push(bio, mem));
  39416. BIO_write(bio, testData, (int)strlen(testData));
  39417. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  39418. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  39419. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  39420. EVP_PKEY_free(key);
  39421. BIO_free(bio);
  39422. BIO_free(mem);
  39423. #endif
  39424. return EXPECT_RESULT();
  39425. }
  39426. static int test_wolfSSL_BIO_up_ref(void)
  39427. {
  39428. EXPECT_DECLS;
  39429. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39430. BIO* bio = NULL;
  39431. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  39432. ExpectIntEQ(BIO_up_ref(NULL), 0);
  39433. ExpectIntEQ(BIO_up_ref(bio), 1);
  39434. BIO_free(bio);
  39435. ExpectIntEQ(BIO_up_ref(bio), 1);
  39436. BIO_free(bio);
  39437. BIO_free(bio);
  39438. #endif
  39439. return EXPECT_RESULT();
  39440. }
  39441. static int test_wolfSSL_BIO_reset(void)
  39442. {
  39443. EXPECT_DECLS;
  39444. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  39445. BIO* bio = NULL;
  39446. byte buf[16];
  39447. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  39448. (word32)XSTRLEN("secure your data")));
  39449. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  39450. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  39451. XMEMSET(buf, 0, 16);
  39452. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  39453. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  39454. /* You cannot write to MEM BIO with read-only mode. */
  39455. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  39456. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  39457. XMEMSET(buf, 0, 16);
  39458. ExpectIntEQ(BIO_reset(bio), 0);
  39459. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  39460. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  39461. BIO_free(bio);
  39462. #endif
  39463. return EXPECT_RESULT();
  39464. }
  39465. #endif /* !NO_BIO */
  39466. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39467. /* test that the callback arg is correct */
  39468. static int certCbArg = 0;
  39469. static int certCb(WOLFSSL* ssl, void* arg)
  39470. {
  39471. if (ssl == NULL || arg != &certCbArg)
  39472. return 0;
  39473. if (wolfSSL_is_server(ssl)) {
  39474. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  39475. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39476. return 0;
  39477. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  39478. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39479. return 0;
  39480. }
  39481. else {
  39482. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  39483. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39484. return 0;
  39485. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  39486. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  39487. return 0;
  39488. }
  39489. return 1;
  39490. }
  39491. static int certSetupCb(WOLFSSL_CTX* ctx)
  39492. {
  39493. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  39494. return TEST_SUCCESS;
  39495. }
  39496. /**
  39497. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  39498. * to stop certificate and key loading
  39499. */
  39500. static int certClearCb(WOLFSSL* ssl)
  39501. {
  39502. /* Clear the loaded certs to force the callbacks to set them up */
  39503. SSL_certs_clear(ssl);
  39504. return TEST_SUCCESS;
  39505. }
  39506. #endif
  39507. static int test_wolfSSL_cert_cb(void)
  39508. {
  39509. EXPECT_DECLS;
  39510. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39511. test_ssl_cbf func_cb_client;
  39512. test_ssl_cbf func_cb_server;
  39513. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  39514. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  39515. func_cb_client.ctx_ready = certSetupCb;
  39516. func_cb_client.ssl_ready = certClearCb;
  39517. func_cb_server.ctx_ready = certSetupCb;
  39518. func_cb_server.ssl_ready = certClearCb;
  39519. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  39520. &func_cb_server, NULL), TEST_SUCCESS);
  39521. #endif
  39522. return EXPECT_RESULT();
  39523. }
  39524. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39525. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  39526. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  39527. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  39528. {
  39529. EXPECT_DECLS;
  39530. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  39531. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  39532. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  39533. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  39534. return EXPECT_RESULT();
  39535. }
  39536. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  39537. {
  39538. const byte* suites = NULL;
  39539. word16 suiteSz = 0;
  39540. const byte* hashSigAlgo = NULL;
  39541. word16 hashSigAlgoSz = 0;
  39542. word16 idx = 0;
  39543. int haveRSA = 0;
  39544. int haveECC = 0;
  39545. (void)arg;
  39546. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  39547. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  39548. return 0;
  39549. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  39550. hashSigAlgoSz == 0)
  39551. return 0;
  39552. for (idx = 0; idx < suiteSz; idx += 2) {
  39553. WOLFSSL_CIPHERSUITE_INFO info =
  39554. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  39555. if (info.rsaAuth)
  39556. haveRSA = 1;
  39557. else if (info.eccAuth)
  39558. haveECC = 1;
  39559. }
  39560. if (hashSigAlgoSz > 0) {
  39561. /* sigalgs extension takes precedence over ciphersuites */
  39562. haveRSA = 0;
  39563. haveECC = 0;
  39564. }
  39565. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  39566. int hashAlgo;
  39567. int sigAlgo;
  39568. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  39569. &hashAlgo, &sigAlgo) != 0)
  39570. return 0;
  39571. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  39572. haveRSA = 1;
  39573. else if (sigAlgo == ECDSAk)
  39574. haveECC = 1;
  39575. }
  39576. if (haveRSA) {
  39577. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  39578. != WOLFSSL_SUCCESS)
  39579. return 0;
  39580. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  39581. != WOLFSSL_SUCCESS)
  39582. return 0;
  39583. }
  39584. else if (haveECC) {
  39585. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  39586. != WOLFSSL_SUCCESS)
  39587. return 0;
  39588. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  39589. != WOLFSSL_SUCCESS)
  39590. return 0;
  39591. }
  39592. return 1;
  39593. }
  39594. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  39595. {
  39596. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  39597. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  39598. return TEST_SUCCESS;
  39599. }
  39600. #endif
  39601. /* Testing dynamic ciphers offered by client */
  39602. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  39603. {
  39604. EXPECT_DECLS;
  39605. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  39606. test_ssl_cbf func_cb_client;
  39607. test_ssl_cbf func_cb_server;
  39608. struct {
  39609. method_provider client_meth;
  39610. const char* client_ciphers;
  39611. const char* client_sigalgs;
  39612. const char* client_ca;
  39613. method_provider server_meth;
  39614. } test_params[] = {
  39615. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  39616. #ifdef WOLFSSL_TLS13
  39617. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  39618. {wolfTLSv1_3_client_method,
  39619. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  39620. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  39621. #endif
  39622. #ifdef HAVE_ECC
  39623. {wolfTLSv1_3_client_method,
  39624. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  39625. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  39626. #endif
  39627. #endif
  39628. #ifndef WOLFSSL_NO_TLS12
  39629. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  39630. {wolfTLSv1_2_client_method,
  39631. "DHE-RSA-AES128-GCM-SHA256",
  39632. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  39633. #endif
  39634. #ifdef HAVE_ECC
  39635. {wolfTLSv1_2_client_method,
  39636. "ECDHE-ECDSA-AES128-GCM-SHA256",
  39637. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  39638. #endif
  39639. #endif
  39640. #endif
  39641. };
  39642. size_t i;
  39643. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  39644. printf("\tTesting %s ciphers with %s sigalgs\n",
  39645. test_params[i].client_ciphers, test_params[i].client_sigalgs);
  39646. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  39647. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  39648. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  39649. test_params[i].client_ciphers;
  39650. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  39651. test_params[i].client_sigalgs;
  39652. func_cb_client.method = test_params[i].client_meth;
  39653. func_cb_client.caPemFile = test_params[i].client_ca;
  39654. func_cb_client.ctx_ready =
  39655. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  39656. func_cb_server.ctx_ready =
  39657. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  39658. func_cb_server.ssl_ready = certClearCb; /* Reuse from previous test */
  39659. func_cb_server.method = test_params[i].server_meth;
  39660. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  39661. &func_cb_server, NULL), TEST_SUCCESS);
  39662. }
  39663. #endif
  39664. return EXPECT_RESULT();
  39665. }
  39666. static int test_wolfSSL_ciphersuite_auth(void)
  39667. {
  39668. EXPECT_DECLS;
  39669. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  39670. WOLFSSL_CIPHERSUITE_INFO info;
  39671. (void)info;
  39672. #ifndef WOLFSSL_NO_TLS12
  39673. #ifdef HAVE_CHACHA
  39674. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  39675. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  39676. ExpectIntEQ(info.rsaAuth, 1);
  39677. ExpectIntEQ(info.eccAuth, 0);
  39678. ExpectIntEQ(info.eccStatic, 0);
  39679. ExpectIntEQ(info.psk, 0);
  39680. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  39681. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  39682. ExpectIntEQ(info.rsaAuth, 0);
  39683. ExpectIntEQ(info.eccAuth, 1);
  39684. ExpectIntEQ(info.eccStatic, 0);
  39685. ExpectIntEQ(info.psk, 0);
  39686. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  39687. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  39688. ExpectIntEQ(info.rsaAuth, 0);
  39689. ExpectIntEQ(info.eccAuth, 0);
  39690. ExpectIntEQ(info.eccStatic, 0);
  39691. ExpectIntEQ(info.psk, 1);
  39692. #endif
  39693. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  39694. #ifndef NO_RSA
  39695. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  39696. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  39697. ExpectIntEQ(info.rsaAuth, 1);
  39698. ExpectIntEQ(info.eccAuth, 0);
  39699. ExpectIntEQ(info.eccStatic, 0);
  39700. ExpectIntEQ(info.psk, 0);
  39701. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  39702. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  39703. ExpectIntEQ(info.rsaAuth, 1);
  39704. ExpectIntEQ(info.eccAuth, 0);
  39705. ExpectIntEQ(info.eccStatic, 1);
  39706. ExpectIntEQ(info.psk, 0);
  39707. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  39708. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  39709. ExpectIntEQ(info.rsaAuth, 1);
  39710. ExpectIntEQ(info.eccAuth, 0);
  39711. ExpectIntEQ(info.eccStatic, 1);
  39712. ExpectIntEQ(info.psk, 0);
  39713. #endif
  39714. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  39715. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  39716. ExpectIntEQ(info.rsaAuth, 0);
  39717. ExpectIntEQ(info.eccAuth, 1);
  39718. ExpectIntEQ(info.eccStatic, 0);
  39719. ExpectIntEQ(info.psk, 0);
  39720. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  39721. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  39722. ExpectIntEQ(info.rsaAuth, 0);
  39723. ExpectIntEQ(info.eccAuth, 1);
  39724. ExpectIntEQ(info.eccStatic, 1);
  39725. ExpectIntEQ(info.psk, 0);
  39726. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  39727. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  39728. ExpectIntEQ(info.rsaAuth, 0);
  39729. ExpectIntEQ(info.eccAuth, 0);
  39730. ExpectIntEQ(info.eccStatic, 0);
  39731. ExpectIntEQ(info.psk, 1);
  39732. #endif
  39733. #endif
  39734. #ifdef WOLFSSL_TLS13
  39735. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  39736. TLS_AES_128_GCM_SHA256);
  39737. ExpectIntEQ(info.rsaAuth, 0);
  39738. ExpectIntEQ(info.eccAuth, 0);
  39739. ExpectIntEQ(info.eccStatic, 0);
  39740. ExpectIntEQ(info.psk, 0);
  39741. #endif
  39742. #endif
  39743. return EXPECT_RESULT();
  39744. }
  39745. static int test_wolfSSL_sigalg_info(void)
  39746. {
  39747. EXPECT_DECLS;
  39748. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  39749. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  39750. word16 len = 0;
  39751. word16 idx = 0;
  39752. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  39753. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  39754. for (idx = 0; idx < len; idx += 2) {
  39755. int hashAlgo;
  39756. int sigAlgo;
  39757. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  39758. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  39759. ExpectIntNE(hashAlgo, 0);
  39760. ExpectIntNE(sigAlgo, 0);
  39761. }
  39762. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  39763. 0xFFFFFFFF, &len);
  39764. for (idx = 0; idx < len; idx += 2) {
  39765. int hashAlgo;
  39766. int sigAlgo;
  39767. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  39768. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  39769. ExpectIntNE(hashAlgo, 0);
  39770. }
  39771. #endif
  39772. return EXPECT_RESULT();
  39773. }
  39774. static int test_wolfSSL_SESSION(void)
  39775. {
  39776. EXPECT_DECLS;
  39777. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39778. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39779. !defined(NO_SESSION_CACHE)
  39780. WOLFSSL* ssl = NULL;
  39781. WOLFSSL_CTX* ctx = NULL;
  39782. WOLFSSL_SESSION* sess = NULL;
  39783. WOLFSSL_SESSION* sess_copy = NULL;
  39784. #ifdef OPENSSL_EXTRA
  39785. #ifdef HAVE_EXT_CACHE
  39786. unsigned char* sessDer = NULL;
  39787. unsigned char* ptr = NULL;
  39788. int sz;
  39789. #endif
  39790. const unsigned char context[] = "user app context";
  39791. unsigned int contextSz = (unsigned int)sizeof(context);
  39792. #endif
  39793. int ret, err;
  39794. SOCKET_T sockfd;
  39795. tcp_ready ready;
  39796. func_args server_args;
  39797. THREAD_TYPE serverThread;
  39798. char msg[80];
  39799. const char* sendGET = "GET";
  39800. /* TLS v1.3 requires session tickets */
  39801. /* CHACHA and POLY1305 required for myTicketEncCb */
  39802. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  39803. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  39804. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  39805. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  39806. #else
  39807. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39808. #endif
  39809. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  39810. WOLFSSL_FILETYPE_PEM));
  39811. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  39812. WOLFSSL_FILETYPE_PEM));
  39813. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  39814. WOLFSSL_SUCCESS);
  39815. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39816. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  39817. #endif
  39818. #ifdef HAVE_SESSION_TICKET
  39819. /* Use session tickets, for ticket tests below */
  39820. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  39821. #endif
  39822. XMEMSET(&server_args, 0, sizeof(func_args));
  39823. #ifdef WOLFSSL_TIRTOS
  39824. fdOpenSession(Task_self());
  39825. #endif
  39826. StartTCP();
  39827. InitTcpReady(&ready);
  39828. #if defined(USE_WINDOWS_API)
  39829. /* use RNG to get random port if using windows */
  39830. ready.port = GetRandomPort();
  39831. #endif
  39832. server_args.signal = &ready;
  39833. start_thread(test_server_nofail, &server_args, &serverThread);
  39834. wait_tcp_ready(&server_args);
  39835. /* client connection */
  39836. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39837. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  39838. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39839. #ifdef WOLFSSL_ASYNC_CRYPT
  39840. err = 0; /* Reset error */
  39841. #endif
  39842. do {
  39843. #ifdef WOLFSSL_ASYNC_CRYPT
  39844. if (err == WC_PENDING_E) {
  39845. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39846. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39847. }
  39848. #endif
  39849. ret = wolfSSL_connect(ssl);
  39850. err = wolfSSL_get_error(ssl, 0);
  39851. } while (err == WC_PENDING_E);
  39852. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  39853. #ifdef WOLFSSL_ASYNC_CRYPT
  39854. err = 0; /* Reset error */
  39855. #endif
  39856. do {
  39857. #ifdef WOLFSSL_ASYNC_CRYPT
  39858. if (err == WC_PENDING_E) {
  39859. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39860. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39861. }
  39862. #endif
  39863. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  39864. err = wolfSSL_get_error(ssl, 0);
  39865. } while (err == WC_PENDING_E);
  39866. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  39867. #ifdef WOLFSSL_ASYNC_CRYPT
  39868. err = 0; /* Reset error */
  39869. #endif
  39870. do {
  39871. #ifdef WOLFSSL_ASYNC_CRYPT
  39872. if (err == WC_PENDING_E) {
  39873. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  39874. if (ret < 0) { break; } else if (ret == 0) { continue; }
  39875. }
  39876. #endif
  39877. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  39878. err = wolfSSL_get_error(ssl, 0);
  39879. } while (err == WC_PENDING_E);
  39880. ExpectIntEQ(ret, 23);
  39881. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  39882. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  39883. #ifdef HAVE_EXT_CACHE
  39884. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  39885. * HAVE_EXT_CACHE we get new objects each time */
  39886. #endif
  39887. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39888. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  39889. sess = wolfSSL_get_session(ssl);
  39890. #ifdef OPENSSL_EXTRA
  39891. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  39892. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  39893. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  39894. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  39895. #ifdef HAVE_SESSION_TICKET
  39896. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  39897. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  39898. SESSION_TICKET_HINT_DEFAULT);
  39899. #else
  39900. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  39901. #endif
  39902. #else
  39903. (void)sess;
  39904. #endif /* OPENSSL_EXTRA */
  39905. /* Retain copy of the session for later testing */
  39906. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  39907. wolfSSL_shutdown(ssl);
  39908. wolfSSL_free(ssl); ssl = NULL;
  39909. join_thread(serverThread);
  39910. FreeTcpReady(&ready);
  39911. #ifdef WOLFSSL_TIRTOS
  39912. fdOpenSession(Task_self());
  39913. #endif
  39914. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  39915. {
  39916. X509 *x509 = NULL;
  39917. char buf[30];
  39918. int bufSz = 0;
  39919. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  39920. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  39921. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  39922. sizeof(buf))), 0);
  39923. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  39924. if (bufSz == 7) {
  39925. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  39926. }
  39927. if (bufSz == 16) {
  39928. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  39929. }
  39930. }
  39931. #endif
  39932. #ifdef HAVE_EXT_CACHE
  39933. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  39934. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  39935. sess_copy = NULL;
  39936. #endif
  39937. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  39938. /* get session from DER and update the timeout */
  39939. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  39940. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  39941. wolfSSL_SESSION_free(sess); sess = NULL;
  39942. sess = NULL;
  39943. ptr = sessDer;
  39944. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  39945. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  39946. (const unsigned char**)&ptr, sz));
  39947. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  39948. sessDer = NULL;
  39949. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  39950. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39951. #endif
  39952. /* successful set session test */
  39953. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39954. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  39955. #ifdef HAVE_SESSION_TICKET
  39956. /* Test set/get session ticket */
  39957. {
  39958. const char* ticket = "This is a session ticket";
  39959. char buf[64] = {0};
  39960. word32 bufSz = (word32)sizeof(buf);
  39961. ExpectIntEQ(SSL_SUCCESS,
  39962. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  39963. (word32)XSTRLEN(ticket)));
  39964. ExpectIntEQ(SSL_SUCCESS,
  39965. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  39966. ExpectStrEQ(ticket, buf);
  39967. }
  39968. #endif
  39969. #ifdef OPENSSL_EXTRA
  39970. /* session timeout case */
  39971. /* make the session to be expired */
  39972. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  39973. XSLEEP_MS(1200);
  39974. /* SSL_set_session should reject specified session but return success
  39975. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  39976. */
  39977. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39978. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  39979. #else
  39980. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  39981. #endif
  39982. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  39983. #ifdef WOLFSSL_SESSION_ID_CTX
  39984. /* fail case with miss match session context IDs (use compatibility API) */
  39985. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  39986. SSL_SUCCESS);
  39987. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39988. wolfSSL_free(ssl); ssl = NULL;
  39989. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  39990. SSL_FAILURE);
  39991. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  39992. SSL_SUCCESS);
  39993. ExpectNotNull(ssl = wolfSSL_new(ctx));
  39994. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  39995. #endif
  39996. #endif /* OPENSSL_EXTRA */
  39997. wolfSSL_free(ssl);
  39998. wolfSSL_SESSION_free(sess);
  39999. wolfSSL_CTX_free(ctx);
  40000. #endif
  40001. return EXPECT_RESULT();
  40002. }
  40003. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40004. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40005. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40006. !defined(WOLFSSL_NO_TLS12)
  40007. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  40008. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  40009. {
  40010. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  40011. /* returns previous timeout value */
  40012. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  40013. #else
  40014. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  40015. #endif
  40016. }
  40017. /* set the session to timeout in a second */
  40018. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  40019. {
  40020. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  40021. }
  40022. /* store the client side session from the first successful connection */
  40023. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  40024. {
  40025. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  40026. NULL); /* ref count 1 */
  40027. }
  40028. /* wait till session is expired then set it in the WOLFSSL struct for use */
  40029. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  40030. {
  40031. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  40032. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40033. WOLFSSL_SUCCESS);
  40034. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  40035. }
  40036. /* set expired session in the WOLFSSL struct for use */
  40037. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  40038. {
  40039. XSLEEP_MS(1200); /* wait a second for session to expire */
  40040. /* set the expired session, call to set session fails but continuing on
  40041. after failure should be handled here */
  40042. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40043. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40044. WOLFSSL_SUCCESS);
  40045. #else
  40046. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40047. WOLFSSL_SUCCESS);
  40048. #endif
  40049. }
  40050. /* check that the expired session was not reused */
  40051. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  40052. {
  40053. /* since the session has expired it should not have been reused */
  40054. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  40055. }
  40056. #endif
  40057. static int test_wolfSSL_SESSION_expire_downgrade(void)
  40058. {
  40059. EXPECT_DECLS;
  40060. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40061. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40062. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40063. !defined(WOLFSSL_NO_TLS12)
  40064. WOLFSSL_CTX* ctx = NULL;
  40065. callback_functions server_cbf, client_cbf;
  40066. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  40067. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  40068. /* force server side to use TLS 1.2 */
  40069. server_cbf.ctx = ctx;
  40070. server_cbf.method = wolfTLSv1_2_server_method;
  40071. client_cbf.method = wolfSSLv23_client_method;
  40072. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40073. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  40074. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  40075. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40076. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40077. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40078. /* set the previously created session and wait till expired */
  40079. server_cbf.ctx = ctx;
  40080. client_cbf.method = wolfSSLv23_client_method;
  40081. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40082. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  40083. client_cbf.on_result =
  40084. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40085. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40086. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40087. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40088. /* set the previously created expired session */
  40089. server_cbf.ctx = ctx;
  40090. client_cbf.method = wolfSSLv23_client_method;
  40091. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40092. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  40093. client_cbf.on_result =
  40094. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40095. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40096. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40097. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40098. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  40099. wolfSSL_CTX_free(ctx);
  40100. #endif
  40101. return EXPECT_RESULT();
  40102. }
  40103. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40104. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40105. static int clientSessRemCountMalloc = 0;
  40106. static int serverSessRemCountMalloc = 0;
  40107. static int clientSessRemCountFree = 0;
  40108. static int serverSessRemCountFree = 0;
  40109. static WOLFSSL_CTX* serverSessCtx = NULL;
  40110. static WOLFSSL_SESSION* serverSess = NULL;
  40111. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40112. !defined(NO_SESSION_CACHE_REF)
  40113. static WOLFSSL_CTX* clientSessCtx = NULL;
  40114. static WOLFSSL_SESSION* clientSess = NULL;
  40115. #endif
  40116. static int serverSessRemIdx = 3;
  40117. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  40118. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  40119. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  40120. {
  40121. int* side;
  40122. (void)ctx;
  40123. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  40124. if (side != NULL) {
  40125. if (*side == WOLFSSL_CLIENT_END)
  40126. clientSessRemCountFree++;
  40127. else
  40128. serverSessRemCountFree++;
  40129. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  40130. }
  40131. }
  40132. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  40133. {
  40134. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  40135. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  40136. !defined(NO_SESSION_CACHE_REF)
  40137. {
  40138. EXPECT_DECLS;
  40139. /* Allow downgrade, set min version, and disable TLS 1.3.
  40140. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  40141. * reference to the session cache. But with WOLFSSL_TLS13 and without
  40142. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  40143. * the session in the cache. In this case we need to downgrade to
  40144. * previous versions to just use the legacy session ID field. */
  40145. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  40146. SSL_SUCCESS);
  40147. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  40148. SSL_SUCCESS);
  40149. return EXPECT_RESULT();
  40150. }
  40151. #else
  40152. return TEST_SUCCESS;
  40153. #endif
  40154. }
  40155. static int SessRemSslSetupCb(WOLFSSL* ssl)
  40156. {
  40157. EXPECT_DECLS;
  40158. int* side;
  40159. if (SSL_is_server(ssl)) {
  40160. side = &sessRemCtx_Server;
  40161. serverSessRemCountMalloc++;
  40162. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  40163. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  40164. SSL_SUCCESS);
  40165. }
  40166. else {
  40167. side = &sessRemCtx_Client;
  40168. clientSessRemCountMalloc++;
  40169. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40170. !defined(NO_SESSION_CACHE_REF)
  40171. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  40172. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  40173. SSL_SUCCESS);
  40174. #endif
  40175. }
  40176. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  40177. serverSessRemIdx, side), SSL_SUCCESS);
  40178. return EXPECT_RESULT();
  40179. }
  40180. #endif
  40181. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  40182. {
  40183. EXPECT_DECLS;
  40184. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  40185. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  40186. /* Check that the remove callback gets called for external data in a
  40187. * session object */
  40188. test_ssl_cbf func_cb;
  40189. XMEMSET(&func_cb, 0, sizeof(func_cb));
  40190. func_cb.ctx_ready = SessRemCtxSetupCb;
  40191. func_cb.on_result = SessRemSslSetupCb;
  40192. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  40193. NULL), TEST_SUCCESS);
  40194. /* Both should have been allocated */
  40195. ExpectIntEQ(clientSessRemCountMalloc, 1);
  40196. ExpectIntEQ(serverSessRemCountMalloc, 1);
  40197. /* This should not be called yet. Session wasn't evicted from cache yet. */
  40198. ExpectIntEQ(clientSessRemCountFree, 0);
  40199. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40200. !defined(NO_SESSION_CACHE_REF)
  40201. /* Force a cache lookup */
  40202. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  40203. /* Force a cache update */
  40204. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  40205. /* This should set the timeout to 0 and call the remove callback from within
  40206. * the session cache. */
  40207. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  40208. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  40209. ExpectIntEQ(clientSessRemCountFree, 1);
  40210. #endif
  40211. /* Server session is in the cache so ex_data isn't free'd with the SSL
  40212. * object */
  40213. ExpectIntEQ(serverSessRemCountFree, 0);
  40214. /* Force a cache lookup */
  40215. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  40216. /* Force a cache update */
  40217. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  40218. /* This should set the timeout to 0 and call the remove callback from within
  40219. * the session cache. */
  40220. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  40221. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  40222. ExpectIntEQ(serverSessRemCountFree, 1);
  40223. /* Need to free the references that we kept */
  40224. SSL_CTX_free(serverSessCtx);
  40225. SSL_SESSION_free(serverSess);
  40226. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  40227. !defined(NO_SESSION_CACHE_REF)
  40228. SSL_CTX_free(clientSessCtx);
  40229. SSL_SESSION_free(clientSess);
  40230. #endif
  40231. #endif
  40232. return EXPECT_RESULT();
  40233. }
  40234. static int test_wolfSSL_ticket_keys(void)
  40235. {
  40236. EXPECT_DECLS;
  40237. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  40238. !defined(NO_WOLFSSL_SERVER)
  40239. WOLFSSL_CTX* ctx = NULL;
  40240. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  40241. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  40242. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  40243. WOLFSSL_FAILURE);
  40244. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  40245. WOLFSSL_FAILURE);
  40246. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  40247. WOLFSSL_FAILURE);
  40248. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  40249. WOLFSSL_FAILURE);
  40250. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  40251. WOLFSSL_FAILURE);
  40252. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  40253. WOLFSSL_FAILURE);
  40254. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  40255. WOLFSSL_FAILURE);
  40256. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  40257. WOLFSSL_FAILURE);
  40258. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  40259. WOLFSSL_FAILURE);
  40260. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  40261. WOLFSSL_FAILURE);
  40262. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  40263. WOLFSSL_FAILURE);
  40264. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  40265. WOLFSSL_FAILURE);
  40266. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  40267. WOLFSSL_FAILURE);
  40268. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  40269. WOLFSSL_FAILURE);
  40270. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  40271. WOLFSSL_SUCCESS);
  40272. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  40273. WOLFSSL_SUCCESS);
  40274. wolfSSL_CTX_free(ctx);
  40275. #endif
  40276. return EXPECT_RESULT();
  40277. }
  40278. #ifndef NO_BIO
  40279. static int test_wolfSSL_d2i_PUBKEY(void)
  40280. {
  40281. EXPECT_DECLS;
  40282. #if defined(OPENSSL_EXTRA)
  40283. BIO* bio = NULL;
  40284. EVP_PKEY* pkey = NULL;
  40285. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40286. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  40287. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  40288. /* RSA PUBKEY test */
  40289. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  40290. sizeof_client_keypub_der_2048), 0);
  40291. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40292. EVP_PKEY_free(pkey);
  40293. pkey = NULL;
  40294. #endif
  40295. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  40296. /* ECC PUBKEY test */
  40297. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  40298. sizeof_ecc_clikeypub_der_256), 0);
  40299. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40300. EVP_PKEY_free(pkey);
  40301. pkey = NULL;
  40302. #endif
  40303. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  40304. /* DSA PUBKEY test */
  40305. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  40306. sizeof_dsa_pub_key_der_2048), 0);
  40307. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40308. EVP_PKEY_free(pkey);
  40309. pkey = NULL;
  40310. #endif
  40311. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  40312. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  40313. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  40314. (HAVE_FIPS_VERSION > 2))
  40315. /* DH PUBKEY test */
  40316. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  40317. sizeof_dh_pub_key_der_2048), 0);
  40318. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  40319. EVP_PKEY_free(pkey);
  40320. pkey = NULL;
  40321. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40322. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  40323. BIO_free(bio);
  40324. (void)pkey;
  40325. #endif
  40326. return EXPECT_RESULT();
  40327. }
  40328. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  40329. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  40330. {
  40331. EXPECT_DECLS;
  40332. BIO* bio = NULL;
  40333. EVP_PKEY* pkey = NULL;
  40334. WOLFSSL_CTX* ctx = NULL;
  40335. #if defined(WOLFSSL_KEY_GEN)
  40336. unsigned char buff[4096];
  40337. unsigned char* bufPtr = buff;
  40338. #endif
  40339. /* test creating new EVP_PKEY with bad arg */
  40340. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  40341. /* test loading RSA key using BIO */
  40342. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  40343. {
  40344. XFILE file = XBADFILE;
  40345. const char* fname = "./certs/server-key.der";
  40346. size_t sz;
  40347. byte* buf = NULL;
  40348. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  40349. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  40350. ExpectTrue((sz = XFTELL(file)) != 0);
  40351. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  40352. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  40353. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  40354. if (file != XBADFILE) {
  40355. XFCLOSE(file);
  40356. }
  40357. /* Test using BIO new mem and loading DER private key */
  40358. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  40359. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  40360. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  40361. BIO_free(bio);
  40362. bio = NULL;
  40363. EVP_PKEY_free(pkey);
  40364. pkey = NULL;
  40365. }
  40366. #endif
  40367. /* test loading ECC key using BIO */
  40368. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  40369. {
  40370. XFILE file = XBADFILE;
  40371. const char* fname = "./certs/ecc-key.der";
  40372. size_t sz;
  40373. byte* buf = NULL;
  40374. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  40375. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  40376. ExpectTrue((sz = XFTELL(file)) != 0);
  40377. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  40378. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  40379. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  40380. if (file != XBADFILE)
  40381. XFCLOSE(file);
  40382. /* Test using BIO new mem and loading DER private key */
  40383. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  40384. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  40385. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  40386. BIO_free(bio);
  40387. bio = NULL;
  40388. EVP_PKEY_free(pkey);
  40389. pkey = NULL;
  40390. }
  40391. #endif
  40392. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40393. #ifndef NO_WOLFSSL_SERVER
  40394. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  40395. #else
  40396. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  40397. #endif
  40398. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  40399. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  40400. {
  40401. RSA* rsa = NULL;
  40402. /* Tests bad parameters */
  40403. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  40404. /* RSA not set yet, expecting to fail*/
  40405. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  40406. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  40407. /* set RSA using bio*/
  40408. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  40409. sizeof_client_key_der_2048), 0);
  40410. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  40411. ExpectNotNull(rsa);
  40412. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  40413. /* i2d RSAprivate key tests */
  40414. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  40415. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  40416. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40417. sizeof_client_key_der_2048);
  40418. bufPtr -= sizeof_client_key_der_2048;
  40419. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40420. sizeof_client_key_der_2048), 0);
  40421. bufPtr = NULL;
  40422. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  40423. sizeof_client_key_der_2048);
  40424. ExpectNotNull(bufPtr);
  40425. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  40426. sizeof_client_key_der_2048), 0);
  40427. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  40428. RSA_free(rsa);
  40429. rsa = RSA_new();
  40430. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  40431. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  40432. RSA_free(rsa);
  40433. }
  40434. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  40435. SSL_CTX_free(ctx);
  40436. ctx = NULL;
  40437. BIO_free(bio);
  40438. bio = NULL;
  40439. return EXPECT_RESULT();
  40440. }
  40441. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  40442. #endif /* !NO_BIO */
  40443. static int test_wolfSSL_sk_GENERAL_NAME(void)
  40444. {
  40445. EXPECT_DECLS;
  40446. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40447. !defined(NO_RSA)
  40448. X509* x509 = NULL;
  40449. GENERAL_NAME* gn = NULL;
  40450. unsigned char buf[4096];
  40451. const unsigned char* bufPt = NULL;
  40452. int bytes = 0;
  40453. int i;
  40454. int j;
  40455. XFILE f = XBADFILE;
  40456. STACK_OF(GENERAL_NAME)* sk = NULL;
  40457. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40458. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40459. if (f != XBADFILE)
  40460. XFCLOSE(f);
  40461. for (j = 0; j < 2; ++j) {
  40462. bufPt = buf;
  40463. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40464. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40465. NID_subject_alt_name, NULL, NULL));
  40466. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  40467. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  40468. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  40469. if (gn != NULL) {
  40470. switch (gn->type) {
  40471. case GEN_DNS:
  40472. fprintf(stderr, "found type GEN_DNS\n");
  40473. break;
  40474. case GEN_EMAIL:
  40475. fprintf(stderr, "found type GEN_EMAIL\n");
  40476. break;
  40477. case GEN_URI:
  40478. fprintf(stderr, "found type GEN_URI\n");
  40479. break;
  40480. }
  40481. }
  40482. }
  40483. X509_free(x509);
  40484. x509 = NULL;
  40485. if (j == 0) {
  40486. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40487. }
  40488. else {
  40489. /*
  40490. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  40491. * it was supposed to. This is a regression test for that bug.
  40492. */
  40493. GENERAL_NAMES_free(sk);
  40494. }
  40495. sk = NULL;
  40496. }
  40497. #endif
  40498. return EXPECT_RESULT();
  40499. }
  40500. static int test_wolfSSL_GENERAL_NAME_print(void)
  40501. {
  40502. EXPECT_DECLS;
  40503. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  40504. X509* x509 = NULL;
  40505. GENERAL_NAME* gn = NULL;
  40506. unsigned char buf[4096];
  40507. const unsigned char* bufPt = NULL;
  40508. int bytes;
  40509. XFILE f = XBADFILE;
  40510. STACK_OF(GENERAL_NAME)* sk = NULL;
  40511. BIO* out = NULL;
  40512. unsigned char outbuf[128];
  40513. X509_EXTENSION* ext = NULL;
  40514. AUTHORITY_INFO_ACCESS* aia = NULL;
  40515. ACCESS_DESCRIPTION* ad = NULL;
  40516. ASN1_IA5STRING *dnsname = NULL;
  40517. const unsigned char v4Addr[] = {192,168,53,1};
  40518. const unsigned char v6Addr[] =
  40519. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  40520. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  40521. const unsigned char email[] =
  40522. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  40523. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  40524. const char* dnsStr = "DNS:example.com";
  40525. const char* uriStr = "URI:http://127.0.0.1:22220";
  40526. const char* v4addStr = "IP Address:192.168.53.1";
  40527. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  40528. const char* emailStr = "email:info@wolfssl.com";
  40529. const char* othrStr = "othername:<unsupported>";
  40530. const char* x400Str = "X400Name:<unsupported>";
  40531. const char* ediStr = "EdiPartyName:<unsupported>";
  40532. /* BIO to output */
  40533. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  40534. /* test for NULL param */
  40535. gn = NULL;
  40536. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  40537. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  40538. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  40539. /* test for GEN_DNS */
  40540. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  40541. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40542. if (f != XBADFILE) {
  40543. XFCLOSE(f);
  40544. f = XBADFILE;
  40545. }
  40546. bufPt = buf;
  40547. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40548. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  40549. NID_subject_alt_name, NULL, NULL));
  40550. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  40551. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40552. XMEMSET(outbuf, 0, sizeof(outbuf));
  40553. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40554. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40555. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  40556. gn = NULL;
  40557. sk = NULL;
  40558. X509_free(x509);
  40559. x509 = NULL;
  40560. /* Lets test for setting as well. */
  40561. ExpectNotNull(gn = GENERAL_NAME_new());
  40562. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  40563. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  40564. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  40565. dnsname = NULL;
  40566. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40567. XMEMSET(outbuf, 0, sizeof(outbuf));
  40568. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40569. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  40570. GENERAL_NAME_free(gn);
  40571. /* test for GEN_URI */
  40572. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  40573. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40574. if (f != XBADFILE) {
  40575. XFCLOSE(f);
  40576. f = XBADFILE;
  40577. }
  40578. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  40579. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  40580. ext));
  40581. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  40582. if (ad != NULL) {
  40583. gn = ad->location;
  40584. }
  40585. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40586. gn = NULL;
  40587. XMEMSET(outbuf,0,sizeof(outbuf));
  40588. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40589. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  40590. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40591. aia = NULL;
  40592. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  40593. ExpectNotNull(aia);
  40594. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  40595. aia = NULL;
  40596. X509_free(x509);
  40597. x509 = NULL;
  40598. /* test for GEN_IPADD */
  40599. /* ip v4 address */
  40600. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40601. if (gn != NULL) {
  40602. gn->type = GEN_IPADD;
  40603. if (gn->d.iPAddress != NULL) {
  40604. gn->d.iPAddress->length = sizeof(v4Addr);
  40605. }
  40606. }
  40607. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  40608. sizeof(v4Addr)), 1);
  40609. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40610. XMEMSET(outbuf,0,sizeof(outbuf));
  40611. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40612. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  40613. GENERAL_NAME_free(gn);
  40614. gn = NULL;
  40615. /* ip v6 address */
  40616. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40617. if (gn != NULL) {
  40618. gn->type = GEN_IPADD;
  40619. if (gn->d.iPAddress != NULL) {
  40620. gn->d.iPAddress->length = sizeof(v6Addr);
  40621. }
  40622. }
  40623. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  40624. sizeof(v6Addr)), 1);
  40625. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40626. XMEMSET(outbuf,0,sizeof(outbuf));
  40627. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40628. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  40629. GENERAL_NAME_free(gn);
  40630. gn = NULL;
  40631. /* test for GEN_EMAIL */
  40632. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40633. if (gn != NULL) {
  40634. gn->type = GEN_EMAIL;
  40635. if (gn->d.rfc822Name != NULL) {
  40636. gn->d.rfc822Name->length = sizeof(email);
  40637. }
  40638. }
  40639. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  40640. 1);
  40641. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40642. XMEMSET(outbuf,0,sizeof(outbuf));
  40643. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40644. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  40645. GENERAL_NAME_free(gn);
  40646. gn = NULL;
  40647. /* test for GEN_OTHERNAME */
  40648. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40649. if (gn != NULL) {
  40650. gn->type = GEN_OTHERNAME;
  40651. }
  40652. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40653. XMEMSET(outbuf,0,sizeof(outbuf));
  40654. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40655. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  40656. GENERAL_NAME_free(gn);
  40657. gn = NULL;
  40658. /* test for GEN_X400 */
  40659. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40660. if (gn != NULL) {
  40661. gn->type = GEN_X400;
  40662. }
  40663. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40664. XMEMSET(outbuf,0,sizeof(outbuf));
  40665. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40666. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  40667. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40668. if (gn != NULL) {
  40669. gn->type = GEN_IA5;
  40670. }
  40671. GENERAL_NAME_free(gn);
  40672. gn = NULL;
  40673. /* test for GEN_EDIPARTY */
  40674. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  40675. if (gn != NULL) {
  40676. gn->type = GEN_EDIPARTY;
  40677. }
  40678. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  40679. XMEMSET(outbuf,0,sizeof(outbuf));
  40680. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  40681. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  40682. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  40683. if (gn != NULL) {
  40684. gn->type = GEN_IA5;
  40685. }
  40686. GENERAL_NAME_free(gn);
  40687. gn = NULL;
  40688. BIO_free(out);
  40689. #endif /* OPENSSL_ALL */
  40690. return EXPECT_RESULT();
  40691. }
  40692. static int test_wolfSSL_sk_DIST_POINT(void)
  40693. {
  40694. EXPECT_DECLS;
  40695. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40696. !defined(NO_RSA)
  40697. X509* x509 = NULL;
  40698. unsigned char buf[4096];
  40699. const unsigned char* bufPt;
  40700. int bytes;
  40701. int i;
  40702. int j;
  40703. XFILE f = XBADFILE;
  40704. DIST_POINT* dp;
  40705. DIST_POINT_NAME* dpn;
  40706. GENERAL_NAME* gn;
  40707. ASN1_IA5STRING* uri;
  40708. STACK_OF(DIST_POINT)* dps = NULL;
  40709. STACK_OF(GENERAL_NAME)* gns = NULL;
  40710. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  40711. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  40712. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  40713. if (f != XBADFILE)
  40714. XFCLOSE(f);
  40715. bufPt = buf;
  40716. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  40717. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  40718. NID_crl_distribution_points, NULL, NULL));
  40719. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  40720. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  40721. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  40722. ExpectNotNull(dpn = dp->distpoint);
  40723. /* this should be type 0, fullname */
  40724. ExpectIntEQ(dpn->type, 0);
  40725. ExpectNotNull(gns = dp->distpoint->name.fullname);
  40726. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40727. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  40728. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  40729. ExpectIntEQ(gn->type, GEN_URI);
  40730. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  40731. ExpectNotNull(uri->data);
  40732. ExpectIntGT(uri->length, 0);
  40733. }
  40734. }
  40735. X509_free(x509);
  40736. CRL_DIST_POINTS_free(dps);
  40737. #endif
  40738. return EXPECT_RESULT();
  40739. }
  40740. static int test_wolfSSL_verify_mode(void)
  40741. {
  40742. EXPECT_DECLS;
  40743. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40744. WOLFSSL* ssl = NULL;
  40745. WOLFSSL_CTX* ctx = NULL;
  40746. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40747. ExpectNotNull(ssl = SSL_new(ctx));
  40748. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40749. SSL_free(ssl);
  40750. ssl = NULL;
  40751. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40752. ExpectNotNull(ssl = SSL_new(ctx));
  40753. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40754. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40755. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40756. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  40757. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40758. SSL_free(ssl);
  40759. ssl = NULL;
  40760. wolfSSL_CTX_set_verify(ctx,
  40761. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40762. ExpectNotNull(ssl = SSL_new(ctx));
  40763. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  40764. ExpectIntEQ(SSL_get_verify_mode(ssl),
  40765. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40766. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  40767. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  40768. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40769. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  40770. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  40771. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  40772. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  40773. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40774. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  40775. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  40776. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  40777. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  40778. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  40779. #endif
  40780. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  40781. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  40782. SSL_free(ssl);
  40783. SSL_CTX_free(ctx);
  40784. #endif
  40785. return EXPECT_RESULT();
  40786. }
  40787. static int test_wolfSSL_verify_depth(void)
  40788. {
  40789. EXPECT_DECLS;
  40790. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40791. WOLFSSL* ssl = NULL;
  40792. WOLFSSL_CTX* ctx = NULL;
  40793. long depth;
  40794. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40795. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  40796. ExpectNotNull(ssl = SSL_new(ctx));
  40797. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  40798. SSL_free(ssl);
  40799. ssl = NULL;
  40800. SSL_CTX_set_verify_depth(ctx, -1);
  40801. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  40802. SSL_CTX_set_verify_depth(ctx, 2);
  40803. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  40804. ExpectNotNull(ssl = SSL_new(ctx));
  40805. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  40806. SSL_free(ssl);
  40807. SSL_CTX_free(ctx);
  40808. #endif
  40809. return EXPECT_RESULT();
  40810. }
  40811. static int test_wolfSSL_verify_result(void)
  40812. {
  40813. EXPECT_DECLS;
  40814. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  40815. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  40816. WOLFSSL* ssl = NULL;
  40817. WOLFSSL_CTX* ctx = NULL;
  40818. long result = 0xDEADBEEF;
  40819. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  40820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40821. ExpectNotNull(ssl = SSL_new(ctx));
  40822. wolfSSL_set_verify_result(ssl, result);
  40823. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  40824. SSL_free(ssl);
  40825. SSL_CTX_free(ctx);
  40826. #endif
  40827. return EXPECT_RESULT();
  40828. }
  40829. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40830. static void sslMsgCb(int w, int version, int type, const void* buf,
  40831. size_t sz, SSL* ssl, void* arg)
  40832. {
  40833. int i;
  40834. unsigned char* pt = (unsigned char*)buf;
  40835. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  40836. (w)?"Writing":"Reading", (int)sz, version, type);
  40837. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  40838. fprintf(stderr, "\n");
  40839. (void)ssl;
  40840. (void)arg;
  40841. }
  40842. #endif /* OPENSSL_EXTRA */
  40843. static int test_wolfSSL_msg_callback(void)
  40844. {
  40845. EXPECT_DECLS;
  40846. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  40847. WOLFSSL* ssl = NULL;
  40848. WOLFSSL_CTX* ctx = NULL;
  40849. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40850. ExpectNotNull(ssl = SSL_new(ctx));
  40851. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  40852. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  40853. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  40854. SSL_free(ssl);
  40855. SSL_CTX_free(ctx);
  40856. #endif
  40857. return EXPECT_RESULT();
  40858. }
  40859. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  40860. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40861. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40862. static void binary_dump(void *ptr, int size)
  40863. {
  40864. #ifdef WOLFSSL_EVP_PRINT
  40865. int i = 0;
  40866. unsigned char *p = (unsigned char *) ptr;
  40867. fprintf(stderr, "{");
  40868. while ((p != NULL) && (i < size)) {
  40869. if ((i % 8) == 0) {
  40870. fprintf(stderr, "\n");
  40871. fprintf(stderr, " ");
  40872. }
  40873. fprintf(stderr, "0x%02x, ", p[i]);
  40874. i++;
  40875. }
  40876. fprintf(stderr, "\n};\n");
  40877. #else
  40878. (void) ptr;
  40879. (void) size;
  40880. #endif
  40881. }
  40882. static int last_val = 0x0f;
  40883. static int check_result(unsigned char *data, int len)
  40884. {
  40885. int i;
  40886. for ( ; len; ) {
  40887. last_val = (last_val + 1) % 16;
  40888. for (i = 0; i < 16; len--, i++, data++)
  40889. if (*data != last_val) {
  40890. return -1;
  40891. }
  40892. }
  40893. return 0;
  40894. }
  40895. static int r_offset;
  40896. static int w_offset;
  40897. static void init_offset(void)
  40898. {
  40899. r_offset = 0;
  40900. w_offset = 0;
  40901. }
  40902. static void get_record(unsigned char *data, unsigned char *buf, int len)
  40903. {
  40904. XMEMCPY(buf, data+r_offset, len);
  40905. r_offset += len;
  40906. }
  40907. static void set_record(unsigned char *data, unsigned char *buf, int len)
  40908. {
  40909. XMEMCPY(data+w_offset, buf, len);
  40910. w_offset += len;
  40911. }
  40912. static void set_plain(unsigned char *plain, int rec)
  40913. {
  40914. int i, j;
  40915. unsigned char *p = plain;
  40916. #define BLOCKSZ 16
  40917. for (i=0; i<(rec/BLOCKSZ); i++) {
  40918. for (j=0; j<BLOCKSZ; j++)
  40919. *p++ = (i % 16);
  40920. }
  40921. }
  40922. #endif
  40923. static int test_wolfSSL_EVP_Cipher_extra(void)
  40924. {
  40925. EXPECT_DECLS;
  40926. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  40927. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  40928. /* aes128-cbc, keylen=16, ivlen=16 */
  40929. byte aes128_cbc_key[] = {
  40930. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40931. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  40932. };
  40933. byte aes128_cbc_iv[] = {
  40934. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  40935. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  40936. };
  40937. /* teset data size table */
  40938. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  40939. int test_drive2[] = {8, 3, 8, 512, 0};
  40940. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  40941. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  40942. int test_drive_len[100];
  40943. int ret = 0;
  40944. EVP_CIPHER_CTX *evp = NULL;
  40945. int ilen = 0;
  40946. int klen = 0;
  40947. int i, j;
  40948. const EVP_CIPHER *type;
  40949. byte *iv;
  40950. byte *key;
  40951. int ivlen;
  40952. int keylen;
  40953. #define RECORDS 16
  40954. #define BUFFSZ 512
  40955. byte plain [BUFFSZ * RECORDS];
  40956. byte cipher[BUFFSZ * RECORDS];
  40957. byte inb[BUFFSZ];
  40958. byte outb[BUFFSZ+16];
  40959. int outl = 0;
  40960. int inl;
  40961. iv = aes128_cbc_iv;
  40962. ivlen = sizeof(aes128_cbc_iv);
  40963. key = aes128_cbc_key;
  40964. keylen = sizeof(aes128_cbc_key);
  40965. type = EVP_aes_128_cbc();
  40966. set_plain(plain, BUFFSZ * RECORDS);
  40967. SSL_library_init();
  40968. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  40969. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  40970. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  40971. klen = EVP_CIPHER_CTX_key_length(evp);
  40972. if (klen > 0 && keylen != klen) {
  40973. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  40974. }
  40975. ilen = EVP_CIPHER_CTX_iv_length(evp);
  40976. if (ilen > 0 && ivlen != ilen) {
  40977. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  40978. }
  40979. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40980. for (j = 0; j<RECORDS; j++)
  40981. {
  40982. inl = BUFFSZ;
  40983. get_record(plain, inb, inl);
  40984. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  40985. set_record(cipher, outb, outl);
  40986. }
  40987. for (i = 0; test_drive[i]; i++) {
  40988. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  40989. init_offset();
  40990. test_drive_len[i] = 0;
  40991. for (j = 0; test_drive[i][j]; j++)
  40992. {
  40993. inl = test_drive[i][j];
  40994. test_drive_len[i] += inl;
  40995. get_record(plain, inb, inl);
  40996. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  40997. 0);
  40998. /* output to cipher buffer, so that following Dec test can detect
  40999. if any error */
  41000. set_record(cipher, outb, outl);
  41001. }
  41002. EVP_CipherFinal(evp, outb, &outl);
  41003. if (outl > 0)
  41004. set_record(cipher, outb, outl);
  41005. }
  41006. for (i = 0; test_drive[i]; i++) {
  41007. last_val = 0x0f;
  41008. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  41009. init_offset();
  41010. for (j = 0; test_drive[i][j]; j++) {
  41011. inl = test_drive[i][j];
  41012. get_record(cipher, inb, inl);
  41013. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  41014. 0);
  41015. binary_dump(outb, outl);
  41016. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  41017. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  41018. }
  41019. ret = EVP_CipherFinal(evp, outb, &outl);
  41020. binary_dump(outb, outl);
  41021. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  41022. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  41023. ExpectTrue(ret);
  41024. }
  41025. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  41026. EVP_CIPHER_CTX_free(evp);
  41027. evp = NULL;
  41028. /* Do an extra test to verify correct behavior with empty input. */
  41029. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41030. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41031. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41032. klen = EVP_CIPHER_CTX_key_length(evp);
  41033. if (klen > 0 && keylen != klen) {
  41034. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41035. }
  41036. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41037. if (ilen > 0 && ivlen != ilen) {
  41038. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41039. }
  41040. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41041. /* outl should be set to 0 after passing NULL, 0 for input args. */
  41042. outl = -1;
  41043. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  41044. ExpectIntEQ(outl, 0);
  41045. EVP_CIPHER_CTX_free(evp);
  41046. #endif /* test_EVP_Cipher */
  41047. return EXPECT_RESULT();
  41048. }
  41049. static int test_wolfSSL_PEM_read_DHparams(void)
  41050. {
  41051. EXPECT_DECLS;
  41052. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  41053. !defined(NO_FILESYSTEM)
  41054. DH* dh = NULL;
  41055. XFILE fp = XBADFILE;
  41056. unsigned char derOut[300];
  41057. unsigned char* derOutBuf = derOut;
  41058. int derOutSz = 0;
  41059. unsigned char derExpected[300];
  41060. int derExpectedSz = 0;
  41061. XMEMSET(derOut, 0, sizeof(derOut));
  41062. XMEMSET(derExpected, 0, sizeof(derExpected));
  41063. /* open DH param file, read into DH struct */
  41064. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  41065. /* bad args */
  41066. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  41067. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  41068. /* good args */
  41069. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41070. if (fp != XBADFILE) {
  41071. XFCLOSE(fp);
  41072. fp = XBADFILE;
  41073. }
  41074. /* read in certs/dh2048.der for comparison against exported params */
  41075. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  41076. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  41077. fp), 0);
  41078. if (fp != XBADFILE) {
  41079. XFCLOSE(fp);
  41080. fp = XBADFILE;
  41081. }
  41082. /* export DH back to DER and compare */
  41083. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  41084. ExpectIntEQ(derOutSz, derExpectedSz);
  41085. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  41086. DH_free(dh);
  41087. dh = NULL;
  41088. /* Test parsing with X9.42 header */
  41089. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  41090. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41091. if (fp != XBADFILE)
  41092. XFCLOSE(fp);
  41093. DH_free(dh);
  41094. #endif
  41095. return EXPECT_RESULT();
  41096. }
  41097. static int test_wolfSSL_X509_get_serialNumber(void)
  41098. {
  41099. EXPECT_DECLS;
  41100. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  41101. ASN1_INTEGER* a = NULL;
  41102. BIGNUM* bn = NULL;
  41103. X509* x509 = NULL;
  41104. char *serialHex = NULL;
  41105. byte serial[3];
  41106. int serialSz;
  41107. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41108. SSL_FILETYPE_PEM));
  41109. ExpectNotNull(a = X509_get_serialNumber(x509));
  41110. /* check on value of ASN1 Integer */
  41111. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  41112. a = NULL;
  41113. /* test setting serial number and then retrieving it */
  41114. ExpectNotNull(a = ASN1_INTEGER_new());
  41115. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  41116. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41117. serialSz = sizeof(serial);
  41118. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41119. WOLFSSL_SUCCESS);
  41120. ExpectIntEQ(serialSz, 1);
  41121. ExpectIntEQ(serial[0], 3);
  41122. ASN1_INTEGER_free(a);
  41123. a = NULL;
  41124. /* test setting serial number with 0's in it */
  41125. serial[0] = 0x01;
  41126. serial[1] = 0x00;
  41127. serial[2] = 0x02;
  41128. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  41129. if (a != NULL) {
  41130. a->data[0] = ASN_INTEGER;
  41131. a->data[1] = sizeof(serial);
  41132. XMEMCPY(&a->data[2], serial, sizeof(serial));
  41133. a->length = sizeof(serial) + 2;
  41134. }
  41135. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  41136. XMEMSET(serial, 0, sizeof(serial));
  41137. serialSz = sizeof(serial);
  41138. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  41139. WOLFSSL_SUCCESS);
  41140. ExpectIntEQ(serialSz, 3);
  41141. ExpectIntEQ(serial[0], 0x01);
  41142. ExpectIntEQ(serial[1], 0x00);
  41143. ExpectIntEQ(serial[2], 0x02);
  41144. ASN1_INTEGER_free(a);
  41145. a = NULL;
  41146. X509_free(x509); /* free's a */
  41147. ExpectNotNull(serialHex = BN_bn2hex(bn));
  41148. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  41149. ExpectStrEQ(serialHex, "01");
  41150. #else
  41151. ExpectStrEQ(serialHex, "1");
  41152. #endif
  41153. OPENSSL_free(serialHex);
  41154. ExpectIntEQ(BN_get_word(bn), 1);
  41155. BN_free(bn);
  41156. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  41157. ExpectNotNull(a = ASN1_INTEGER_new());
  41158. if (a != NULL) {
  41159. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  41160. DYNAMIC_TYPE_OPENSSL));
  41161. a->isDynamic = 1;
  41162. ASN1_INTEGER_free(a);
  41163. }
  41164. #endif
  41165. return EXPECT_RESULT();
  41166. }
  41167. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  41168. {
  41169. EXPECT_DECLS;
  41170. #if defined(OPENSSL_EXTRA)
  41171. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  41172. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  41173. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  41174. #endif
  41175. return EXPECT_RESULT();
  41176. }
  41177. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  41178. {
  41179. EXPECT_DECLS;
  41180. #if defined(OPENSSL_EXTRA)
  41181. #define MAX_HEXSTR_BUFSZ 9
  41182. #define NUM_CASES 5
  41183. struct Output {
  41184. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  41185. long ret;
  41186. };
  41187. int i;
  41188. int j;
  41189. const char* inputs[NUM_CASES] = {
  41190. "aabcd1357e",
  41191. "01:12:23:34:a5:b6:c7:d8:e9",
  41192. ":01:02",
  41193. "012",
  41194. ":ab:ac:d"
  41195. };
  41196. struct Output expectedOutputs[NUM_CASES] = {
  41197. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  41198. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  41199. {{0x01, 0x02}, 2},
  41200. {{0x00}, 0},
  41201. {{0x00}, 0}
  41202. };
  41203. long len = 0;
  41204. unsigned char* returnedBuf = NULL;
  41205. for (i = 0; i < NUM_CASES && EXPECT_SUCCESS(); ++i) {
  41206. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  41207. if (returnedBuf == NULL) {
  41208. ExpectIntEQ(expectedOutputs[i].ret, 0);
  41209. continue;
  41210. }
  41211. ExpectIntEQ(expectedOutputs[i].ret, len);
  41212. for (j = 0; j < len; ++j) {
  41213. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  41214. }
  41215. OPENSSL_free(returnedBuf);
  41216. returnedBuf = NULL;
  41217. }
  41218. #endif
  41219. return EXPECT_RESULT();
  41220. }
  41221. static int test_wolfSSL_X509_CA_num(void)
  41222. {
  41223. EXPECT_DECLS;
  41224. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41225. defined(HAVE_ECC) && !defined(NO_RSA)
  41226. WOLFSSL_X509_STORE *store = NULL;
  41227. WOLFSSL_X509 *x509_1 = NULL;
  41228. WOLFSSL_X509 *x509_2 = NULL;
  41229. int ca_num = 0;
  41230. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  41231. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41232. WOLFSSL_FILETYPE_PEM));
  41233. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  41234. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  41235. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  41236. WOLFSSL_FILETYPE_PEM));
  41237. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  41238. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  41239. wolfSSL_X509_free(x509_1);
  41240. wolfSSL_X509_free(x509_2);
  41241. wolfSSL_X509_STORE_free(store);
  41242. #endif
  41243. return EXPECT_RESULT();
  41244. }
  41245. static int test_wolfSSL_X509_check_ca(void)
  41246. {
  41247. EXPECT_DECLS;
  41248. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41249. WOLFSSL_X509 *x509 = NULL;
  41250. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41251. WOLFSSL_FILETYPE_PEM));
  41252. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  41253. wolfSSL_X509_free(x509);
  41254. #endif
  41255. return EXPECT_RESULT();
  41256. }
  41257. static int test_wolfSSL_X509_check_ip_asc(void)
  41258. {
  41259. EXPECT_DECLS;
  41260. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41261. WOLFSSL_X509 *x509 = NULL;
  41262. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  41263. WOLFSSL_FILETYPE_PEM));
  41264. #if 0
  41265. /* TODO: add cert gen for testing positive case */
  41266. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  41267. #endif
  41268. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  41269. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  41270. wolfSSL_X509_free(x509);
  41271. #endif
  41272. return EXPECT_RESULT();
  41273. }
  41274. static int test_wolfSSL_make_cert(void)
  41275. {
  41276. EXPECT_DECLS;
  41277. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  41278. defined(WOLFSSL_CERT_EXT)
  41279. int ret;
  41280. Cert cert;
  41281. CertName name;
  41282. RsaKey key;
  41283. WC_RNG rng;
  41284. byte der[FOURK_BUF];
  41285. word32 idx;
  41286. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  41287. #ifdef OPENSSL_EXTRA
  41288. const unsigned char* pt;
  41289. int certSz;
  41290. X509* x509 = NULL;
  41291. X509_NAME* x509name;
  41292. X509_NAME_ENTRY* entry;
  41293. ASN1_STRING* entryValue;
  41294. #endif
  41295. XMEMSET(&name, 0, sizeof(CertName));
  41296. /* set up cert name */
  41297. XMEMCPY(name.country, "US", sizeof("US"));
  41298. name.countryEnc = CTC_PRINTABLE;
  41299. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  41300. name.stateEnc = CTC_UTF8;
  41301. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  41302. name.localityEnc = CTC_UTF8;
  41303. XMEMCPY(name.sur, "Test", sizeof("Test"));
  41304. name.surEnc = CTC_UTF8;
  41305. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  41306. name.orgEnc = CTC_UTF8;
  41307. XMEMCPY(name.unit, "Development", sizeof("Development"));
  41308. name.unitEnc = CTC_UTF8;
  41309. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  41310. name.commonNameEnc = CTC_UTF8;
  41311. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  41312. name.serialDevEnc = CTC_PRINTABLE;
  41313. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  41314. name.userIdEnc = CTC_PRINTABLE;
  41315. #ifdef WOLFSSL_MULTI_ATTRIB
  41316. #if CTC_MAX_ATTRIB > 2
  41317. {
  41318. NameAttrib* n;
  41319. n = &name.name[0];
  41320. n->id = ASN_DOMAIN_COMPONENT;
  41321. n->type = CTC_UTF8;
  41322. n->sz = sizeof("com");
  41323. XMEMCPY(n->value, "com", sizeof("com"));
  41324. n = &name.name[1];
  41325. n->id = ASN_DOMAIN_COMPONENT;
  41326. n->type = CTC_UTF8;
  41327. n->sz = sizeof("wolfssl");
  41328. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  41329. }
  41330. #endif
  41331. #endif /* WOLFSSL_MULTI_ATTRIB */
  41332. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  41333. #ifndef HAVE_FIPS
  41334. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  41335. #else
  41336. ExpectIntEQ(wc_InitRng(&rng), 0);
  41337. #endif
  41338. /* load test RSA key */
  41339. idx = 0;
  41340. #if defined(USE_CERT_BUFFERS_1024)
  41341. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  41342. sizeof_server_key_der_1024), 0);
  41343. #elif defined(USE_CERT_BUFFERS_2048)
  41344. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  41345. sizeof_server_key_der_2048), 0);
  41346. #else
  41347. /* error case, no RSA key loaded, happens later */
  41348. (void)idx;
  41349. #endif
  41350. XMEMSET(&cert, 0 , sizeof(Cert));
  41351. ExpectIntEQ(wc_InitCert(&cert), 0);
  41352. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  41353. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  41354. cert.serialSz = (int)sizeof(mySerial);
  41355. cert.isCA = 1;
  41356. #ifndef NO_SHA256
  41357. cert.sigType = CTC_SHA256wRSA;
  41358. #else
  41359. cert.sigType = CTC_SHAwRSA;
  41360. #endif
  41361. /* add SKID from the Public Key */
  41362. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41363. /* add AKID from the Public Key */
  41364. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  41365. ret = 0;
  41366. do {
  41367. #if defined(WOLFSSL_ASYNC_CRYPT)
  41368. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  41369. #endif
  41370. if (ret >= 0) {
  41371. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  41372. }
  41373. } while (ret == WC_PENDING_E);
  41374. ExpectIntGT(ret, 0);
  41375. #ifdef OPENSSL_EXTRA
  41376. /* der holds a certificate with DC's now check X509 parsing of it */
  41377. certSz = ret;
  41378. pt = der;
  41379. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  41380. ExpectNotNull(x509name = X509_get_subject_name(x509));
  41381. #ifdef WOLFSSL_MULTI_ATTRIB
  41382. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41383. -1)), 5);
  41384. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41385. idx)), 6);
  41386. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41387. idx)), -1);
  41388. #endif /* WOLFSSL_MULTI_ATTRIB */
  41389. /* compare DN at index 0 */
  41390. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  41391. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41392. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  41393. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  41394. #ifndef WOLFSSL_MULTI_ATTRIB
  41395. /* compare Serial Number */
  41396. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  41397. -1)), 7);
  41398. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41399. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41400. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  41401. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  41402. #endif
  41403. #ifdef WOLFSSL_MULTI_ATTRIB
  41404. /* get first and second DC and compare result */
  41405. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41406. -1)), 5);
  41407. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41408. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41409. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  41410. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  41411. idx)), 6);
  41412. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  41413. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  41414. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  41415. #endif /* WOLFSSL_MULTI_ATTRIB */
  41416. /* try invalid index locations for regression test and sanity check */
  41417. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  41418. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  41419. X509_free(x509);
  41420. #endif /* OPENSSL_EXTRA */
  41421. wc_FreeRsaKey(&key);
  41422. wc_FreeRng(&rng);
  41423. #endif
  41424. return EXPECT_RESULT();
  41425. }
  41426. static int test_wolfSSL_X509_get_version(void)
  41427. {
  41428. EXPECT_DECLS;
  41429. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41430. WOLFSSL_X509 *x509 = NULL;
  41431. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41432. WOLFSSL_FILETYPE_PEM));
  41433. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  41434. wolfSSL_X509_free(x509);
  41435. #endif
  41436. return EXPECT_RESULT();
  41437. }
  41438. #if defined(OPENSSL_ALL)
  41439. static int test_wolfSSL_sk_CIPHER_description(void)
  41440. {
  41441. EXPECT_DECLS;
  41442. #if !defined(NO_RSA)
  41443. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41444. int i;
  41445. int numCiphers = 0;
  41446. const SSL_METHOD *method = NULL;
  41447. const SSL_CIPHER *cipher = NULL;
  41448. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41449. SSL_CTX *ctx = NULL;
  41450. SSL *ssl = NULL;
  41451. char buf[256];
  41452. char test_str[9] = "0000000";
  41453. const char badStr[] = "unknown";
  41454. const char certPath[] = "./certs/client-cert.pem";
  41455. XMEMSET(buf, 0, sizeof(buf));
  41456. ExpectNotNull(method = TLSv1_2_client_method());
  41457. ExpectNotNull(ctx = SSL_CTX_new(method));
  41458. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41459. SSL_CTX_set_verify_depth(ctx, 4);
  41460. SSL_CTX_set_options(ctx, flags);
  41461. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41462. WOLFSSL_SUCCESS);
  41463. ExpectNotNull(ssl = SSL_new(ctx));
  41464. /* SSL_get_ciphers returns a stack of all configured ciphers
  41465. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  41466. */
  41467. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41468. /* loop through the amount of supportedCiphers */
  41469. numCiphers = sk_num(supportedCiphers);
  41470. for (i = 0; i < numCiphers; ++i) {
  41471. int j;
  41472. /* sk_value increments "sk->data.cipher->cipherOffset".
  41473. * wolfSSL_sk_CIPHER_description sets the description for
  41474. * the cipher based on the provided offset.
  41475. */
  41476. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41477. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41478. }
  41479. /* Search cipher description string for "unknown" descriptor */
  41480. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  41481. int k = 0;
  41482. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  41483. test_str[k] = badStr[k];
  41484. j++;
  41485. k++;
  41486. }
  41487. }
  41488. /* Fail if test_str == badStr == "unknown" */
  41489. ExpectStrNE(test_str,badStr);
  41490. }
  41491. SSL_free(ssl);
  41492. SSL_CTX_free(ctx);
  41493. #endif
  41494. return EXPECT_RESULT();
  41495. }
  41496. static int test_wolfSSL_get_ciphers_compat(void)
  41497. {
  41498. EXPECT_DECLS;
  41499. #if !defined(NO_RSA)
  41500. const SSL_METHOD *method = NULL;
  41501. const char certPath[] = "./certs/client-cert.pem";
  41502. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41503. SSL_CTX *ctx = NULL;
  41504. WOLFSSL *ssl = NULL;
  41505. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  41506. ExpectNotNull(method = SSLv23_client_method());
  41507. ExpectNotNull(ctx = SSL_CTX_new(method));
  41508. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41509. SSL_CTX_set_verify_depth(ctx, 4);
  41510. SSL_CTX_set_options(ctx, flags);
  41511. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  41512. WOLFSSL_SUCCESS);
  41513. ExpectNotNull(ssl = SSL_new(ctx));
  41514. /* Test Bad NULL input */
  41515. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  41516. /* Test for Good input */
  41517. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  41518. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  41519. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  41520. SSL_free(ssl);
  41521. SSL_CTX_free(ctx);
  41522. #endif
  41523. return EXPECT_RESULT();
  41524. }
  41525. static int test_wolfSSL_X509_PUBKEY_get(void)
  41526. {
  41527. EXPECT_DECLS;
  41528. WOLFSSL_X509_PUBKEY pubkey;
  41529. WOLFSSL_X509_PUBKEY* key;
  41530. WOLFSSL_EVP_PKEY evpkey ;
  41531. WOLFSSL_EVP_PKEY* evpPkey;
  41532. WOLFSSL_EVP_PKEY* retEvpPkey;
  41533. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  41534. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  41535. key = &pubkey;
  41536. evpPkey = &evpkey;
  41537. evpPkey->type = WOLFSSL_SUCCESS;
  41538. key->pkey = evpPkey;
  41539. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41540. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  41541. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  41542. key->pkey = NULL;
  41543. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  41544. return EXPECT_RESULT();
  41545. }
  41546. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  41547. {
  41548. EXPECT_DECLS;
  41549. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41550. DSA *dsa = NULL;
  41551. DSA *setDsa = NULL;
  41552. EVP_PKEY *pkey = NULL;
  41553. EVP_PKEY *set1Pkey = NULL;
  41554. SHA_CTX sha;
  41555. byte signature[DSA_SIG_SIZE];
  41556. byte hash[WC_SHA_DIGEST_SIZE];
  41557. word32 bytes;
  41558. int answer;
  41559. #ifdef USE_CERT_BUFFERS_1024
  41560. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  41561. int dsaKeySz = sizeof_dsa_key_der_1024;
  41562. byte tmp[ONEK_BUF];
  41563. XMEMSET(tmp, 0, sizeof(tmp));
  41564. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41565. bytes = dsaKeySz;
  41566. #elif defined(USE_CERT_BUFFERS_2048)
  41567. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  41568. int dsaKeySz = sizeof_dsa_key_der_2048;
  41569. byte tmp[TWOK_BUF];
  41570. XMEMSET(tmp, 0, sizeof(tmp));
  41571. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  41572. bytes = dsaKeySz;
  41573. #else
  41574. byte tmp[TWOK_BUF];
  41575. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  41576. int dsaKeySz;
  41577. XFILE fp = XBADFILE;
  41578. XMEMSET(tmp, 0, sizeof(tmp));
  41579. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  41580. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  41581. if (fp != XBADFILE)
  41582. XFCLOSE(fp);
  41583. #endif /* END USE_CERT_BUFFERS_1024 */
  41584. /* Create hash to later Sign and Verify */
  41585. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  41586. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  41587. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  41588. /* Initialize pkey with der format dsa key */
  41589. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  41590. (long)dsaKeySz));
  41591. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  41592. /* Should Fail: NULL argument */
  41593. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  41594. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  41595. /* Should Pass: Initialized pkey argument */
  41596. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  41597. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  41598. #ifdef USE_CERT_BUFFERS_1024
  41599. ExpectIntEQ(DSA_bits(dsa), 1024);
  41600. #else
  41601. ExpectIntEQ(DSA_bits(dsa), 2048);
  41602. #endif
  41603. /* Sign */
  41604. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  41605. /* Verify. */
  41606. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  41607. WOLFSSL_SUCCESS);
  41608. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  41609. /* Should Fail: set1Pkey not initialized */
  41610. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41611. /* Initialize set1Pkey */
  41612. set1Pkey = EVP_PKEY_new();
  41613. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  41614. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  41615. WOLFSSL_SUCCESS);
  41616. /* Should Pass: set dsa into set1Pkey */
  41617. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  41618. DSA_free(dsa);
  41619. DSA_free(setDsa);
  41620. EVP_PKEY_free(pkey);
  41621. EVP_PKEY_free(set1Pkey);
  41622. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41623. return EXPECT_RESULT();
  41624. } /* END test_EVP_PKEY_set1_get1_DSA */
  41625. static int test_wolfSSL_DSA_generate_parameters(void)
  41626. {
  41627. EXPECT_DECLS;
  41628. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41629. !defined(HAVE_FIPS)
  41630. DSA *dsa = NULL;
  41631. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  41632. NULL));
  41633. DSA_free(dsa);
  41634. #endif
  41635. return EXPECT_RESULT();
  41636. }
  41637. static int test_wolfSSL_DSA_SIG(void)
  41638. {
  41639. EXPECT_DECLS;
  41640. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  41641. !defined(HAVE_FIPS)
  41642. DSA *dsa = NULL;
  41643. DSA *dsa2 = NULL;
  41644. DSA_SIG *sig = NULL;
  41645. const BIGNUM *p = NULL;
  41646. const BIGNUM *q = NULL;
  41647. const BIGNUM *g = NULL;
  41648. const BIGNUM *pub = NULL;
  41649. const BIGNUM *priv = NULL;
  41650. BIGNUM *dup_p = NULL;
  41651. BIGNUM *dup_q = NULL;
  41652. BIGNUM *dup_g = NULL;
  41653. BIGNUM *dup_pub = NULL;
  41654. BIGNUM *dup_priv = NULL;
  41655. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  41656. ExpectNotNull(dsa = DSA_new());
  41657. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  41658. NULL), 1);
  41659. ExpectIntEQ(DSA_generate_key(dsa), 1);
  41660. DSA_get0_pqg(dsa, &p, &q, &g);
  41661. DSA_get0_key(dsa, &pub, &priv);
  41662. ExpectNotNull(dup_p = BN_dup(p));
  41663. ExpectNotNull(dup_q = BN_dup(q));
  41664. ExpectNotNull(dup_g = BN_dup(g));
  41665. ExpectNotNull(dup_pub = BN_dup(pub));
  41666. ExpectNotNull(dup_priv = BN_dup(priv));
  41667. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  41668. ExpectNotNull(dsa2 = DSA_new());
  41669. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  41670. if (EXPECT_FAIL()) {
  41671. BN_free(dup_p);
  41672. BN_free(dup_q);
  41673. BN_free(dup_g);
  41674. }
  41675. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  41676. if (EXPECT_FAIL()) {
  41677. BN_free(dup_pub);
  41678. BN_free(dup_priv);
  41679. }
  41680. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  41681. DSA_free(dsa);
  41682. DSA_free(dsa2);
  41683. DSA_SIG_free(sig);
  41684. #endif
  41685. return EXPECT_RESULT();
  41686. }
  41687. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  41688. {
  41689. EXPECT_DECLS;
  41690. #ifdef HAVE_ECC
  41691. WOLFSSL_EC_KEY* ecKey = NULL;
  41692. WOLFSSL_EC_KEY* ecGet1 = NULL;
  41693. EVP_PKEY* pkey = NULL;
  41694. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41695. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41696. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  41697. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41698. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41699. /* Should fail since ecKey is empty */
  41700. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41701. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41702. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41703. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  41704. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  41705. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  41706. wolfSSL_EC_KEY_free(ecKey);
  41707. wolfSSL_EC_KEY_free(ecGet1);
  41708. EVP_PKEY_free(pkey);
  41709. #endif /* HAVE_ECC */
  41710. return EXPECT_RESULT();
  41711. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  41712. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  41713. {
  41714. EXPECT_DECLS;
  41715. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41716. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41717. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  41718. DH *dh = NULL;
  41719. DH *setDh = NULL;
  41720. EVP_PKEY *pkey = NULL;
  41721. XFILE f = XBADFILE;
  41722. unsigned char buf[4096];
  41723. const unsigned char* pt = buf;
  41724. const char* dh2048 = "./certs/dh2048.der";
  41725. long len = 0;
  41726. int code = -1;
  41727. XMEMSET(buf, 0, sizeof(buf));
  41728. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  41729. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41730. if (f != XBADFILE)
  41731. XFCLOSE(f);
  41732. /* Load dh2048.der into DH with internal format */
  41733. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  41734. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  41735. ExpectIntEQ(code, 0);
  41736. code = -1;
  41737. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41738. /* Set DH into PKEY */
  41739. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  41740. /* Get DH from PKEY */
  41741. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  41742. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  41743. ExpectIntEQ(code, 0);
  41744. EVP_PKEY_free(pkey);
  41745. DH_free(setDh);
  41746. DH_free(dh);
  41747. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  41748. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41749. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41750. return EXPECT_RESULT();
  41751. } /* END test_EVP_PKEY_set1_get1_DH */
  41752. static int test_wolfSSL_CTX_ctrl(void)
  41753. {
  41754. EXPECT_DECLS;
  41755. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41756. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41757. char caFile[] = "./certs/client-ca.pem";
  41758. char clientFile[] = "./certs/client-cert.pem";
  41759. SSL_CTX* ctx = NULL;
  41760. X509* x509 = NULL;
  41761. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41762. byte buf[6000];
  41763. char file[] = "./certs/dsaparams.pem";
  41764. XFILE f = XBADFILE;
  41765. int bytes;
  41766. BIO* bio = NULL;
  41767. DSA* dsa = NULL;
  41768. DH* dh = NULL;
  41769. #endif
  41770. #ifdef HAVE_ECC
  41771. WOLFSSL_EC_KEY* ecKey = NULL;
  41772. #endif
  41773. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41774. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  41775. WOLFSSL_FILETYPE_PEM));
  41776. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  41777. if (EXPECT_FAIL()) {
  41778. wolfSSL_X509_free(x509);
  41779. }
  41780. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  41781. WOLFSSL_FILETYPE_PEM));
  41782. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41783. /* Initialize DH */
  41784. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  41785. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  41786. if (f != XBADFILE)
  41787. XFCLOSE(f);
  41788. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  41789. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  41790. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  41791. #endif
  41792. #ifdef HAVE_ECC
  41793. /* Initialize WOLFSSL_EC_KEY */
  41794. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41795. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41796. #endif
  41797. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  41798. /* additional test of getting EVP_PKEY key size from X509
  41799. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  41800. * allowed with user RSA */
  41801. {
  41802. EVP_PKEY* pkey = NULL;
  41803. #if defined(HAVE_ECC)
  41804. X509* ecX509 = NULL;
  41805. #endif /* HAVE_ECC */
  41806. ExpectNotNull(pkey = X509_get_pubkey(x509));
  41807. /* current RSA key is 2048 bit (256 bytes) */
  41808. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  41809. EVP_PKEY_free(pkey);
  41810. pkey = NULL;
  41811. #if defined(HAVE_ECC)
  41812. #if defined(USE_CERT_BUFFERS_256)
  41813. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  41814. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  41815. SSL_FILETYPE_ASN1));
  41816. #else
  41817. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  41818. cliEccCertFile, SSL_FILETYPE_PEM));
  41819. #endif
  41820. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  41821. /* current ECC key is 256 bit (32 bytes) */
  41822. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  41823. X509_free(ecX509);
  41824. EVP_PKEY_free(pkey);
  41825. #endif /* HAVE_ECC */
  41826. }
  41827. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  41828. /* Tests should fail with passed in NULL pointer */
  41829. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  41830. SSL_FAILURE);
  41831. #if !defined(NO_DH) && !defined(NO_DSA)
  41832. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  41833. SSL_FAILURE);
  41834. #endif
  41835. #ifdef HAVE_ECC
  41836. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  41837. SSL_FAILURE);
  41838. #endif
  41839. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  41840. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  41841. */
  41842. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  41843. SSL_SUCCESS);
  41844. if (EXPECT_FAIL()) {
  41845. wolfSSL_X509_free(x509);
  41846. }
  41847. /* Test with SSL_CTRL_OPTIONS
  41848. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  41849. */
  41850. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  41851. NULL) == SSL_OP_NO_TLSv1);
  41852. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  41853. /* Test with SSL_CTRL_SET_TMP_DH
  41854. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  41855. */
  41856. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  41857. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  41858. SSL_SUCCESS);
  41859. #endif
  41860. /* Test with SSL_CTRL_SET_TMP_ECDH
  41861. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  41862. */
  41863. #ifdef HAVE_ECC
  41864. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  41865. SSL_SUCCESS);
  41866. #endif
  41867. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41868. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  41869. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  41870. #endif
  41871. /* Test for min/max proto */
  41872. #ifndef WOLFSSL_NO_TLS12
  41873. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  41874. 0, NULL), SSL_SUCCESS);
  41875. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  41876. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  41877. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  41878. #endif
  41879. #ifdef WOLFSSL_TLS13
  41880. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41881. 0, NULL), SSL_SUCCESS);
  41882. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41883. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  41884. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  41885. #ifndef WOLFSSL_NO_TLS12
  41886. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  41887. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  41888. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  41889. #endif
  41890. #endif
  41891. /* Cleanup and Pass */
  41892. #if !defined(NO_DH) && !defined(NO_DSA)
  41893. #ifndef NO_BIO
  41894. BIO_free(bio);
  41895. DSA_free(dsa);
  41896. DH_free(dh);
  41897. #endif
  41898. #endif
  41899. #ifdef HAVE_ECC
  41900. wolfSSL_EC_KEY_free(ecKey);
  41901. #endif
  41902. SSL_CTX_free(ctx);
  41903. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41904. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  41905. return EXPECT_RESULT();
  41906. }
  41907. static int test_wolfSSL_EVP_PKEY_assign(void)
  41908. {
  41909. EXPECT_DECLS;
  41910. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  41911. int type;
  41912. WOLFSSL_EVP_PKEY* pkey = NULL;
  41913. #ifndef NO_RSA
  41914. WOLFSSL_RSA* rsa = NULL;
  41915. #endif
  41916. #ifndef NO_DSA
  41917. WOLFSSL_DSA* dsa = NULL;
  41918. #endif
  41919. #ifdef HAVE_ECC
  41920. WOLFSSL_EC_KEY* ecKey = NULL;
  41921. #endif
  41922. #ifndef NO_RSA
  41923. type = EVP_PKEY_RSA;
  41924. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41925. ExpectNotNull(rsa = wolfSSL_RSA_new());
  41926. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  41927. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41928. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  41929. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  41930. if (EXPECT_FAIL()) {
  41931. wolfSSL_RSA_free(rsa);
  41932. }
  41933. wolfSSL_EVP_PKEY_free(pkey);
  41934. pkey = NULL;
  41935. #endif /* NO_RSA */
  41936. #ifndef NO_DSA
  41937. type = EVP_PKEY_DSA;
  41938. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41939. ExpectNotNull(dsa = wolfSSL_DSA_new());
  41940. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  41941. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41942. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  41943. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  41944. if (EXPECT_FAIL()) {
  41945. wolfSSL_DSA_free(dsa);
  41946. }
  41947. wolfSSL_EVP_PKEY_free(pkey);
  41948. pkey = NULL;
  41949. #endif /* NO_DSA */
  41950. #ifdef HAVE_ECC
  41951. type = EVP_PKEY_EC;
  41952. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41953. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  41954. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  41955. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  41956. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  41957. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  41958. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41959. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  41960. if (EXPECT_FAIL()) {
  41961. wolfSSL_EC_KEY_free(ecKey);
  41962. }
  41963. wolfSSL_EVP_PKEY_free(pkey);
  41964. pkey = NULL;
  41965. #endif /* HAVE_ECC */
  41966. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  41967. return EXPECT_RESULT();
  41968. }
  41969. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  41970. {
  41971. EXPECT_DECLS;
  41972. #if !defined(NO_DH) && \
  41973. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41974. XFILE f = XBADFILE;
  41975. unsigned char buf[4096];
  41976. const unsigned char* pt = buf;
  41977. const char* params1 = "./certs/dh2048.der";
  41978. long len = 0;
  41979. WOLFSSL_DH* dh = NULL;
  41980. WOLFSSL_EVP_PKEY* pkey = NULL;
  41981. XMEMSET(buf, 0, sizeof(buf));
  41982. /* Load DH parameters DER. */
  41983. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  41984. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41985. if (f != XBADFILE)
  41986. XFCLOSE(f);
  41987. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  41988. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  41989. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41990. /* Bad cases */
  41991. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  41992. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  41993. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  41994. /* Good case */
  41995. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  41996. if (EXPECT_FAIL()) {
  41997. wolfSSL_DH_free(dh);
  41998. }
  41999. EVP_PKEY_free(pkey);
  42000. #endif
  42001. return EXPECT_RESULT();
  42002. }
  42003. static int test_wolfSSL_EVP_PKEY_base_id(void)
  42004. {
  42005. EXPECT_DECLS;
  42006. WOLFSSL_EVP_PKEY* pkey = NULL;
  42007. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42008. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  42009. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  42010. EVP_PKEY_free(pkey);
  42011. return EXPECT_RESULT();
  42012. }
  42013. static int test_wolfSSL_EVP_PKEY_id(void)
  42014. {
  42015. EXPECT_DECLS;
  42016. WOLFSSL_EVP_PKEY* pkey = NULL;
  42017. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42018. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  42019. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  42020. EVP_PKEY_free(pkey);
  42021. return EXPECT_RESULT();
  42022. }
  42023. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  42024. {
  42025. EXPECT_DECLS;
  42026. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  42027. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  42028. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  42029. ECC_MIN_KEY_SZ <= 256)
  42030. EVP_PKEY_CTX* ctx = NULL;
  42031. EVP_PKEY* pkey = NULL;
  42032. /* Test error conditions. */
  42033. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  42034. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42035. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  42036. #ifndef NO_RSA
  42037. EVP_PKEY_CTX_free(ctx);
  42038. /* Parameter generation for RSA not supported yet. */
  42039. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  42040. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  42041. #endif
  42042. #ifdef HAVE_ECC
  42043. EVP_PKEY_CTX_free(ctx);
  42044. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42045. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  42046. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  42047. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  42048. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  42049. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  42050. WOLFSSL_SUCCESS);
  42051. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42052. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42053. #endif
  42054. EVP_PKEY_CTX_free(ctx);
  42055. EVP_PKEY_free(pkey);
  42056. #endif
  42057. return EXPECT_RESULT();
  42058. }
  42059. static int test_wolfSSL_EVP_PKEY_keygen(void)
  42060. {
  42061. EXPECT_DECLS;
  42062. WOLFSSL_EVP_PKEY* pkey = NULL;
  42063. EVP_PKEY_CTX* ctx = NULL;
  42064. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42065. WOLFSSL_EVP_PKEY* params = NULL;
  42066. DH* dh = NULL;
  42067. const BIGNUM* pubkey = NULL;
  42068. const BIGNUM* privkey = NULL;
  42069. ASN1_INTEGER* asn1int = NULL;
  42070. unsigned int length = 0;
  42071. byte* derBuffer = NULL;
  42072. #endif
  42073. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42074. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42075. /* Bad cases */
  42076. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  42077. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  42078. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  42079. /* Good case */
  42080. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  42081. EVP_PKEY_CTX_free(ctx);
  42082. ctx = NULL;
  42083. EVP_PKEY_free(pkey);
  42084. pkey = NULL;
  42085. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42086. /* Test DH keygen */
  42087. {
  42088. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  42089. ExpectNotNull(dh = DH_get_2048_256());
  42090. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42091. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  42092. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42093. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42094. DH_free(dh);
  42095. dh = NULL;
  42096. EVP_PKEY_CTX_free(ctx);
  42097. EVP_PKEY_free(params);
  42098. /* try exporting generated key to DER, to verify */
  42099. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  42100. DH_get0_key(dh, &pubkey, &privkey);
  42101. ExpectNotNull(pubkey);
  42102. ExpectNotNull(privkey);
  42103. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  42104. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  42105. ASN1_INTEGER_free(asn1int);
  42106. DH_free(dh);
  42107. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42108. EVP_PKEY_free(pkey);
  42109. }
  42110. #endif
  42111. return EXPECT_RESULT();
  42112. }
  42113. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  42114. {
  42115. EXPECT_DECLS;
  42116. WOLFSSL_EVP_PKEY* pkey = NULL;
  42117. EVP_PKEY_CTX *ctx = NULL;
  42118. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42119. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42120. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42121. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  42122. EVP_PKEY_CTX_free(ctx);
  42123. EVP_PKEY_free(pkey);
  42124. return EXPECT_RESULT();
  42125. }
  42126. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  42127. {
  42128. EXPECT_DECLS;
  42129. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  42130. WOLFSSL_EVP_PKEY* pkey = NULL;
  42131. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42132. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  42133. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  42134. EVP_PKEY_free(pkey);
  42135. #endif
  42136. return EXPECT_RESULT();
  42137. }
  42138. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  42139. {
  42140. EXPECT_DECLS;
  42141. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  42142. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  42143. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  42144. !defined(NO_FILESYSTEM)
  42145. WOLFSSL_EVP_PKEY* params = NULL;
  42146. WOLFSSL_EVP_PKEY* copy = NULL;
  42147. DH* dh = NULL;
  42148. BIGNUM* p1;
  42149. BIGNUM* g1;
  42150. BIGNUM* q1;
  42151. BIGNUM* p2;
  42152. BIGNUM* g2;
  42153. BIGNUM* q2;
  42154. /* create DH with DH_get_2048_256 params */
  42155. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  42156. ExpectNotNull(dh = DH_get_2048_256());
  42157. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42158. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  42159. (const BIGNUM**)&q1,
  42160. (const BIGNUM**)&g1);
  42161. DH_free(dh);
  42162. dh = NULL;
  42163. /* create DH with random generated DH params */
  42164. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  42165. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  42166. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  42167. DH_free(dh);
  42168. dh = NULL;
  42169. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  42170. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  42171. ExpectNotNull(dh->p);
  42172. ExpectNotNull(dh->g);
  42173. ExpectNotNull(dh->q);
  42174. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  42175. (const BIGNUM**)&q2,
  42176. (const BIGNUM**)&g2);
  42177. ExpectIntEQ(BN_cmp(p1, p2), 0);
  42178. ExpectIntEQ(BN_cmp(q1, q2), 0);
  42179. ExpectIntEQ(BN_cmp(g1, g2), 0);
  42180. DH_free(dh);
  42181. EVP_PKEY_free(copy);
  42182. EVP_PKEY_free(params);
  42183. #endif
  42184. return EXPECT_RESULT();
  42185. }
  42186. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  42187. {
  42188. EXPECT_DECLS;
  42189. WOLFSSL_EVP_PKEY* pkey = NULL;
  42190. EVP_PKEY_CTX* ctx = NULL;
  42191. int bits = 2048;
  42192. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42193. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42194. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  42195. WOLFSSL_SUCCESS);
  42196. EVP_PKEY_CTX_free(ctx);
  42197. EVP_PKEY_free(pkey);
  42198. return EXPECT_RESULT();
  42199. }
  42200. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  42201. {
  42202. EXPECT_DECLS;
  42203. /* This is large enough to be used for all key sizes */
  42204. byte key[AES_256_KEY_SIZE] = {0};
  42205. byte iv[AES_BLOCK_SIZE] = {0};
  42206. int i;
  42207. int nids[] = {
  42208. #ifdef HAVE_AES_CBC
  42209. NID_aes_128_cbc,
  42210. #endif
  42211. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42212. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42213. #ifdef HAVE_AESGCM
  42214. NID_aes_128_gcm,
  42215. #endif
  42216. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42217. #ifdef WOLFSSL_AES_COUNTER
  42218. NID_aes_128_ctr,
  42219. #endif
  42220. #ifndef NO_DES3
  42221. NID_des_cbc,
  42222. NID_des_ede3_cbc,
  42223. #endif
  42224. };
  42225. int iv_lengths[] = {
  42226. #ifdef HAVE_AES_CBC
  42227. AES_BLOCK_SIZE,
  42228. #endif
  42229. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42230. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42231. #ifdef HAVE_AESGCM
  42232. GCM_NONCE_MID_SZ,
  42233. #endif
  42234. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42235. #ifdef WOLFSSL_AES_COUNTER
  42236. AES_BLOCK_SIZE,
  42237. #endif
  42238. #ifndef NO_DES3
  42239. DES_BLOCK_SIZE,
  42240. DES_BLOCK_SIZE,
  42241. #endif
  42242. };
  42243. int nidsLen = (sizeof(nids)/sizeof(int));
  42244. for (i = 0; i < nidsLen; i++) {
  42245. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  42246. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  42247. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42248. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42249. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  42250. EVP_CIPHER_CTX_free(ctx);
  42251. }
  42252. return EXPECT_RESULT();
  42253. }
  42254. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  42255. {
  42256. EXPECT_DECLS;
  42257. byte key[AES_256_KEY_SIZE] = {0};
  42258. byte iv[AES_BLOCK_SIZE] = {0};
  42259. int i;
  42260. int nids[] = {
  42261. #ifdef HAVE_AES_CBC
  42262. NID_aes_128_cbc,
  42263. NID_aes_256_cbc,
  42264. #endif
  42265. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42266. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42267. #ifdef HAVE_AESGCM
  42268. NID_aes_128_gcm,
  42269. NID_aes_256_gcm,
  42270. #endif
  42271. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42272. #ifdef WOLFSSL_AES_COUNTER
  42273. NID_aes_128_ctr,
  42274. NID_aes_256_ctr,
  42275. #endif
  42276. #ifndef NO_DES3
  42277. NID_des_cbc,
  42278. NID_des_ede3_cbc,
  42279. #endif
  42280. };
  42281. int key_lengths[] = {
  42282. #ifdef HAVE_AES_CBC
  42283. AES_128_KEY_SIZE,
  42284. AES_256_KEY_SIZE,
  42285. #endif
  42286. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42287. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42288. #ifdef HAVE_AESGCM
  42289. AES_128_KEY_SIZE,
  42290. AES_256_KEY_SIZE,
  42291. #endif
  42292. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42293. #ifdef WOLFSSL_AES_COUNTER
  42294. AES_128_KEY_SIZE,
  42295. AES_256_KEY_SIZE,
  42296. #endif
  42297. #ifndef NO_DES3
  42298. DES_KEY_SIZE,
  42299. DES3_KEY_SIZE,
  42300. #endif
  42301. };
  42302. int nidsLen = (sizeof(nids)/sizeof(int));
  42303. for (i = 0; i < nidsLen; i++) {
  42304. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  42305. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  42306. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42307. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42308. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  42309. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  42310. WOLFSSL_SUCCESS);
  42311. EVP_CIPHER_CTX_free(ctx);
  42312. }
  42313. return EXPECT_RESULT();
  42314. }
  42315. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  42316. {
  42317. EXPECT_DECLS;
  42318. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  42319. int ivLen, keyLen;
  42320. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  42321. #ifdef HAVE_AESGCM
  42322. byte key[AES_128_KEY_SIZE] = {0};
  42323. byte iv[AES_BLOCK_SIZE] = {0};
  42324. const EVP_CIPHER *init = EVP_aes_128_gcm();
  42325. #else
  42326. byte key[DES3_KEY_SIZE] = {0};
  42327. byte iv[DES_BLOCK_SIZE] = {0};
  42328. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  42329. #endif
  42330. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42331. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42332. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  42333. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  42334. /* Bad cases */
  42335. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  42336. WOLFSSL_FAILURE);
  42337. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  42338. WOLFSSL_FAILURE);
  42339. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  42340. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  42341. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  42342. WOLFSSL_FAILURE);
  42343. /* Good case */
  42344. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  42345. EVP_CIPHER_CTX_free(ctx);
  42346. #endif
  42347. return EXPECT_RESULT();
  42348. }
  42349. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  42350. {
  42351. EXPECT_DECLS;
  42352. WOLFSSL_ENGINE* e = NULL;
  42353. int id = 0;
  42354. EVP_PKEY_CTX *ctx = NULL;
  42355. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  42356. EVP_PKEY_CTX_free(ctx);
  42357. return EXPECT_RESULT();
  42358. }
  42359. static int test_wolfSSL_EVP_rc4(void)
  42360. {
  42361. EXPECT_DECLS;
  42362. #if !defined(NO_RC4)
  42363. ExpectNotNull(wolfSSL_EVP_rc4());
  42364. #endif
  42365. return EXPECT_RESULT();
  42366. }
  42367. static int test_wolfSSL_EVP_enc_null(void)
  42368. {
  42369. EXPECT_DECLS;
  42370. ExpectNotNull(wolfSSL_EVP_enc_null());
  42371. return EXPECT_RESULT();
  42372. }
  42373. static int test_wolfSSL_EVP_rc2_cbc(void)
  42374. {
  42375. EXPECT_DECLS;
  42376. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  42377. ExpectNull(wolfSSL_EVP_rc2_cbc());
  42378. #endif
  42379. return EXPECT_RESULT();
  42380. }
  42381. static int test_wolfSSL_EVP_mdc2(void)
  42382. {
  42383. EXPECT_DECLS;
  42384. #if !defined(NO_WOLFSSL_STUB)
  42385. ExpectNull(wolfSSL_EVP_mdc2());
  42386. #endif
  42387. return EXPECT_RESULT();
  42388. }
  42389. static int test_wolfSSL_EVP_md4(void)
  42390. {
  42391. EXPECT_DECLS;
  42392. #if !defined(NO_MD4)
  42393. ExpectNotNull(wolfSSL_EVP_md4());
  42394. #endif
  42395. return EXPECT_RESULT();
  42396. }
  42397. static int test_wolfSSL_EVP_aes_256_gcm(void)
  42398. {
  42399. EXPECT_DECLS;
  42400. #ifdef HAVE_AESGCM
  42401. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  42402. #endif
  42403. return EXPECT_RESULT();
  42404. }
  42405. static int test_wolfSSL_EVP_aes_192_gcm(void)
  42406. {
  42407. EXPECT_DECLS;
  42408. #ifdef HAVE_AESGCM
  42409. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  42410. #endif
  42411. return EXPECT_RESULT();
  42412. }
  42413. static int test_wolfSSL_EVP_aes_256_ccm(void)
  42414. {
  42415. EXPECT_DECLS;
  42416. #ifdef HAVE_AESCCM
  42417. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  42418. #endif
  42419. return EXPECT_RESULT();
  42420. }
  42421. static int test_wolfSSL_EVP_aes_192_ccm(void)
  42422. {
  42423. EXPECT_DECLS;
  42424. #ifdef HAVE_AESCCM
  42425. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  42426. #endif
  42427. return EXPECT_RESULT();
  42428. }
  42429. static int test_wolfSSL_EVP_aes_128_ccm(void)
  42430. {
  42431. EXPECT_DECLS;
  42432. #ifdef HAVE_AESCCM
  42433. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  42434. #endif
  42435. return EXPECT_RESULT();
  42436. }
  42437. static int test_wolfSSL_EVP_ripemd160(void)
  42438. {
  42439. EXPECT_DECLS;
  42440. #if !defined(NO_WOLFSSL_STUB)
  42441. ExpectNull(wolfSSL_EVP_ripemd160());
  42442. #endif
  42443. return EXPECT_RESULT();
  42444. }
  42445. static int test_wolfSSL_EVP_get_digestbynid(void)
  42446. {
  42447. EXPECT_DECLS;
  42448. #ifndef NO_MD5
  42449. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  42450. #endif
  42451. #ifndef NO_SHA
  42452. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  42453. #endif
  42454. #ifndef NO_SHA256
  42455. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  42456. #endif
  42457. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  42458. return EXPECT_RESULT();
  42459. }
  42460. static int test_wolfSSL_EVP_MD_nid(void)
  42461. {
  42462. EXPECT_DECLS;
  42463. #ifndef NO_MD5
  42464. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  42465. #endif
  42466. #ifndef NO_SHA
  42467. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  42468. #endif
  42469. #ifndef NO_SHA256
  42470. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  42471. #endif
  42472. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  42473. return EXPECT_RESULT();
  42474. }
  42475. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  42476. {
  42477. EXPECT_DECLS;
  42478. #if defined(HAVE_ECC)
  42479. WOLFSSL_EVP_PKEY* pkey = NULL;
  42480. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  42481. ExpectNotNull(pkey = EVP_PKEY_new());
  42482. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  42483. EVP_PKEY_free(pkey);
  42484. #endif
  42485. return EXPECT_RESULT();
  42486. }
  42487. static int test_wolfSSL_EVP_X_STATE(void)
  42488. {
  42489. EXPECT_DECLS;
  42490. #if !defined(NO_DES3) && !defined(NO_RC4)
  42491. byte key[DES3_KEY_SIZE] = {0};
  42492. byte iv[DES_IV_SIZE] = {0};
  42493. EVP_CIPHER_CTX *ctx = NULL;
  42494. const EVP_CIPHER *init = NULL;
  42495. /* Bad test cases */
  42496. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42497. ExpectNotNull(init = EVP_des_ede3_cbc());
  42498. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42499. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42500. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  42501. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  42502. EVP_CIPHER_CTX_free(ctx);
  42503. ctx = NULL;
  42504. /* Good test case */
  42505. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42506. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42507. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42508. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42509. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  42510. EVP_CIPHER_CTX_free(ctx);
  42511. #endif
  42512. return EXPECT_RESULT();
  42513. }
  42514. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  42515. {
  42516. EXPECT_DECLS;
  42517. #if !defined(NO_DES3) && !defined(NO_RC4)
  42518. byte key[DES3_KEY_SIZE] = {0};
  42519. byte iv[DES_IV_SIZE] = {0};
  42520. EVP_CIPHER_CTX *ctx = NULL;
  42521. const EVP_CIPHER *init = NULL;
  42522. /* Bad test cases */
  42523. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42524. ExpectNotNull(init = EVP_des_ede3_cbc());
  42525. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42526. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42527. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  42528. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  42529. EVP_CIPHER_CTX_free(ctx);
  42530. ctx = NULL;
  42531. /* Good test case */
  42532. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  42533. ExpectNotNull(init = wolfSSL_EVP_rc4());
  42534. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  42535. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  42536. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  42537. EVP_CIPHER_CTX_free(ctx);
  42538. #endif
  42539. return EXPECT_RESULT();
  42540. }
  42541. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  42542. {
  42543. EXPECT_DECLS;
  42544. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  42545. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  42546. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  42547. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  42548. #ifdef HAVE_AES_CBC
  42549. #ifdef WOLFSSL_AES_128
  42550. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  42551. #endif
  42552. #ifdef WOLFSSL_AES_192
  42553. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  42554. #endif
  42555. #ifdef WOLFSSL_AES_256
  42556. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  42557. #endif
  42558. #endif
  42559. #ifdef HAVE_AESGCM
  42560. #ifdef WOLFSSL_AES_128
  42561. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  42562. #endif
  42563. #ifdef WOLFSSL_AES_192
  42564. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  42565. #endif
  42566. #ifdef WOLFSSL_AES_256
  42567. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  42568. #endif
  42569. #endif
  42570. #ifdef HAVE_AESCCM
  42571. #ifdef WOLFSSL_AES_128
  42572. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  42573. #endif
  42574. #ifdef WOLFSSL_AES_192
  42575. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  42576. #endif
  42577. #ifdef WOLFSSL_AES_256
  42578. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  42579. #endif
  42580. #endif
  42581. #ifdef WOLFSSL_AES_COUNTER
  42582. #ifdef WOLFSSL_AES_128
  42583. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  42584. #endif
  42585. #ifdef WOLFSSL_AES_192
  42586. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  42587. #endif
  42588. #ifdef WOLFSSL_AES_256
  42589. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  42590. #endif
  42591. #endif
  42592. #ifdef HAVE_AES_ECB
  42593. #ifdef WOLFSSL_AES_128
  42594. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  42595. #endif
  42596. #ifdef WOLFSSL_AES_192
  42597. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  42598. #endif
  42599. #ifdef WOLFSSL_AES_256
  42600. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  42601. #endif
  42602. #endif
  42603. #ifdef WOLFSSL_AES_OFB
  42604. #ifdef WOLFSSL_AES_128
  42605. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  42606. #endif
  42607. #ifdef WOLFSSL_AES_192
  42608. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  42609. #endif
  42610. #ifdef WOLFSSL_AES_256
  42611. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  42612. #endif
  42613. #endif
  42614. #ifndef NO_RC4
  42615. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  42616. #endif
  42617. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42618. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  42619. #endif
  42620. #endif
  42621. #ifdef WOLFSSL_SM4_ECB
  42622. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  42623. #endif
  42624. #ifdef WOLFSSL_SM4_CBC
  42625. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  42626. #endif
  42627. #ifdef WOLFSSL_SM4_CTR
  42628. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  42629. #endif
  42630. #ifdef WOLFSSL_SM4_GCM
  42631. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  42632. #endif
  42633. #ifdef WOLFSSL_SM4_CCM
  42634. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  42635. #endif
  42636. return EXPECT_RESULT();
  42637. }
  42638. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  42639. {
  42640. EXPECT_DECLS;
  42641. int nids[] = {
  42642. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42643. #ifdef WOLFSSL_AES_128
  42644. NID_aes_128_cbc,
  42645. #endif
  42646. #ifdef WOLFSSL_AES_192
  42647. NID_aes_192_cbc,
  42648. #endif
  42649. #ifdef WOLFSSL_AES_256
  42650. NID_aes_256_cbc,
  42651. #endif
  42652. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42653. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42654. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42655. #ifdef HAVE_AESGCM
  42656. #ifdef WOLFSSL_AES_128
  42657. NID_aes_128_gcm,
  42658. #endif
  42659. #ifdef WOLFSSL_AES_192
  42660. NID_aes_192_gcm,
  42661. #endif
  42662. #ifdef WOLFSSL_AES_256
  42663. NID_aes_256_gcm,
  42664. #endif
  42665. #endif /* HAVE_AESGCM */
  42666. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42667. #ifdef WOLFSSL_AES_COUNTER
  42668. #ifdef WOLFSSL_AES_128
  42669. NID_aes_128_ctr,
  42670. #endif
  42671. #ifdef WOLFSSL_AES_192
  42672. NID_aes_192_ctr,
  42673. #endif
  42674. #ifdef WOLFSSL_AES_256
  42675. NID_aes_256_ctr,
  42676. #endif
  42677. #endif
  42678. #ifndef NO_DES3
  42679. NID_des_cbc,
  42680. NID_des_ede3_cbc,
  42681. #endif
  42682. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42683. NID_chacha20_poly1305,
  42684. #endif
  42685. };
  42686. int iv_lengths[] = {
  42687. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  42688. #ifdef WOLFSSL_AES_128
  42689. AES_BLOCK_SIZE,
  42690. #endif
  42691. #ifdef WOLFSSL_AES_192
  42692. AES_BLOCK_SIZE,
  42693. #endif
  42694. #ifdef WOLFSSL_AES_256
  42695. AES_BLOCK_SIZE,
  42696. #endif
  42697. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  42698. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  42699. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42700. #ifdef HAVE_AESGCM
  42701. #ifdef WOLFSSL_AES_128
  42702. GCM_NONCE_MID_SZ,
  42703. #endif
  42704. #ifdef WOLFSSL_AES_192
  42705. GCM_NONCE_MID_SZ,
  42706. #endif
  42707. #ifdef WOLFSSL_AES_256
  42708. GCM_NONCE_MID_SZ,
  42709. #endif
  42710. #endif /* HAVE_AESGCM */
  42711. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  42712. #ifdef WOLFSSL_AES_COUNTER
  42713. #ifdef WOLFSSL_AES_128
  42714. AES_BLOCK_SIZE,
  42715. #endif
  42716. #ifdef WOLFSSL_AES_192
  42717. AES_BLOCK_SIZE,
  42718. #endif
  42719. #ifdef WOLFSSL_AES_256
  42720. AES_BLOCK_SIZE,
  42721. #endif
  42722. #endif
  42723. #ifndef NO_DES3
  42724. DES_BLOCK_SIZE,
  42725. DES_BLOCK_SIZE,
  42726. #endif
  42727. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  42728. CHACHA20_POLY1305_AEAD_IV_SIZE,
  42729. #endif
  42730. };
  42731. int i;
  42732. int nidsLen = (sizeof(nids)/sizeof(int));
  42733. for (i = 0; i < nidsLen; i++) {
  42734. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  42735. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  42736. }
  42737. return EXPECT_RESULT();
  42738. }
  42739. static int test_wolfSSL_EVP_SignInit_ex(void)
  42740. {
  42741. EXPECT_DECLS;
  42742. WOLFSSL_EVP_MD_CTX mdCtx;
  42743. WOLFSSL_ENGINE* e = 0;
  42744. const EVP_MD* md = EVP_sha256();
  42745. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42746. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  42747. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42748. return EXPECT_RESULT();
  42749. }
  42750. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  42751. {
  42752. EXPECT_DECLS;
  42753. #if !defined(NO_SHA256)
  42754. WOLFSSL_EVP_MD_CTX mdCtx;
  42755. unsigned int s = 0;
  42756. unsigned char md[WC_SHA256_DIGEST_SIZE];
  42757. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  42758. /* Bad Case */
  42759. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  42760. (HAVE_FIPS_VERSION > 2))
  42761. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42762. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  42763. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  42764. #else
  42765. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42766. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  42767. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42768. #endif
  42769. /* Good Case */
  42770. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  42771. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  42772. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  42773. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  42774. #endif
  42775. return EXPECT_RESULT();
  42776. }
  42777. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  42778. {
  42779. EXPECT_DECLS;
  42780. #if defined(OPENSSL_EXTRA)
  42781. EVP_PKEY* pkey = NULL;
  42782. EVP_PKEY_CTX* ctx = NULL;
  42783. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42784. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42785. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  42786. /* void */
  42787. EVP_PKEY_CTX_free(ctx);
  42788. #else
  42789. /* int */
  42790. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  42791. #endif
  42792. EVP_PKEY_free(pkey);
  42793. #endif
  42794. return EXPECT_RESULT();
  42795. }
  42796. static int test_wolfSSL_EVP_PKEY_param_check(void)
  42797. {
  42798. EXPECT_DECLS;
  42799. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  42800. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  42801. DH *dh = NULL;
  42802. DH *setDh = NULL;
  42803. EVP_PKEY *pkey = NULL;
  42804. EVP_PKEY_CTX* ctx = NULL;
  42805. FILE* f = NULL;
  42806. unsigned char buf[512];
  42807. const unsigned char* pt = buf;
  42808. const char* dh2048 = "./certs/dh2048.der";
  42809. long len = 0;
  42810. int code = -1;
  42811. XMEMSET(buf, 0, sizeof(buf));
  42812. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  42813. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42814. if (f != XBADFILE)
  42815. XFCLOSE(f);
  42816. /* Load dh2048.der into DH with internal format */
  42817. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  42818. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42819. ExpectIntEQ(code, 0);
  42820. code = -1;
  42821. pkey = wolfSSL_EVP_PKEY_new();
  42822. /* Set DH into PKEY */
  42823. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42824. /* create ctx from pkey */
  42825. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42826. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  42827. /* TODO: more invalid cases */
  42828. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  42829. EVP_PKEY_CTX_free(ctx);
  42830. EVP_PKEY_free(pkey);
  42831. DH_free(setDh);
  42832. DH_free(dh);
  42833. #endif
  42834. #endif
  42835. return EXPECT_RESULT();
  42836. }
  42837. static int test_wolfSSL_EVP_BytesToKey(void)
  42838. {
  42839. EXPECT_DECLS;
  42840. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  42841. byte key[AES_BLOCK_SIZE] = {0};
  42842. byte iv[AES_BLOCK_SIZE] = {0};
  42843. int count = 0;
  42844. const EVP_MD* md = EVP_sha256();
  42845. const EVP_CIPHER *type;
  42846. const unsigned char *salt = (unsigned char *)"salt1234";
  42847. int sz = 5;
  42848. const byte data[] = {
  42849. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42850. 0x72,0x6c,0x64
  42851. };
  42852. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  42853. /* Bad cases */
  42854. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  42855. 0);
  42856. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  42857. 16);
  42858. md = "2";
  42859. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  42860. WOLFSSL_FAILURE);
  42861. /* Good case */
  42862. md = EVP_sha256();
  42863. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  42864. 16);
  42865. #endif
  42866. return EXPECT_RESULT();
  42867. }
  42868. static int test_evp_cipher_aes_gcm(void)
  42869. {
  42870. EXPECT_DECLS;
  42871. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  42872. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  42873. (HAVE_FIPS_VERSION >= 2)))
  42874. /*
  42875. * This test checks data at various points in the encrypt/decrypt process
  42876. * against known values produced using the same test with OpenSSL. This
  42877. * interop testing is critical for verifying the correctness of our
  42878. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  42879. * a flow supported by OpenSSL that uses the control command
  42880. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  42881. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  42882. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  42883. * wolfSSL OpenSSH clients because there was a bug in this flow that
  42884. * happened to "cancel out" if both sides of the connection had the bug.
  42885. */
  42886. enum {
  42887. NUM_ENCRYPTIONS = 3,
  42888. AAD_SIZE = 4
  42889. };
  42890. byte plainText1[] = {
  42891. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  42892. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  42893. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  42894. };
  42895. byte plainText2[] = {
  42896. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  42897. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  42898. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  42899. };
  42900. byte plainText3[] = {
  42901. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  42902. 0x5b, 0x57, 0x10
  42903. };
  42904. byte* plainTexts[NUM_ENCRYPTIONS] = {
  42905. plainText1,
  42906. plainText2,
  42907. plainText3
  42908. };
  42909. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  42910. sizeof(plainText1),
  42911. sizeof(plainText2),
  42912. sizeof(plainText3)
  42913. };
  42914. byte aad1[AAD_SIZE] = {
  42915. 0x00, 0x00, 0x00, 0x01
  42916. };
  42917. byte aad2[AAD_SIZE] = {
  42918. 0x00, 0x00, 0x00, 0x10
  42919. };
  42920. byte aad3[AAD_SIZE] = {
  42921. 0x00, 0x00, 0x01, 0x00
  42922. };
  42923. byte* aads[NUM_ENCRYPTIONS] = {
  42924. aad1,
  42925. aad2,
  42926. aad3
  42927. };
  42928. const byte iv[GCM_NONCE_MID_SZ] = {
  42929. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  42930. };
  42931. byte currentIv[GCM_NONCE_MID_SZ];
  42932. const byte key[] = {
  42933. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  42934. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  42935. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  42936. };
  42937. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  42938. {
  42939. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42940. 0xEF
  42941. },
  42942. {
  42943. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42944. 0xF0
  42945. },
  42946. {
  42947. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  42948. 0xF1
  42949. }
  42950. };
  42951. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  42952. {
  42953. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  42954. 0x3D, 0x32, 0x18, 0x34, 0xA9
  42955. },
  42956. {
  42957. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  42958. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  42959. },
  42960. {
  42961. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  42962. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  42963. }
  42964. };
  42965. const byte expCipherText1[] = {
  42966. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  42967. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  42968. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  42969. };
  42970. const byte expCipherText2[] = {
  42971. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  42972. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  42973. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  42974. };
  42975. const byte expCipherText3[] = {
  42976. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  42977. 0x80, 0x5E, 0x6B,
  42978. };
  42979. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  42980. expCipherText1,
  42981. expCipherText2,
  42982. expCipherText3
  42983. };
  42984. byte* cipherText = NULL;
  42985. byte* calcPlainText = NULL;
  42986. byte tag[AES_BLOCK_SIZE];
  42987. EVP_CIPHER_CTX* encCtx = NULL;
  42988. EVP_CIPHER_CTX* decCtx = NULL;
  42989. int i, j, outl;
  42990. /****************************************************/
  42991. for (i = 0; i < 3; ++i) {
  42992. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  42993. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  42994. /* First iteration, set key before IV. */
  42995. if (i == 0) {
  42996. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  42997. SSL_SUCCESS);
  42998. /*
  42999. * The call to EVP_CipherInit below (with NULL key) should clear the
  43000. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  43001. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  43002. * behavior.
  43003. */
  43004. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43005. (void*)iv), SSL_SUCCESS);
  43006. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  43007. SSL_SUCCESS);
  43008. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43009. currentIv), SSL_FAILURE);
  43010. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  43011. SSL_SUCCESS);
  43012. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  43013. SSL_SUCCESS);
  43014. }
  43015. /* Second iteration, IV before key. */
  43016. else {
  43017. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  43018. SSL_SUCCESS);
  43019. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  43020. SSL_SUCCESS);
  43021. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  43022. SSL_SUCCESS);
  43023. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  43024. SSL_SUCCESS);
  43025. }
  43026. /*
  43027. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  43028. * been issued first.
  43029. */
  43030. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43031. currentIv), SSL_FAILURE);
  43032. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43033. (void*)iv), SSL_SUCCESS);
  43034. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43035. (void*)iv), SSL_SUCCESS);
  43036. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  43037. /*************** Encrypt ***************/
  43038. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43039. currentIv), SSL_SUCCESS);
  43040. /* Check current IV against expected. */
  43041. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43042. /* Add AAD. */
  43043. if (i == 2) {
  43044. /* Test streaming API. */
  43045. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  43046. AAD_SIZE), SSL_SUCCESS);
  43047. }
  43048. else {
  43049. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  43050. AAD_SIZE);
  43051. }
  43052. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43053. DYNAMIC_TYPE_TMP_BUFFER));
  43054. /* Encrypt plaintext. */
  43055. if (i == 2) {
  43056. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  43057. plainTexts[j], plainTextSzs[j]),
  43058. SSL_SUCCESS);
  43059. }
  43060. else {
  43061. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  43062. plainTextSzs[j]), plainTextSzs[j]);
  43063. }
  43064. if (i == 2) {
  43065. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  43066. SSL_SUCCESS);
  43067. }
  43068. else {
  43069. /*
  43070. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  43071. * akin to calling EVP_CipherFinal.
  43072. */
  43073. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  43074. }
  43075. /* Check ciphertext against expected. */
  43076. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  43077. 0);
  43078. /* Get and check tag against expected. */
  43079. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  43080. sizeof(tag), tag), SSL_SUCCESS);
  43081. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  43082. /*************** Decrypt ***************/
  43083. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43084. currentIv), SSL_SUCCESS);
  43085. /* Check current IV against expected. */
  43086. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43087. /* Add AAD. */
  43088. if (i == 2) {
  43089. /* Test streaming API. */
  43090. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  43091. AAD_SIZE), SSL_SUCCESS);
  43092. }
  43093. else {
  43094. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  43095. AAD_SIZE);
  43096. }
  43097. /* Set expected tag. */
  43098. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  43099. sizeof(tag), tag), SSL_SUCCESS);
  43100. /* Decrypt ciphertext. */
  43101. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43102. DYNAMIC_TYPE_TMP_BUFFER));
  43103. if (i == 2) {
  43104. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  43105. cipherText, plainTextSzs[j]),
  43106. SSL_SUCCESS);
  43107. }
  43108. else {
  43109. /* This first EVP_Cipher call will check the tag, too. */
  43110. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  43111. plainTextSzs[j]), plainTextSzs[j]);
  43112. }
  43113. if (i == 2) {
  43114. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  43115. SSL_SUCCESS);
  43116. }
  43117. else {
  43118. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  43119. }
  43120. /* Check plaintext against expected. */
  43121. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  43122. 0);
  43123. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43124. cipherText = NULL;
  43125. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43126. calcPlainText = NULL;
  43127. }
  43128. EVP_CIPHER_CTX_free(encCtx);
  43129. encCtx = NULL;
  43130. EVP_CIPHER_CTX_free(decCtx);
  43131. decCtx = NULL;
  43132. }
  43133. #endif
  43134. return EXPECT_RESULT();
  43135. }
  43136. static int test_wolfSSL_OBJ_ln(void)
  43137. {
  43138. EXPECT_DECLS;
  43139. const int nid_set[] = {
  43140. NID_commonName,
  43141. NID_serialNumber,
  43142. NID_countryName,
  43143. NID_localityName,
  43144. NID_stateOrProvinceName,
  43145. NID_organizationName,
  43146. NID_organizationalUnitName,
  43147. NID_domainComponent,
  43148. NID_businessCategory,
  43149. NID_jurisdictionCountryName,
  43150. NID_jurisdictionStateOrProvinceName,
  43151. NID_emailAddress
  43152. };
  43153. const char* ln_set[] = {
  43154. "commonName",
  43155. "serialNumber",
  43156. "countryName",
  43157. "localityName",
  43158. "stateOrProvinceName",
  43159. "organizationName",
  43160. "organizationalUnitName",
  43161. "domainComponent",
  43162. "businessCategory",
  43163. "jurisdictionCountryName",
  43164. "jurisdictionStateOrProvinceName",
  43165. "emailAddress",
  43166. };
  43167. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  43168. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  43169. #ifdef HAVE_ECC
  43170. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43171. {
  43172. EC_builtin_curve r[27];
  43173. size_t nCurves = sizeof(r) / sizeof(r[0]);
  43174. nCurves = EC_get_builtin_curves(r, nCurves);
  43175. for (i = 0; i < nCurves; i++) {
  43176. /* skip ECC_CURVE_INVALID */
  43177. if (r[i].nid != ECC_CURVE_INVALID) {
  43178. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  43179. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  43180. }
  43181. }
  43182. }
  43183. #endif
  43184. #endif
  43185. for (i = 0; i < maxIdx; i++) {
  43186. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  43187. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  43188. }
  43189. return EXPECT_RESULT();
  43190. }
  43191. static int test_wolfSSL_OBJ_sn(void)
  43192. {
  43193. EXPECT_DECLS;
  43194. int i = 0, maxIdx = 7;
  43195. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  43196. NID_stateOrProvinceName,NID_organizationName,
  43197. NID_organizationalUnitName,NID_emailAddress};
  43198. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  43199. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  43200. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  43201. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  43202. WOLFSSL_EMAIL_ADDR};
  43203. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  43204. for (i = 0; i < maxIdx; i++) {
  43205. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  43206. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  43207. }
  43208. return EXPECT_RESULT();
  43209. }
  43210. #if !defined(NO_BIO)
  43211. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  43212. {
  43213. return lh_strhash(s[3]);
  43214. }
  43215. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  43216. {
  43217. return XSTRCMP(a[3], b[3]);
  43218. }
  43219. #endif
  43220. static int test_wolfSSL_TXT_DB(void)
  43221. {
  43222. EXPECT_DECLS;
  43223. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43224. BIO *bio = NULL;
  43225. TXT_DB *db = NULL;
  43226. const int columns = 6;
  43227. const char *fields[6] = {
  43228. "V",
  43229. "320926161116Z",
  43230. "",
  43231. "12BD",
  43232. "unknown",
  43233. "/CN=rsa doe",
  43234. };
  43235. char** fields_copy = NULL;
  43236. /* Test read */
  43237. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  43238. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  43239. ExpectNotNull(db = TXT_DB_read(bio, columns));
  43240. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  43241. DYNAMIC_TYPE_OPENSSL));
  43242. if (fields_copy != NULL) {
  43243. XMEMCPY(fields_copy, fields, sizeof(fields));
  43244. }
  43245. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  43246. if (EXPECT_FAIL()) {
  43247. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  43248. }
  43249. BIO_free(bio);
  43250. bio = NULL;
  43251. /* Test write */
  43252. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  43253. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  43254. BIO_free(bio);
  43255. /* Test index */
  43256. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  43257. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  43258. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43259. fields[3] = "12DA";
  43260. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43261. fields[3] = "FFFF";
  43262. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43263. fields[3] = "";
  43264. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  43265. TXT_DB_free(db);
  43266. #endif
  43267. return EXPECT_RESULT();
  43268. }
  43269. static int test_wolfSSL_NCONF(void)
  43270. {
  43271. EXPECT_DECLS;
  43272. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43273. const char* confFile = "./tests/NCONF_test.cnf";
  43274. CONF* conf = NULL;
  43275. long eline = 0;
  43276. long num = 0;
  43277. ExpectNotNull(conf = NCONF_new(NULL));
  43278. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  43279. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  43280. ExpectIntEQ(num, 1234);
  43281. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  43282. ExpectIntEQ(num, 4321);
  43283. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  43284. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  43285. "./test-dir/file1");
  43286. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  43287. "./test-dir/file1:./test-dir/file2:./section1:file2");
  43288. NCONF_free(conf);
  43289. #endif
  43290. return EXPECT_RESULT();
  43291. }
  43292. #endif /* OPENSSL_ALL */
  43293. static int test_wolfSSL_X509V3_EXT_get(void) {
  43294. EXPECT_DECLS;
  43295. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43296. XFILE f = XBADFILE;
  43297. int numOfExt =0;
  43298. int extNid = 0;
  43299. int i = 0;
  43300. WOLFSSL_X509* x509 = NULL;
  43301. WOLFSSL_X509_EXTENSION* ext = NULL;
  43302. const WOLFSSL_v3_ext_method* method = NULL;
  43303. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43304. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43305. if (f != XBADFILE)
  43306. XFCLOSE(f);
  43307. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  43308. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43309. for (i = 0; i < numOfExt; i++) {
  43310. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43311. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  43312. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43313. ExpectIntEQ(method->ext_nid, extNid);
  43314. }
  43315. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  43316. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  43317. wolfSSL_X509_free(x509);
  43318. #endif
  43319. return EXPECT_RESULT();
  43320. }
  43321. static int test_wolfSSL_X509V3_EXT_nconf(void)
  43322. {
  43323. EXPECT_DECLS;
  43324. #ifdef OPENSSL_ALL
  43325. const char *ext_names[] = {
  43326. "subjectKeyIdentifier",
  43327. "authorityKeyIdentifier",
  43328. "subjectAltName",
  43329. "keyUsage",
  43330. "extendedKeyUsage",
  43331. };
  43332. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  43333. int ext_nids[] = {
  43334. NID_subject_key_identifier,
  43335. NID_authority_key_identifier,
  43336. NID_subject_alt_name,
  43337. NID_key_usage,
  43338. NID_ext_key_usage,
  43339. };
  43340. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  43341. const char *ext_values[] = {
  43342. "hash",
  43343. "hash",
  43344. "DNS:example.com, IP:127.0.0.1",
  43345. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  43346. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  43347. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  43348. "OCSPSigning",
  43349. };
  43350. size_t i;
  43351. X509_EXTENSION* ext = NULL;
  43352. X509* x509 = NULL;
  43353. unsigned int keyUsageFlags;
  43354. unsigned int extKeyUsageFlags;
  43355. ExpectNotNull(x509 = X509_new());
  43356. /* keyUsage / extKeyUsage should match string above */
  43357. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43358. | KU_NON_REPUDIATION
  43359. | KU_KEY_ENCIPHERMENT
  43360. | KU_DATA_ENCIPHERMENT
  43361. | KU_KEY_AGREEMENT
  43362. | KU_KEY_CERT_SIGN
  43363. | KU_CRL_SIGN
  43364. | KU_ENCIPHER_ONLY
  43365. | KU_DECIPHER_ONLY;
  43366. extKeyUsageFlags = XKU_SSL_CLIENT
  43367. | XKU_SSL_SERVER
  43368. | XKU_CODE_SIGN
  43369. | XKU_SMIME
  43370. | XKU_TIMESTAMP
  43371. | XKU_OCSP_SIGN;
  43372. for (i = 0; i < ext_names_count; i++) {
  43373. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  43374. ext_values[i]));
  43375. X509_EXTENSION_free(ext);
  43376. ext = NULL;
  43377. }
  43378. for (i = 0; i < ext_nids_count; i++) {
  43379. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  43380. ext_values[i]));
  43381. X509_EXTENSION_free(ext);
  43382. ext = NULL;
  43383. }
  43384. /* Test adding extension to X509 */
  43385. for (i = 0; i < ext_nids_count; i++) {
  43386. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  43387. ext_values[i]));
  43388. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  43389. if (ext_nids[i] == NID_key_usage) {
  43390. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43391. }
  43392. else if (ext_nids[i] == NID_ext_key_usage) {
  43393. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43394. }
  43395. X509_EXTENSION_free(ext);
  43396. ext = NULL;
  43397. }
  43398. X509_free(x509);
  43399. #endif
  43400. return EXPECT_RESULT();
  43401. }
  43402. static int test_wolfSSL_X509V3_EXT(void) {
  43403. EXPECT_DECLS;
  43404. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43405. XFILE f = XBADFILE;
  43406. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  43407. char* str = NULL;
  43408. unsigned char* data = NULL;
  43409. const WOLFSSL_v3_ext_method* method = NULL;
  43410. WOLFSSL_X509* x509 = NULL;
  43411. WOLFSSL_X509_EXTENSION* ext = NULL;
  43412. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  43413. WOLFSSL_ASN1_OBJECT *obj = NULL;
  43414. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  43415. WOLFSSL_ASN1_STRING* asn1str = NULL;
  43416. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  43417. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  43418. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  43419. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  43420. WOLFSSL_GENERAL_NAME* gn = NULL;
  43421. /* Check NULL argument */
  43422. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  43423. /* Using OCSP cert with X509V3 extensions */
  43424. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  43425. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43426. if (f != XBADFILE)
  43427. XFCLOSE(f);
  43428. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  43429. /* Basic Constraints */
  43430. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43431. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43432. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  43433. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  43434. ExpectIntEQ(bc->ca, 1);
  43435. ExpectNull(bc->pathlen);
  43436. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  43437. bc = NULL;
  43438. i++;
  43439. /* Subject Key Identifier */
  43440. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43441. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43442. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  43443. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43444. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  43445. asn1str));
  43446. X509_EXTENSION_free(ext2);
  43447. ext2 = NULL;
  43448. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43449. ExpectNotNull(method->i2s);
  43450. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  43451. wolfSSL_ASN1_STRING_free(asn1str);
  43452. asn1str = NULL;
  43453. if (str != NULL) {
  43454. actual = strcmp(str,
  43455. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43456. }
  43457. ExpectIntEQ(actual, 0);
  43458. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43459. str = NULL;
  43460. i++;
  43461. /* Authority Key Identifier */
  43462. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43463. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43464. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  43465. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  43466. ext));
  43467. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  43468. ExpectNotNull(asn1str = aKeyId->keyid);
  43469. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  43470. asn1str));
  43471. asn1str = NULL;
  43472. if (str != NULL) {
  43473. actual = strcmp(str,
  43474. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  43475. }
  43476. ExpectIntEQ(actual, 0);
  43477. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43478. str = NULL;
  43479. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  43480. aKeyId = NULL;
  43481. i++;
  43482. /* Key Usage */
  43483. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43484. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43485. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  43486. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  43487. #if defined(WOLFSSL_QT)
  43488. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  43489. #else
  43490. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  43491. #endif
  43492. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  43493. if (data != NULL) {
  43494. #ifdef BIG_ENDIAN_ORDER
  43495. actual = data[1];
  43496. #else
  43497. actual = data[0];
  43498. #endif
  43499. }
  43500. ExpectIntEQ(actual, expected);
  43501. wolfSSL_ASN1_STRING_free(asn1str);
  43502. asn1str = NULL;
  43503. #if 1
  43504. i++;
  43505. /* Authority Info Access */
  43506. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  43507. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  43508. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  43509. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  43510. ext));
  43511. #if defined(WOLFSSL_QT)
  43512. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43513. #else
  43514. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  43515. #endif
  43516. /* URI entry is an ACCESS_DESCRIPTION type */
  43517. #if defined(WOLFSSL_QT)
  43518. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  43519. #else
  43520. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  43521. #endif
  43522. ExpectNotNull(adObj = ad->method);
  43523. /* Make sure nid is OCSP */
  43524. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  43525. /* GENERAL_NAME stores URI as an ASN1_STRING */
  43526. ExpectNotNull(gn = ad->location);
  43527. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  43528. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  43529. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  43530. #if defined(WOLFSSL_QT)
  43531. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  43532. #else
  43533. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  43534. #endif
  43535. if (str != NULL) {
  43536. actual = strcmp(str, "http://127.0.0.1:22220");
  43537. }
  43538. ExpectIntEQ(actual, 0);
  43539. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  43540. aia = NULL;
  43541. #else
  43542. (void) aia; (void) ad; (void) adObj; (void) gn;
  43543. #endif
  43544. wolfSSL_X509_free(x509);
  43545. #endif
  43546. return EXPECT_RESULT();
  43547. }
  43548. static int test_wolfSSL_X509_get_extension_flags(void)
  43549. {
  43550. EXPECT_DECLS;
  43551. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43552. XFILE f = XBADFILE;
  43553. X509* x509 = NULL;
  43554. unsigned int extFlags;
  43555. unsigned int keyUsageFlags;
  43556. unsigned int extKeyUsageFlags;
  43557. /* client-int-cert.pem has the following extension flags. */
  43558. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  43559. /* and the following key usage flags. */
  43560. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43561. | KU_NON_REPUDIATION
  43562. | KU_KEY_ENCIPHERMENT;
  43563. /* and the following extended key usage flags. */
  43564. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  43565. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  43566. XBADFILE);
  43567. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43568. if (f != XBADFILE) {
  43569. XFCLOSE(f);
  43570. f = XBADFILE;
  43571. }
  43572. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43573. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43574. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  43575. X509_free(x509);
  43576. x509 = NULL;
  43577. /* client-cert-ext.pem has the following extension flags. */
  43578. extFlags = EXFLAG_KUSAGE;
  43579. /* and the following key usage flags. */
  43580. keyUsageFlags = KU_DIGITAL_SIGNATURE
  43581. | KU_KEY_CERT_SIGN
  43582. | KU_CRL_SIGN;
  43583. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  43584. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43585. if (f != XBADFILE)
  43586. XFCLOSE(f);
  43587. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  43588. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  43589. X509_free(x509);
  43590. #endif /* OPENSSL_ALL */
  43591. return EXPECT_RESULT();
  43592. }
  43593. static int test_wolfSSL_X509_get_ext(void)
  43594. {
  43595. EXPECT_DECLS;
  43596. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43597. int ret = 0;
  43598. XFILE f = XBADFILE;
  43599. WOLFSSL_X509* x509 = NULL;
  43600. WOLFSSL_X509_EXTENSION* foundExtension;
  43601. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43602. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43603. if (f != XBADFILE)
  43604. XFCLOSE(f);
  43605. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  43606. /* wolfSSL_X509_get_ext() valid input */
  43607. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  43608. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  43609. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  43610. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  43611. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  43612. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  43613. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  43614. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  43615. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  43616. wolfSSL_X509_free(x509);
  43617. #endif
  43618. return EXPECT_RESULT();
  43619. }
  43620. static int test_wolfSSL_X509_get_ext_by_NID(void)
  43621. {
  43622. EXPECT_DECLS;
  43623. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43624. int rc;
  43625. XFILE f = XBADFILE;
  43626. WOLFSSL_X509* x509 = NULL;
  43627. ASN1_OBJECT* obj = NULL;
  43628. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43629. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43630. if (f != XBADFILE)
  43631. XFCLOSE(f);
  43632. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43633. -1), 0);
  43634. /* Start search from last location (should fail) */
  43635. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43636. rc), -1);
  43637. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  43638. -2), -1);
  43639. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  43640. -1), -1);
  43641. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  43642. /* NID_ext_key_usage, check also its nid and oid */
  43643. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  43644. -1);
  43645. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  43646. x509, rc)));
  43647. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  43648. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  43649. wolfSSL_X509_free(x509);
  43650. #endif
  43651. return EXPECT_RESULT();
  43652. }
  43653. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  43654. {
  43655. EXPECT_DECLS;
  43656. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43657. int rc;
  43658. XFILE f = XBADFILE;
  43659. WOLFSSL_X509* x509 = NULL;
  43660. WOLFSSL_X509_EXTENSION* ext;
  43661. WOLFSSL_ASN1_STRING* sanString;
  43662. byte* sanDer;
  43663. const byte expectedDer[] = {
  43664. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  43665. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  43666. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43667. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  43668. if (f != XBADFILE)
  43669. XFCLOSE(f);
  43670. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  43671. ExpectNotNull(ext = X509_get_ext(x509, rc));
  43672. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  43673. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  43674. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  43675. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  43676. X509_free(x509);
  43677. #endif
  43678. return EXPECT_RESULT();
  43679. }
  43680. static int test_wolfSSL_X509_EXTENSION_new(void)
  43681. {
  43682. EXPECT_DECLS;
  43683. #if defined (OPENSSL_ALL)
  43684. WOLFSSL_X509_EXTENSION* ext = NULL;
  43685. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  43686. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  43687. wolfSSL_X509_EXTENSION_free(ext);
  43688. #endif
  43689. return EXPECT_RESULT();
  43690. }
  43691. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  43692. {
  43693. EXPECT_DECLS;
  43694. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43695. WOLFSSL_X509* x509 = NULL;
  43696. WOLFSSL_X509_EXTENSION* ext = NULL;
  43697. WOLFSSL_ASN1_OBJECT* o = NULL;
  43698. XFILE file = XBADFILE;
  43699. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43700. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43701. if (file != XBADFILE)
  43702. XFCLOSE(file);
  43703. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  43704. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43705. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  43706. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  43707. ExpectIntEQ(o->nid, 128);
  43708. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  43709. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  43710. wolfSSL_X509_free(x509);
  43711. #endif
  43712. return EXPECT_RESULT();
  43713. }
  43714. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  43715. {
  43716. EXPECT_DECLS;
  43717. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43718. WOLFSSL_X509* x509 = NULL;
  43719. WOLFSSL_X509_EXTENSION* ext;
  43720. WOLFSSL_ASN1_STRING* str;
  43721. XFILE file = XBADFILE;
  43722. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43723. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43724. if (file != XBADFILE)
  43725. XFCLOSE(file);
  43726. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43727. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  43728. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  43729. wolfSSL_X509_free(x509);
  43730. #endif
  43731. return EXPECT_RESULT();
  43732. }
  43733. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  43734. {
  43735. EXPECT_DECLS;
  43736. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  43737. WOLFSSL_X509* x509 = NULL;
  43738. WOLFSSL_X509_EXTENSION* ext;
  43739. XFILE file = XBADFILE;
  43740. int crit;
  43741. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43742. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  43743. if (file != XBADFILE)
  43744. XFCLOSE(file);
  43745. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  43746. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  43747. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  43748. wolfSSL_X509_free(x509);
  43749. #endif
  43750. return EXPECT_RESULT();
  43751. }
  43752. static int test_wolfSSL_X509V3_EXT_print(void)
  43753. {
  43754. EXPECT_DECLS;
  43755. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  43756. !defined(NO_RSA)
  43757. {
  43758. XFILE f = XBADFILE;
  43759. WOLFSSL_X509* x509 = NULL;
  43760. X509_EXTENSION * ext = NULL;
  43761. int loc;
  43762. BIO *bio = NULL;
  43763. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  43764. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  43765. if (f != XBADFILE)
  43766. fclose(f);
  43767. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  43768. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43769. NID_basic_constraints, -1), -1);
  43770. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43771. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43772. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43773. NID_subject_key_identifier, -1), -1);
  43774. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43775. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43776. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  43777. NID_authority_key_identifier, -1), -1);
  43778. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  43779. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  43780. wolfSSL_BIO_free(bio);
  43781. wolfSSL_X509_free(x509);
  43782. }
  43783. {
  43784. X509 *x509 = NULL;
  43785. BIO *bio = NULL;
  43786. X509_EXTENSION *ext;
  43787. unsigned int i;
  43788. unsigned int idx;
  43789. /* Some NIDs to test with */
  43790. int nids[] = {
  43791. /* NID_key_usage, currently X509_get_ext returns this as a bit
  43792. * string, which messes up X509V3_EXT_print */
  43793. /* NID_ext_key_usage, */
  43794. NID_subject_alt_name,
  43795. };
  43796. int* n;
  43797. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  43798. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  43799. WOLFSSL_FILETYPE_PEM));
  43800. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  43801. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  43802. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  43803. * update the index */
  43804. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  43805. ExpectNotNull(ext = X509_get_ext(x509, idx));
  43806. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  43807. ExpectIntGT(fprintf(stderr, "\n"), 0);
  43808. }
  43809. BIO_free(bio);
  43810. X509_free(x509);
  43811. }
  43812. #endif
  43813. return EXPECT_RESULT();
  43814. }
  43815. static int test_wolfSSL_X509_cmp(void)
  43816. {
  43817. EXPECT_DECLS;
  43818. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43819. XFILE file1 = XBADFILE;
  43820. XFILE file2 = XBADFILE;
  43821. WOLFSSL_X509* cert1 = NULL;
  43822. WOLFSSL_X509* cert2 = NULL;
  43823. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  43824. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  43825. XBADFILE);
  43826. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  43827. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  43828. if (file1 != XBADFILE)
  43829. fclose(file1);
  43830. if (file2 != XBADFILE)
  43831. fclose(file2);
  43832. /* wolfSSL_X509_cmp() testing matching certs */
  43833. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  43834. /* wolfSSL_X509_cmp() testing mismatched certs */
  43835. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  43836. /* wolfSSL_X509_cmp() testing NULL, valid args */
  43837. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  43838. /* wolfSSL_X509_cmp() testing valid, NULL args */
  43839. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  43840. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  43841. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  43842. wolfSSL_X509_free(cert1);
  43843. wolfSSL_X509_free(cert2);
  43844. #endif
  43845. return EXPECT_RESULT();
  43846. }
  43847. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  43848. {
  43849. EXPECT_DECLS;
  43850. #if defined(OPENSSL_ALL)
  43851. EVP_PKEY* pkey;
  43852. pkey = EVP_PKEY_new();
  43853. ExpectNotNull(pkey);
  43854. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  43855. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  43856. EVP_PKEY_free(pkey);
  43857. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  43858. EVP_PKEY_free(pkey);
  43859. EVP_PKEY_free(pkey);
  43860. #endif
  43861. return EXPECT_RESULT();
  43862. }
  43863. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  43864. {
  43865. EXPECT_DECLS;
  43866. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  43867. EVP_PKEY* pkey = NULL;
  43868. const unsigned char* p;
  43869. unsigned char *der = NULL;
  43870. unsigned char *tmp = NULL;
  43871. int derLen;
  43872. p = client_keypub_der_2048;
  43873. /* Check that key can be successfully decoded. */
  43874. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  43875. sizeof_client_keypub_der_2048));
  43876. /* Check that key can be successfully encoded. */
  43877. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  43878. /* Ensure that the encoded version matches the original. */
  43879. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  43880. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  43881. /* Do same test except with pre-allocated buffer to ensure the der pointer
  43882. * is advanced. */
  43883. tmp = der;
  43884. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  43885. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  43886. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  43887. ExpectTrue(der + derLen == tmp);
  43888. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43889. EVP_PKEY_free(pkey);
  43890. #endif
  43891. return EXPECT_RESULT();
  43892. }
  43893. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  43894. {
  43895. EXPECT_DECLS;
  43896. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  43897. !defined(NO_ASN) && !defined(NO_PWDBASED)
  43898. EVP_PKEY* pkey = NULL;
  43899. const unsigned char* p;
  43900. unsigned char *der = NULL;
  43901. unsigned char *tmp = NULL;
  43902. int derLen;
  43903. unsigned char pub_buf[65];
  43904. const int pub_len = 65;
  43905. BN_CTX* ctx;
  43906. EC_GROUP* curve = NULL;
  43907. EC_KEY* ephemeral_key = NULL;
  43908. const EC_POINT* h;
  43909. /* Generate an x963 key pair and get public part into pub_buf */
  43910. ExpectNotNull(ctx = BN_CTX_new());
  43911. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  43912. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  43913. NID_X9_62_prime256v1));
  43914. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  43915. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  43916. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  43917. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  43918. /* Prepare the EVP_PKEY */
  43919. ExpectNotNull(pkey = EVP_PKEY_new());
  43920. p = pub_buf;
  43921. /* Check that key can be successfully decoded. */
  43922. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  43923. pub_len));
  43924. /* Check that key can be successfully encoded. */
  43925. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  43926. /* Ensure that the encoded version matches the original. */
  43927. ExpectIntEQ(derLen, pub_len);
  43928. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  43929. /* Do same test except with pre-allocated buffer to ensure the der pointer
  43930. * is advanced. */
  43931. tmp = der;
  43932. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  43933. ExpectIntEQ(derLen, pub_len);
  43934. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  43935. ExpectTrue(der + derLen == tmp);
  43936. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43937. EVP_PKEY_free(pkey);
  43938. EC_KEY_free(ephemeral_key);
  43939. EC_GROUP_free(curve);
  43940. #endif
  43941. return EXPECT_RESULT();
  43942. }
  43943. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  43944. {
  43945. EXPECT_DECLS;
  43946. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  43947. DSA* dsa = NULL;
  43948. byte derIn[] = {
  43949. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  43950. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  43951. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  43952. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  43953. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  43954. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  43955. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  43956. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  43957. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  43958. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  43959. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  43960. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  43961. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  43962. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  43963. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  43964. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  43965. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  43966. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  43967. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  43968. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  43969. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  43970. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  43971. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  43972. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  43973. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  43974. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  43975. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  43976. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  43977. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  43978. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  43979. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  43980. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  43981. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  43982. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  43983. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  43984. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  43985. 0x1e, 0xde, 0xc4
  43986. };
  43987. int derInLen = sizeof(derIn);
  43988. byte* derOut = NULL;
  43989. int derOutLen;
  43990. byte* p = derIn;
  43991. /* Check that params can be successfully decoded. */
  43992. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  43993. /* Check that params can be successfully encoded. */
  43994. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  43995. /* Ensure that the encoded version matches the original. */
  43996. ExpectIntEQ(derInLen, derOutLen);
  43997. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  43998. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  43999. DSA_free(dsa);
  44000. #endif
  44001. return EXPECT_RESULT();
  44002. }
  44003. static int test_wolfSSL_i2d_PrivateKey(void)
  44004. {
  44005. EXPECT_DECLS;
  44006. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  44007. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44008. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44009. {
  44010. EVP_PKEY* pkey = NULL;
  44011. const unsigned char* server_key =
  44012. (const unsigned char*)server_key_der_2048;
  44013. unsigned char buf[FOURK_BUF];
  44014. unsigned char* pt = NULL;
  44015. int bufSz;
  44016. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  44017. (long)sizeof_server_key_der_2048));
  44018. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  44019. pt = buf;
  44020. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  44021. ExpectIntNE((pt - buf), 0);
  44022. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  44023. EVP_PKEY_free(pkey);
  44024. }
  44025. #endif
  44026. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44027. {
  44028. EVP_PKEY* pkey = NULL;
  44029. const unsigned char* client_key =
  44030. (const unsigned char*)ecc_clikey_der_256;
  44031. unsigned char buf[FOURK_BUF];
  44032. unsigned char* pt = NULL;
  44033. int bufSz;
  44034. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  44035. (long)sizeof_ecc_clikey_der_256)));
  44036. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  44037. pt = buf;
  44038. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  44039. ExpectIntNE((pt - buf), 0);
  44040. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  44041. EVP_PKEY_free(pkey);
  44042. }
  44043. #endif
  44044. #endif
  44045. return EXPECT_RESULT();
  44046. }
  44047. static int test_wolfSSL_OCSP_id_get0_info(void)
  44048. {
  44049. EXPECT_DECLS;
  44050. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  44051. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44052. X509* cert = NULL;
  44053. X509* issuer = NULL;
  44054. OCSP_CERTID* id = NULL;
  44055. OCSP_CERTID* id2 = NULL;
  44056. ASN1_STRING* name = NULL;
  44057. ASN1_OBJECT* pmd = NULL;
  44058. ASN1_STRING* keyHash = NULL;
  44059. ASN1_INTEGER* serial = NULL;
  44060. ASN1_INTEGER* x509Int;
  44061. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  44062. SSL_FILETYPE_PEM));
  44063. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  44064. SSL_FILETYPE_PEM));
  44065. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  44066. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  44067. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  44068. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  44069. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  44070. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  44071. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  44072. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  44073. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  44074. ExpectNotNull(serial);
  44075. /* compare serial number to one in cert, should be equal */
  44076. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  44077. ExpectIntEQ(x509Int->length, serial->length);
  44078. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  44079. /* test OCSP_id_cmp */
  44080. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44081. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  44082. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  44083. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  44084. if (id != NULL) {
  44085. id->issuerHash[0] = ~id->issuerHash[0];
  44086. }
  44087. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  44088. OCSP_CERTID_free(id);
  44089. OCSP_CERTID_free(id2);
  44090. X509_free(cert); /* free's x509Int */
  44091. X509_free(issuer);
  44092. #endif
  44093. return EXPECT_RESULT();
  44094. }
  44095. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  44096. {
  44097. EXPECT_DECLS;
  44098. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44099. WOLFSSL_OCSP_CERTID certId;
  44100. byte* targetBuffer = NULL;
  44101. byte* p;
  44102. /* OCSP CertID bytes taken from PCAP */
  44103. byte rawCertId[] = {
  44104. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44105. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44106. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44107. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44108. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44109. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44110. 0xcf, 0xbc, 0x91
  44111. };
  44112. int ret = 0;
  44113. int i;
  44114. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  44115. certId.rawCertId = rawCertId;
  44116. certId.rawCertIdSize = sizeof(rawCertId);
  44117. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  44118. DYNAMIC_TYPE_TMP_BUFFER));
  44119. p = targetBuffer;
  44120. /* Function returns the size of the encoded data. */
  44121. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  44122. /* If target buffer is not null, function increments targetBuffer to point
  44123. * just past the end of the encoded data. */
  44124. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  44125. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  44126. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  44127. }
  44128. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44129. targetBuffer = NULL;
  44130. /* If target buffer is null, function allocates memory for a buffer and
  44131. * copies the encoded data into it. targetBuffer then points to the start of
  44132. * this newly allocate buffer. */
  44133. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  44134. sizeof(rawCertId));
  44135. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  44136. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  44137. }
  44138. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  44139. #endif
  44140. return EXPECT_RESULT();
  44141. }
  44142. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  44143. {
  44144. EXPECT_DECLS;
  44145. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  44146. WOLFSSL_OCSP_CERTID* certId;
  44147. WOLFSSL_OCSP_CERTID* certIdGood;
  44148. WOLFSSL_OCSP_CERTID* certIdBad;
  44149. const unsigned char* rawCertIdPtr;
  44150. const unsigned char rawCertId[] = {
  44151. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  44152. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  44153. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  44154. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  44155. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  44156. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  44157. 0xcf, 0xbc, 0x91
  44158. };
  44159. rawCertIdPtr = &rawCertId[0];
  44160. /* If the cert ID is NULL the function should allocate it and copy the
  44161. * data to it. */
  44162. certId = NULL;
  44163. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  44164. sizeof(rawCertId)));
  44165. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44166. if (certId != NULL) {
  44167. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44168. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  44169. }
  44170. /* If the cert ID is not NULL the function will just copy the data to it. */
  44171. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  44172. DYNAMIC_TYPE_TMP_BUFFER));
  44173. ExpectNotNull(certId);
  44174. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  44175. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  44176. rawCertIdPtr = &rawCertId[0];
  44177. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  44178. sizeof(rawCertId)));
  44179. ExpectPtrEq(certIdGood, certId);
  44180. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  44181. if (certId != NULL) {
  44182. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  44183. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44184. certId = NULL;
  44185. }
  44186. /* The below tests should fail when passed bad parameters. NULL should
  44187. * always be returned. */
  44188. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  44189. sizeof(rawCertId)));
  44190. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  44191. sizeof(rawCertId)));
  44192. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  44193. #endif
  44194. return EXPECT_RESULT();
  44195. }
  44196. static int test_wolfSSL_OCSP_id_cmp(void)
  44197. {
  44198. EXPECT_DECLS;
  44199. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44200. OCSP_CERTID id1;
  44201. OCSP_CERTID id2;
  44202. XMEMSET(&id1, 0, sizeof(id1));
  44203. XMEMSET(&id2, 0, sizeof(id2));
  44204. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  44205. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44206. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  44207. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  44208. #endif
  44209. return EXPECT_RESULT();
  44210. }
  44211. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  44212. {
  44213. EXPECT_DECLS;
  44214. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44215. WOLFSSL_OCSP_SINGLERESP single;
  44216. const WOLFSSL_OCSP_CERTID* certId;
  44217. XMEMSET(&single, 0, sizeof(single));
  44218. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  44219. ExpectPtrEq(&single, certId);
  44220. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  44221. #endif
  44222. return EXPECT_RESULT();
  44223. }
  44224. static int test_wolfSSL_OCSP_single_get0_status(void)
  44225. {
  44226. EXPECT_DECLS;
  44227. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44228. WOLFSSL_OCSP_SINGLERESP single;
  44229. CertStatus certStatus;
  44230. WOLFSSL_ASN1_TIME* thisDate;
  44231. WOLFSSL_ASN1_TIME* nextDate;
  44232. int ret, i;
  44233. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44234. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  44235. /* Fill the date fields with some dummy data. */
  44236. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  44237. certStatus.thisDateParsed.data[i] = i;
  44238. certStatus.nextDateParsed.data[i] = i;
  44239. }
  44240. certStatus.status = CERT_GOOD;
  44241. single.status = &certStatus;
  44242. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  44243. &nextDate);
  44244. ExpectIntEQ(ret, CERT_GOOD);
  44245. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  44246. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  44247. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  44248. CERT_GOOD);
  44249. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  44250. NULL), CERT_GOOD);
  44251. #endif
  44252. return EXPECT_RESULT();
  44253. }
  44254. static int test_wolfSSL_OCSP_resp_count(void)
  44255. {
  44256. EXPECT_DECLS;
  44257. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44258. WOLFSSL_OCSP_BASICRESP basicResp;
  44259. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44260. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44261. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44262. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44263. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44264. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  44265. basicResp.single = &singleRespOne;
  44266. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  44267. singleRespOne.next = &singleRespTwo;
  44268. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  44269. #endif
  44270. return EXPECT_RESULT();
  44271. }
  44272. static int test_wolfSSL_OCSP_resp_get0(void)
  44273. {
  44274. EXPECT_DECLS;
  44275. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  44276. WOLFSSL_OCSP_BASICRESP basicResp;
  44277. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  44278. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  44279. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  44280. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44281. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  44282. basicResp.single = &singleRespOne;
  44283. singleRespOne.next = &singleRespTwo;
  44284. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  44285. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  44286. #endif
  44287. return EXPECT_RESULT();
  44288. }
  44289. static int test_wolfSSL_EVP_PKEY_derive(void)
  44290. {
  44291. EXPECT_DECLS;
  44292. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  44293. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  44294. EVP_PKEY_CTX *ctx = NULL;
  44295. unsigned char *skey = NULL;
  44296. size_t skeylen;
  44297. EVP_PKEY *pkey = NULL;
  44298. EVP_PKEY *peerkey = NULL;
  44299. const unsigned char* key;
  44300. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  44301. /* DH */
  44302. key = dh_key_der_2048;
  44303. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44304. sizeof_dh_key_der_2048)));
  44305. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  44306. key = dh_key_der_2048;
  44307. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  44308. sizeof_dh_key_der_2048)));
  44309. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  44310. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44311. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44312. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44313. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44314. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  44315. DYNAMIC_TYPE_OPENSSL));
  44316. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44317. EVP_PKEY_CTX_free(ctx);
  44318. ctx = NULL;
  44319. EVP_PKEY_free(peerkey);
  44320. peerkey = NULL;
  44321. EVP_PKEY_free(pkey);
  44322. pkey = NULL;
  44323. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44324. skey = NULL;
  44325. #endif
  44326. #ifdef HAVE_ECC
  44327. /* ECDH */
  44328. key = ecc_clikey_der_256;
  44329. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  44330. sizeof_ecc_clikey_der_256)));
  44331. key = ecc_clikeypub_der_256;
  44332. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  44333. sizeof_ecc_clikeypub_der_256)));
  44334. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44335. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  44336. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  44337. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  44338. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  44339. DYNAMIC_TYPE_OPENSSL));
  44340. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  44341. EVP_PKEY_CTX_free(ctx);
  44342. EVP_PKEY_free(peerkey);
  44343. EVP_PKEY_free(pkey);
  44344. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  44345. #endif /* HAVE_ECC */
  44346. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  44347. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  44348. return EXPECT_RESULT();
  44349. }
  44350. static int test_wolfSSL_EVP_PBE_scrypt(void)
  44351. {
  44352. EXPECT_DECLS;
  44353. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  44354. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  44355. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  44356. int ret;
  44357. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  44358. int pwdlen = sizeof(pwd);
  44359. const byte salt[] = {'N','a','C','l'};
  44360. int saltlen = sizeof(salt);
  44361. byte key[80];
  44362. word64 numOvr32 = (word64)INT32_MAX + 1;
  44363. /* expected derived key for N:16, r:1, p:1 */
  44364. const byte expectedKey[] = {
  44365. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  44366. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  44367. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  44368. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  44369. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  44370. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  44371. 0xE7, 0xE9, 0xC0, 0x9A};
  44372. /* N r p mx key keylen */
  44373. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  44374. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  44375. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  44376. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  44377. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  44378. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  44379. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  44380. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  44381. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  44382. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  44383. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  44384. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  44385. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  44386. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  44387. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  44388. ExpectIntEQ(ret, 1); /* should succeed */
  44389. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  44390. key, 64);
  44391. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  44392. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  44393. key, 64);
  44394. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  44395. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  44396. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  44397. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  44398. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  44399. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  44400. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  44401. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  44402. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  44403. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  44404. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  44405. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  44406. ExpectIntEQ(ret, 1);
  44407. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  44408. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  44409. #endif /* !NO_PWDBASED && !NO_SHA256 */
  44410. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  44411. return EXPECT_RESULT();
  44412. }
  44413. static int test_no_op_functions(void)
  44414. {
  44415. EXPECT_DECLS;
  44416. #if defined(OPENSSL_EXTRA)
  44417. /* this makes sure wolfSSL can compile and run these no-op functions */
  44418. SSL_load_error_strings();
  44419. ENGINE_load_builtin_engines();
  44420. OpenSSL_add_all_ciphers();
  44421. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  44422. #endif
  44423. return EXPECT_RESULT();
  44424. }
  44425. static int test_wolfSSL_CRYPTO_memcmp(void)
  44426. {
  44427. EXPECT_DECLS;
  44428. #ifdef OPENSSL_EXTRA
  44429. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44430. "implementation of TLS/SSL for embedded devices to the cloud.";
  44431. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44432. "implementation of TLS/SSL for embedded devices to the cloud.";
  44433. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  44434. "implementation of TLS/SSL for embedded devices to the cloud!";
  44435. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  44436. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  44437. #endif
  44438. return EXPECT_RESULT();
  44439. }
  44440. /*----------------------------------------------------------------------------*
  44441. | wolfCrypt ASN
  44442. *----------------------------------------------------------------------------*/
  44443. static int test_wc_CreateEncryptedPKCS8Key(void)
  44444. {
  44445. EXPECT_DECLS;
  44446. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  44447. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  44448. WC_RNG rng;
  44449. byte* encKey = NULL;
  44450. word32 encKeySz = 0;
  44451. word32 decKeySz = 0;
  44452. const char password[] = "Lorem ipsum dolor sit amet";
  44453. word32 passwordSz = (word32)XSTRLEN(password);
  44454. word32 tradIdx = 0;
  44455. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44456. ExpectIntEQ(wc_InitRng(&rng), 0);
  44457. /* Call with NULL for out buffer to get necessary length. */
  44458. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44459. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  44460. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44461. LENGTH_ONLY_E);
  44462. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  44463. DYNAMIC_TYPE_TMP_BUFFER));
  44464. /* Call with the allocated out buffer. */
  44465. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  44466. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  44467. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  44468. 0);
  44469. /* Decrypt the encrypted PKCS8 key we just made. */
  44470. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  44471. passwordSz)), 0);
  44472. /* encKey now holds the decrypted key (decrypted in place). */
  44473. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  44474. /* Check that the decrypted key matches the key prior to encryption. */
  44475. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  44476. sizeof_server_key_der_2048), 0);
  44477. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44478. wc_FreeRng(&rng);
  44479. #endif
  44480. return EXPECT_RESULT();
  44481. }
  44482. static int test_wc_GetPkcs8TraditionalOffset(void)
  44483. {
  44484. EXPECT_DECLS;
  44485. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  44486. int length;
  44487. int derSz = 0;
  44488. word32 inOutIdx;
  44489. const char* path = "./certs/server-keyPkcs8.der";
  44490. XFILE file = XBADFILE;
  44491. byte der[2048];
  44492. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  44493. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  44494. if (file != XBADFILE)
  44495. XFCLOSE(file);
  44496. /* valid case */
  44497. inOutIdx = 0;
  44498. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44499. 0);
  44500. /* inOutIdx > sz */
  44501. inOutIdx = 4000;
  44502. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44503. BAD_FUNC_ARG);
  44504. /* null input */
  44505. inOutIdx = 0;
  44506. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  44507. BAD_FUNC_ARG);
  44508. /* invalid input, fill buffer with 1's */
  44509. XMEMSET(der, 1, sizeof(der));
  44510. inOutIdx = 0;
  44511. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  44512. ASN_PARSE_E);
  44513. #endif /* NO_ASN */
  44514. return EXPECT_RESULT();
  44515. }
  44516. static int test_wc_SetSubjectRaw(void)
  44517. {
  44518. EXPECT_DECLS;
  44519. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44520. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44521. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44522. WOLFSSL_X509* x509 = NULL;
  44523. int peerCertSz;
  44524. const byte* peerCertBuf = NULL;
  44525. Cert forgedCert;
  44526. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44527. WOLFSSL_FILETYPE_ASN1));
  44528. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44529. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44530. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  44531. wolfSSL_FreeX509(x509);
  44532. #endif
  44533. return EXPECT_RESULT();
  44534. }
  44535. static int test_wc_GetSubjectRaw(void)
  44536. {
  44537. EXPECT_DECLS;
  44538. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44539. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  44540. Cert cert;
  44541. byte *subjectRaw;
  44542. ExpectIntEQ(0, wc_InitCert(&cert));
  44543. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  44544. #endif
  44545. return EXPECT_RESULT();
  44546. }
  44547. static int test_wc_SetIssuerRaw(void)
  44548. {
  44549. EXPECT_DECLS;
  44550. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44551. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44552. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44553. WOLFSSL_X509* x509 = NULL;
  44554. int peerCertSz;
  44555. const byte* peerCertBuf;
  44556. Cert forgedCert;
  44557. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44558. WOLFSSL_FILETYPE_ASN1));
  44559. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44560. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44561. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  44562. wolfSSL_FreeX509(x509);
  44563. #endif
  44564. return EXPECT_RESULT();
  44565. }
  44566. static int test_wc_SetIssueBuffer(void)
  44567. {
  44568. EXPECT_DECLS;
  44569. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44570. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  44571. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  44572. WOLFSSL_X509* x509 = NULL;
  44573. int peerCertSz;
  44574. const byte* peerCertBuf;
  44575. Cert forgedCert;
  44576. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  44577. WOLFSSL_FILETYPE_ASN1));
  44578. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  44579. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  44580. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  44581. wolfSSL_FreeX509(x509);
  44582. #endif
  44583. return EXPECT_RESULT();
  44584. }
  44585. /*
  44586. * Testing wc_SetSubjectKeyId
  44587. */
  44588. static int test_wc_SetSubjectKeyId(void)
  44589. {
  44590. EXPECT_DECLS;
  44591. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44592. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44593. Cert cert;
  44594. const char* file = "certs/ecc-client-keyPub.pem";
  44595. ExpectIntEQ(0, wc_InitCert(&cert));
  44596. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  44597. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  44598. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  44599. #endif
  44600. return EXPECT_RESULT();
  44601. } /* END test_wc_SetSubjectKeyId */
  44602. /*
  44603. * Testing wc_SetSubject
  44604. */
  44605. static int test_wc_SetSubject(void)
  44606. {
  44607. EXPECT_DECLS;
  44608. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  44609. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  44610. Cert cert;
  44611. const char* file = "./certs/ca-ecc-cert.pem";
  44612. ExpectIntEQ(0, wc_InitCert(&cert));
  44613. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  44614. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  44615. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  44616. #endif
  44617. return EXPECT_RESULT();
  44618. } /* END test_wc_SetSubject */
  44619. static int test_CheckCertSignature(void)
  44620. {
  44621. EXPECT_DECLS;
  44622. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  44623. WOLFSSL_CERT_MANAGER* cm = NULL;
  44624. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44625. XFILE fp = XBADFILE;
  44626. byte cert[4096];
  44627. int certSz;
  44628. #endif
  44629. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  44630. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44631. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  44632. #ifndef NO_RSA
  44633. #ifdef USE_CERT_BUFFERS_1024
  44634. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  44635. sizeof_server_cert_der_1024, NULL, cm));
  44636. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44637. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  44638. WOLFSSL_FILETYPE_ASN1));
  44639. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  44640. sizeof_server_cert_der_1024, NULL, cm));
  44641. #elif defined(USE_CERT_BUFFERS_2048)
  44642. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  44643. sizeof_server_cert_der_2048, NULL, cm));
  44644. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44645. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  44646. WOLFSSL_FILETYPE_ASN1));
  44647. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  44648. sizeof_server_cert_der_2048, NULL, cm));
  44649. #endif
  44650. #endif
  44651. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44652. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  44653. sizeof_serv_ecc_der_256, NULL, cm));
  44654. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  44655. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  44656. WOLFSSL_FILETYPE_ASN1));
  44657. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  44658. NULL, cm));
  44659. #endif
  44660. #if !defined(NO_FILESYSTEM)
  44661. wolfSSL_CertManagerFree(cm);
  44662. cm = NULL;
  44663. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  44664. #ifndef NO_RSA
  44665. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  44666. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44667. if (fp != XBADFILE) {
  44668. XFCLOSE(fp);
  44669. fp = XBADFILE;
  44670. }
  44671. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44672. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44673. "./certs/ca-cert.pem", NULL));
  44674. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44675. #endif
  44676. #ifdef HAVE_ECC
  44677. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  44678. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  44679. if (fp != XBADFILE) {
  44680. XFCLOSE(fp);
  44681. fp = XBADFILE;
  44682. }
  44683. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  44684. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  44685. "./certs/ca-ecc-cert.pem", NULL));
  44686. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  44687. #endif
  44688. #endif
  44689. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  44690. (void)fp;
  44691. (void)cert;
  44692. (void)certSz;
  44693. #endif
  44694. wolfSSL_CertManagerFree(cm);
  44695. #endif
  44696. return EXPECT_RESULT();
  44697. }
  44698. static int test_wc_ParseCert(void)
  44699. {
  44700. EXPECT_DECLS;
  44701. #if !defined(NO_CERTS) && !defined(NO_RSA)
  44702. DecodedCert decodedCert;
  44703. const byte* rawCert = client_cert_der_2048;
  44704. const int rawCertSize = sizeof_client_cert_der_2048;
  44705. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  44706. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44707. #ifndef IGNORE_NAME_CONSTRAINTS
  44708. /* check that the subjects emailAddress was not put in the alt name list */
  44709. ExpectNotNull(decodedCert.subjectEmail);
  44710. ExpectNull(decodedCert.altEmailNames);
  44711. #endif
  44712. wc_FreeDecodedCert(&decodedCert);
  44713. #endif
  44714. return EXPECT_RESULT();
  44715. }
  44716. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  44717. * the API safely errors out on badly-formed ASN input.
  44718. * NOTE: Test not compatible with released FIPS implementations!
  44719. */
  44720. static int test_wc_ParseCert_Error(void)
  44721. {
  44722. EXPECT_DECLS;
  44723. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  44724. (!defined(HAVE_FIPS) || \
  44725. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  44726. DecodedCert decodedCert;
  44727. int i;
  44728. /* Certificate data */
  44729. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  44730. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44731. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  44732. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  44733. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  44734. /* Test data */
  44735. const struct testStruct {
  44736. const byte* c;
  44737. const int cSz;
  44738. const int expRet;
  44739. } t[] = {
  44740. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  44741. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  44742. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  44743. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  44744. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  44745. };
  44746. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  44747. for (i = 0; i < tSz; i++) {
  44748. WOLFSSL_MSG_EX("i == %d", i);
  44749. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  44750. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  44751. wc_FreeDecodedCert(&decodedCert);
  44752. }
  44753. #endif
  44754. return EXPECT_RESULT();
  44755. }
  44756. static int test_MakeCertWithPathLen(void)
  44757. {
  44758. EXPECT_DECLS;
  44759. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  44760. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44761. const byte expectedPathLen = 7;
  44762. Cert cert;
  44763. DecodedCert decodedCert;
  44764. byte der[FOURK_BUF];
  44765. int derSize = 0;
  44766. WC_RNG rng;
  44767. ecc_key key;
  44768. int ret;
  44769. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44770. XMEMSET(&key, 0, sizeof(ecc_key));
  44771. XMEMSET(&cert, 0, sizeof(Cert));
  44772. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  44773. ExpectIntEQ(wc_InitRng(&rng), 0);
  44774. ExpectIntEQ(wc_ecc_init(&key), 0);
  44775. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44776. ExpectIntEQ(wc_InitCert(&cert), 0);
  44777. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44778. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44779. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44780. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44781. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44782. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  44783. CTC_NAME_SIZE);
  44784. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  44785. CTC_NAME_SIZE);
  44786. cert.selfSigned = 1;
  44787. cert.isCA = 1;
  44788. cert.pathLen = expectedPathLen;
  44789. cert.pathLenSet = 1;
  44790. cert.sigType = CTC_SHA256wECDSA;
  44791. #ifdef WOLFSSL_CERT_EXT
  44792. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  44793. #endif
  44794. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44795. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  44796. FOURK_BUF, NULL, &key, &rng), 0);
  44797. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44798. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44799. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  44800. wc_FreeDecodedCert(&decodedCert);
  44801. ret = wc_ecc_free(&key);
  44802. ExpectIntEQ(ret, 0);
  44803. ret = wc_FreeRng(&rng);
  44804. ExpectIntEQ(ret, 0);
  44805. #endif
  44806. return EXPECT_RESULT();
  44807. }
  44808. static int test_MakeCertWithCaFalse(void)
  44809. {
  44810. EXPECT_DECLS;
  44811. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  44812. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  44813. const byte expectedIsCa = 0;
  44814. Cert cert;
  44815. DecodedCert decodedCert;
  44816. byte der[FOURK_BUF];
  44817. int derSize = 0;
  44818. WC_RNG rng;
  44819. ecc_key key;
  44820. int ret;
  44821. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44822. XMEMSET(&key, 0, sizeof(ecc_key));
  44823. XMEMSET(&cert, 0, sizeof(Cert));
  44824. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  44825. ExpectIntEQ(wc_InitRng(&rng), 0);
  44826. ExpectIntEQ(wc_ecc_init(&key), 0);
  44827. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  44828. ExpectIntEQ(wc_InitCert(&cert), 0);
  44829. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  44830. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  44831. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  44832. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  44833. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  44834. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  44835. CTC_NAME_SIZE);
  44836. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  44837. CTC_NAME_SIZE);
  44838. cert.selfSigned = 1;
  44839. cert.isCA = expectedIsCa;
  44840. cert.isCaSet = 1;
  44841. cert.sigType = CTC_SHA256wECDSA;
  44842. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  44843. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  44844. FOURK_BUF, NULL, &key, &rng), 0);
  44845. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  44846. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  44847. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  44848. wc_FreeDecodedCert(&decodedCert);
  44849. ret = wc_ecc_free(&key);
  44850. ExpectIntEQ(ret, 0);
  44851. ret = wc_FreeRng(&rng);
  44852. ExpectIntEQ(ret, 0);
  44853. #endif
  44854. return EXPECT_RESULT();
  44855. }
  44856. /*----------------------------------------------------------------------------*
  44857. | wolfCrypt ECC
  44858. *----------------------------------------------------------------------------*/
  44859. static int test_wc_ecc_get_curve_size_from_name(void)
  44860. {
  44861. EXPECT_DECLS;
  44862. #ifdef HAVE_ECC
  44863. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44864. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  44865. #endif
  44866. /* invalid case */
  44867. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  44868. /* NULL input */
  44869. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  44870. #endif /* HAVE_ECC */
  44871. return EXPECT_RESULT();
  44872. }
  44873. static int test_wc_ecc_get_curve_id_from_name(void)
  44874. {
  44875. EXPECT_DECLS;
  44876. #ifdef HAVE_ECC
  44877. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44878. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  44879. ECC_SECP256R1);
  44880. #endif
  44881. /* invalid case */
  44882. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  44883. /* NULL input */
  44884. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  44885. #endif /* HAVE_ECC */
  44886. return EXPECT_RESULT();
  44887. }
  44888. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  44889. !defined(HAVE_SELFTEST) && \
  44890. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  44891. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  44892. {
  44893. EXPECT_DECLS;
  44894. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44895. ecc_key* key;
  44896. const ecc_set_type* params = NULL;
  44897. int ret;
  44898. #endif
  44899. WOLFSSL_EC_KEY *ecKey = NULL;
  44900. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44901. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  44902. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  44903. if (EXPECT_SUCCESS()) {
  44904. ret = EC_KEY_generate_key(ecKey);
  44905. } else
  44906. ret = 0;
  44907. if (ret == 1) {
  44908. /* normal test */
  44909. key = (ecc_key*)ecKey->internal;
  44910. if (key != NULL) {
  44911. params = key->dp;
  44912. }
  44913. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  44914. ECC_SECP256R1);
  44915. }
  44916. #endif
  44917. /* invalid case, NULL input*/
  44918. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  44919. wolfSSL_EC_KEY_free(ecKey);
  44920. return EXPECT_RESULT();
  44921. }
  44922. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  44923. static int test_wc_ecc_get_curve_id_from_params(void)
  44924. {
  44925. EXPECT_DECLS;
  44926. #ifdef HAVE_ECC
  44927. const byte prime[] =
  44928. {
  44929. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44930. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44931. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44932. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  44933. };
  44934. const byte primeInvalid[] =
  44935. {
  44936. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44937. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44938. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44939. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  44940. };
  44941. const byte Af[] =
  44942. {
  44943. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  44944. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44945. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  44946. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  44947. };
  44948. const byte Bf[] =
  44949. {
  44950. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  44951. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  44952. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  44953. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  44954. };
  44955. const byte order[] =
  44956. {
  44957. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  44958. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  44959. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  44960. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  44961. };
  44962. const byte Gx[] =
  44963. {
  44964. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  44965. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  44966. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  44967. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  44968. };
  44969. const byte Gy[] =
  44970. {
  44971. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  44972. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  44973. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  44974. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  44975. };
  44976. int cofactor = 1;
  44977. int fieldSize = 256;
  44978. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  44979. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  44980. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  44981. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  44982. ECC_SECP256R1);
  44983. #endif
  44984. /* invalid case, fieldSize = 0 */
  44985. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  44986. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44987. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  44988. /* invalid case, NULL prime */
  44989. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  44990. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44991. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  44992. /* invalid case, invalid prime */
  44993. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  44994. primeInvalid, sizeof(primeInvalid),
  44995. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  44996. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  44997. #endif
  44998. return EXPECT_RESULT();
  44999. }
  45000. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  45001. {
  45002. EXPECT_DECLS;
  45003. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45004. !defined(HAVE_FAST_RSA)
  45005. WOLFSSL_RSA* rsa = NULL;
  45006. WOLFSSL_EVP_PKEY* pkey = NULL;
  45007. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45008. const char* in = "What is easy to do is easy not to do.";
  45009. size_t inlen = XSTRLEN(in);
  45010. size_t outEncLen = 0;
  45011. byte* outEnc = NULL;
  45012. byte* outDec = NULL;
  45013. size_t outDecLen = 0;
  45014. size_t rsaKeySz = 2048/8; /* Bytes */
  45015. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45016. byte* inTmp = NULL;
  45017. byte* outEncTmp = NULL;
  45018. byte* outDecTmp = NULL;
  45019. #endif
  45020. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45021. DYNAMIC_TYPE_TMP_BUFFER));
  45022. if (outEnc != NULL) {
  45023. XMEMSET(outEnc, 0, rsaKeySz);
  45024. }
  45025. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45026. DYNAMIC_TYPE_TMP_BUFFER));
  45027. if (outDec != NULL) {
  45028. XMEMSET(outDec, 0, rsaKeySz);
  45029. }
  45030. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45031. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45032. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45033. if (EXPECT_FAIL()) {
  45034. RSA_free(rsa);
  45035. }
  45036. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45037. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45038. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45039. WOLFSSL_SUCCESS);
  45040. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  45041. since ctx uses it.*/
  45042. ExpectIntEQ(pkey->ref.count, 2);
  45043. EVP_PKEY_free(pkey);
  45044. ExpectIntEQ(pkey->ref.count, 1);
  45045. /* Encrypt data */
  45046. /* Check that we can get the required output buffer length by passing in a
  45047. * NULL output buffer. */
  45048. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  45049. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45050. ExpectIntEQ(rsaKeySz, outEncLen);
  45051. /* Now do the actual encryption. */
  45052. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  45053. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45054. /* Decrypt data */
  45055. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45056. /* Check that we can get the required output buffer length by passing in a
  45057. * NULL output buffer. */
  45058. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  45059. WOLFSSL_SUCCESS);
  45060. ExpectIntEQ(rsaKeySz, outDecLen);
  45061. /* Now do the actual decryption. */
  45062. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  45063. WOLFSSL_SUCCESS);
  45064. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  45065. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45066. /* The input length must be the same size as the RSA key.*/
  45067. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45068. DYNAMIC_TYPE_TMP_BUFFER));
  45069. if (inTmp != NULL) {
  45070. XMEMSET(inTmp, 9, rsaKeySz);
  45071. }
  45072. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45073. DYNAMIC_TYPE_TMP_BUFFER));
  45074. if (outEncTmp != NULL) {
  45075. XMEMSET(outEncTmp, 0, rsaKeySz);
  45076. }
  45077. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45078. DYNAMIC_TYPE_TMP_BUFFER));
  45079. if (outDecTmp != NULL) {
  45080. XMEMSET(outDecTmp, 0, rsaKeySz);
  45081. }
  45082. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45083. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45084. WOLFSSL_SUCCESS);
  45085. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  45086. WOLFSSL_SUCCESS);
  45087. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45088. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  45089. outEncLen), WOLFSSL_SUCCESS);
  45090. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  45091. #endif
  45092. EVP_PKEY_CTX_free(ctx);
  45093. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45094. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45095. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45096. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45097. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45098. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45099. #endif
  45100. #endif
  45101. return EXPECT_RESULT();
  45102. }
  45103. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45104. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45105. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45106. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45107. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45108. #endif
  45109. #endif
  45110. #endif
  45111. #if defined(OPENSSL_EXTRA)
  45112. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45113. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45114. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45115. #endif
  45116. #endif
  45117. #endif
  45118. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45119. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45120. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45121. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45122. #endif
  45123. #endif
  45124. #endif
  45125. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  45126. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  45127. {
  45128. EXPECT_DECLS;
  45129. #if defined(OPENSSL_EXTRA)
  45130. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45131. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45132. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45133. WOLFSSL_RSA* rsa = NULL;
  45134. #endif
  45135. #endif
  45136. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45137. WOLFSSL_DSA* dsa = NULL;
  45138. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45139. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45140. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45141. WOLFSSL_EC_KEY* ecKey = NULL;
  45142. #endif
  45143. #endif
  45144. WOLFSSL_EVP_PKEY* pkey = NULL;
  45145. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45146. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  45147. const char* in = "What is easy to do is easy not to do.";
  45148. size_t inlen = XSTRLEN(in);
  45149. byte hash[SHA256_DIGEST_LENGTH] = {0};
  45150. byte zero[SHA256_DIGEST_LENGTH] = {0};
  45151. SHA256_CTX c;
  45152. byte* sig = NULL;
  45153. byte* sigVerify = NULL;
  45154. size_t siglen;
  45155. size_t siglenOnlyLen;
  45156. size_t keySz = 2048/8; /* Bytes */
  45157. ExpectNotNull(sig =
  45158. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45159. ExpectNotNull(sigVerify =
  45160. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  45161. siglen = keySz;
  45162. ExpectNotNull(XMEMSET(sig, 0, keySz));
  45163. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  45164. /* Generate hash */
  45165. SHA256_Init(&c);
  45166. SHA256_Update(&c, in, inlen);
  45167. SHA256_Final(hash, &c);
  45168. #ifdef WOLFSSL_SMALL_STACK_CACHE
  45169. /* workaround for small stack cache case */
  45170. wc_Sha256Free((wc_Sha256*)&c);
  45171. #endif
  45172. /* Generate key */
  45173. ExpectNotNull(pkey = EVP_PKEY_new());
  45174. switch (keyType) {
  45175. case EVP_PKEY_RSA:
  45176. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45177. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45178. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45179. {
  45180. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45181. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45182. }
  45183. #endif
  45184. #endif
  45185. break;
  45186. case EVP_PKEY_DSA:
  45187. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45188. ExpectNotNull(dsa = DSA_new());
  45189. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  45190. NULL, 0, NULL, NULL, NULL), 1);
  45191. ExpectIntEQ(DSA_generate_key(dsa), 1);
  45192. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  45193. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45194. break;
  45195. case EVP_PKEY_EC:
  45196. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45197. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45198. {
  45199. ExpectNotNull(ecKey = EC_KEY_new());
  45200. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  45201. ExpectIntEQ(
  45202. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45203. if (EXPECT_FAIL()) {
  45204. EC_KEY_free(ecKey);
  45205. }
  45206. }
  45207. #endif
  45208. #endif
  45209. break;
  45210. }
  45211. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45212. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45213. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45214. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45215. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45216. if (keyType == EVP_PKEY_RSA)
  45217. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45218. WOLFSSL_SUCCESS);
  45219. #endif
  45220. #endif
  45221. /* Check returning only length */
  45222. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  45223. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45224. ExpectIntGT(siglenOnlyLen, 0);
  45225. /* Sign data */
  45226. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  45227. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  45228. ExpectIntGE(siglenOnlyLen, siglen);
  45229. /* Verify signature */
  45230. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  45231. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45232. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45233. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45234. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45235. if (keyType == EVP_PKEY_RSA)
  45236. ExpectIntEQ(
  45237. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  45238. WOLFSSL_SUCCESS);
  45239. #endif
  45240. #endif
  45241. ExpectIntEQ(EVP_PKEY_verify(
  45242. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  45243. WOLFSSL_SUCCESS);
  45244. ExpectIntEQ(EVP_PKEY_verify(
  45245. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  45246. WOLFSSL_FAILURE);
  45247. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45248. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45249. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45250. if (keyType == EVP_PKEY_RSA) {
  45251. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  45252. /* Try RSA sign/verify with no padding. */
  45253. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45254. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45255. WOLFSSL_SUCCESS);
  45256. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45257. siglen), WOLFSSL_SUCCESS);
  45258. ExpectIntGE(siglenOnlyLen, siglen);
  45259. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45260. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45261. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  45262. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45263. siglen), WOLFSSL_SUCCESS);
  45264. #endif
  45265. /* Wrong padding schemes. */
  45266. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45267. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  45268. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45269. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  45270. siglen), WOLFSSL_SUCCESS);
  45271. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  45272. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45273. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  45274. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  45275. siglen), WOLFSSL_SUCCESS);
  45276. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45277. WOLFSSL_SUCCESS);
  45278. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  45279. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  45280. }
  45281. #endif
  45282. #endif
  45283. /* error cases */
  45284. siglen = keySz; /* Reset because sig size may vary slightly */
  45285. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  45286. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  45287. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  45288. WOLFSSL_SUCCESS);
  45289. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  45290. WOLFSSL_SUCCESS);
  45291. EVP_PKEY_free(pkey);
  45292. pkey = NULL;
  45293. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45294. DSA_free(dsa);
  45295. dsa = NULL;
  45296. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45297. EVP_PKEY_CTX_free(ctx_verify);
  45298. ctx_verify = NULL;
  45299. EVP_PKEY_CTX_free(ctx);
  45300. ctx = NULL;
  45301. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45302. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45303. #endif /* OPENSSL_EXTRA */
  45304. return EXPECT_RESULT();
  45305. }
  45306. #endif
  45307. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  45308. {
  45309. EXPECT_DECLS;
  45310. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45311. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  45312. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45313. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  45314. #endif
  45315. #endif
  45316. return EXPECT_RESULT();
  45317. }
  45318. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  45319. {
  45320. EXPECT_DECLS;
  45321. #if defined(OPENSSL_EXTRA)
  45322. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45323. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  45324. #endif
  45325. #endif
  45326. return EXPECT_RESULT();
  45327. }
  45328. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  45329. {
  45330. EXPECT_DECLS;
  45331. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45332. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45333. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  45334. #endif
  45335. #endif
  45336. return EXPECT_RESULT();
  45337. }
  45338. static int test_EVP_PKEY_rsa(void)
  45339. {
  45340. EXPECT_DECLS;
  45341. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45342. WOLFSSL_RSA* rsa = NULL;
  45343. WOLFSSL_EVP_PKEY* pkey = NULL;
  45344. ExpectNotNull(rsa = wolfSSL_RSA_new());
  45345. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45346. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  45347. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  45348. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45349. if (EXPECT_FAIL()) {
  45350. wolfSSL_RSA_free(rsa);
  45351. }
  45352. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  45353. wolfSSL_EVP_PKEY_free(pkey);
  45354. #endif
  45355. return EXPECT_RESULT();
  45356. }
  45357. static int test_EVP_PKEY_ec(void)
  45358. {
  45359. EXPECT_DECLS;
  45360. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45361. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45362. WOLFSSL_EC_KEY* ecKey = NULL;
  45363. WOLFSSL_EVP_PKEY* pkey = NULL;
  45364. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45365. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45366. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  45367. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  45368. /* Should fail since ecKey is empty */
  45369. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  45370. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45371. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45372. if (EXPECT_FAIL()) {
  45373. wolfSSL_EC_KEY_free(ecKey);
  45374. }
  45375. wolfSSL_EVP_PKEY_free(pkey);
  45376. #endif
  45377. #endif
  45378. return EXPECT_RESULT();
  45379. }
  45380. static int test_EVP_PKEY_cmp(void)
  45381. {
  45382. EXPECT_DECLS;
  45383. #if defined(OPENSSL_EXTRA)
  45384. EVP_PKEY *a = NULL;
  45385. EVP_PKEY *b = NULL;
  45386. const unsigned char *in;
  45387. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45388. in = client_key_der_2048;
  45389. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45390. &in, (long)sizeof_client_key_der_2048));
  45391. in = client_key_der_2048;
  45392. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45393. &in, (long)sizeof_client_key_der_2048));
  45394. /* Test success case RSA */
  45395. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45396. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  45397. #else
  45398. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45399. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45400. EVP_PKEY_free(b);
  45401. b = NULL;
  45402. EVP_PKEY_free(a);
  45403. a = NULL;
  45404. #endif
  45405. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45406. in = ecc_clikey_der_256;
  45407. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45408. &in, (long)sizeof_ecc_clikey_der_256));
  45409. in = ecc_clikey_der_256;
  45410. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45411. &in, (long)sizeof_ecc_clikey_der_256));
  45412. /* Test success case ECC */
  45413. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45414. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  45415. #else
  45416. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45417. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45418. EVP_PKEY_free(b);
  45419. b = NULL;
  45420. EVP_PKEY_free(a);
  45421. a = NULL;
  45422. #endif
  45423. /* Test failure cases */
  45424. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  45425. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45426. in = client_key_der_2048;
  45427. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  45428. &in, (long)sizeof_client_key_der_2048));
  45429. in = ecc_clikey_der_256;
  45430. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  45431. &in, (long)sizeof_ecc_clikey_der_256));
  45432. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45433. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  45434. #else
  45435. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45436. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  45437. EVP_PKEY_free(b);
  45438. b = NULL;
  45439. EVP_PKEY_free(a);
  45440. a = NULL;
  45441. #endif
  45442. /* invalid or empty failure cases */
  45443. a = EVP_PKEY_new();
  45444. b = EVP_PKEY_new();
  45445. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  45446. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  45447. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  45448. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  45449. #ifdef NO_RSA
  45450. /* Type check will fail since RSA is the default EVP key type */
  45451. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  45452. #else
  45453. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  45454. #endif
  45455. #else
  45456. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  45457. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  45458. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  45459. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  45460. #endif
  45461. EVP_PKEY_free(b);
  45462. EVP_PKEY_free(a);
  45463. (void)in;
  45464. #endif
  45465. return EXPECT_RESULT();
  45466. }
  45467. static int test_ERR_load_crypto_strings(void)
  45468. {
  45469. #if defined(OPENSSL_ALL)
  45470. ERR_load_crypto_strings();
  45471. return TEST_SUCCESS;
  45472. #else
  45473. return TEST_SKIPPED;
  45474. #endif
  45475. }
  45476. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45477. static void free_x509(X509* x)
  45478. {
  45479. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  45480. }
  45481. #endif
  45482. static int test_sk_X509(void)
  45483. {
  45484. EXPECT_DECLS;
  45485. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  45486. {
  45487. STACK_OF(X509)* s = NULL;
  45488. ExpectNotNull(s = sk_X509_new_null());
  45489. ExpectIntEQ(sk_X509_num(s), 0);
  45490. sk_X509_pop_free(s, NULL);
  45491. ExpectNotNull(s = sk_X509_new_null());
  45492. ExpectIntEQ(sk_X509_num(s), 0);
  45493. sk_X509_pop_free(s, NULL);
  45494. ExpectNotNull(s = sk_X509_new_null());
  45495. sk_X509_push(s, (X509*)1);
  45496. ExpectIntEQ(sk_X509_num(s), 1);
  45497. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  45498. sk_X509_push(s, (X509*)2);
  45499. ExpectIntEQ(sk_X509_num(s), 2);
  45500. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  45501. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  45502. sk_X509_push(s, (X509*)2);
  45503. sk_X509_pop_free(s, free_x509);
  45504. }
  45505. {
  45506. /* Push a list of 10 X509s onto stack, then verify that
  45507. * value(), push(), shift(), and pop() behave as expected. */
  45508. STACK_OF(X509)* s = NULL;
  45509. X509* xList[10];
  45510. int i = 0;
  45511. const int len = (sizeof(xList) / sizeof(xList[0]));
  45512. for (i = 0; i < len; ++i) {
  45513. xList[i] = NULL;
  45514. ExpectNotNull(xList[i] = X509_new());
  45515. }
  45516. /* test push, pop, and free */
  45517. ExpectNotNull(s = sk_X509_new_null());
  45518. for (i = 0; i < len; ++i) {
  45519. sk_X509_push(s, xList[i]);
  45520. ExpectIntEQ(sk_X509_num(s), i + 1);
  45521. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45522. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45523. }
  45524. /* pop returns and removes last pushed on stack, which is index 0
  45525. * in sk_x509_value */
  45526. for (i = 0; i < len; ++i) {
  45527. X509 * x = sk_X509_value(s, 0);
  45528. X509 * y = sk_X509_pop(s);
  45529. X509 * z = xList[len - 1 - i];
  45530. ExpectIntEQ((x == y), 1);
  45531. ExpectIntEQ((x == z), 1);
  45532. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45533. }
  45534. sk_free(s);
  45535. s = NULL;
  45536. /* test push, shift, and free */
  45537. ExpectNotNull(s = sk_X509_new_null());
  45538. for (i = 0; i < len; ++i) {
  45539. sk_X509_push(s, xList[i]);
  45540. ExpectIntEQ(sk_X509_num(s), i + 1);
  45541. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  45542. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  45543. }
  45544. /* shift returns and removes first pushed on stack, which is index i
  45545. * in sk_x509_value() */
  45546. for (i = 0; i < len; ++i) {
  45547. X509 * x = sk_X509_value(s, len - 1 - i);
  45548. X509 * y = sk_X509_shift(s);
  45549. X509 * z = xList[i];
  45550. ExpectIntEQ((x == y), 1);
  45551. ExpectIntEQ((x == z), 1);
  45552. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  45553. }
  45554. sk_free(s);
  45555. for (i = 0; i < len; ++i)
  45556. X509_free(xList[i]);
  45557. }
  45558. #endif
  45559. return EXPECT_RESULT();
  45560. }
  45561. static int test_sk_X509_CRL(void)
  45562. {
  45563. EXPECT_DECLS;
  45564. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  45565. X509_CRL* crl = NULL;
  45566. XFILE fp = XBADFILE;
  45567. STACK_OF(X509_CRL)* s = NULL;
  45568. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  45569. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45570. NULL, NULL));
  45571. if (fp != XBADFILE)
  45572. XFCLOSE(fp);
  45573. ExpectNotNull(s = sk_X509_CRL_new());
  45574. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  45575. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  45576. if (EXPECT_FAIL()) {
  45577. X509_CRL_free(crl);
  45578. }
  45579. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  45580. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  45581. sk_X509_CRL_free(s);
  45582. #endif
  45583. return EXPECT_RESULT();
  45584. }
  45585. static int test_X509_get_signature_nid(void)
  45586. {
  45587. EXPECT_DECLS;
  45588. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45589. X509* x509 = NULL;
  45590. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  45591. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45592. SSL_FILETYPE_PEM));
  45593. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  45594. X509_free(x509);
  45595. #endif
  45596. return EXPECT_RESULT();
  45597. }
  45598. static int test_X509_REQ(void)
  45599. {
  45600. EXPECT_DECLS;
  45601. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  45602. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  45603. X509_NAME* name = NULL;
  45604. #ifndef NO_RSA
  45605. X509_NAME* subject = NULL;
  45606. #endif
  45607. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45608. X509_REQ* req = NULL;
  45609. EVP_PKEY* priv = NULL;
  45610. EVP_PKEY* pub = NULL;
  45611. unsigned char* der = NULL;
  45612. int len;
  45613. #endif
  45614. #ifndef NO_RSA
  45615. EVP_MD_CTX *mctx = NULL;
  45616. EVP_PKEY_CTX *pkctx = NULL;
  45617. #ifdef USE_CERT_BUFFERS_1024
  45618. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  45619. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  45620. #elif defined(USE_CERT_BUFFERS_2048)
  45621. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  45622. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  45623. #endif
  45624. #endif
  45625. #ifdef HAVE_ECC
  45626. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  45627. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  45628. #endif
  45629. ExpectNotNull(name = X509_NAME_new());
  45630. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  45631. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  45632. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  45633. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  45634. #ifndef NO_RSA
  45635. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  45636. (long)sizeof_client_key_der_2048));
  45637. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  45638. (long)sizeof_client_keypub_der_2048));
  45639. ExpectNotNull(req = X509_REQ_new());
  45640. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  45641. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  45642. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45643. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  45644. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  45645. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45646. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  45647. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45648. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  45649. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45650. len = i2d_X509_REQ(req, &der);
  45651. DEBUG_WRITE_DER(der, len, "req.der");
  45652. #ifdef USE_CERT_BUFFERS_1024
  45653. ExpectIntEQ(len, 381);
  45654. #else
  45655. ExpectIntEQ(len, 643);
  45656. #endif
  45657. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45658. der = NULL;
  45659. mctx = EVP_MD_CTX_new();
  45660. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  45661. WOLFSSL_SUCCESS);
  45662. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  45663. EVP_MD_CTX_free(mctx);
  45664. mctx = NULL;
  45665. X509_REQ_free(NULL);
  45666. X509_REQ_free(req);
  45667. req = NULL;
  45668. /* Test getting the subject from a newly created X509_REQ */
  45669. ExpectNotNull(req = X509_REQ_new());
  45670. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  45671. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  45672. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  45673. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  45674. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  45675. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  45676. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  45677. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  45678. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  45679. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  45680. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  45681. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  45682. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  45683. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45684. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45685. len = i2d_X509_REQ(req, &der);
  45686. DEBUG_WRITE_DER(der, len, "req2.der");
  45687. #ifdef USE_CERT_BUFFERS_1024
  45688. ExpectIntEQ(len, 435);
  45689. #else
  45690. ExpectIntEQ(len, 696);
  45691. #endif
  45692. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45693. der = NULL;
  45694. EVP_PKEY_free(pub);
  45695. pub = NULL;
  45696. EVP_PKEY_free(priv);
  45697. priv = NULL;
  45698. X509_REQ_free(req);
  45699. req = NULL;
  45700. #endif
  45701. #ifdef HAVE_ECC
  45702. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  45703. sizeof_ecc_clikey_der_256));
  45704. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  45705. sizeof_ecc_clikeypub_der_256));
  45706. ExpectNotNull(req = X509_REQ_new());
  45707. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  45708. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  45709. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  45710. /* Signature is random and may be shorter or longer. */
  45711. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  45712. ExpectIntLE(len, 253);
  45713. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  45714. X509_REQ_free(req);
  45715. EVP_PKEY_free(pub);
  45716. EVP_PKEY_free(priv);
  45717. #ifdef FP_ECC
  45718. wc_ecc_fp_free();
  45719. #endif
  45720. #endif /* HAVE_ECC */
  45721. X509_NAME_free(name);
  45722. #endif
  45723. return EXPECT_RESULT();
  45724. }
  45725. static int test_wolfssl_PKCS7(void)
  45726. {
  45727. EXPECT_DECLS;
  45728. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45729. !defined(NO_RSA)
  45730. PKCS7* pkcs7 = NULL;
  45731. byte data[FOURK_BUF];
  45732. word32 len = sizeof(data);
  45733. const byte* p = data;
  45734. byte content[] = "Test data to encode.";
  45735. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45736. BIO* bio = NULL;
  45737. byte key[sizeof(client_key_der_2048)];
  45738. word32 keySz = (word32)sizeof(key);
  45739. byte* out = NULL;
  45740. #endif
  45741. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  45742. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  45743. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  45744. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  45745. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45746. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  45747. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  45748. PKCS7_free(pkcs7);
  45749. pkcs7 = NULL;
  45750. /* fail case, without PKCS7_NOVERIFY */
  45751. p = data;
  45752. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45753. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45754. 0), WOLFSSL_FAILURE);
  45755. PKCS7_free(pkcs7);
  45756. pkcs7 = NULL;
  45757. /* success case, with PKCS7_NOVERIFY */
  45758. p = data;
  45759. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  45760. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  45761. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  45762. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  45763. /* test i2d */
  45764. XMEMCPY(key, client_key_der_2048, keySz);
  45765. if (pkcs7 != NULL) {
  45766. pkcs7->privateKey = key;
  45767. pkcs7->privateKeySz = (word32)sizeof(key);
  45768. pkcs7->encryptOID = RSAk;
  45769. #ifdef NO_SHA
  45770. pkcs7->hashOID = SHA256h;
  45771. #else
  45772. pkcs7->hashOID = SHAh;
  45773. #endif
  45774. }
  45775. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45776. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  45777. #ifndef NO_ASN_TIME
  45778. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  45779. #else
  45780. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  45781. #endif
  45782. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45783. BIO_free(bio);
  45784. #endif
  45785. PKCS7_free(NULL);
  45786. PKCS7_free(pkcs7);
  45787. #endif
  45788. return EXPECT_RESULT();
  45789. }
  45790. static int test_wolfSSL_PKCS7_sign(void)
  45791. {
  45792. EXPECT_DECLS;
  45793. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  45794. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45795. PKCS7* p7 = NULL;
  45796. PKCS7* p7Ver = NULL;
  45797. byte* out = NULL;
  45798. byte* tmpPtr = NULL;
  45799. int outLen = 0;
  45800. int flags = 0;
  45801. byte data[] = "Test data to encode.";
  45802. const char* cert = "./certs/server-cert.pem";
  45803. const char* key = "./certs/server-key.pem";
  45804. const char* ca = "./certs/ca-cert.pem";
  45805. WOLFSSL_BIO* certBio = NULL;
  45806. WOLFSSL_BIO* keyBio = NULL;
  45807. WOLFSSL_BIO* caBio = NULL;
  45808. WOLFSSL_BIO* inBio = NULL;
  45809. X509* signCert = NULL;
  45810. EVP_PKEY* signKey = NULL;
  45811. X509* caCert = NULL;
  45812. X509_STORE* store = NULL;
  45813. /* read signer cert/key into BIO */
  45814. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  45815. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  45816. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45817. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45818. /* read CA cert into store (for verify) */
  45819. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  45820. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45821. ExpectNotNull(store = X509_STORE_new());
  45822. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45823. /* data to be signed into BIO */
  45824. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45825. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45826. /* PKCS7_sign, bad args: signer NULL */
  45827. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  45828. /* PKCS7_sign, bad args: signer key NULL */
  45829. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  45830. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  45831. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  45832. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  45833. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  45834. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  45835. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  45836. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  45837. {
  45838. flags = PKCS7_BINARY;
  45839. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45840. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45841. /* verify with d2i_PKCS7 */
  45842. tmpPtr = out;
  45843. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45844. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45845. PKCS7_free(p7Ver);
  45846. p7Ver = NULL;
  45847. /* verify with wc_PKCS7_VerifySignedData */
  45848. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45849. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  45850. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45851. /* compare the signer found to expected signer */
  45852. ExpectIntNE(p7Ver->verifyCertSz, 0);
  45853. tmpPtr = NULL;
  45854. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  45855. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  45856. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  45857. tmpPtr = NULL;
  45858. wc_PKCS7_Free(p7Ver);
  45859. p7Ver = NULL;
  45860. ExpectNotNull(out);
  45861. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45862. out = NULL;
  45863. PKCS7_free(p7);
  45864. p7 = NULL;
  45865. }
  45866. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  45867. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  45868. {
  45869. /* re-populate input BIO, may have been consumed */
  45870. BIO_free(inBio);
  45871. inBio = NULL;
  45872. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45873. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45874. flags = PKCS7_BINARY | PKCS7_STREAM;
  45875. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45876. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45877. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45878. /* PKCS7_final, bad args: PKCS7 null */
  45879. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  45880. /* PKCS7_final, bad args: PKCS7 null */
  45881. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  45882. tmpPtr = out;
  45883. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45884. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45885. PKCS7_free(p7Ver);
  45886. p7Ver = NULL;
  45887. ExpectNotNull(out);
  45888. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45889. out = NULL;
  45890. PKCS7_free(p7);
  45891. p7 = NULL;
  45892. }
  45893. /* TEST SUCCESS: Detached, not streaming, not MIME */
  45894. {
  45895. /* re-populate input BIO, may have been consumed */
  45896. BIO_free(inBio);
  45897. inBio = NULL;
  45898. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45899. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45900. flags = PKCS7_BINARY | PKCS7_DETACHED;
  45901. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45902. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45903. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45904. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45905. if (p7Ver != NULL) {
  45906. p7Ver->content = data;
  45907. p7Ver->contentSz = sizeof(data);
  45908. }
  45909. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45910. wc_PKCS7_Free(p7Ver);
  45911. p7Ver = NULL;
  45912. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  45913. * yet support detached content */
  45914. tmpPtr = out;
  45915. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  45916. PKCS7_free(p7Ver);
  45917. p7Ver = NULL;
  45918. ExpectNotNull(out);
  45919. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45920. out = NULL;
  45921. PKCS7_free(p7);
  45922. p7 = NULL;
  45923. }
  45924. /* TEST SUCCESS: Detached, streaming, not MIME */
  45925. {
  45926. /* re-populate input BIO, may have been consumed */
  45927. BIO_free(inBio);
  45928. inBio = NULL;
  45929. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45930. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45931. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  45932. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45933. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  45934. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  45935. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  45936. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  45937. if (p7Ver != NULL) {
  45938. p7Ver->content = data;
  45939. p7Ver->contentSz = sizeof(data);
  45940. }
  45941. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  45942. wc_PKCS7_Free(p7Ver);
  45943. p7Ver = NULL;
  45944. ExpectNotNull(out);
  45945. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45946. PKCS7_free(p7);
  45947. p7 = NULL;
  45948. }
  45949. X509_STORE_free(store);
  45950. X509_free(caCert);
  45951. X509_free(signCert);
  45952. EVP_PKEY_free(signKey);
  45953. BIO_free(inBio);
  45954. BIO_free(keyBio);
  45955. BIO_free(certBio);
  45956. BIO_free(caBio);
  45957. #endif
  45958. return EXPECT_RESULT();
  45959. }
  45960. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  45961. {
  45962. EXPECT_DECLS;
  45963. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  45964. PKCS7_SIGNED* pkcs7 = NULL;
  45965. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  45966. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  45967. PKCS7_SIGNED_free(pkcs7);
  45968. #endif
  45969. return EXPECT_RESULT();
  45970. }
  45971. #ifndef NO_BIO
  45972. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  45973. {
  45974. EXPECT_DECLS;
  45975. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  45976. PKCS7* pkcs7 = NULL;
  45977. BIO* bio = NULL;
  45978. const byte* cert_buf = NULL;
  45979. int ret = 0;
  45980. WC_RNG rng;
  45981. const byte data[] = { /* Hello World */
  45982. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45983. 0x72,0x6c,0x64
  45984. };
  45985. #ifndef NO_RSA
  45986. #if defined(USE_CERT_BUFFERS_2048)
  45987. byte key[sizeof(client_key_der_2048)];
  45988. byte cert[sizeof(client_cert_der_2048)];
  45989. word32 keySz = (word32)sizeof(key);
  45990. word32 certSz = (word32)sizeof(cert);
  45991. XMEMSET(key, 0, keySz);
  45992. XMEMSET(cert, 0, certSz);
  45993. XMEMCPY(key, client_key_der_2048, keySz);
  45994. XMEMCPY(cert, client_cert_der_2048, certSz);
  45995. #elif defined(USE_CERT_BUFFERS_1024)
  45996. byte key[sizeof_client_key_der_1024];
  45997. byte cert[sizeof(sizeof_client_cert_der_1024)];
  45998. word32 keySz = (word32)sizeof(key);
  45999. word32 certSz = (word32)sizeof(cert);
  46000. XMEMSET(key, 0, keySz);
  46001. XMEMSET(cert, 0, certSz);
  46002. XMEMCPY(key, client_key_der_1024, keySz);
  46003. XMEMCPY(cert, client_cert_der_1024, certSz);
  46004. #else
  46005. unsigned char cert[ONEK_BUF];
  46006. unsigned char key[ONEK_BUF];
  46007. XFILE fp = XBADFILE;
  46008. int certSz;
  46009. int keySz;
  46010. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  46011. XBADFILE);
  46012. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  46013. fp), 0);
  46014. if (fp != XBADFILE) {
  46015. XFCLOSE(fp);
  46016. fp = XBADFILE;
  46017. }
  46018. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  46019. XBADFILE);
  46020. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  46021. 0);
  46022. if (fp != XBADFILE) {
  46023. XFCLOSE(fp);
  46024. fp = XBADFILE;
  46025. }
  46026. #endif
  46027. #elif defined(HAVE_ECC)
  46028. #if defined(USE_CERT_BUFFERS_256)
  46029. unsigned char cert[sizeof(cliecc_cert_der_256)];
  46030. unsigned char key[sizeof(ecc_clikey_der_256)];
  46031. int certSz = (int)sizeof(cert);
  46032. int keySz = (int)sizeof(key);
  46033. XMEMSET(cert, 0, certSz);
  46034. XMEMSET(key, 0, keySz);
  46035. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  46036. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  46037. #else
  46038. unsigned char cert[ONEK_BUF];
  46039. unsigned char key[ONEK_BUF];
  46040. XFILE fp = XBADFILE;
  46041. int certSz, keySz;
  46042. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  46043. XBADFILE);
  46044. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  46045. fp), 0);
  46046. if (fp != XBADFILE) {
  46047. XFCLOSE(fp);
  46048. fp = XBADFILE;
  46049. }
  46050. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  46051. XBADFILE);
  46052. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  46053. 0);
  46054. if (fp != XBADFILE) {
  46055. XFCLOSE(fp);
  46056. fp = XBADFILE;
  46057. }
  46058. #endif
  46059. #else
  46060. #error PKCS7 requires ECC or RSA
  46061. #endif
  46062. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  46063. /* initialize with DER encoded cert */
  46064. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  46065. /* init rng */
  46066. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46067. ExpectIntEQ(wc_InitRng(&rng), 0);
  46068. if (pkcs7 != NULL) {
  46069. pkcs7->rng = &rng;
  46070. pkcs7->content = (byte*)data; /* not used for ex */
  46071. pkcs7->contentSz = (word32)sizeof(data);
  46072. pkcs7->contentOID = SIGNED_DATA;
  46073. pkcs7->privateKey = key;
  46074. pkcs7->privateKeySz = (word32)sizeof(key);
  46075. pkcs7->encryptOID = RSAk;
  46076. #ifdef NO_SHA
  46077. pkcs7->hashOID = SHA256h;
  46078. #else
  46079. pkcs7->hashOID = SHAh;
  46080. #endif
  46081. pkcs7->signedAttribs = NULL;
  46082. pkcs7->signedAttribsSz = 0;
  46083. }
  46084. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46085. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  46086. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  46087. /* Read PKCS#7 PEM from BIO */
  46088. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  46089. ExpectIntGE(ret, 0);
  46090. BIO_free(bio);
  46091. wc_PKCS7_Free(pkcs7);
  46092. wc_FreeRng(&rng);
  46093. #endif
  46094. return EXPECT_RESULT();
  46095. }
  46096. #ifdef HAVE_SMIME
  46097. static int test_wolfSSL_SMIME_read_PKCS7(void)
  46098. {
  46099. EXPECT_DECLS;
  46100. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  46101. !defined(NO_RSA)
  46102. PKCS7* pkcs7 = NULL;
  46103. BIO* bio = NULL;
  46104. BIO* bcont = NULL;
  46105. BIO* out = NULL;
  46106. const byte* outBuf = NULL;
  46107. int outBufLen = 0;
  46108. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  46109. XFILE smimeTestFile = XBADFILE;
  46110. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  46111. XBADFILE);
  46112. /* smime-test.p7s */
  46113. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  46114. ExpectNotNull(bio);
  46115. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46116. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46117. ExpectNotNull(pkcs7);
  46118. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46119. PKCS7_NOVERIFY), SSL_SUCCESS);
  46120. XFCLOSE(smimeTestFile);
  46121. if (bcont) BIO_free(bcont);
  46122. bcont = NULL;
  46123. wolfSSL_PKCS7_free(pkcs7);
  46124. pkcs7 = NULL;
  46125. /* smime-test-multipart.p7s */
  46126. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  46127. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46128. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46129. ExpectNotNull(pkcs7);
  46130. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46131. PKCS7_NOVERIFY), SSL_SUCCESS);
  46132. XFCLOSE(smimeTestFile);
  46133. if (bcont) BIO_free(bcont);
  46134. bcont = NULL;
  46135. wolfSSL_PKCS7_free(pkcs7);
  46136. pkcs7 = NULL;
  46137. /* smime-test-multipart-badsig.p7s */
  46138. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  46139. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46140. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46141. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  46142. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46143. PKCS7_NOVERIFY), SSL_FAILURE);
  46144. XFCLOSE(smimeTestFile);
  46145. if (bcont) BIO_free(bcont);
  46146. bcont = NULL;
  46147. wolfSSL_PKCS7_free(pkcs7);
  46148. pkcs7 = NULL;
  46149. /* smime-test-canon.p7s */
  46150. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46151. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46152. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46153. ExpectNotNull(pkcs7);
  46154. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  46155. PKCS7_NOVERIFY), SSL_SUCCESS);
  46156. XFCLOSE(smimeTestFile);
  46157. if (bcont) BIO_free(bcont);
  46158. bcont = NULL;
  46159. wolfSSL_PKCS7_free(pkcs7);
  46160. pkcs7 = NULL;
  46161. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  46162. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  46163. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  46164. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  46165. ExpectNotNull(pkcs7);
  46166. out = wolfSSL_BIO_new(BIO_s_mem());
  46167. ExpectNotNull(out);
  46168. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  46169. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  46170. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  46171. /* Content-Type should not show up at beginning of output buffer */
  46172. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  46173. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  46174. BIO_free(out);
  46175. BIO_free(bio);
  46176. if (bcont) BIO_free(bcont);
  46177. wolfSSL_PKCS7_free(pkcs7);
  46178. #endif
  46179. return EXPECT_RESULT();
  46180. }
  46181. static int test_wolfSSL_SMIME_write_PKCS7(void)
  46182. {
  46183. EXPECT_DECLS;
  46184. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  46185. PKCS7* p7 = NULL;
  46186. PKCS7* p7Ver = NULL;
  46187. int flags = 0;
  46188. byte data[] = "Test data to encode.";
  46189. const char* cert = "./certs/server-cert.pem";
  46190. const char* key = "./certs/server-key.pem";
  46191. const char* ca = "./certs/ca-cert.pem";
  46192. WOLFSSL_BIO* certBio = NULL;
  46193. WOLFSSL_BIO* keyBio = NULL;
  46194. WOLFSSL_BIO* caBio = NULL;
  46195. WOLFSSL_BIO* inBio = NULL;
  46196. WOLFSSL_BIO* outBio = NULL;
  46197. WOLFSSL_BIO* content = NULL;
  46198. X509* signCert = NULL;
  46199. EVP_PKEY* signKey = NULL;
  46200. X509* caCert = NULL;
  46201. X509_STORE* store = NULL;
  46202. /* read signer cert/key into BIO */
  46203. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  46204. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  46205. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46206. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46207. /* read CA cert into store (for verify) */
  46208. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  46209. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46210. ExpectNotNull(store = X509_STORE_new());
  46211. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46212. /* generate and verify SMIME: not detached */
  46213. {
  46214. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46215. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46216. flags = PKCS7_STREAM;
  46217. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46218. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46219. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46220. /* bad arg: out NULL */
  46221. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  46222. /* bad arg: pkcs7 NULL */
  46223. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  46224. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46225. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46226. BIO_free(content);
  46227. content = NULL;
  46228. BIO_free(inBio);
  46229. inBio = NULL;
  46230. BIO_free(outBio);
  46231. outBio = NULL;
  46232. PKCS7_free(p7Ver);
  46233. p7Ver = NULL;
  46234. PKCS7_free(p7);
  46235. p7 = NULL;
  46236. }
  46237. /* generate and verify SMIME: not detached, add Content-Type */
  46238. {
  46239. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46240. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46241. flags = PKCS7_STREAM | PKCS7_TEXT;
  46242. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46243. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46244. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46245. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46246. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46247. BIO_free(content);
  46248. content = NULL;
  46249. BIO_free(inBio);
  46250. inBio = NULL;
  46251. BIO_free(outBio);
  46252. outBio = NULL;
  46253. PKCS7_free(p7Ver);
  46254. p7Ver = NULL;
  46255. PKCS7_free(p7);
  46256. p7 = NULL;
  46257. }
  46258. /* generate and verify SMIME: detached */
  46259. {
  46260. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46261. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46262. flags = PKCS7_DETACHED | PKCS7_STREAM;
  46263. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46264. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46265. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46266. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46267. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46268. BIO_free(content);
  46269. content = NULL;
  46270. BIO_free(inBio);
  46271. inBio = NULL;
  46272. BIO_free(outBio);
  46273. outBio = NULL;
  46274. PKCS7_free(p7Ver);
  46275. p7Ver = NULL;
  46276. PKCS7_free(p7);
  46277. p7 = NULL;
  46278. }
  46279. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  46280. {
  46281. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46282. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46283. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  46284. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46285. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  46286. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  46287. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  46288. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  46289. BIO_free(content);
  46290. content = NULL;
  46291. BIO_free(inBio);
  46292. inBio = NULL;
  46293. BIO_free(outBio);
  46294. outBio = NULL;
  46295. PKCS7_free(p7Ver);
  46296. p7Ver = NULL;
  46297. PKCS7_free(p7);
  46298. p7 = NULL;
  46299. }
  46300. X509_STORE_free(store);
  46301. X509_free(caCert);
  46302. X509_free(signCert);
  46303. EVP_PKEY_free(signKey);
  46304. BIO_free(keyBio);
  46305. BIO_free(certBio);
  46306. BIO_free(caBio);
  46307. #endif
  46308. return EXPECT_RESULT();
  46309. }
  46310. #endif /* HAVE_SMIME */
  46311. #endif /* !NO_BIO */
  46312. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  46313. * returns 0) */
  46314. static int test_X509_STORE_No_SSL_CTX(void)
  46315. {
  46316. EXPECT_DECLS;
  46317. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  46318. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46319. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  46320. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46321. defined(HAVE_CRL) && !defined(NO_RSA)
  46322. X509_STORE * store = NULL;
  46323. X509_STORE_CTX * storeCtx = NULL;
  46324. X509_CRL * crl = NULL;
  46325. X509 * ca = NULL;
  46326. X509 * cert = NULL;
  46327. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46328. const char srvCert[] = "./certs/server-cert.pem";
  46329. const char caCert[] = "./certs/ca-cert.pem";
  46330. const char caDir[] = "./certs/crl/hash_pem";
  46331. XFILE fp = XBADFILE;
  46332. X509_LOOKUP * lookup = NULL;
  46333. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46334. /* Set up store with CA */
  46335. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46336. SSL_FILETYPE_PEM)));
  46337. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46338. /* Add CRL lookup directory to store
  46339. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  46340. * of crl.pem */
  46341. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46342. X509_LOOKUP_hash_dir())));
  46343. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  46344. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  46345. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46346. SSL_SUCCESS);
  46347. /* Add CRL to store NOT containing the verified certificate, which
  46348. * forces use of the CRL lookup directory */
  46349. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46350. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46351. NULL, NULL));
  46352. if (fp != XBADFILE)
  46353. XFCLOSE(fp);
  46354. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46355. /* Create verification context outside of an SSL session */
  46356. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46357. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46358. SSL_FILETYPE_PEM)));
  46359. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46360. /* Perform verification, which should NOT indicate CRL missing due to the
  46361. * store CM's X509 store pointer being NULL */
  46362. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46363. X509_CRL_free(crl);
  46364. X509_STORE_free(store);
  46365. X509_STORE_CTX_free(storeCtx);
  46366. X509_free(cert);
  46367. X509_free(ca);
  46368. #endif
  46369. return EXPECT_RESULT();
  46370. }
  46371. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  46372. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  46373. static int test_X509_LOOKUP_add_dir(void)
  46374. {
  46375. EXPECT_DECLS;
  46376. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  46377. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  46378. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  46379. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  46380. defined(HAVE_CRL) && !defined(NO_RSA)
  46381. X509_STORE * store = NULL;
  46382. X509_STORE_CTX * storeCtx = NULL;
  46383. X509_CRL * crl = NULL;
  46384. X509 * ca = NULL;
  46385. X509 * cert = NULL;
  46386. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  46387. const char srvCert[] = "./certs/server-cert.pem";
  46388. const char caCert[] = "./certs/ca-cert.pem";
  46389. const char caDir[] = "./certs/crl/hash_der";
  46390. XFILE fp = XBADFILE;
  46391. X509_LOOKUP * lookup = NULL;
  46392. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46393. /* Set up store with CA */
  46394. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46395. SSL_FILETYPE_PEM)));
  46396. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46397. /* Add CRL lookup directory to store.
  46398. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  46399. * of crl.der */
  46400. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46401. X509_LOOKUP_hash_dir())));
  46402. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  46403. SSL_SUCCESS);
  46404. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46405. SSL_SUCCESS);
  46406. /* Add CRL to store NOT containing the verified certificate, which
  46407. * forces use of the CRL lookup directory */
  46408. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46409. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46410. NULL, NULL));
  46411. if (fp != XBADFILE) {
  46412. XFCLOSE(fp);
  46413. fp = XBADFILE;
  46414. }
  46415. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46416. /* Create verification context outside of an SSL session */
  46417. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46418. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46419. SSL_FILETYPE_PEM)));
  46420. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46421. /* Perform verification, which should NOT return CRL missing */
  46422. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  46423. X509_CRL_free(crl);
  46424. crl = NULL;
  46425. X509_STORE_free(store);
  46426. store = NULL;
  46427. X509_STORE_CTX_free(storeCtx);
  46428. storeCtx = NULL;
  46429. X509_free(cert);
  46430. cert = NULL;
  46431. X509_free(ca);
  46432. ca = NULL;
  46433. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  46434. * We should get CRL_MISSING at the end, because the lookup
  46435. * dir has only ASN1 CRLs. */
  46436. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  46437. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  46438. SSL_FILETYPE_PEM)));
  46439. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  46440. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  46441. X509_LOOKUP_hash_dir())));
  46442. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  46443. SSL_SUCCESS);
  46444. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  46445. SSL_SUCCESS);
  46446. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  46447. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46448. NULL, NULL));
  46449. if (fp != XBADFILE) {
  46450. XFCLOSE(fp);
  46451. fp = XBADFILE;
  46452. }
  46453. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  46454. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  46455. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  46456. SSL_FILETYPE_PEM)));
  46457. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  46458. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  46459. * in dir containing only ASN1/DER. */
  46460. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  46461. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  46462. X509_CRL_free(crl);
  46463. X509_STORE_free(store);
  46464. X509_STORE_CTX_free(storeCtx);
  46465. X509_free(cert);
  46466. X509_free(ca);
  46467. #endif
  46468. return EXPECT_RESULT();
  46469. }
  46470. /*----------------------------------------------------------------------------*
  46471. | Certificate Failure Checks
  46472. *----------------------------------------------------------------------------*/
  46473. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46474. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  46475. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46476. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  46477. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  46478. int type)
  46479. {
  46480. int ret;
  46481. WOLFSSL_CERT_MANAGER* cm = NULL;
  46482. switch (type) {
  46483. case TESTING_RSA:
  46484. #ifdef NO_RSA
  46485. fprintf(stderr, "RSA disabled, skipping test\n");
  46486. return ASN_SIG_CONFIRM_E;
  46487. #else
  46488. break;
  46489. #endif
  46490. case TESTING_ECC:
  46491. #ifndef HAVE_ECC
  46492. fprintf(stderr, "ECC disabled, skipping test\n");
  46493. return ASN_SIG_CONFIRM_E;
  46494. #else
  46495. break;
  46496. #endif
  46497. default:
  46498. fprintf(stderr, "Bad function argument\n");
  46499. return BAD_FUNC_ARG;
  46500. }
  46501. cm = wolfSSL_CertManagerNew();
  46502. if (cm == NULL) {
  46503. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  46504. return -1;
  46505. }
  46506. #ifndef NO_FILESYSTEM
  46507. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  46508. if (ret != WOLFSSL_SUCCESS) {
  46509. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  46510. wolfSSL_CertManagerFree(cm);
  46511. return ret;
  46512. }
  46513. #else
  46514. (void)ca;
  46515. #endif
  46516. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  46517. WOLFSSL_FILETYPE_ASN1);
  46518. /* Let ExpectIntEQ handle return code */
  46519. wolfSSL_CertManagerFree(cm);
  46520. return ret;
  46521. }
  46522. #endif
  46523. #if !defined(NO_FILESYSTEM)
  46524. static int test_RsaSigFailure_cm(void)
  46525. {
  46526. EXPECT_DECLS;
  46527. #ifndef NO_RSA
  46528. const char* ca_cert = "./certs/ca-cert.pem";
  46529. const char* server_cert = "./certs/server-cert.der";
  46530. byte* cert_buf = NULL;
  46531. size_t cert_sz = 0;
  46532. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46533. if (cert_buf != NULL) {
  46534. /* corrupt DER - invert last byte, which is signature */
  46535. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46536. /* test bad cert */
  46537. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46538. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46539. WOLFSSL_FATAL_ERROR);
  46540. #else
  46541. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  46542. ASN_SIG_CONFIRM_E);
  46543. #endif
  46544. }
  46545. /* load_file() uses malloc. */
  46546. if (cert_buf != NULL) {
  46547. free(cert_buf);
  46548. }
  46549. #endif /* !NO_RSA */
  46550. return EXPECT_RESULT();
  46551. }
  46552. static int test_EccSigFailure_cm(void)
  46553. {
  46554. EXPECT_DECLS;
  46555. #ifdef HAVE_ECC
  46556. /* self-signed ECC cert, so use server cert as CA */
  46557. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  46558. const char* server_cert = "./certs/server-ecc.der";
  46559. byte* cert_buf = NULL;
  46560. size_t cert_sz = 0;
  46561. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  46562. if (cert_buf != NULL) {
  46563. /* corrupt DER - invert last byte, which is signature */
  46564. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  46565. /* test bad cert */
  46566. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46567. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46568. WOLFSSL_FATAL_ERROR);
  46569. #else
  46570. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  46571. ASN_SIG_CONFIRM_E);
  46572. #endif
  46573. }
  46574. /* load_file() uses malloc. */
  46575. if (cert_buf != NULL) {
  46576. free(cert_buf);
  46577. }
  46578. #ifdef FP_ECC
  46579. wc_ecc_fp_free();
  46580. #endif
  46581. #endif /* HAVE_ECC */
  46582. return EXPECT_RESULT();
  46583. }
  46584. #endif /* !NO_FILESYSTEM */
  46585. #endif /* NO_CERTS */
  46586. #ifdef WOLFSSL_TLS13
  46587. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  46588. #ifdef WC_SHA384_DIGEST_SIZE
  46589. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  46590. #else
  46591. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  46592. #endif
  46593. #endif
  46594. #ifdef WOLFSSL_EARLY_DATA
  46595. static const char earlyData[] = "Early Data";
  46596. static char earlyDataBuffer[1];
  46597. #endif
  46598. static int test_tls13_apis(void)
  46599. {
  46600. EXPECT_DECLS;
  46601. int ret;
  46602. #ifndef WOLFSSL_NO_TLS12
  46603. #ifndef NO_WOLFSSL_CLIENT
  46604. WOLFSSL_CTX* clientTls12Ctx = NULL;
  46605. WOLFSSL* clientTls12Ssl = NULL;
  46606. #endif
  46607. #ifndef NO_WOLFSSL_SERVER
  46608. WOLFSSL_CTX* serverTls12Ctx = NULL;
  46609. WOLFSSL* serverTls12Ssl = NULL;
  46610. #endif
  46611. #endif
  46612. #ifndef NO_WOLFSSL_CLIENT
  46613. WOLFSSL_CTX* clientCtx = NULL;
  46614. WOLFSSL* clientSsl = NULL;
  46615. #endif
  46616. #ifndef NO_WOLFSSL_SERVER
  46617. WOLFSSL_CTX* serverCtx = NULL;
  46618. WOLFSSL* serverSsl = NULL;
  46619. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46620. const char* ourCert = svrCertFile;
  46621. const char* ourKey = svrKeyFile;
  46622. #endif
  46623. #endif
  46624. int required;
  46625. #ifdef WOLFSSL_EARLY_DATA
  46626. int outSz;
  46627. #endif
  46628. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  46629. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  46630. #ifdef HAVE_PQC
  46631. WOLFSSL_KYBER_LEVEL1
  46632. #else
  46633. WOLFSSL_ECC_SECP256R1
  46634. #endif
  46635. };
  46636. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  46637. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  46638. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  46639. int numGroups = 2;
  46640. #endif
  46641. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46642. char groupList[] =
  46643. #ifndef NO_ECC_SECP
  46644. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  46645. "P-521:"
  46646. #endif
  46647. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  46648. "P-384:"
  46649. #endif
  46650. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  46651. "P-256"
  46652. #ifdef HAVE_PQC
  46653. ":P256_KYBER_LEVEL1"
  46654. #endif
  46655. #endif
  46656. #endif /* !defined(NO_ECC_SECP) */
  46657. #ifdef HAVE_PQC
  46658. ":KYBER_LEVEL1"
  46659. #endif
  46660. "";
  46661. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46662. (void)ret;
  46663. #ifndef WOLFSSL_NO_TLS12
  46664. #ifndef NO_WOLFSSL_CLIENT
  46665. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  46666. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  46667. #endif
  46668. #ifndef NO_WOLFSSL_SERVER
  46669. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  46670. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46671. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  46672. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  46673. WOLFSSL_FILETYPE_PEM);
  46674. #endif
  46675. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  46676. #endif
  46677. #endif
  46678. #ifndef NO_WOLFSSL_CLIENT
  46679. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  46680. clientSsl = wolfSSL_new(clientCtx);
  46681. #endif
  46682. #ifndef NO_WOLFSSL_SERVER
  46683. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  46684. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  46685. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  46686. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  46687. #endif
  46688. serverSsl = wolfSSL_new(serverCtx);
  46689. ExpectNotNull(serverSsl);
  46690. #endif
  46691. #ifdef WOLFSSL_SEND_HRR_COOKIE
  46692. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  46693. #ifndef NO_WOLFSSL_CLIENT
  46694. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  46695. #endif
  46696. #ifndef NO_WOLFSSL_SERVER
  46697. #ifndef WOLFSSL_NO_TLS12
  46698. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  46699. BAD_FUNC_ARG);
  46700. #endif
  46701. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  46702. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  46703. WOLFSSL_SUCCESS);
  46704. #endif
  46705. #endif
  46706. #ifdef HAVE_SUPPORTED_CURVES
  46707. #ifdef HAVE_ECC
  46708. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  46709. BAD_FUNC_ARG);
  46710. #ifndef NO_WOLFSSL_SERVER
  46711. do {
  46712. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  46713. #ifdef WOLFSSL_ASYNC_CRYPT
  46714. if (ret == WC_PENDING_E)
  46715. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  46716. #endif
  46717. }
  46718. while (ret == WC_PENDING_E);
  46719. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46720. #endif
  46721. #ifndef NO_WOLFSSL_CLIENT
  46722. #ifndef WOLFSSL_NO_TLS12
  46723. do {
  46724. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  46725. #ifdef WOLFSSL_ASYNC_CRYPT
  46726. if (ret == WC_PENDING_E)
  46727. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  46728. #endif
  46729. }
  46730. while (ret == WC_PENDING_E);
  46731. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46732. #endif
  46733. do {
  46734. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  46735. #ifdef WOLFSSL_ASYNC_CRYPT
  46736. if (ret == WC_PENDING_E)
  46737. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  46738. #endif
  46739. }
  46740. while (ret == WC_PENDING_E);
  46741. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46742. #endif
  46743. #elif defined(HAVE_CURVE25519)
  46744. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  46745. #ifndef NO_WOLFSSL_SERVER
  46746. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  46747. WOLFSSL_SUCCESS);
  46748. #endif
  46749. #ifndef NO_WOLFSSL_CLIENT
  46750. #ifndef WOLFSSL_NO_TLS12
  46751. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  46752. WOLFSSL_SUCCESS);
  46753. #endif
  46754. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  46755. WOLFSSL_SUCCESS);
  46756. #endif
  46757. #elif defined(HAVE_CURVE448)
  46758. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  46759. #ifndef NO_WOLFSSL_SERVER
  46760. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  46761. WOLFSSL_SUCCESS);
  46762. #endif
  46763. #ifndef NO_WOLFSSL_CLIENT
  46764. #ifndef WOLFSSL_NO_TLS12
  46765. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  46766. WOLFSSL_SUCCESS);
  46767. #endif
  46768. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  46769. WOLFSSL_SUCCESS);
  46770. #endif
  46771. #else
  46772. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  46773. BAD_FUNC_ARG);
  46774. #ifndef NO_WOLFSSL_CLIENT
  46775. #ifndef WOLFSSL_NO_TLS12
  46776. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  46777. NOT_COMPILED_IN);
  46778. #endif
  46779. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  46780. NOT_COMPILED_IN);
  46781. #endif
  46782. #endif
  46783. #if defined(HAVE_PQC)
  46784. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  46785. #ifndef NO_WOLFSSL_SERVER
  46786. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  46787. WOLFSSL_SUCCESS);
  46788. #endif
  46789. #ifndef NO_WOLFSSL_CLIENT
  46790. #ifndef WOLFSSL_NO_TLS12
  46791. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  46792. BAD_FUNC_ARG);
  46793. #endif
  46794. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  46795. WOLFSSL_SUCCESS);
  46796. #endif
  46797. #endif
  46798. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  46799. #ifndef NO_WOLFSSL_SERVER
  46800. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  46801. #endif
  46802. #ifndef NO_WOLFSSL_CLIENT
  46803. #ifndef WOLFSSL_NO_TLS12
  46804. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  46805. #endif
  46806. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  46807. #endif
  46808. #endif /* HAVE_SUPPORTED_CURVES */
  46809. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46810. #ifndef NO_WOLFSSL_CLIENT
  46811. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  46812. #endif
  46813. #ifndef NO_WOLFSSL_SERVER
  46814. #ifndef WOLFSSL_NO_TLS12
  46815. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  46816. #endif
  46817. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  46818. #endif
  46819. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  46820. #ifndef NO_WOLFSSL_CLIENT
  46821. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  46822. #endif
  46823. #ifndef NO_WOLFSSL_SERVER
  46824. #ifndef WOLFSSL_NO_TLS12
  46825. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  46826. #endif
  46827. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  46828. #endif
  46829. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46830. #ifndef NO_WOLFSSL_CLIENT
  46831. #ifndef WOLFSSL_NO_TLS12
  46832. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  46833. #endif
  46834. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  46835. #endif
  46836. #ifndef NO_WOLFSSL_SERVER
  46837. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  46838. #endif
  46839. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  46840. #ifndef NO_WOLFSSL_CLIENT
  46841. #ifndef WOLFSSL_NO_TLS12
  46842. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  46843. #endif
  46844. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  46845. #endif
  46846. #ifndef NO_WOLFSSL_SERVER
  46847. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  46848. #endif
  46849. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  46850. #ifndef NO_WOLFSSL_CLIENT
  46851. #ifndef WOLFSSL_NO_TLS12
  46852. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  46853. #endif
  46854. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  46855. #endif
  46856. #ifndef NO_WOLFSSL_SERVER
  46857. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  46858. #endif
  46859. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  46860. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  46861. #ifndef NO_WOLFSSL_CLIENT
  46862. #ifndef WOLFSSL_NO_TLS12
  46863. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  46864. BAD_FUNC_ARG);
  46865. #endif
  46866. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  46867. #endif
  46868. #ifndef NO_WOLFSSL_SERVER
  46869. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  46870. #endif
  46871. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  46872. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46873. #ifndef NO_WOLFSSL_SERVER
  46874. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  46875. #endif
  46876. #ifndef NO_WOLFSSL_CLIENT
  46877. #ifndef WOLFSSL_NO_TLS12
  46878. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  46879. BAD_FUNC_ARG);
  46880. #endif
  46881. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  46882. #endif
  46883. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  46884. #ifndef NO_WOLFSSL_SERVER
  46885. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  46886. #endif
  46887. #ifndef NO_WOLFSSL_CLIENT
  46888. #ifndef WOLFSSL_NO_TLS12
  46889. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  46890. BAD_FUNC_ARG);
  46891. #endif
  46892. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  46893. #endif
  46894. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  46895. #ifndef NO_WOLFSSL_CLIENT
  46896. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  46897. #endif
  46898. #ifndef NO_WOLFSSL_SERVER
  46899. #ifndef WOLFSSL_NO_TLS12
  46900. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  46901. BAD_FUNC_ARG);
  46902. #endif
  46903. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  46904. #endif
  46905. #endif
  46906. #ifdef HAVE_ECC
  46907. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  46908. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  46909. #ifndef NO_WOLFSSL_SERVER
  46910. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  46911. #endif
  46912. #ifndef NO_WOLFSSL_CLIENT
  46913. #ifndef WOLFSSL_NO_TLS12
  46914. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  46915. #endif
  46916. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  46917. #endif
  46918. #endif
  46919. #ifdef HAVE_SUPPORTED_CURVES
  46920. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46921. #ifndef NO_WOLFSSL_CLIENT
  46922. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  46923. #endif
  46924. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46925. #ifndef NO_WOLFSSL_CLIENT
  46926. #ifndef WOLFSSL_NO_TLS12
  46927. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  46928. BAD_FUNC_ARG);
  46929. #endif
  46930. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  46931. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46932. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  46933. WOLFSSL_SUCCESS);
  46934. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  46935. BAD_FUNC_ARG);
  46936. #endif
  46937. #ifndef NO_WOLFSSL_SERVER
  46938. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  46939. WOLFSSL_SUCCESS);
  46940. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  46941. BAD_FUNC_ARG);
  46942. #endif
  46943. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  46944. #ifndef NO_WOLFSSL_CLIENT
  46945. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  46946. #endif
  46947. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  46948. #ifndef NO_WOLFSSL_CLIENT
  46949. #ifndef WOLFSSL_NO_TLS12
  46950. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  46951. BAD_FUNC_ARG);
  46952. #endif
  46953. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  46954. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  46955. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  46956. WOLFSSL_SUCCESS);
  46957. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  46958. BAD_FUNC_ARG);
  46959. #endif
  46960. #ifndef NO_WOLFSSL_SERVER
  46961. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  46962. WOLFSSL_SUCCESS);
  46963. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  46964. BAD_FUNC_ARG);
  46965. #endif
  46966. #ifdef OPENSSL_EXTRA
  46967. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46968. #ifndef NO_WOLFSSL_CLIENT
  46969. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  46970. WOLFSSL_FAILURE);
  46971. #endif
  46972. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  46973. WOLFSSL_FAILURE);
  46974. #ifndef NO_WOLFSSL_CLIENT
  46975. #ifndef WOLFSSL_NO_TLS12
  46976. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  46977. WOLFSSL_FAILURE);
  46978. #endif
  46979. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  46980. WOLFSSL_SUCCESS);
  46981. #endif
  46982. #ifndef NO_WOLFSSL_SERVER
  46983. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  46984. WOLFSSL_SUCCESS);
  46985. #endif
  46986. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  46987. #ifndef NO_WOLFSSL_CLIENT
  46988. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  46989. #endif
  46990. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  46991. #ifndef NO_WOLFSSL_CLIENT
  46992. #ifndef WOLFSSL_NO_TLS12
  46993. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  46994. WOLFSSL_FAILURE);
  46995. #endif
  46996. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  46997. WOLFSSL_SUCCESS);
  46998. #endif
  46999. #ifndef NO_WOLFSSL_SERVER
  47000. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  47001. WOLFSSL_SUCCESS);
  47002. #endif
  47003. #endif /* OPENSSL_EXTRA */
  47004. #endif /* HAVE_SUPPORTED_CURVES */
  47005. #endif /* HAVE_ECC */
  47006. #ifdef WOLFSSL_EARLY_DATA
  47007. #ifndef OPENSSL_EXTRA
  47008. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47009. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47010. #else
  47011. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47012. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47013. #endif
  47014. #ifndef NO_WOLFSSL_CLIENT
  47015. #ifndef OPENSSL_EXTRA
  47016. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47017. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47018. #else
  47019. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47020. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47021. #endif
  47022. #endif
  47023. #ifndef NO_WOLFSSL_SERVER
  47024. #ifndef WOLFSSL_NO_TLS12
  47025. #ifndef OPENSSL_EXTRA
  47026. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47027. BAD_FUNC_ARG);
  47028. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47029. #else
  47030. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47031. BAD_FUNC_ARG);
  47032. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47033. #endif
  47034. #endif
  47035. #ifndef OPENSSL_EXTRA
  47036. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47037. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  47038. WOLFSSL_SUCCESS);
  47039. #else
  47040. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  47041. #endif
  47042. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  47043. #else
  47044. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  47045. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  47046. #endif
  47047. #endif
  47048. #ifndef OPENSSL_EXTRA
  47049. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47050. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47051. #else
  47052. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47053. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  47054. #endif
  47055. #ifndef NO_WOLFSSL_CLIENT
  47056. #ifndef OPENSSL_EXTRA
  47057. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47058. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  47059. #else
  47060. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  47061. #endif
  47062. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  47063. #else
  47064. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  47065. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  47066. #endif
  47067. #endif
  47068. #ifndef NO_WOLFSSL_SERVER
  47069. #ifndef WOLFSSL_NO_TLS12
  47070. #ifndef OPENSSL_EXTRA
  47071. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47072. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47073. #else
  47074. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  47075. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  47076. #endif
  47077. #endif
  47078. #ifndef OPENSSL_EXTRA
  47079. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  47080. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  47081. #else
  47082. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  47083. #endif
  47084. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  47085. #else
  47086. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  47087. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  47088. #endif
  47089. #endif
  47090. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  47091. &outSz), BAD_FUNC_ARG);
  47092. #ifndef NO_WOLFSSL_CLIENT
  47093. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  47094. &outSz), BAD_FUNC_ARG);
  47095. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  47096. BAD_FUNC_ARG);
  47097. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47098. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  47099. #endif
  47100. #ifndef NO_WOLFSSL_SERVER
  47101. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  47102. sizeof(earlyData), &outSz), SIDE_ERROR);
  47103. #endif
  47104. #ifndef NO_WOLFSSL_CLIENT
  47105. #ifndef WOLFSSL_NO_TLS12
  47106. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  47107. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  47108. #endif
  47109. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  47110. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  47111. #endif
  47112. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  47113. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  47114. #ifndef NO_WOLFSSL_SERVER
  47115. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  47116. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  47117. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  47118. &outSz), BAD_FUNC_ARG);
  47119. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47120. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  47121. #endif
  47122. #ifndef NO_WOLFSSL_CLIENT
  47123. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  47124. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  47125. #endif
  47126. #ifndef NO_WOLFSSL_SERVER
  47127. #ifndef WOLFSSL_NO_TLS12
  47128. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  47129. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  47130. #endif
  47131. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  47132. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  47133. #endif
  47134. #endif
  47135. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  47136. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  47137. #endif
  47138. #ifndef NO_WOLFSSL_SERVER
  47139. wolfSSL_free(serverSsl);
  47140. wolfSSL_CTX_free(serverCtx);
  47141. #endif
  47142. #ifndef NO_WOLFSSL_CLIENT
  47143. wolfSSL_free(clientSsl);
  47144. wolfSSL_CTX_free(clientCtx);
  47145. #endif
  47146. #ifndef WOLFSSL_NO_TLS12
  47147. #ifndef NO_WOLFSSL_SERVER
  47148. wolfSSL_free(serverTls12Ssl);
  47149. wolfSSL_CTX_free(serverTls12Ctx);
  47150. #endif
  47151. #ifndef NO_WOLFSSL_CLIENT
  47152. wolfSSL_free(clientTls12Ssl);
  47153. wolfSSL_CTX_free(clientTls12Ctx);
  47154. #endif
  47155. #endif
  47156. return EXPECT_RESULT();
  47157. }
  47158. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47159. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47160. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47161. /* Called when writing. */
  47162. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47163. {
  47164. (void)ssl;
  47165. (void)buf;
  47166. (void)sz;
  47167. (void)ctx;
  47168. /* Force error return from wolfSSL_accept_TLSv13(). */
  47169. return WANT_WRITE;
  47170. }
  47171. /* Called when reading. */
  47172. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  47173. {
  47174. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  47175. int len = (int)msg->length;
  47176. (void)ssl;
  47177. (void)sz;
  47178. /* Pass back as much of message as will fit in buffer. */
  47179. if (len > sz)
  47180. len = sz;
  47181. XMEMCPY(buf, msg->buffer, len);
  47182. /* Move over returned data. */
  47183. msg->buffer += len;
  47184. msg->length -= len;
  47185. /* Amount actually copied. */
  47186. return len;
  47187. }
  47188. #endif
  47189. static int test_tls13_cipher_suites(void)
  47190. {
  47191. EXPECT_DECLS;
  47192. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  47193. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  47194. defined(BUILD_TLS_AES_256_GCM_SHA384)
  47195. WOLFSSL_CTX* ctx = NULL;
  47196. WOLFSSL *ssl = NULL;
  47197. int i;
  47198. byte clientHello[] = {
  47199. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  47200. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  47201. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  47202. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  47203. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  47204. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  47205. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  47206. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  47207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  47209. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  47210. 0x13, 0x01,
  47211. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  47212. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  47213. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  47214. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  47215. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  47216. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  47217. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  47218. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  47219. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  47220. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  47221. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  47222. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  47223. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  47224. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  47225. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  47226. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  47227. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  47228. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  47229. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  47230. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  47231. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  47232. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  47233. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  47234. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  47235. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  47236. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  47237. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  47238. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  47239. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  47240. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  47241. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  47242. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  47243. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  47244. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  47245. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  47246. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47247. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47248. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  47249. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  47250. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  47251. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  47252. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  47253. };
  47254. WOLFSSL_BUFFER_INFO msg;
  47255. /* Offset into ClientHello message data of first cipher suite. */
  47256. const int csOff = 78;
  47257. /* Server cipher list. */
  47258. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  47259. /* Suite list with duplicates. */
  47260. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  47261. "TLS13-AES128-GCM-SHA256:"
  47262. "TLS13-AES256-GCM-SHA384:"
  47263. "TLS13-AES256-GCM-SHA384:"
  47264. "TLS13-AES128-GCM-SHA256";
  47265. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47266. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47267. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  47268. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47269. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  47270. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  47271. #endif
  47272. /* Set up wolfSSL context. */
  47273. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47274. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  47275. WOLFSSL_FILETYPE_PEM));
  47276. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  47277. WOLFSSL_FILETYPE_PEM));
  47278. /* Read from 'msg'. */
  47279. wolfSSL_SetIORecv(ctx, CsRecv);
  47280. /* No where to send to - dummy sender. */
  47281. wolfSSL_SetIOSend(ctx, CsSend);
  47282. /* Test cipher suite list with many copies of a cipher suite. */
  47283. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47284. msg.buffer = clientHello;
  47285. msg.length = (unsigned int)sizeof(clientHello);
  47286. wolfSSL_SetIOReadCtx(ssl, &msg);
  47287. /* Force server to have as many occurrences of same cipher suite as
  47288. * possible. */
  47289. if (ssl != NULL) {
  47290. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  47291. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  47292. for (i = 0; i < suites->suiteSz; i += 2) {
  47293. suites->suites[i + 0] = TLS13_BYTE;
  47294. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  47295. }
  47296. }
  47297. /* Test multiple occurrences of same cipher suite. */
  47298. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  47299. wolfSSL_free(ssl);
  47300. ssl = NULL;
  47301. /* Set client order opposite to server order:
  47302. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  47303. clientHello[csOff + 0] = TLS13_BYTE;
  47304. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  47305. clientHello[csOff + 2] = TLS13_BYTE;
  47306. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  47307. /* Test server order negotiation. */
  47308. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47309. msg.buffer = clientHello;
  47310. msg.length = (unsigned int)sizeof(clientHello);
  47311. wolfSSL_SetIOReadCtx(ssl, &msg);
  47312. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47313. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47314. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  47315. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  47316. /* Check refined order - server order. */
  47317. ExpectIntEQ(ssl->suites->suiteSz, 4);
  47318. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47319. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47320. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47321. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47322. wolfSSL_free(ssl);
  47323. ssl = NULL;
  47324. /* Test client order negotiation. */
  47325. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47326. msg.buffer = clientHello;
  47327. msg.length = (unsigned int)sizeof(clientHello);
  47328. wolfSSL_SetIOReadCtx(ssl, &msg);
  47329. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  47330. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  47331. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  47332. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  47333. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  47334. /* Check refined order - client order. */
  47335. ExpectIntEQ(ssl->suites->suiteSz, 4);
  47336. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  47337. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47338. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  47339. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47340. wolfSSL_free(ssl);
  47341. ssl = NULL;
  47342. /* Check duplicate detection is working. */
  47343. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  47344. ExpectIntEQ(ctx->suites->suiteSz, 4);
  47345. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47346. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  47347. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47348. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  47349. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  47350. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  47351. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  47352. ExpectIntEQ(ctx->suites->suiteSz, 4);
  47353. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  47354. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  47355. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  47356. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  47357. #endif
  47358. wolfSSL_CTX_free(ctx);
  47359. #endif
  47360. return EXPECT_RESULT();
  47361. }
  47362. #endif
  47363. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  47364. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  47365. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47366. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  47367. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  47368. const unsigned char* priv, unsigned int privSz,
  47369. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  47370. unsigned char* out, unsigned int* outlen,
  47371. void* ctx)
  47372. {
  47373. int result;
  47374. /* Test fail when context associated with WOLFSSL is NULL */
  47375. if (ctx == NULL) {
  47376. return -1;
  47377. }
  47378. (void)ssl;
  47379. /* return 0 on success */
  47380. PRIVATE_KEY_UNLOCK();
  47381. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  47382. PRIVATE_KEY_LOCK();
  47383. return result;
  47384. }
  47385. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  47386. EXPECT_DECLS;
  47387. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  47388. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  47389. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  47390. WOLFSSL_SUCCESS);
  47391. #endif
  47392. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  47393. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  47394. WOLFSSL_SUCCESS);
  47395. #endif
  47396. return EXPECT_RESULT();
  47397. }
  47398. static int test_dh_ssl_setup(WOLFSSL* ssl)
  47399. {
  47400. EXPECT_DECLS;
  47401. static int dh_test_ctx = 1;
  47402. int ret;
  47403. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  47404. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  47405. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47406. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47407. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47408. }
  47409. return EXPECT_RESULT();
  47410. }
  47411. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  47412. {
  47413. EXPECT_DECLS;
  47414. int ret;
  47415. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  47416. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  47417. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  47418. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  47419. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47420. }
  47421. return EXPECT_RESULT();
  47422. }
  47423. #endif
  47424. static int test_DhCallbacks(void)
  47425. {
  47426. EXPECT_DECLS;
  47427. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  47428. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  47429. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  47430. WOLFSSL_CTX *ctx = NULL;
  47431. WOLFSSL *ssl = NULL;
  47432. int test;
  47433. test_ssl_cbf func_cb_client;
  47434. test_ssl_cbf func_cb_server;
  47435. /* Test that DH callback APIs work. */
  47436. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47437. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  47438. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  47439. /* load client ca cert */
  47440. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  47441. WOLFSSL_SUCCESS);
  47442. /* test with NULL arguments */
  47443. wolfSSL_SetDhAgreeCtx(NULL, &test);
  47444. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  47445. /* test success case */
  47446. test = 1;
  47447. ExpectNotNull(ssl = wolfSSL_new(ctx));
  47448. wolfSSL_SetDhAgreeCtx(ssl, &test);
  47449. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  47450. wolfSSL_free(ssl);
  47451. wolfSSL_CTX_free(ctx);
  47452. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47453. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47454. /* set callbacks to use DH functions */
  47455. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47456. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  47457. func_cb_client.method = wolfTLSv1_2_client_method;
  47458. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47459. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  47460. func_cb_server.method = wolfTLSv1_2_server_method;
  47461. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47462. &func_cb_server, NULL), TEST_SUCCESS);
  47463. /* Test fail */
  47464. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  47465. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  47466. /* set callbacks to use DH functions */
  47467. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  47468. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  47469. func_cb_client.method = wolfTLSv1_2_client_method;
  47470. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  47471. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  47472. func_cb_server.method = wolfTLSv1_2_server_method;
  47473. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  47474. &func_cb_server, NULL), TEST_FAIL);
  47475. #endif
  47476. return EXPECT_RESULT();
  47477. }
  47478. #endif /* HAVE_PK_CALLBACKS */
  47479. #ifdef HAVE_HASHDRBG
  47480. #ifdef TEST_RESEED_INTERVAL
  47481. static int test_wc_RNG_GenerateBlock_Reseed(void)
  47482. {
  47483. EXPECT_DECLS;
  47484. int i;
  47485. WC_RNG rng;
  47486. byte key[32];
  47487. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47488. ExpectIntEQ(wc_InitRng(&rng), 0);
  47489. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  47490. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47491. }
  47492. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47493. return EXPECT_RESULT();
  47494. }
  47495. #endif /* TEST_RESEED_INTERVAL */
  47496. static int test_wc_RNG_GenerateBlock(void)
  47497. {
  47498. EXPECT_DECLS;
  47499. int i;
  47500. WC_RNG rng;
  47501. byte key[32];
  47502. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47503. ExpectIntEQ(wc_InitRng(&rng), 0);
  47504. for (i = 0; i < 10; i++) {
  47505. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  47506. }
  47507. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47508. return EXPECT_RESULT();
  47509. }
  47510. #endif /* HAVE_HASHDRBG */
  47511. /*
  47512. * Testing get_rand_digit
  47513. */
  47514. static int test_get_rand_digit(void)
  47515. {
  47516. EXPECT_DECLS;
  47517. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  47518. WC_RNG rng;
  47519. mp_digit d;
  47520. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47521. ExpectIntEQ(wc_InitRng(&rng), 0);
  47522. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  47523. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  47524. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  47525. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  47526. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47527. #endif
  47528. return EXPECT_RESULT();
  47529. } /* End test_get_rand_digit*/
  47530. /*
  47531. * Testing get_digit_count
  47532. */
  47533. static int test_get_digit_count(void)
  47534. {
  47535. EXPECT_DECLS;
  47536. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  47537. mp_int a;
  47538. XMEMSET(&a, 0, sizeof(mp_int));
  47539. ExpectIntEQ(mp_init(&a), 0);
  47540. ExpectIntEQ(get_digit_count(NULL), 0);
  47541. ExpectIntEQ(get_digit_count(&a), 0);
  47542. mp_clear(&a);
  47543. #endif
  47544. return EXPECT_RESULT();
  47545. } /* End test_get_digit_count*/
  47546. /*
  47547. * Testing mp_cond_copy
  47548. */
  47549. static int test_mp_cond_copy(void)
  47550. {
  47551. EXPECT_DECLS;
  47552. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  47553. defined(WOLFSSL_PUBLIC_MP)
  47554. mp_int a;
  47555. mp_int b;
  47556. int copy = 0;
  47557. XMEMSET(&a, 0, sizeof(mp_int));
  47558. XMEMSET(&b, 0, sizeof(mp_int));
  47559. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47560. ExpectIntEQ(mp_init(&b), MP_OKAY);
  47561. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  47562. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  47563. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  47564. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  47565. mp_clear(&a);
  47566. mp_clear(&b);
  47567. #endif
  47568. return EXPECT_RESULT();
  47569. } /* End test_mp_cond_copy*/
  47570. /*
  47571. * Testing mp_rand
  47572. */
  47573. static int test_mp_rand(void)
  47574. {
  47575. EXPECT_DECLS;
  47576. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  47577. mp_int a;
  47578. WC_RNG rng;
  47579. int digits = 1;
  47580. XMEMSET(&a, 0, sizeof(mp_int));
  47581. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47582. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47583. ExpectIntEQ(wc_InitRng(&rng), 0);
  47584. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  47585. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  47586. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  47587. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  47588. mp_clear(&a);
  47589. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  47590. #endif
  47591. return EXPECT_RESULT();
  47592. } /* End test_mp_rand*/
  47593. /*
  47594. * Testing get_digit
  47595. */
  47596. static int test_get_digit(void)
  47597. {
  47598. EXPECT_DECLS;
  47599. #if defined(WOLFSSL_PUBLIC_MP)
  47600. mp_int a;
  47601. int n = 0;
  47602. XMEMSET(&a, 0, sizeof(mp_int));
  47603. ExpectIntEQ(mp_init(&a), MP_OKAY);
  47604. ExpectIntEQ(get_digit(NULL, n), 0);
  47605. ExpectIntEQ(get_digit(&a, n), 0);
  47606. mp_clear(&a);
  47607. #endif
  47608. return EXPECT_RESULT();
  47609. } /* End test_get_digit*/
  47610. /*
  47611. * Testing wc_export_int
  47612. */
  47613. static int test_wc_export_int(void)
  47614. {
  47615. EXPECT_DECLS;
  47616. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  47617. defined(WOLFSSL_PUBLIC_MP)
  47618. mp_int mp;
  47619. byte buf[32];
  47620. word32 keySz = (word32)sizeof(buf);
  47621. word32 len = (word32)sizeof(buf);
  47622. XMEMSET(&mp, 0, sizeof(mp_int));
  47623. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  47624. ExpectIntEQ(mp_set(&mp, 1234), 0);
  47625. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47626. BAD_FUNC_ARG);
  47627. len = sizeof(buf)-1;
  47628. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  47629. BUFFER_E);
  47630. len = sizeof(buf);
  47631. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  47632. len = 4; /* test input too small */
  47633. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  47634. len = sizeof(buf);
  47635. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  47636. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  47637. ExpectIntEQ(len, 5);
  47638. mp_clear(&mp);
  47639. #endif
  47640. return EXPECT_RESULT();
  47641. } /* End test_wc_export_int*/
  47642. static int test_wc_InitRngNonce(void)
  47643. {
  47644. EXPECT_DECLS;
  47645. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47646. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  47647. HAVE_FIPS_VERSION >= 2))
  47648. WC_RNG rng;
  47649. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47650. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47651. word32 nonceSz = sizeof(nonce);
  47652. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  47653. ExpectIntEQ(wc_FreeRng(&rng), 0);
  47654. #endif
  47655. return EXPECT_RESULT();
  47656. } /* End test_wc_InitRngNonce*/
  47657. /*
  47658. * Testing wc_InitRngNonce_ex
  47659. */
  47660. static int test_wc_InitRngNonce_ex(void)
  47661. {
  47662. EXPECT_DECLS;
  47663. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  47664. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  47665. HAVE_FIPS_VERSION >= 2))
  47666. WC_RNG rng;
  47667. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  47668. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  47669. word32 nonceSz = sizeof(nonce);
  47670. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  47671. 0);
  47672. ExpectIntEQ(wc_FreeRng(&rng), 0);
  47673. #endif
  47674. return EXPECT_RESULT();
  47675. } /* End test_wc_InitRngNonce_ex */
  47676. static int test_wolfSSL_X509_CRL(void)
  47677. {
  47678. EXPECT_DECLS;
  47679. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  47680. X509_CRL *crl = NULL;
  47681. char pem[][100] = {
  47682. "./certs/crl/crl.pem",
  47683. "./certs/crl/crl2.pem",
  47684. "./certs/crl/caEccCrl.pem",
  47685. "./certs/crl/eccCliCRL.pem",
  47686. "./certs/crl/eccSrvCRL.pem",
  47687. ""
  47688. };
  47689. #ifndef NO_BIO
  47690. BIO *bio = NULL;
  47691. #endif
  47692. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47693. char der[][100] = {
  47694. "./certs/crl/crl.der",
  47695. "./certs/crl/crl2.der",
  47696. ""};
  47697. #endif
  47698. XFILE fp = XBADFILE;
  47699. int i;
  47700. for (i = 0; pem[i][0] != '\0'; i++)
  47701. {
  47702. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  47703. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47704. NULL, NULL));
  47705. ExpectNotNull(crl);
  47706. X509_CRL_free(crl);
  47707. if (fp != XBADFILE) {
  47708. XFCLOSE(fp);
  47709. fp = XBADFILE;
  47710. }
  47711. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  47712. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  47713. NULL));
  47714. if (EXPECT_FAIL()) {
  47715. crl = NULL;
  47716. }
  47717. ExpectNotNull(crl);
  47718. X509_CRL_free(crl);
  47719. crl = NULL;
  47720. if (fp != XBADFILE) {
  47721. XFCLOSE(fp);
  47722. fp = XBADFILE;
  47723. }
  47724. }
  47725. #ifndef NO_BIO
  47726. for (i = 0; pem[i][0] != '\0'; i++)
  47727. {
  47728. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  47729. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  47730. X509_CRL_free(crl);
  47731. crl = NULL;
  47732. BIO_free(bio);
  47733. bio = NULL;
  47734. }
  47735. #endif
  47736. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  47737. for (i = 0; der[i][0] != '\0'; i++) {
  47738. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  47739. ExpectTrue((fp != XBADFILE));
  47740. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  47741. ExpectNotNull(crl);
  47742. X509_CRL_free(crl);
  47743. if (fp != XBADFILE) {
  47744. XFCLOSE(fp);
  47745. fp = XBADFILE;
  47746. }
  47747. fp = XFOPEN(der[i], "rb");
  47748. ExpectTrue((fp != XBADFILE));
  47749. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  47750. if (EXPECT_FAIL()) {
  47751. crl = NULL;
  47752. }
  47753. ExpectNotNull(crl);
  47754. X509_CRL_free(crl);
  47755. crl = NULL;
  47756. if (fp != XBADFILE) {
  47757. XFCLOSE(fp);
  47758. fp = XBADFILE;
  47759. }
  47760. }
  47761. #endif
  47762. #endif
  47763. return EXPECT_RESULT();
  47764. }
  47765. static int test_wolfSSL_X509_load_crl_file(void)
  47766. {
  47767. EXPECT_DECLS;
  47768. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  47769. !defined(NO_RSA) && !defined(NO_BIO)
  47770. int i;
  47771. char pem[][100] = {
  47772. "./certs/crl/crl.pem",
  47773. "./certs/crl/crl2.pem",
  47774. "./certs/crl/caEccCrl.pem",
  47775. "./certs/crl/eccCliCRL.pem",
  47776. "./certs/crl/eccSrvCRL.pem",
  47777. ""
  47778. };
  47779. char der[][100] = {
  47780. "./certs/crl/crl.der",
  47781. "./certs/crl/crl2.der",
  47782. ""
  47783. };
  47784. WOLFSSL_X509_STORE* store = NULL;
  47785. WOLFSSL_X509_LOOKUP* lookup = NULL;
  47786. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  47787. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47788. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47789. X509_FILETYPE_PEM), 1);
  47790. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47791. X509_FILETYPE_PEM), 1);
  47792. if (store) {
  47793. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47794. WOLFSSL_FILETYPE_PEM), 1);
  47795. /* since store hasn't yet known the revoked cert*/
  47796. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47797. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  47798. }
  47799. for (i = 0; pem[i][0] != '\0'; i++) {
  47800. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  47801. 1);
  47802. }
  47803. if (store) {
  47804. /* since store knows crl list */
  47805. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47806. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  47807. CRL_CERT_REVOKED);
  47808. }
  47809. /* once feeing store */
  47810. X509_STORE_free(store);
  47811. store = NULL;
  47812. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  47813. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  47814. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  47815. X509_FILETYPE_PEM), 1);
  47816. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  47817. X509_FILETYPE_PEM), 1);
  47818. if (store) {
  47819. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  47820. WOLFSSL_FILETYPE_PEM), 1);
  47821. /* since store hasn't yet known the revoked cert*/
  47822. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47823. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  47824. }
  47825. for (i = 0; der[i][0] != '\0'; i++) {
  47826. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  47827. 1);
  47828. }
  47829. if (store) {
  47830. /* since store knows crl list */
  47831. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  47832. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  47833. CRL_CERT_REVOKED);
  47834. }
  47835. /* test for incorrect parameter */
  47836. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  47837. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  47838. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  47839. X509_STORE_free(store);
  47840. store = NULL;
  47841. #endif
  47842. return EXPECT_RESULT();
  47843. }
  47844. static int test_wolfSSL_i2d_X509(void)
  47845. {
  47846. EXPECT_DECLS;
  47847. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  47848. const unsigned char* cert_buf = server_cert_der_2048;
  47849. unsigned char* out = NULL;
  47850. unsigned char* tmp = NULL;
  47851. X509* cert = NULL;
  47852. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  47853. /* Pointer should be advanced */
  47854. ExpectPtrGT(cert_buf, server_cert_der_2048);
  47855. ExpectIntGT(i2d_X509(cert, &out), 0);
  47856. ExpectNotNull(out);
  47857. tmp = out;
  47858. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  47859. ExpectPtrGT(tmp, out);
  47860. if (out != NULL)
  47861. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  47862. X509_free(cert);
  47863. #endif
  47864. return EXPECT_RESULT();
  47865. }
  47866. static int test_wolfSSL_d2i_X509_REQ(void)
  47867. {
  47868. EXPECT_DECLS;
  47869. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  47870. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  47871. !defined(WOLFSSL_SP_MATH)
  47872. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  47873. * were generated by libest
  47874. * ./certs/csr.attr.der contains sample attributes
  47875. * ./certs/csr.ext.der contains sample extensions */
  47876. const char* csrFile = "./certs/csr.signed.der";
  47877. const char* csrPopFile = "./certs/csr.attr.der";
  47878. const char* csrExtFile = "./certs/csr.ext.der";
  47879. /* ./certs/csr.dsa.pem is generated using
  47880. * openssl req -newkey dsa:certs/dsaparams.pem \
  47881. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  47882. * -outform PEM
  47883. * with the passphrase "wolfSSL"
  47884. */
  47885. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47886. const char* csrDsaFile = "./certs/csr.dsa.pem";
  47887. XFILE f = XBADFILE;
  47888. #endif
  47889. BIO* bio = NULL;
  47890. X509* req = NULL;
  47891. EVP_PKEY *pub_key = NULL;
  47892. {
  47893. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  47894. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47895. /*
  47896. * Extract the public key from the CSR
  47897. */
  47898. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47899. /*
  47900. * Verify the signature in the CSR
  47901. */
  47902. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47903. X509_free(req);
  47904. req = NULL;
  47905. BIO_free(bio);
  47906. bio = NULL;
  47907. EVP_PKEY_free(pub_key);
  47908. pub_key = NULL;
  47909. }
  47910. {
  47911. #ifdef OPENSSL_ALL
  47912. X509_ATTRIBUTE* attr;
  47913. ASN1_TYPE *at;
  47914. #endif
  47915. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  47916. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47917. /*
  47918. * Extract the public key from the CSR
  47919. */
  47920. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47921. /*
  47922. * Verify the signature in the CSR
  47923. */
  47924. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47925. #ifdef OPENSSL_ALL
  47926. /*
  47927. * Obtain the challenge password from the CSR
  47928. */
  47929. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  47930. -1), 1);
  47931. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  47932. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  47933. ExpectNotNull(at->value.asn1_string);
  47934. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  47935. "2xIE+qqp/rhyTXP+");
  47936. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  47937. #endif
  47938. X509_free(req);
  47939. req = NULL;
  47940. BIO_free(bio);
  47941. bio = NULL;
  47942. EVP_PKEY_free(pub_key);
  47943. pub_key = NULL;
  47944. }
  47945. {
  47946. #ifdef OPENSSL_ALL
  47947. X509_ATTRIBUTE* attr;
  47948. ASN1_TYPE *at;
  47949. STACK_OF(X509_EXTENSION) *exts = NULL;
  47950. #endif
  47951. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  47952. /* This CSR contains an Extension Request attribute so
  47953. * we test extension parsing in a CSR attribute here. */
  47954. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  47955. /*
  47956. * Extract the public key from the CSR
  47957. */
  47958. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47959. /*
  47960. * Verify the signature in the CSR
  47961. */
  47962. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47963. #ifdef OPENSSL_ALL
  47964. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  47965. req));
  47966. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  47967. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  47968. /*
  47969. * Obtain the challenge password from the CSR
  47970. */
  47971. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  47972. -1), 0);
  47973. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  47974. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  47975. ExpectNotNull(at->value.asn1_string);
  47976. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  47977. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  47978. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  47979. #endif
  47980. X509_free(req);
  47981. req = NULL;
  47982. BIO_free(bio);
  47983. bio = NULL;
  47984. EVP_PKEY_free(pub_key);
  47985. pub_key = NULL;
  47986. }
  47987. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  47988. {
  47989. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  47990. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  47991. /*
  47992. * Extract the public key from the CSR
  47993. */
  47994. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  47995. /*
  47996. * Verify the signature in the CSR
  47997. */
  47998. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  47999. X509_free(req);
  48000. req = NULL;
  48001. BIO_free(bio);
  48002. /* Run the same test, but with a file pointer instead of a BIO.
  48003. * (PEM_read_X509_REQ)*/
  48004. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  48005. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  48006. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48007. X509_free(req);
  48008. EVP_PKEY_free(pub_key);
  48009. }
  48010. #endif /* !NO_DSA && !HAVE_SELFTEST */
  48011. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  48012. return EXPECT_RESULT();
  48013. }
  48014. static int test_wolfSSL_PEM_read_X509(void)
  48015. {
  48016. EXPECT_DECLS;
  48017. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48018. !defined(NO_RSA)
  48019. X509 *x509 = NULL;
  48020. XFILE fp = XBADFILE;
  48021. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  48022. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  48023. X509_free(x509);
  48024. if (fp != XBADFILE)
  48025. XFCLOSE(fp);
  48026. #endif
  48027. return EXPECT_RESULT();
  48028. }
  48029. static int test_wolfSSL_PEM_read(void)
  48030. {
  48031. EXPECT_DECLS;
  48032. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  48033. const char* filename = "./certs/server-keyEnc.pem";
  48034. XFILE fp = XBADFILE;
  48035. char* name = NULL;
  48036. char* header = NULL;
  48037. byte* data = NULL;
  48038. long len;
  48039. EVP_CIPHER_INFO cipher;
  48040. WOLFSSL_BIO* bio = NULL;
  48041. byte* fileData = NULL;
  48042. size_t fileDataSz = 0;
  48043. byte* out;
  48044. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  48045. /* Fail cases. */
  48046. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  48047. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  48048. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  48049. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  48050. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48051. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48052. ExpectIntGT(XSTRLEN(header), 0);
  48053. ExpectIntGT(len, 0);
  48054. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48055. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  48056. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48057. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48058. DYNAMIC_TYPE_TMP_BUFFER));
  48059. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48060. if (fp != XBADFILE) {
  48061. XFCLOSE(fp);
  48062. fp = XBADFILE;
  48063. }
  48064. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48065. /* Fail cases. */
  48066. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  48067. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  48068. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  48069. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  48070. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48071. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48072. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48073. /* Fail cases. */
  48074. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  48075. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  48076. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  48077. #ifndef NO_DES3
  48078. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  48079. #endif
  48080. /* Fail cases. */
  48081. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  48082. (void*)"yassl123"), WOLFSSL_FAILURE);
  48083. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  48084. (void*)"yassl123"), WOLFSSL_FAILURE);
  48085. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  48086. (void*)"yassl123"), WOLFSSL_FAILURE);
  48087. #if !defined(NO_DES3) && !defined(NO_MD5)
  48088. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  48089. (void*)"yassl123"), WOLFSSL_SUCCESS);
  48090. #endif
  48091. BIO_free(bio);
  48092. bio = NULL;
  48093. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48094. fileData = NULL;
  48095. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48096. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48097. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48098. name = NULL;
  48099. header = NULL;
  48100. data = NULL;
  48101. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  48102. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  48103. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  48104. ExpectIntEQ(XSTRLEN(header), 0);
  48105. ExpectIntGT(len, 0);
  48106. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  48107. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  48108. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  48109. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  48110. DYNAMIC_TYPE_TMP_BUFFER));
  48111. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  48112. if (fp != XBADFILE)
  48113. XFCLOSE(fp);
  48114. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48115. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  48116. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  48117. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  48118. BIO_free(bio);
  48119. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48120. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48121. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48122. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48123. #endif
  48124. return EXPECT_RESULT();
  48125. }
  48126. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  48127. {
  48128. EXPECT_DECLS;
  48129. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48130. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48131. const byte iv[12] = { 0 };
  48132. const byte key[16] = { 0 };
  48133. const byte cleartext[16] = { 0 };
  48134. const byte aad[] = {
  48135. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  48136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  48137. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  48138. 0x4f
  48139. };
  48140. byte out1Part[16];
  48141. byte outTag1Part[16];
  48142. byte out2Part[16];
  48143. byte outTag2Part[16];
  48144. byte decryptBuf[16];
  48145. int len = 0;
  48146. int tlen;
  48147. EVP_CIPHER_CTX* ctx = NULL;
  48148. /* ENCRYPT */
  48149. /* Send AAD and data in 1 part */
  48150. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48151. tlen = 0;
  48152. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48153. 1);
  48154. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48155. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48156. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  48157. sizeof(cleartext)), 1);
  48158. tlen += len;
  48159. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  48160. tlen += len;
  48161. ExpectIntEQ(tlen, sizeof(cleartext));
  48162. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48163. outTag1Part), 1);
  48164. EVP_CIPHER_CTX_free(ctx);
  48165. ctx = NULL;
  48166. /* DECRYPT */
  48167. /* Send AAD and data in 1 part */
  48168. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48169. tlen = 0;
  48170. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48171. 1);
  48172. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48173. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  48174. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  48175. sizeof(cleartext)), 1);
  48176. tlen += len;
  48177. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48178. outTag1Part), 1);
  48179. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  48180. tlen += len;
  48181. ExpectIntEQ(tlen, sizeof(cleartext));
  48182. EVP_CIPHER_CTX_free(ctx);
  48183. ctx = NULL;
  48184. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48185. /* ENCRYPT */
  48186. /* Send AAD and data in 2 parts */
  48187. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48188. tlen = 0;
  48189. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48190. 1);
  48191. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48192. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48193. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48194. 1);
  48195. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  48196. tlen += len;
  48197. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  48198. sizeof(cleartext) - 1), 1);
  48199. tlen += len;
  48200. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  48201. tlen += len;
  48202. ExpectIntEQ(tlen, sizeof(cleartext));
  48203. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  48204. outTag2Part), 1);
  48205. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  48206. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  48207. EVP_CIPHER_CTX_free(ctx);
  48208. ctx = NULL;
  48209. /* DECRYPT */
  48210. /* Send AAD and data in 2 parts */
  48211. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  48212. tlen = 0;
  48213. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  48214. 1);
  48215. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  48216. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  48217. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  48218. 1);
  48219. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  48220. tlen += len;
  48221. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  48222. sizeof(cleartext) - 1), 1);
  48223. tlen += len;
  48224. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  48225. outTag1Part), 1);
  48226. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  48227. tlen += len;
  48228. ExpectIntEQ(tlen, sizeof(cleartext));
  48229. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  48230. /* Test AAD reuse */
  48231. EVP_CIPHER_CTX_free(ctx);
  48232. #endif
  48233. return EXPECT_RESULT();
  48234. }
  48235. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  48236. {
  48237. EXPECT_DECLS;
  48238. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48239. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48240. /* Zero length plain text */
  48241. byte key[] = {
  48242. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48243. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48244. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48245. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48246. }; /* align */
  48247. byte iv[] = {
  48248. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48249. }; /* align */
  48250. byte plaintxt[1];
  48251. int ivSz = 12;
  48252. int plaintxtSz = 0;
  48253. unsigned char tag[16];
  48254. unsigned char tag_kat[] = {
  48255. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  48256. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  48257. };
  48258. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48259. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48260. int ciphertxtSz = 0;
  48261. int decryptedtxtSz = 0;
  48262. int len = 0;
  48263. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48264. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48265. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  48266. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48267. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48268. plaintxtSz));
  48269. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48270. ciphertxtSz += len;
  48271. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  48272. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48273. ExpectIntEQ(0, ciphertxtSz);
  48274. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  48275. EVP_CIPHER_CTX_init(de);
  48276. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  48277. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48278. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48279. decryptedtxtSz = len;
  48280. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  48281. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48282. decryptedtxtSz += len;
  48283. ExpectIntEQ(0, decryptedtxtSz);
  48284. EVP_CIPHER_CTX_free(en);
  48285. EVP_CIPHER_CTX_free(de);
  48286. #endif
  48287. return EXPECT_RESULT();
  48288. }
  48289. static int test_wolfssl_EVP_aes_gcm(void)
  48290. {
  48291. EXPECT_DECLS;
  48292. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  48293. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48294. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48295. byte *key = (byte*)"01234567890123456789012345678901";
  48296. /* A 128 bit IV */
  48297. byte *iv = (byte*)"0123456789012345";
  48298. int ivSz = AES_BLOCK_SIZE;
  48299. /* Message to be encrypted */
  48300. byte *plaintxt = (byte*)"for things to change you have to change";
  48301. /* Additional non-confidential data */
  48302. byte *aad = (byte*)"Don't spend major time on minor things.";
  48303. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48304. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48305. int aadSz = (int)XSTRLEN((char*)aad);
  48306. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48307. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48308. int ciphertxtSz = 0;
  48309. int decryptedtxtSz = 0;
  48310. int len = 0;
  48311. int i = 0;
  48312. EVP_CIPHER_CTX en[2];
  48313. EVP_CIPHER_CTX de[2];
  48314. for (i = 0; i < 2; i++) {
  48315. EVP_CIPHER_CTX_init(&en[i]);
  48316. if (i == 0) {
  48317. /* Default uses 96-bits IV length */
  48318. #ifdef WOLFSSL_AES_128
  48319. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  48320. key, iv));
  48321. #elif defined(WOLFSSL_AES_192)
  48322. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  48323. key, iv));
  48324. #elif defined(WOLFSSL_AES_256)
  48325. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  48326. key, iv));
  48327. #endif
  48328. }
  48329. else {
  48330. #ifdef WOLFSSL_AES_128
  48331. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  48332. NULL, NULL));
  48333. #elif defined(WOLFSSL_AES_192)
  48334. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  48335. NULL, NULL));
  48336. #elif defined(WOLFSSL_AES_256)
  48337. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  48338. NULL, NULL));
  48339. #endif
  48340. /* non-default must to set the IV length first */
  48341. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  48342. ivSz, NULL));
  48343. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48344. }
  48345. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48346. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48347. plaintxtSz));
  48348. ciphertxtSz = len;
  48349. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48350. ciphertxtSz += len;
  48351. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  48352. AES_BLOCK_SIZE, tag));
  48353. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48354. EVP_CIPHER_CTX_init(&de[i]);
  48355. if (i == 0) {
  48356. /* Default uses 96-bits IV length */
  48357. #ifdef WOLFSSL_AES_128
  48358. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48359. key, iv));
  48360. #elif defined(WOLFSSL_AES_192)
  48361. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48362. key, iv));
  48363. #elif defined(WOLFSSL_AES_256)
  48364. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48365. key, iv));
  48366. #endif
  48367. }
  48368. else {
  48369. #ifdef WOLFSSL_AES_128
  48370. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48371. NULL, NULL));
  48372. #elif defined(WOLFSSL_AES_192)
  48373. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48374. NULL, NULL));
  48375. #elif defined(WOLFSSL_AES_256)
  48376. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48377. NULL, NULL));
  48378. #endif
  48379. /* non-default must to set the IV length first */
  48380. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48381. ivSz, NULL));
  48382. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48383. }
  48384. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48385. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48386. ciphertxtSz));
  48387. decryptedtxtSz = len;
  48388. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48389. AES_BLOCK_SIZE, tag));
  48390. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48391. decryptedtxtSz += len;
  48392. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48393. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48394. /* modify tag*/
  48395. if (i == 0) {
  48396. /* Default uses 96-bits IV length */
  48397. #ifdef WOLFSSL_AES_128
  48398. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48399. key, iv));
  48400. #elif defined(WOLFSSL_AES_192)
  48401. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48402. key, iv));
  48403. #elif defined(WOLFSSL_AES_256)
  48404. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48405. key, iv));
  48406. #endif
  48407. }
  48408. else {
  48409. #ifdef WOLFSSL_AES_128
  48410. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  48411. NULL, NULL));
  48412. #elif defined(WOLFSSL_AES_192)
  48413. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  48414. NULL, NULL));
  48415. #elif defined(WOLFSSL_AES_256)
  48416. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  48417. NULL, NULL));
  48418. #endif
  48419. /* non-default must to set the IV length first */
  48420. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  48421. ivSz, NULL));
  48422. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48423. }
  48424. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48425. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48426. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  48427. AES_BLOCK_SIZE, tag));
  48428. /* fail due to wrong tag */
  48429. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48430. ciphertxtSz));
  48431. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48432. ExpectIntEQ(0, len);
  48433. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48434. }
  48435. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48436. return EXPECT_RESULT();
  48437. }
  48438. static int test_wolfssl_EVP_aria_gcm(void)
  48439. {
  48440. int res = TEST_SKIPPED;
  48441. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  48442. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48443. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48444. byte *key = (byte*)"01234567890123456789012345678901";
  48445. /* A 128 bit IV */
  48446. byte *iv = (byte*)"0123456789012345";
  48447. int ivSz = ARIA_BLOCK_SIZE;
  48448. /* Message to be encrypted */
  48449. const int plaintxtSz = 40;
  48450. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  48451. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  48452. /* Additional non-confidential data */
  48453. byte *aad = (byte*)"Don't spend major time on minor things.";
  48454. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  48455. int aadSz = (int)XSTRLEN((char*)aad);
  48456. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  48457. byte decryptedtxt[plaintxtSz];
  48458. int ciphertxtSz = 0;
  48459. int decryptedtxtSz = 0;
  48460. int len = 0;
  48461. int i = 0;
  48462. #define TEST_ARIA_GCM_COUNT 6
  48463. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  48464. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  48465. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  48466. EVP_CIPHER_CTX_init(&en[i]);
  48467. switch (i) {
  48468. case 0:
  48469. /* Default uses 96-bits IV length */
  48470. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  48471. break;
  48472. case 1:
  48473. /* Default uses 96-bits IV length */
  48474. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  48475. break;
  48476. case 2:
  48477. /* Default uses 96-bits IV length */
  48478. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  48479. break;
  48480. case 3:
  48481. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48482. /* non-default must to set the IV length first */
  48483. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48484. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48485. break;
  48486. case 4:
  48487. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48488. /* non-default must to set the IV length first */
  48489. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48490. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48491. break;
  48492. case 5:
  48493. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48494. /* non-default must to set the IV length first */
  48495. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48496. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48497. break;
  48498. }
  48499. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  48500. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48501. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  48502. ciphertxtSz = len;
  48503. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48504. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  48505. ciphertxtSz += len;
  48506. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  48507. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48508. EVP_CIPHER_CTX_init(&de[i]);
  48509. switch (i) {
  48510. case 0:
  48511. /* Default uses 96-bits IV length */
  48512. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  48513. break;
  48514. case 1:
  48515. /* Default uses 96-bits IV length */
  48516. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  48517. break;
  48518. case 2:
  48519. /* Default uses 96-bits IV length */
  48520. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  48521. break;
  48522. case 3:
  48523. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  48524. /* non-default must to set the IV length first */
  48525. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48526. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48527. break;
  48528. case 4:
  48529. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  48530. /* non-default must to set the IV length first */
  48531. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48532. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48533. break;
  48534. case 5:
  48535. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  48536. /* non-default must to set the IV length first */
  48537. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  48538. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48539. break;
  48540. }
  48541. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48542. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48543. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48544. decryptedtxtSz = len;
  48545. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48546. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48547. decryptedtxtSz += len;
  48548. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  48549. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48550. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  48551. /* modify tag*/
  48552. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48553. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48554. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  48555. /* fail due to wrong tag */
  48556. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  48557. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48558. AssertIntEQ(0, len);
  48559. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48560. }
  48561. res = TEST_RES_CHECK(1);
  48562. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  48563. return res;
  48564. }
  48565. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  48566. {
  48567. EXPECT_DECLS;
  48568. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48569. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48570. /* Zero length plain text */
  48571. byte key[] = {
  48572. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48573. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48574. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48575. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48576. }; /* align */
  48577. byte iv[] = {
  48578. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  48579. }; /* align */
  48580. byte plaintxt[1];
  48581. int ivSz = 12;
  48582. int plaintxtSz = 0;
  48583. unsigned char tag[16];
  48584. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48585. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48586. int ciphertxtSz = 0;
  48587. int decryptedtxtSz = 0;
  48588. int len = 0;
  48589. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  48590. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48591. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  48592. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48593. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48594. plaintxtSz));
  48595. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48596. ciphertxtSz += len;
  48597. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48598. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48599. ExpectIntEQ(0, ciphertxtSz);
  48600. EVP_CIPHER_CTX_init(de);
  48601. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  48602. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48603. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48604. decryptedtxtSz = len;
  48605. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48606. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48607. decryptedtxtSz += len;
  48608. ExpectIntEQ(0, decryptedtxtSz);
  48609. EVP_CIPHER_CTX_free(en);
  48610. EVP_CIPHER_CTX_free(de);
  48611. #endif
  48612. return EXPECT_RESULT();
  48613. }
  48614. static int test_wolfssl_EVP_aes_ccm(void)
  48615. {
  48616. EXPECT_DECLS;
  48617. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  48618. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  48619. /* A 256 bit key, AES_128 will use the first 128 bit*/
  48620. byte *key = (byte*)"01234567890123456789012345678901";
  48621. /* A 128 bit IV */
  48622. byte *iv = (byte*)"0123456789012";
  48623. int ivSz = (int)XSTRLEN((char*)iv);
  48624. /* Message to be encrypted */
  48625. byte *plaintxt = (byte*)"for things to change you have to change";
  48626. /* Additional non-confidential data */
  48627. byte *aad = (byte*)"Don't spend major time on minor things.";
  48628. unsigned char tag[AES_BLOCK_SIZE] = {0};
  48629. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48630. int aadSz = (int)XSTRLEN((char*)aad);
  48631. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  48632. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  48633. int ciphertxtSz = 0;
  48634. int decryptedtxtSz = 0;
  48635. int len = 0;
  48636. int i = 0;
  48637. int ret;
  48638. EVP_CIPHER_CTX en[2];
  48639. EVP_CIPHER_CTX de[2];
  48640. for (i = 0; i < 2; i++) {
  48641. EVP_CIPHER_CTX_init(&en[i]);
  48642. if (i == 0) {
  48643. /* Default uses 96-bits IV length */
  48644. #ifdef WOLFSSL_AES_128
  48645. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  48646. key, iv));
  48647. #elif defined(WOLFSSL_AES_192)
  48648. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  48649. key, iv));
  48650. #elif defined(WOLFSSL_AES_256)
  48651. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  48652. key, iv));
  48653. #endif
  48654. }
  48655. else {
  48656. #ifdef WOLFSSL_AES_128
  48657. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  48658. NULL, NULL));
  48659. #elif defined(WOLFSSL_AES_192)
  48660. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  48661. NULL, NULL));
  48662. #elif defined(WOLFSSL_AES_256)
  48663. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  48664. NULL, NULL));
  48665. #endif
  48666. /* non-default must to set the IV length first */
  48667. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  48668. ivSz, NULL));
  48669. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48670. }
  48671. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48672. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48673. plaintxtSz));
  48674. ciphertxtSz = len;
  48675. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48676. ciphertxtSz += len;
  48677. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  48678. AES_BLOCK_SIZE, tag));
  48679. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  48680. ExpectIntEQ(ret, 1);
  48681. EVP_CIPHER_CTX_init(&de[i]);
  48682. if (i == 0) {
  48683. /* Default uses 96-bits IV length */
  48684. #ifdef WOLFSSL_AES_128
  48685. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  48686. key, iv));
  48687. #elif defined(WOLFSSL_AES_192)
  48688. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  48689. key, iv));
  48690. #elif defined(WOLFSSL_AES_256)
  48691. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  48692. key, iv));
  48693. #endif
  48694. }
  48695. else {
  48696. #ifdef WOLFSSL_AES_128
  48697. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  48698. NULL, NULL));
  48699. #elif defined(WOLFSSL_AES_192)
  48700. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  48701. NULL, NULL));
  48702. #elif defined(WOLFSSL_AES_256)
  48703. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  48704. NULL, NULL));
  48705. #endif
  48706. /* non-default must to set the IV length first */
  48707. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  48708. ivSz, NULL));
  48709. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48710. }
  48711. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48712. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48713. ciphertxtSz));
  48714. decryptedtxtSz = len;
  48715. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48716. AES_BLOCK_SIZE, tag));
  48717. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48718. decryptedtxtSz += len;
  48719. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48720. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48721. /* modify tag*/
  48722. tag[AES_BLOCK_SIZE-1]+=0xBB;
  48723. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48724. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48725. AES_BLOCK_SIZE, tag));
  48726. /* fail due to wrong tag */
  48727. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48728. ciphertxtSz));
  48729. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48730. ExpectIntEQ(0, len);
  48731. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  48732. ExpectIntEQ(ret, 1);
  48733. }
  48734. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  48735. return EXPECT_RESULT();
  48736. }
  48737. static int test_wolfssl_EVP_chacha20_poly1305(void)
  48738. {
  48739. EXPECT_DECLS;
  48740. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  48741. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  48742. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  48743. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48744. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  48745. byte cipherText[sizeof(plainText)];
  48746. byte decryptedText[sizeof(plainText)];
  48747. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  48748. EVP_CIPHER_CTX* ctx = NULL;
  48749. int outSz;
  48750. /* Encrypt. */
  48751. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48752. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48753. NULL), WOLFSSL_SUCCESS);
  48754. /* Invalid IV length. */
  48755. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48756. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  48757. /* Valid IV length. */
  48758. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48759. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48760. /* Invalid tag length. */
  48761. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48762. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  48763. /* Valid tag length. */
  48764. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48765. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  48766. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48767. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48768. WOLFSSL_SUCCESS);
  48769. ExpectIntEQ(outSz, sizeof(aad));
  48770. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48771. sizeof(plainText)), WOLFSSL_SUCCESS);
  48772. ExpectIntEQ(outSz, sizeof(plainText));
  48773. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48774. ExpectIntEQ(outSz, 0);
  48775. /* Invalid tag length. */
  48776. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48777. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  48778. /* Valid tag length. */
  48779. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  48780. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48781. EVP_CIPHER_CTX_free(ctx);
  48782. ctx = NULL;
  48783. /* Decrypt. */
  48784. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48785. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  48786. NULL), WOLFSSL_SUCCESS);
  48787. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  48788. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  48789. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48790. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  48791. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48792. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  48793. WOLFSSL_SUCCESS);
  48794. ExpectIntEQ(outSz, sizeof(aad));
  48795. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48796. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48797. ExpectIntEQ(outSz, sizeof(cipherText));
  48798. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48799. WOLFSSL_SUCCESS);
  48800. ExpectIntEQ(outSz, 0);
  48801. EVP_CIPHER_CTX_free(ctx);
  48802. ctx = NULL;
  48803. /* Test partial Inits. CipherInit() allow setting of key and iv
  48804. * in separate calls. */
  48805. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48806. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  48807. key, NULL, 1), WOLFSSL_SUCCESS);
  48808. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48809. WOLFSSL_SUCCESS);
  48810. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  48811. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  48812. ExpectIntEQ(outSz, sizeof(aad));
  48813. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48814. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48815. ExpectIntEQ(outSz, sizeof(cipherText));
  48816. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48817. WOLFSSL_SUCCESS);
  48818. ExpectIntEQ(outSz, 0);
  48819. EVP_CIPHER_CTX_free(ctx);
  48820. #endif
  48821. return EXPECT_RESULT();
  48822. }
  48823. static int test_wolfssl_EVP_chacha20(void)
  48824. {
  48825. EXPECT_DECLS;
  48826. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  48827. byte key[CHACHA_MAX_KEY_SZ];
  48828. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  48829. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  48830. byte cipherText[sizeof(plainText)];
  48831. byte decryptedText[sizeof(plainText)];
  48832. EVP_CIPHER_CTX* ctx = NULL;
  48833. int outSz;
  48834. XMEMSET(key, 0, sizeof(key));
  48835. XMEMSET(iv, 0, sizeof(iv));
  48836. /* Encrypt. */
  48837. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48838. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48839. NULL), WOLFSSL_SUCCESS);
  48840. /* Any tag length must fail - not an AEAD cipher. */
  48841. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  48842. 16, NULL), WOLFSSL_FAILURE);
  48843. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48844. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48845. sizeof(plainText)), WOLFSSL_SUCCESS);
  48846. ExpectIntEQ(outSz, sizeof(plainText));
  48847. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  48848. ExpectIntEQ(outSz, 0);
  48849. EVP_CIPHER_CTX_free(ctx);
  48850. ctx = NULL;
  48851. /* Decrypt. */
  48852. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48853. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  48854. NULL), WOLFSSL_SUCCESS);
  48855. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48856. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48857. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48858. ExpectIntEQ(outSz, sizeof(cipherText));
  48859. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48860. WOLFSSL_SUCCESS);
  48861. ExpectIntEQ(outSz, 0);
  48862. EVP_CIPHER_CTX_free(ctx);
  48863. ctx = NULL;
  48864. /* Test partial Inits. CipherInit() allow setting of key and iv
  48865. * in separate calls. */
  48866. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48867. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  48868. key, NULL, 1), WOLFSSL_SUCCESS);
  48869. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  48870. WOLFSSL_SUCCESS);
  48871. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48872. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48873. ExpectIntEQ(outSz, sizeof(cipherText));
  48874. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  48875. WOLFSSL_SUCCESS);
  48876. ExpectIntEQ(outSz, 0);
  48877. EVP_CIPHER_CTX_free(ctx);
  48878. #endif
  48879. return EXPECT_RESULT();
  48880. }
  48881. static int test_wolfssl_EVP_sm4_ecb(void)
  48882. {
  48883. int res = TEST_SKIPPED;
  48884. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  48885. EXPECT_DECLS;
  48886. byte key[SM4_KEY_SIZE];
  48887. byte plainText[SM4_BLOCK_SIZE] = {
  48888. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  48889. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  48890. };
  48891. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48892. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48893. EVP_CIPHER_CTX* ctx;
  48894. int outSz;
  48895. XMEMSET(key, 0, sizeof(key));
  48896. /* Encrypt. */
  48897. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48898. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  48899. WOLFSSL_SUCCESS);
  48900. /* Any tag length must fail - not an AEAD cipher. */
  48901. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48902. WOLFSSL_FAILURE);
  48903. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  48904. WOLFSSL_SUCCESS);
  48905. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48906. sizeof(plainText)), WOLFSSL_SUCCESS);
  48907. ExpectIntEQ(outSz, sizeof(plainText));
  48908. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  48909. WOLFSSL_SUCCESS);
  48910. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  48911. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48912. EVP_CIPHER_CTX_free(ctx);
  48913. /* Decrypt. */
  48914. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48915. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  48916. WOLFSSL_SUCCESS);
  48917. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  48918. WOLFSSL_SUCCESS);
  48919. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48920. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48921. ExpectIntEQ(outSz, sizeof(plainText));
  48922. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48923. WOLFSSL_SUCCESS);
  48924. ExpectIntEQ(outSz, 0);
  48925. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48926. EVP_CIPHER_CTX_free(ctx);
  48927. res = EXPECT_RESULT();
  48928. #endif
  48929. return res;
  48930. }
  48931. static int test_wolfssl_EVP_sm4_cbc(void)
  48932. {
  48933. int res = TEST_SKIPPED;
  48934. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  48935. EXPECT_DECLS;
  48936. byte key[SM4_KEY_SIZE];
  48937. byte iv[SM4_BLOCK_SIZE];
  48938. byte plainText[SM4_BLOCK_SIZE] = {
  48939. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  48940. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  48941. };
  48942. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48943. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  48944. EVP_CIPHER_CTX* ctx;
  48945. int outSz;
  48946. XMEMSET(key, 0, sizeof(key));
  48947. XMEMSET(iv, 0, sizeof(iv));
  48948. /* Encrypt. */
  48949. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48950. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  48951. WOLFSSL_SUCCESS);
  48952. /* Any tag length must fail - not an AEAD cipher. */
  48953. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  48954. WOLFSSL_FAILURE);
  48955. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48956. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  48957. sizeof(plainText)), WOLFSSL_SUCCESS);
  48958. ExpectIntEQ(outSz, sizeof(plainText));
  48959. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  48960. WOLFSSL_SUCCESS);
  48961. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  48962. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  48963. EVP_CIPHER_CTX_free(ctx);
  48964. /* Decrypt. */
  48965. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48966. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  48967. WOLFSSL_SUCCESS);
  48968. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  48969. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48970. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48971. ExpectIntEQ(outSz, sizeof(plainText));
  48972. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48973. WOLFSSL_SUCCESS);
  48974. ExpectIntEQ(outSz, 0);
  48975. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48976. EVP_CIPHER_CTX_free(ctx);
  48977. /* Test partial Inits. CipherInit() allow setting of key and iv
  48978. * in separate calls. */
  48979. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  48980. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  48981. WOLFSSL_SUCCESS);
  48982. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  48983. WOLFSSL_SUCCESS);
  48984. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  48985. sizeof(cipherText)), WOLFSSL_SUCCESS);
  48986. ExpectIntEQ(outSz, sizeof(plainText));
  48987. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  48988. WOLFSSL_SUCCESS);
  48989. ExpectIntEQ(outSz, 0);
  48990. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  48991. EVP_CIPHER_CTX_free(ctx);
  48992. res = EXPECT_RESULT();
  48993. #endif
  48994. return res;
  48995. }
  48996. static int test_wolfssl_EVP_sm4_ctr(void)
  48997. {
  48998. int res = TEST_SKIPPED;
  48999. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  49000. EXPECT_DECLS;
  49001. byte key[SM4_KEY_SIZE];
  49002. byte iv[SM4_BLOCK_SIZE];
  49003. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49004. byte cipherText[sizeof(plainText)];
  49005. byte decryptedText[sizeof(plainText)];
  49006. EVP_CIPHER_CTX* ctx;
  49007. int outSz;
  49008. XMEMSET(key, 0, sizeof(key));
  49009. XMEMSET(iv, 0, sizeof(iv));
  49010. /* Encrypt. */
  49011. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49012. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49013. WOLFSSL_SUCCESS);
  49014. /* Any tag length must fail - not an AEAD cipher. */
  49015. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49016. WOLFSSL_FAILURE);
  49017. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49018. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49019. sizeof(plainText)), WOLFSSL_SUCCESS);
  49020. ExpectIntEQ(outSz, sizeof(plainText));
  49021. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49022. ExpectIntEQ(outSz, 0);
  49023. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49024. EVP_CIPHER_CTX_free(ctx);
  49025. /* Decrypt. */
  49026. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49027. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49028. WOLFSSL_SUCCESS);
  49029. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49030. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49031. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49032. ExpectIntEQ(outSz, sizeof(cipherText));
  49033. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49034. WOLFSSL_SUCCESS);
  49035. ExpectIntEQ(outSz, 0);
  49036. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49037. EVP_CIPHER_CTX_free(ctx);
  49038. /* Test partial Inits. CipherInit() allow setting of key and iv
  49039. * in separate calls. */
  49040. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49041. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  49042. WOLFSSL_SUCCESS);
  49043. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49044. WOLFSSL_SUCCESS);
  49045. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49046. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49047. ExpectIntEQ(outSz, sizeof(cipherText));
  49048. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49049. WOLFSSL_SUCCESS);
  49050. ExpectIntEQ(outSz, 0);
  49051. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49052. EVP_CIPHER_CTX_free(ctx);
  49053. res = EXPECT_RESULT();
  49054. #endif
  49055. return res;
  49056. }
  49057. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  49058. {
  49059. int res = TEST_SKIPPED;
  49060. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  49061. /* Zero length plain text */
  49062. EXPECT_DECLS;
  49063. byte key[] = {
  49064. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49065. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49066. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49067. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49068. }; /* align */
  49069. byte iv[] = {
  49070. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49071. }; /* align */
  49072. byte plaintxt[1];
  49073. int ivSz = 12;
  49074. int plaintxtSz = 0;
  49075. unsigned char tag[16];
  49076. unsigned char tag_kat[16] = {
  49077. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  49078. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  49079. };
  49080. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49081. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49082. int ciphertxtSz = 0;
  49083. int decryptedtxtSz = 0;
  49084. int len = 0;
  49085. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49086. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49087. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  49088. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49089. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49090. plaintxtSz));
  49091. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49092. ciphertxtSz += len;
  49093. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49094. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49095. ExpectIntEQ(0, ciphertxtSz);
  49096. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49097. EVP_CIPHER_CTX_init(de);
  49098. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  49099. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49100. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49101. decryptedtxtSz = len;
  49102. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49103. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49104. decryptedtxtSz += len;
  49105. ExpectIntEQ(0, decryptedtxtSz);
  49106. EVP_CIPHER_CTX_free(en);
  49107. EVP_CIPHER_CTX_free(de);
  49108. res = EXPECT_RESULT();
  49109. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  49110. return res;
  49111. }
  49112. static int test_wolfssl_EVP_sm4_gcm(void)
  49113. {
  49114. int res = TEST_SKIPPED;
  49115. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  49116. EXPECT_DECLS;
  49117. byte *key = (byte*)"0123456789012345";
  49118. /* A 128 bit IV */
  49119. byte *iv = (byte*)"0123456789012345";
  49120. int ivSz = SM4_BLOCK_SIZE;
  49121. /* Message to be encrypted */
  49122. byte *plaintxt = (byte*)"for things to change you have to change";
  49123. /* Additional non-confidential data */
  49124. byte *aad = (byte*)"Don't spend major time on minor things.";
  49125. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  49126. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49127. int aadSz = (int)XSTRLEN((char*)aad);
  49128. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49129. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49130. int ciphertxtSz = 0;
  49131. int decryptedtxtSz = 0;
  49132. int len = 0;
  49133. int i = 0;
  49134. EVP_CIPHER_CTX en[2];
  49135. EVP_CIPHER_CTX de[2];
  49136. for (i = 0; i < 2; i++) {
  49137. EVP_CIPHER_CTX_init(&en[i]);
  49138. if (i == 0) {
  49139. /* Default uses 96-bits IV length */
  49140. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  49141. iv));
  49142. }
  49143. else {
  49144. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  49145. NULL));
  49146. /* non-default must to set the IV length first */
  49147. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  49148. ivSz, NULL));
  49149. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49150. }
  49151. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49152. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49153. plaintxtSz));
  49154. ciphertxtSz = len;
  49155. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49156. ciphertxtSz += len;
  49157. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  49158. SM4_BLOCK_SIZE, tag));
  49159. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49160. EVP_CIPHER_CTX_init(&de[i]);
  49161. if (i == 0) {
  49162. /* Default uses 96-bits IV length */
  49163. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  49164. iv));
  49165. }
  49166. else {
  49167. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  49168. NULL));
  49169. /* non-default must to set the IV length first */
  49170. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  49171. ivSz, NULL));
  49172. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49173. }
  49174. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49175. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49176. ciphertxtSz));
  49177. decryptedtxtSz = len;
  49178. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49179. SM4_BLOCK_SIZE, tag));
  49180. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49181. decryptedtxtSz += len;
  49182. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49183. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49184. /* modify tag*/
  49185. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  49186. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49187. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49188. SM4_BLOCK_SIZE, tag));
  49189. /* fail due to wrong tag */
  49190. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49191. ciphertxtSz));
  49192. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49193. ExpectIntEQ(0, len);
  49194. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49195. }
  49196. res = EXPECT_RESULT();
  49197. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  49198. return res;
  49199. }
  49200. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  49201. {
  49202. int res = TEST_SKIPPED;
  49203. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  49204. /* Zero length plain text */
  49205. EXPECT_DECLS;
  49206. byte key[] = {
  49207. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49208. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49209. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49210. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49211. }; /* align */
  49212. byte iv[] = {
  49213. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49214. }; /* align */
  49215. byte plaintxt[1];
  49216. int ivSz = 12;
  49217. int plaintxtSz = 0;
  49218. unsigned char tag[16];
  49219. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49220. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49221. int ciphertxtSz = 0;
  49222. int decryptedtxtSz = 0;
  49223. int len = 0;
  49224. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49225. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49226. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  49227. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49228. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49229. plaintxtSz));
  49230. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49231. ciphertxtSz += len;
  49232. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  49233. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49234. ExpectIntEQ(0, ciphertxtSz);
  49235. EVP_CIPHER_CTX_init(de);
  49236. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  49237. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49238. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49239. decryptedtxtSz = len;
  49240. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  49241. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49242. decryptedtxtSz += len;
  49243. ExpectIntEQ(0, decryptedtxtSz);
  49244. EVP_CIPHER_CTX_free(en);
  49245. EVP_CIPHER_CTX_free(de);
  49246. res = EXPECT_RESULT();
  49247. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  49248. return res;
  49249. }
  49250. static int test_wolfssl_EVP_sm4_ccm(void)
  49251. {
  49252. int res = TEST_SKIPPED;
  49253. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  49254. EXPECT_DECLS;
  49255. byte *key = (byte*)"0123456789012345";
  49256. byte *iv = (byte*)"0123456789012";
  49257. int ivSz = (int)XSTRLEN((char*)iv);
  49258. /* Message to be encrypted */
  49259. byte *plaintxt = (byte*)"for things to change you have to change";
  49260. /* Additional non-confidential data */
  49261. byte *aad = (byte*)"Don't spend major time on minor things.";
  49262. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  49263. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49264. int aadSz = (int)XSTRLEN((char*)aad);
  49265. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  49266. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  49267. int ciphertxtSz = 0;
  49268. int decryptedtxtSz = 0;
  49269. int len = 0;
  49270. int i = 0;
  49271. EVP_CIPHER_CTX en[2];
  49272. EVP_CIPHER_CTX de[2];
  49273. for (i = 0; i < 2; i++) {
  49274. EVP_CIPHER_CTX_init(&en[i]);
  49275. if (i == 0) {
  49276. /* Default uses 96-bits IV length */
  49277. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  49278. iv));
  49279. }
  49280. else {
  49281. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  49282. NULL));
  49283. /* non-default must to set the IV length first */
  49284. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  49285. ivSz, NULL));
  49286. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49287. }
  49288. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49289. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49290. plaintxtSz));
  49291. ciphertxtSz = len;
  49292. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49293. ciphertxtSz += len;
  49294. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  49295. SM4_BLOCK_SIZE, tag));
  49296. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49297. EVP_CIPHER_CTX_init(&de[i]);
  49298. if (i == 0) {
  49299. /* Default uses 96-bits IV length */
  49300. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  49301. iv));
  49302. }
  49303. else {
  49304. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  49305. NULL));
  49306. /* non-default must to set the IV length first */
  49307. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  49308. ivSz, NULL));
  49309. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49310. }
  49311. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49312. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49313. ciphertxtSz));
  49314. decryptedtxtSz = len;
  49315. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49316. SM4_BLOCK_SIZE, tag));
  49317. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49318. decryptedtxtSz += len;
  49319. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49320. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49321. /* modify tag*/
  49322. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  49323. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49324. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49325. SM4_BLOCK_SIZE, tag));
  49326. /* fail due to wrong tag */
  49327. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49328. ciphertxtSz));
  49329. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49330. ExpectIntEQ(0, len);
  49331. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49332. }
  49333. res = EXPECT_RESULT();
  49334. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  49335. return res;
  49336. }
  49337. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  49338. {
  49339. EXPECT_DECLS;
  49340. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  49341. EVP_PKEY_CTX* ctx = NULL;
  49342. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  49343. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  49344. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  49345. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  49346. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  49347. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  49348. byte outKey[34];
  49349. size_t outKeySz = sizeof(outKey);
  49350. /* These expected outputs were gathered by running the same test below using
  49351. * OpenSSL. */
  49352. const byte extractAndExpand[] = {
  49353. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  49354. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  49355. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  49356. };
  49357. const byte extractOnly[] = {
  49358. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  49359. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  49360. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  49361. };
  49362. const byte expandOnly[] = {
  49363. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  49364. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  49365. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  49366. };
  49367. const byte extractAndExpandAddInfo[] = {
  49368. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  49369. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  49370. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  49371. };
  49372. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  49373. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  49374. /* NULL ctx. */
  49375. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  49376. /* NULL md. */
  49377. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  49378. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  49379. /* NULL ctx. */
  49380. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  49381. WOLFSSL_FAILURE);
  49382. /* NULL salt is ok. */
  49383. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  49384. WOLFSSL_SUCCESS);
  49385. /* Salt length <= 0. */
  49386. /* Length 0 salt is ok. */
  49387. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  49388. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  49389. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  49390. WOLFSSL_SUCCESS);
  49391. /* NULL ctx. */
  49392. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  49393. WOLFSSL_FAILURE);
  49394. /* NULL key. */
  49395. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  49396. WOLFSSL_FAILURE);
  49397. /* Key length <= 0 */
  49398. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  49399. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  49400. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  49401. WOLFSSL_SUCCESS);
  49402. /* NULL ctx. */
  49403. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  49404. WOLFSSL_FAILURE);
  49405. /* NULL info is ok. */
  49406. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  49407. WOLFSSL_SUCCESS);
  49408. /* Info length <= 0 */
  49409. /* Length 0 info is ok. */
  49410. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  49411. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  49412. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  49413. WOLFSSL_SUCCESS);
  49414. /* NULL ctx. */
  49415. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49416. WOLFSSL_FAILURE);
  49417. /* Extract and expand (default). */
  49418. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49419. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  49420. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  49421. /* Extract only. */
  49422. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  49423. WOLFSSL_SUCCESS);
  49424. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49425. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  49426. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  49427. outKeySz = sizeof(outKey);
  49428. /* Expand only. */
  49429. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  49430. WOLFSSL_SUCCESS);
  49431. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49432. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  49433. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  49434. outKeySz = sizeof(outKey);
  49435. /* Extract and expand with appended additional info. */
  49436. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  49437. WOLFSSL_SUCCESS);
  49438. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  49439. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  49440. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  49441. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  49442. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  49443. EVP_PKEY_CTX_free(ctx);
  49444. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  49445. return EXPECT_RESULT();
  49446. }
  49447. #ifndef NO_BIO
  49448. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  49449. {
  49450. EXPECT_DECLS;
  49451. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49452. BIO* bio = NULL;
  49453. X509_INFO* info = NULL;
  49454. STACK_OF(X509_INFO)* sk = NULL;
  49455. char* subject = NULL;
  49456. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  49457. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49458. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  49459. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  49460. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  49461. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  49462. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  49463. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  49464. /* using dereference to maintain testing for Apache port*/
  49465. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  49466. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  49467. 0, 0));
  49468. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  49469. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49470. X509_INFO_free(info);
  49471. info = NULL;
  49472. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  49473. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  49474. 0, 0));
  49475. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  49476. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  49477. X509_INFO_free(info);
  49478. ExpectNull(info = sk_X509_INFO_pop(sk));
  49479. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  49480. BIO_free(bio);
  49481. #endif
  49482. return EXPECT_RESULT();
  49483. }
  49484. #endif /* !NO_BIO */
  49485. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  49486. {
  49487. EXPECT_DECLS;
  49488. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49489. X509 *x509 = NULL;
  49490. X509_NAME* name = NULL;
  49491. int idx = 0;
  49492. X509_NAME_ENTRY *ne = NULL;
  49493. ASN1_OBJECT *object = NULL;
  49494. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  49495. WOLFSSL_FILETYPE_PEM));
  49496. ExpectNotNull(name = X509_get_subject_name(x509));
  49497. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  49498. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  49499. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  49500. X509_free(x509);
  49501. #endif
  49502. return EXPECT_RESULT();
  49503. }
  49504. static int test_wolfSSL_X509_STORE_get1_certs(void)
  49505. {
  49506. EXPECT_DECLS;
  49507. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  49508. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49509. X509_STORE_CTX *storeCtx = NULL;
  49510. X509_STORE *store = NULL;
  49511. X509 *caX509 = NULL;
  49512. X509 *svrX509 = NULL;
  49513. X509_NAME *subject = NULL;
  49514. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  49515. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  49516. SSL_FILETYPE_PEM));
  49517. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49518. SSL_FILETYPE_PEM)));
  49519. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  49520. ExpectNotNull(store = X509_STORE_new());
  49521. ExpectNotNull(subject = X509_get_subject_name(caX509));
  49522. /* Errors */
  49523. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  49524. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  49525. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  49526. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  49527. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  49528. SSL_SUCCESS);
  49529. /* Should find the cert */
  49530. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49531. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  49532. sk_X509_pop_free(certs, NULL);
  49533. certs = NULL;
  49534. /* Should not find the cert */
  49535. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  49536. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  49537. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  49538. sk_X509_pop_free(certs, NULL);
  49539. certs = NULL;
  49540. X509_STORE_free(store);
  49541. X509_STORE_CTX_free(storeCtx);
  49542. X509_free(svrX509);
  49543. X509_free(caX509);
  49544. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  49545. return EXPECT_RESULT();
  49546. }
  49547. static int test_wolfSSL_dup_CA_list(void)
  49548. {
  49549. int res = TEST_SKIPPED;
  49550. #if defined(OPENSSL_ALL)
  49551. EXPECT_DECLS;
  49552. STACK_OF(X509_NAME) *originalStack = NULL;
  49553. STACK_OF(X509_NAME) *copyStack = NULL;
  49554. int originalCount = 0;
  49555. int copyCount = 0;
  49556. X509_NAME *name = NULL;
  49557. int i;
  49558. originalStack = sk_X509_NAME_new_null();
  49559. ExpectNotNull(originalStack);
  49560. for (i = 0; i < 3; i++) {
  49561. name = X509_NAME_new();
  49562. ExpectNotNull(name);
  49563. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  49564. }
  49565. copyStack = SSL_dup_CA_list(originalStack);
  49566. ExpectNotNull(copyStack);
  49567. originalCount = sk_X509_NAME_num(originalStack);
  49568. copyCount = sk_X509_NAME_num(copyStack);
  49569. AssertIntEQ(originalCount, copyCount);
  49570. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  49571. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  49572. originalStack = NULL;
  49573. copyStack = NULL;
  49574. res = EXPECT_RESULT();
  49575. #endif /* OPENSSL_ALL */
  49576. return res;
  49577. }
  49578. static int test_ForceZero(void)
  49579. {
  49580. EXPECT_DECLS;
  49581. unsigned char data[32];
  49582. unsigned int i, j, len;
  49583. /* Test case with 0 length */
  49584. ForceZero(data, 0);
  49585. /* Test ForceZero */
  49586. for (i = 0; i < sizeof(data); i++) {
  49587. for (len = 1; len < sizeof(data) - i; len++) {
  49588. for (j = 0; j < sizeof(data); j++)
  49589. data[j] = j + 1;
  49590. ForceZero(data + i, len);
  49591. for (j = 0; j < sizeof(data); j++) {
  49592. if (j < i || j >= i + len) {
  49593. ExpectIntNE(data[j], 0x00);
  49594. }
  49595. else {
  49596. ExpectIntEQ(data[j], 0x00);
  49597. }
  49598. }
  49599. }
  49600. }
  49601. return EXPECT_RESULT();
  49602. }
  49603. #ifndef NO_BIO
  49604. static int test_wolfSSL_X509_print(void)
  49605. {
  49606. EXPECT_DECLS;
  49607. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49608. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  49609. X509 *x509 = NULL;
  49610. BIO *bio = NULL;
  49611. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49612. const X509_ALGOR *cert_sig_alg;
  49613. #endif
  49614. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  49615. WOLFSSL_FILETYPE_PEM));
  49616. /* print to memory */
  49617. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49618. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49619. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  49620. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  49621. /* Will print IP address subject alt name. */
  49622. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  49623. #elif defined(NO_ASN_TIME)
  49624. /* Will print IP address subject alt name but not Validity. */
  49625. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  49626. #else
  49627. /* Will print IP address subject alt name. */
  49628. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  49629. #endif
  49630. #elif defined(NO_ASN_TIME)
  49631. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  49632. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  49633. #else
  49634. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  49635. #endif
  49636. BIO_free(bio);
  49637. bio = NULL;
  49638. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49639. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  49640. /* Print signature */
  49641. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  49642. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  49643. #endif
  49644. /* print to stderr */
  49645. #if !defined(NO_WOLFSSL_DIR)
  49646. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  49647. #endif
  49648. /* print again */
  49649. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  49650. X509_free(x509);
  49651. BIO_free(bio);
  49652. #endif
  49653. return EXPECT_RESULT();
  49654. }
  49655. static int test_wolfSSL_X509_CRL_print(void)
  49656. {
  49657. EXPECT_DECLS;
  49658. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  49659. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  49660. X509_CRL* crl = NULL;
  49661. BIO *bio = NULL;
  49662. XFILE fp = XBADFILE;
  49663. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  49664. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49665. NULL, NULL));
  49666. if (fp != XBADFILE)
  49667. XFCLOSE(fp);
  49668. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49669. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  49670. X509_CRL_free(crl);
  49671. BIO_free(bio);
  49672. #endif
  49673. return EXPECT_RESULT();
  49674. }
  49675. static int test_wolfSSL_BIO_get_len(void)
  49676. {
  49677. EXPECT_DECLS;
  49678. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49679. BIO *bio = NULL;
  49680. const char txt[] = "Some example text to push to the BIO.";
  49681. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  49682. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49683. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  49684. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  49685. BIO_free(bio);
  49686. bio = NULL;
  49687. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49688. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  49689. BIO_free(bio);
  49690. #endif
  49691. return EXPECT_RESULT();
  49692. }
  49693. #endif /* !NO_BIO */
  49694. static int test_wolfSSL_RSA(void)
  49695. {
  49696. EXPECT_DECLS;
  49697. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  49698. defined(WOLFSSL_KEY_GEN)
  49699. RSA* rsa = NULL;
  49700. const BIGNUM *n;
  49701. const BIGNUM *e;
  49702. const BIGNUM *d;
  49703. const BIGNUM *p;
  49704. const BIGNUM *q;
  49705. const BIGNUM *dmp1;
  49706. const BIGNUM *dmq1;
  49707. const BIGNUM *iqmp;
  49708. ExpectNotNull(rsa = RSA_new());
  49709. ExpectIntEQ(RSA_size(NULL), 0);
  49710. ExpectIntEQ(RSA_size(rsa), 0);
  49711. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  49712. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  49713. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  49714. #ifdef WOLFSSL_RSA_KEY_CHECK
  49715. ExpectIntEQ(RSA_check_key(rsa), 0);
  49716. #endif
  49717. RSA_free(rsa);
  49718. rsa = NULL;
  49719. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49720. ExpectIntEQ(RSA_size(rsa), 256);
  49721. #ifdef WOLFSSL_RSA_KEY_CHECK
  49722. ExpectIntEQ(RSA_check_key(NULL), 0);
  49723. ExpectIntEQ(RSA_check_key(rsa), 1);
  49724. #endif
  49725. /* sanity check */
  49726. ExpectIntEQ(RSA_bits(NULL), 0);
  49727. /* key */
  49728. ExpectIntEQ(RSA_bits(rsa), 2048);
  49729. RSA_get0_key(rsa, &n, &e, &d);
  49730. ExpectPtrEq(rsa->n, n);
  49731. ExpectPtrEq(rsa->e, e);
  49732. ExpectPtrEq(rsa->d, d);
  49733. n = NULL;
  49734. e = NULL;
  49735. d = NULL;
  49736. ExpectNotNull(n = BN_new());
  49737. ExpectNotNull(e = BN_new());
  49738. ExpectNotNull(d = BN_new());
  49739. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  49740. if (EXPECT_FAIL()) {
  49741. BN_free((BIGNUM*)n);
  49742. BN_free((BIGNUM*)e);
  49743. BN_free((BIGNUM*)d);
  49744. }
  49745. ExpectPtrEq(rsa->n, n);
  49746. ExpectPtrEq(rsa->e, e);
  49747. ExpectPtrEq(rsa->d, d);
  49748. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  49749. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  49750. /* crt_params */
  49751. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  49752. ExpectPtrEq(rsa->dmp1, dmp1);
  49753. ExpectPtrEq(rsa->dmq1, dmq1);
  49754. ExpectPtrEq(rsa->iqmp, iqmp);
  49755. dmp1 = NULL;
  49756. dmq1 = NULL;
  49757. iqmp = NULL;
  49758. ExpectNotNull(dmp1 = BN_new());
  49759. ExpectNotNull(dmq1 = BN_new());
  49760. ExpectNotNull(iqmp = BN_new());
  49761. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49762. (BIGNUM*)iqmp), 1);
  49763. if (EXPECT_FAIL()) {
  49764. BN_free((BIGNUM*)dmp1);
  49765. BN_free((BIGNUM*)dmq1);
  49766. BN_free((BIGNUM*)iqmp);
  49767. }
  49768. ExpectPtrEq(rsa->dmp1, dmp1);
  49769. ExpectPtrEq(rsa->dmq1, dmq1);
  49770. ExpectPtrEq(rsa->iqmp, iqmp);
  49771. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  49772. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  49773. (BIGNUM*)iqmp), 0);
  49774. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  49775. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  49776. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  49777. ExpectNull(dmp1);
  49778. ExpectNull(dmq1);
  49779. ExpectNull(iqmp);
  49780. /* factors */
  49781. RSA_get0_factors(rsa, NULL, NULL);
  49782. RSA_get0_factors(rsa, &p, &q);
  49783. ExpectPtrEq(rsa->p, p);
  49784. ExpectPtrEq(rsa->q, q);
  49785. p = NULL;
  49786. q = NULL;
  49787. ExpectNotNull(p = BN_new());
  49788. ExpectNotNull(q = BN_new());
  49789. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  49790. if (EXPECT_FAIL()) {
  49791. BN_free((BIGNUM*)p);
  49792. BN_free((BIGNUM*)q);
  49793. }
  49794. ExpectPtrEq(rsa->p, p);
  49795. ExpectPtrEq(rsa->q, q);
  49796. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  49797. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  49798. RSA_get0_factors(NULL, NULL, NULL);
  49799. RSA_get0_factors(NULL, &p, &q);
  49800. ExpectNull(p);
  49801. ExpectNull(q);
  49802. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  49803. ExpectIntEQ(RSA_bits(rsa), 21);
  49804. RSA_free(rsa);
  49805. rsa = NULL;
  49806. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  49807. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  49808. ExpectIntEQ(RSA_size(rsa), 384);
  49809. ExpectIntEQ(RSA_bits(rsa), 3072);
  49810. RSA_free(rsa);
  49811. rsa = NULL;
  49812. #endif
  49813. /* remove for now with odd key size until adjusting rsa key size check with
  49814. wc_MakeRsaKey()
  49815. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  49816. RSA_free(rsa);
  49817. rsa = NULL;
  49818. */
  49819. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  49820. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  49821. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  49822. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  49823. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  49824. {
  49825. byte buff[FOURK_BUF];
  49826. byte der[FOURK_BUF];
  49827. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  49828. XFILE f = XBADFILE;
  49829. int bytes;
  49830. /* test loading encrypted RSA private pem w/o password */
  49831. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  49832. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  49833. if (f != XBADFILE)
  49834. XFCLOSE(f);
  49835. XMEMSET(der, 0, sizeof(der));
  49836. /* test that error value is returned with no password */
  49837. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  49838. 0);
  49839. }
  49840. #endif
  49841. #endif
  49842. return EXPECT_RESULT();
  49843. }
  49844. static int test_wolfSSL_RSA_DER(void)
  49845. {
  49846. EXPECT_DECLS;
  49847. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49848. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  49849. RSA *rsa = NULL;
  49850. int i;
  49851. const unsigned char *buff = NULL;
  49852. unsigned char *newBuff = NULL;
  49853. struct tbl_s
  49854. {
  49855. const unsigned char *der;
  49856. int sz;
  49857. } tbl[] = {
  49858. #ifdef USE_CERT_BUFFERS_1024
  49859. {client_key_der_1024, sizeof_client_key_der_1024},
  49860. {server_key_der_1024, sizeof_server_key_der_1024},
  49861. #endif
  49862. #ifdef USE_CERT_BUFFERS_2048
  49863. {client_key_der_2048, sizeof_client_key_der_2048},
  49864. {server_key_der_2048, sizeof_server_key_der_2048},
  49865. #endif
  49866. {NULL, 0}
  49867. };
  49868. /* Public Key DER */
  49869. struct tbl_s pub[] = {
  49870. #ifdef USE_CERT_BUFFERS_1024
  49871. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  49872. #endif
  49873. #ifdef USE_CERT_BUFFERS_2048
  49874. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  49875. #endif
  49876. {NULL, 0}
  49877. };
  49878. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  49879. buff = pub[0].der;
  49880. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  49881. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  49882. buff = tbl[0].der;
  49883. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  49884. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  49885. rsa = RSA_new();
  49886. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  49887. RSA_free(rsa);
  49888. rsa = NULL;
  49889. for (i = 0; tbl[i].der != NULL; i++)
  49890. {
  49891. /* Passing in pointer results in pointer moving. */
  49892. buff = tbl[i].der;
  49893. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  49894. ExpectNotNull(rsa);
  49895. RSA_free(rsa);
  49896. rsa = NULL;
  49897. }
  49898. for (i = 0; tbl[i].der != NULL; i++)
  49899. {
  49900. /* Passing in pointer results in pointer moving. */
  49901. buff = tbl[i].der;
  49902. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  49903. ExpectNotNull(rsa);
  49904. RSA_free(rsa);
  49905. rsa = NULL;
  49906. }
  49907. for (i = 0; pub[i].der != NULL; i++)
  49908. {
  49909. buff = pub[i].der;
  49910. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  49911. ExpectNotNull(rsa);
  49912. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  49913. newBuff = NULL;
  49914. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  49915. ExpectNotNull(newBuff);
  49916. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  49917. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49918. RSA_free(rsa);
  49919. rsa = NULL;
  49920. }
  49921. #endif
  49922. return EXPECT_RESULT();
  49923. }
  49924. static int test_wolfSSL_RSA_print(void)
  49925. {
  49926. EXPECT_DECLS;
  49927. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49928. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49929. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  49930. BIO *bio = NULL;
  49931. WOLFSSL_RSA* rsa = NULL;
  49932. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  49933. ExpectNotNull(rsa = RSA_new());
  49934. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  49935. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  49936. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  49937. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  49938. /* Some very large number of indent spaces. */
  49939. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  49940. /* RSA is empty. */
  49941. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  49942. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  49943. RSA_free(rsa);
  49944. rsa = NULL;
  49945. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  49946. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  49947. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  49948. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  49949. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  49950. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  49951. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  49952. BIO_free(bio);
  49953. RSA_free(rsa);
  49954. #endif
  49955. return EXPECT_RESULT();
  49956. }
  49957. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  49958. {
  49959. EXPECT_DECLS;
  49960. #ifndef NO_RSA
  49961. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  49962. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49963. RSA *rsa = NULL;
  49964. const unsigned char *derBuf = client_key_der_2048;
  49965. unsigned char em[256] = {0}; /* len = 2048/8 */
  49966. /* Random data simulating a hash */
  49967. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  49968. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  49969. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  49970. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  49971. };
  49972. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  49973. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  49974. RSA_PSS_SALTLEN_DIGEST), 0);
  49975. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  49976. RSA_PSS_SALTLEN_DIGEST), 0);
  49977. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  49978. RSA_PSS_SALTLEN_DIGEST), 0);
  49979. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  49980. RSA_PSS_SALTLEN_DIGEST), 0);
  49981. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  49982. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  49983. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49984. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  49985. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49986. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  49987. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49988. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  49989. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49990. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49991. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  49992. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  49993. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49994. RSA_PSS_SALTLEN_DIGEST), 1);
  49995. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  49996. RSA_PSS_SALTLEN_DIGEST), 1);
  49997. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  49998. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  49999. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50000. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50001. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50002. RSA_PSS_SALTLEN_MAX), 1);
  50003. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50004. RSA_PSS_SALTLEN_MAX), 1);
  50005. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  50006. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  50007. RSA_free(rsa);
  50008. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50009. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50010. #endif
  50011. return EXPECT_RESULT();
  50012. }
  50013. static int test_wolfSSL_RSA_sign_sha3(void)
  50014. {
  50015. EXPECT_DECLS;
  50016. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  50017. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50018. RSA* rsa = NULL;
  50019. const unsigned char *derBuf = client_key_der_2048;
  50020. unsigned char sigRet[256] = {0};
  50021. unsigned int sigLen = sizeof(sigRet);
  50022. /* Random data simulating a hash */
  50023. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  50024. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50025. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50026. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50027. };
  50028. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50029. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  50030. rsa), 1);
  50031. RSA_free(rsa);
  50032. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50033. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  50034. return EXPECT_RESULT();
  50035. }
  50036. static int test_wolfSSL_RSA_get0_key(void)
  50037. {
  50038. EXPECT_DECLS;
  50039. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  50040. RSA *rsa = NULL;
  50041. const BIGNUM* n = NULL;
  50042. const BIGNUM* e = NULL;
  50043. const BIGNUM* d = NULL;
  50044. const unsigned char* der;
  50045. int derSz;
  50046. #ifdef USE_CERT_BUFFERS_1024
  50047. der = client_key_der_1024;
  50048. derSz = sizeof_client_key_der_1024;
  50049. #elif defined(USE_CERT_BUFFERS_2048)
  50050. der = client_key_der_2048;
  50051. derSz = sizeof_client_key_der_2048;
  50052. #else
  50053. der = NULL;
  50054. derSz = 0;
  50055. #endif
  50056. if (der != NULL) {
  50057. RSA_get0_key(NULL, NULL, NULL, NULL);
  50058. RSA_get0_key(rsa, NULL, NULL, NULL);
  50059. RSA_get0_key(NULL, &n, &e, &d);
  50060. ExpectNull(n);
  50061. ExpectNull(e);
  50062. ExpectNull(d);
  50063. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  50064. ExpectNotNull(rsa);
  50065. RSA_get0_key(rsa, NULL, NULL, NULL);
  50066. RSA_get0_key(rsa, &n, NULL, NULL);
  50067. ExpectNotNull(n);
  50068. RSA_get0_key(rsa, NULL, &e, NULL);
  50069. ExpectNotNull(e);
  50070. RSA_get0_key(rsa, NULL, NULL, &d);
  50071. ExpectNotNull(d);
  50072. RSA_get0_key(rsa, &n, &e, &d);
  50073. ExpectNotNull(n);
  50074. ExpectNotNull(e);
  50075. ExpectNotNull(d);
  50076. RSA_free(rsa);
  50077. }
  50078. #endif
  50079. return EXPECT_RESULT();
  50080. }
  50081. static int test_wolfSSL_RSA_meth(void)
  50082. {
  50083. EXPECT_DECLS;
  50084. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50085. RSA *rsa = NULL;
  50086. RSA_METHOD *rsa_meth = NULL;
  50087. #ifdef WOLFSSL_KEY_GEN
  50088. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50089. RSA_free(rsa);
  50090. rsa = NULL;
  50091. #else
  50092. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50093. #endif
  50094. ExpectNotNull(RSA_get_default_method());
  50095. wolfSSL_RSA_meth_free(NULL);
  50096. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  50097. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  50098. RSA_METHOD_FLAG_NO_CHECK));
  50099. #ifndef NO_WOLFSSL_STUB
  50100. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  50101. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  50102. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  50103. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  50104. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  50105. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  50106. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  50107. #endif
  50108. ExpectIntEQ(RSA_flags(NULL), 0);
  50109. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  50110. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  50111. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  50112. ExpectNotNull(rsa = RSA_new());
  50113. /* No method set. */
  50114. ExpectIntEQ(RSA_flags(rsa), 0);
  50115. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50116. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50117. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50118. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  50119. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  50120. if (EXPECT_FAIL()) {
  50121. wolfSSL_RSA_meth_free(rsa_meth);
  50122. }
  50123. ExpectNull(RSA_get_method(NULL));
  50124. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  50125. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  50126. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50127. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50128. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  50129. RSA_METHOD_FLAG_NO_CHECK);
  50130. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  50131. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  50132. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  50133. /* rsa_meth is freed here */
  50134. RSA_free(rsa);
  50135. #endif
  50136. return EXPECT_RESULT();
  50137. }
  50138. static int test_wolfSSL_RSA_verify(void)
  50139. {
  50140. EXPECT_DECLS;
  50141. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  50142. !defined(NO_FILESYSTEM)
  50143. #ifndef NO_BIO
  50144. XFILE fp = XBADFILE;
  50145. RSA *pKey = NULL;
  50146. RSA *pubKey = NULL;
  50147. X509 *cert = NULL;
  50148. const char *text = "Hello wolfSSL !";
  50149. unsigned char hash[SHA256_DIGEST_LENGTH];
  50150. unsigned char signature[2048/8];
  50151. unsigned int signatureLength;
  50152. byte *buf = NULL;
  50153. BIO *bio = NULL;
  50154. SHA256_CTX c;
  50155. EVP_PKEY *evpPkey = NULL;
  50156. EVP_PKEY *evpPubkey = NULL;
  50157. size_t sz;
  50158. /* generate hash */
  50159. SHA256_Init(&c);
  50160. SHA256_Update(&c, text, strlen(text));
  50161. SHA256_Final(hash, &c);
  50162. #ifdef WOLFSSL_SMALL_STACK_CACHE
  50163. /* workaround for small stack cache case */
  50164. wc_Sha256Free((wc_Sha256*)&c);
  50165. #endif
  50166. /* read privete key file */
  50167. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  50168. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  50169. ExpectTrue((sz = XFTELL(fp)) > 0);
  50170. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  50171. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  50172. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  50173. if (fp != XBADFILE) {
  50174. XFCLOSE(fp);
  50175. fp = XBADFILE;
  50176. }
  50177. /* read private key and sign hash data */
  50178. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  50179. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  50180. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  50181. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  50182. signature, &signatureLength, pKey), SSL_SUCCESS);
  50183. /* read public key and verify signed data */
  50184. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  50185. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  50186. if (fp != XBADFILE)
  50187. XFCLOSE(fp);
  50188. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  50189. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  50190. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  50191. signatureLength, pubKey), SSL_SUCCESS);
  50192. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  50193. signatureLength, NULL), SSL_FAILURE);
  50194. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  50195. signatureLength, pubKey), SSL_FAILURE);
  50196. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  50197. signatureLength, pubKey), SSL_FAILURE);
  50198. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  50199. signatureLength, NULL), SSL_FAILURE);
  50200. RSA_free(pKey);
  50201. EVP_PKEY_free(evpPkey);
  50202. RSA_free(pubKey);
  50203. EVP_PKEY_free(evpPubkey);
  50204. X509_free(cert);
  50205. BIO_free(bio);
  50206. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  50207. #endif
  50208. #endif
  50209. return EXPECT_RESULT();
  50210. }
  50211. static int test_wolfSSL_RSA_sign(void)
  50212. {
  50213. EXPECT_DECLS;
  50214. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50215. RSA *rsa;
  50216. unsigned char hash[SHA256_DIGEST_LENGTH];
  50217. #ifdef USE_CERT_BUFFERS_1024
  50218. const unsigned char* privDer = client_key_der_1024;
  50219. size_t privDerSz = sizeof_client_key_der_1024;
  50220. const unsigned char* pubDer = client_keypub_der_1024;
  50221. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50222. unsigned char signature[1024/8];
  50223. #else
  50224. const unsigned char* privDer = client_key_der_2048;
  50225. size_t privDerSz = sizeof_client_key_der_2048;
  50226. const unsigned char* pubDer = client_keypub_der_2048;
  50227. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50228. unsigned char signature[2048/8];
  50229. #endif
  50230. unsigned int signatureLen;
  50231. const unsigned char* der;
  50232. XMEMSET(hash, 0, sizeof(hash));
  50233. der = privDer;
  50234. rsa = NULL;
  50235. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50236. /* Invalid parameters. */
  50237. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  50238. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  50239. &signatureLen, rsa), 0);
  50240. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  50241. &signatureLen, rsa), 0);
  50242. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  50243. &signatureLen, rsa), 0);
  50244. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50245. NULL, rsa), 0);
  50246. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50247. &signatureLen, NULL), 0);
  50248. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  50249. &signatureLen, rsa), 1);
  50250. RSA_free(rsa);
  50251. der = pubDer;
  50252. rsa = NULL;
  50253. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50254. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  50255. signatureLen, rsa), 1);
  50256. RSA_free(rsa);
  50257. #endif
  50258. return EXPECT_RESULT();
  50259. }
  50260. static int test_wolfSSL_RSA_sign_ex(void)
  50261. {
  50262. EXPECT_DECLS;
  50263. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50264. RSA *rsa = NULL;
  50265. unsigned char hash[SHA256_DIGEST_LENGTH];
  50266. #ifdef USE_CERT_BUFFERS_1024
  50267. const unsigned char* privDer = client_key_der_1024;
  50268. size_t privDerSz = sizeof_client_key_der_1024;
  50269. const unsigned char* pubDer = client_keypub_der_1024;
  50270. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50271. unsigned char signature[1024/8];
  50272. #else
  50273. const unsigned char* privDer = client_key_der_2048;
  50274. size_t privDerSz = sizeof_client_key_der_2048;
  50275. const unsigned char* pubDer = client_keypub_der_2048;
  50276. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50277. unsigned char signature[2048/8];
  50278. #endif
  50279. unsigned int signatureLen;
  50280. const unsigned char* der;
  50281. unsigned char encodedHash[51];
  50282. unsigned int encodedHashLen;
  50283. const unsigned char expEncHash[] = {
  50284. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  50285. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  50286. 0x00, 0x04, 0x20,
  50287. /* Hash data */
  50288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50291. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50292. };
  50293. XMEMSET(hash, 0, sizeof(hash));
  50294. ExpectNotNull(rsa = wolfSSL_RSA_new());
  50295. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50296. &signatureLen, rsa, 1), 0);
  50297. wolfSSL_RSA_free(rsa);
  50298. der = privDer;
  50299. rsa = NULL;
  50300. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50301. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  50302. -1), 0);
  50303. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  50304. signature, &signatureLen, rsa, 1), 0);
  50305. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  50306. &signatureLen, rsa, 1), 0);
  50307. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  50308. &signatureLen, rsa, 1), 0);
  50309. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50310. NULL, rsa, 1), 0);
  50311. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50312. &signatureLen, NULL, 1), 0);
  50313. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50314. &signatureLen, rsa, -1), 0);
  50315. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  50316. &signatureLen, rsa, 0), 0);
  50317. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  50318. &signatureLen, rsa, 0), 0);
  50319. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50320. NULL, rsa, 0), 0);
  50321. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  50322. &signatureLen, rsa, 1), 1);
  50323. /* Test returning encoded hash. */
  50324. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  50325. &encodedHashLen, rsa, 0), 1);
  50326. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  50327. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  50328. RSA_free(rsa);
  50329. der = pubDer;
  50330. rsa = NULL;
  50331. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50332. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  50333. signatureLen, rsa), 1);
  50334. RSA_free(rsa);
  50335. #endif
  50336. return EXPECT_RESULT();
  50337. }
  50338. static int test_wolfSSL_RSA_public_decrypt(void)
  50339. {
  50340. EXPECT_DECLS;
  50341. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50342. RSA *rsa;
  50343. unsigned char msg[SHA256_DIGEST_LENGTH];
  50344. #ifdef USE_CERT_BUFFERS_1024
  50345. const unsigned char* pubDer = client_keypub_der_1024;
  50346. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50347. unsigned char decMsg[1024/8];
  50348. const unsigned char encMsg[] = {
  50349. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50350. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50351. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50352. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50353. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50354. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50355. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50356. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50357. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50358. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50359. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50360. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50361. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50362. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50363. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50364. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50365. };
  50366. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50367. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50368. defined(WC_RSA_NO_PADDING)
  50369. const unsigned char encMsgNoPad[] = {
  50370. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50371. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50372. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50373. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50374. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50375. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50376. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50377. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50378. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50379. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50380. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50381. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50382. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50383. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50384. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50385. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50386. };
  50387. #endif
  50388. #else
  50389. const unsigned char* pubDer = client_keypub_der_2048;
  50390. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50391. unsigned char decMsg[2048/8];
  50392. const unsigned char encMsg[] = {
  50393. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50394. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50395. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50396. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50397. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50398. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50399. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50400. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50401. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50402. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50403. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50404. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50405. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50406. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50407. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50408. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50409. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50410. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50411. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50412. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50413. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50414. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50415. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50416. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50417. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50418. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50419. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50420. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50421. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50422. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50423. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50424. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50425. };
  50426. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50427. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50428. defined(WC_RSA_NO_PADDING)
  50429. const unsigned char encMsgNoPad[] = {
  50430. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50431. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50432. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50433. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50434. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50435. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50436. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50437. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50438. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50439. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50440. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50441. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50442. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50443. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50444. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50445. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50446. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50447. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50448. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50449. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50450. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50451. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50452. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50453. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50454. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50455. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50456. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50457. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50458. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50459. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50460. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50461. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50462. };
  50463. #endif
  50464. #endif
  50465. const unsigned char* der;
  50466. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50467. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50468. defined(WC_RSA_NO_PADDING)
  50469. int i;
  50470. #endif
  50471. XMEMSET(msg, 0, sizeof(msg));
  50472. der = pubDer;
  50473. rsa = NULL;
  50474. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50475. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  50476. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  50477. RSA_PKCS1_PADDING), -1);
  50478. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  50479. RSA_PKCS1_PADDING), -1);
  50480. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50481. RSA_PKCS1_PADDING), -1);
  50482. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  50483. RSA_PKCS1_PADDING), -1);
  50484. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50485. RSA_PKCS1_PSS_PADDING), -1);
  50486. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  50487. RSA_PKCS1_PADDING), 32);
  50488. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  50489. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  50490. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  50491. defined(WC_RSA_NO_PADDING)
  50492. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  50493. rsa, RSA_NO_PADDING), sizeof(decMsg));
  50494. /* Zeros before actual data. */
  50495. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  50496. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50497. }
  50498. /* Check actual data. */
  50499. XMEMSET(msg, 0x01, sizeof(msg));
  50500. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  50501. #endif
  50502. RSA_free(rsa);
  50503. #endif
  50504. return EXPECT_RESULT();
  50505. }
  50506. static int test_wolfSSL_RSA_private_encrypt(void)
  50507. {
  50508. EXPECT_DECLS;
  50509. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50510. RSA *rsa;
  50511. unsigned char msg[SHA256_DIGEST_LENGTH];
  50512. #ifdef USE_CERT_BUFFERS_1024
  50513. const unsigned char* privDer = client_key_der_1024;
  50514. size_t privDerSz = sizeof_client_key_der_1024;
  50515. unsigned char encMsg[1024/8];
  50516. const unsigned char expEncMsg[] = {
  50517. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  50518. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  50519. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  50520. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  50521. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  50522. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  50523. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  50524. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  50525. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  50526. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  50527. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  50528. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  50529. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  50530. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  50531. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  50532. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  50533. };
  50534. #ifdef WC_RSA_NO_PADDING
  50535. const unsigned char expEncMsgNoPad[] = {
  50536. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  50537. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  50538. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  50539. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  50540. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  50541. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  50542. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  50543. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  50544. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  50545. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  50546. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  50547. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  50548. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  50549. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  50550. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  50551. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  50552. };
  50553. #endif
  50554. #else
  50555. const unsigned char* privDer = client_key_der_2048;
  50556. size_t privDerSz = sizeof_client_key_der_2048;
  50557. unsigned char encMsg[2048/8];
  50558. const unsigned char expEncMsg[] = {
  50559. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  50560. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  50561. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  50562. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  50563. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  50564. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  50565. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  50566. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  50567. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  50568. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  50569. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  50570. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  50571. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  50572. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  50573. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  50574. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  50575. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  50576. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  50577. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  50578. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  50579. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  50580. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  50581. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  50582. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  50583. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  50584. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  50585. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  50586. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  50587. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  50588. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  50589. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  50590. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  50591. };
  50592. #ifdef WC_RSA_NO_PADDING
  50593. const unsigned char expEncMsgNoPad[] = {
  50594. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  50595. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  50596. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  50597. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  50598. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  50599. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  50600. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  50601. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  50602. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  50603. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  50604. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  50605. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  50606. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  50607. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  50608. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  50609. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  50610. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  50611. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  50612. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  50613. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  50614. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  50615. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  50616. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  50617. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  50618. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  50619. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  50620. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  50621. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  50622. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  50623. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  50624. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  50625. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  50626. };
  50627. #endif
  50628. #endif
  50629. const unsigned char* der;
  50630. XMEMSET(msg, 0x00, sizeof(msg));
  50631. der = privDer;
  50632. rsa = NULL;
  50633. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50634. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  50635. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  50636. -1);
  50637. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50638. RSA_PKCS1_PADDING), -1);
  50639. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  50640. RSA_PKCS1_PADDING), -1);
  50641. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  50642. RSA_PKCS1_PADDING), -1);
  50643. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50644. RSA_PKCS1_PSS_PADDING), -1);
  50645. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50646. RSA_PKCS1_PADDING), sizeof(encMsg));
  50647. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  50648. #ifdef WC_RSA_NO_PADDING
  50649. /* Non-zero message. */
  50650. XMEMSET(msg, 0x01, sizeof(msg));
  50651. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  50652. RSA_NO_PADDING), sizeof(encMsg));
  50653. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  50654. #endif
  50655. RSA_free(rsa);
  50656. #endif
  50657. return EXPECT_RESULT();
  50658. }
  50659. static int test_wolfSSL_RSA_public_encrypt(void)
  50660. {
  50661. EXPECT_DECLS;
  50662. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50663. RSA* rsa = NULL;
  50664. const unsigned char msg[2048/8] = { 0 };
  50665. unsigned char encMsg[2048/8];
  50666. ExpectNotNull(rsa = RSA_new());
  50667. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  50668. RSA_PKCS1_PADDING), -1);
  50669. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  50670. RSA_PKCS1_PADDING), -1);
  50671. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  50672. RSA_PKCS1_PADDING), -1);
  50673. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  50674. RSA_PKCS1_PADDING), -1);
  50675. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50676. RSA_PKCS1_PSS_PADDING), -1);
  50677. /* Empty RSA key. */
  50678. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  50679. RSA_PKCS1_PADDING), -1);
  50680. RSA_free(rsa);
  50681. #endif
  50682. return EXPECT_RESULT();
  50683. }
  50684. static int test_wolfSSL_RSA_private_decrypt(void)
  50685. {
  50686. EXPECT_DECLS;
  50687. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  50688. RSA* rsa = NULL;
  50689. unsigned char msg[2048/8];
  50690. const unsigned char encMsg[2048/8] = { 0 };
  50691. ExpectNotNull(rsa = RSA_new());
  50692. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  50693. RSA_PKCS1_PADDING), -1);
  50694. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  50695. RSA_PKCS1_PADDING), -1);
  50696. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  50697. RSA_PKCS1_PADDING), -1);
  50698. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  50699. RSA_PKCS1_PADDING), -1);
  50700. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50701. RSA_PKCS1_PSS_PADDING), -1);
  50702. /* Empty RSA key. */
  50703. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  50704. RSA_PKCS1_PADDING), -1);
  50705. RSA_free(rsa);
  50706. #endif
  50707. return EXPECT_RESULT();
  50708. }
  50709. static int test_wolfSSL_RSA_GenAdd(void)
  50710. {
  50711. EXPECT_DECLS;
  50712. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50713. RSA *rsa;
  50714. #ifdef USE_CERT_BUFFERS_1024
  50715. const unsigned char* privDer = client_key_der_1024;
  50716. size_t privDerSz = sizeof_client_key_der_1024;
  50717. const unsigned char* pubDer = client_keypub_der_1024;
  50718. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50719. #else
  50720. const unsigned char* privDer = client_key_der_2048;
  50721. size_t privDerSz = sizeof_client_key_der_2048;
  50722. const unsigned char* pubDer = client_keypub_der_2048;
  50723. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50724. #endif
  50725. const unsigned char* der;
  50726. der = privDer;
  50727. rsa = NULL;
  50728. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50729. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  50730. #ifndef RSA_LOW_MEM
  50731. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  50732. #else
  50733. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  50734. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50735. #endif
  50736. RSA_free(rsa);
  50737. der = pubDer;
  50738. rsa = NULL;
  50739. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50740. /* Need private values. */
  50741. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  50742. RSA_free(rsa);
  50743. #endif
  50744. return EXPECT_RESULT();
  50745. }
  50746. static int test_wolfSSL_RSA_blinding_on(void)
  50747. {
  50748. EXPECT_DECLS;
  50749. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  50750. RSA *rsa;
  50751. WOLFSSL_BN_CTX *bnCtx = NULL;
  50752. #ifdef USE_CERT_BUFFERS_1024
  50753. const unsigned char* privDer = client_key_der_1024;
  50754. size_t privDerSz = sizeof_client_key_der_1024;
  50755. #else
  50756. const unsigned char* privDer = client_key_der_2048;
  50757. size_t privDerSz = sizeof_client_key_der_2048;
  50758. #endif
  50759. const unsigned char* der;
  50760. der = privDer;
  50761. rsa = NULL;
  50762. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50763. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  50764. /* Does nothing so all parameters are valid. */
  50765. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  50766. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  50767. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  50768. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  50769. wolfSSL_BN_CTX_free(bnCtx);
  50770. RSA_free(rsa);
  50771. #endif
  50772. return EXPECT_RESULT();
  50773. }
  50774. static int test_wolfSSL_RSA_ex_data(void)
  50775. {
  50776. EXPECT_DECLS;
  50777. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50778. RSA* rsa = NULL;
  50779. unsigned char data[1];
  50780. ExpectNotNull(rsa = RSA_new());
  50781. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  50782. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50783. #ifdef MAX_EX_DATA
  50784. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  50785. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  50786. #endif
  50787. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  50788. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  50789. #ifdef HAVE_EX_DATA
  50790. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  50791. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  50792. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  50793. #else
  50794. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  50795. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  50796. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  50797. #endif
  50798. RSA_free(rsa);
  50799. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50800. return EXPECT_RESULT();
  50801. }
  50802. static int test_wolfSSL_RSA_LoadDer(void)
  50803. {
  50804. EXPECT_DECLS;
  50805. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  50806. defined(OPENSSL_EXTRA_X509_SMALL))
  50807. RSA *rsa = NULL;
  50808. #ifdef USE_CERT_BUFFERS_1024
  50809. const unsigned char* privDer = client_key_der_1024;
  50810. size_t privDerSz = sizeof_client_key_der_1024;
  50811. #else
  50812. const unsigned char* privDer = client_key_der_2048;
  50813. size_t privDerSz = sizeof_client_key_der_2048;
  50814. #endif
  50815. ExpectNotNull(rsa = RSA_new());
  50816. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  50817. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  50818. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  50819. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  50820. RSA_free(rsa);
  50821. #endif /* !NO_RSA && OPENSSL_EXTRA */
  50822. return EXPECT_RESULT();
  50823. }
  50824. /* Local API. */
  50825. static int test_wolfSSL_RSA_To_Der(void)
  50826. {
  50827. EXPECT_DECLS;
  50828. #ifdef WOLFSSL_TEST_STATIC_BUILD
  50829. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  50830. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  50831. RSA* rsa;
  50832. #ifdef USE_CERT_BUFFERS_1024
  50833. const unsigned char* privDer = client_key_der_1024;
  50834. size_t privDerSz = sizeof_client_key_der_1024;
  50835. const unsigned char* pubDer = client_keypub_der_1024;
  50836. size_t pubDerSz = sizeof_client_keypub_der_1024;
  50837. unsigned char out[sizeof(client_key_der_1024)];
  50838. #else
  50839. const unsigned char* privDer = client_key_der_2048;
  50840. size_t privDerSz = sizeof_client_key_der_2048;
  50841. const unsigned char* pubDer = client_keypub_der_2048;
  50842. size_t pubDerSz = sizeof_client_keypub_der_2048;
  50843. unsigned char out[sizeof(client_key_der_2048)];
  50844. #endif
  50845. const unsigned char* der;
  50846. unsigned char* outDer = NULL;
  50847. der = privDer;
  50848. rsa = NULL;
  50849. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50850. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50851. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  50852. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  50853. outDer = out;
  50854. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50855. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  50856. outDer = NULL;
  50857. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  50858. ExpectNotNull(outDer);
  50859. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  50860. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  50861. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  50862. outDer = out;
  50863. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  50864. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  50865. RSA_free(rsa);
  50866. ExpectNotNull(rsa = RSA_new());
  50867. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50868. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  50869. RSA_free(rsa);
  50870. der = pubDer;
  50871. rsa = NULL;
  50872. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  50873. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  50874. RSA_free(rsa);
  50875. #endif
  50876. #endif
  50877. return EXPECT_RESULT();
  50878. }
  50879. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50880. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  50881. {
  50882. EXPECT_DECLS;
  50883. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  50884. XFILE file = XBADFILE;
  50885. const char* fname = "./certs/server-keyPub.pem";
  50886. RSA *rsa = NULL;
  50887. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  50888. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  50889. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  50890. ExpectIntEQ(RSA_size(rsa), 256);
  50891. RSA_free(rsa);
  50892. if (file != XBADFILE)
  50893. XFCLOSE(file);
  50894. #endif
  50895. return EXPECT_RESULT();
  50896. }
  50897. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  50898. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  50899. {
  50900. EXPECT_DECLS;
  50901. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50902. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  50903. RSA* rsa = NULL;
  50904. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  50905. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  50906. /* Valid but stub so returns 0. */
  50907. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  50908. #endif
  50909. return EXPECT_RESULT();
  50910. }
  50911. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  50912. {
  50913. EXPECT_DECLS;
  50914. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50915. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50916. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  50917. RSA* rsa = NULL;
  50918. #ifdef USE_CERT_BUFFERS_1024
  50919. const unsigned char* privDer = client_key_der_1024;
  50920. size_t privDerSz = sizeof_client_key_der_1024;
  50921. #else
  50922. const unsigned char* privDer = client_key_der_2048;
  50923. size_t privDerSz = sizeof_client_key_der_2048;
  50924. #endif
  50925. const unsigned char* der;
  50926. #ifndef NO_AES
  50927. unsigned char passwd[] = "password";
  50928. #endif
  50929. ExpectNotNull(rsa = RSA_new());
  50930. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50931. NULL, NULL), 0);
  50932. RSA_free(rsa);
  50933. der = privDer;
  50934. rsa = NULL;
  50935. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50936. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  50937. NULL, NULL), 0);
  50938. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  50939. NULL, NULL), 0);
  50940. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  50941. NULL, NULL), 1);
  50942. #ifndef NO_AES
  50943. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50944. NULL, 0, NULL, NULL), 1);
  50945. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  50946. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  50947. #endif
  50948. RSA_free(rsa);
  50949. #endif
  50950. return EXPECT_RESULT();
  50951. }
  50952. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  50953. {
  50954. EXPECT_DECLS;
  50955. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  50956. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  50957. defined(WOLFSSL_DER_TO_PEM))
  50958. RSA* rsa = NULL;
  50959. #ifdef USE_CERT_BUFFERS_1024
  50960. const unsigned char* privDer = client_key_der_1024;
  50961. size_t privDerSz = sizeof_client_key_der_1024;
  50962. #else
  50963. const unsigned char* privDer = client_key_der_2048;
  50964. size_t privDerSz = sizeof_client_key_der_2048;
  50965. #endif
  50966. const unsigned char* der;
  50967. #ifndef NO_AES
  50968. unsigned char passwd[] = "password";
  50969. #endif
  50970. unsigned char* pem = NULL;
  50971. int plen;
  50972. ExpectNotNull(rsa = RSA_new());
  50973. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50974. &plen), 0);
  50975. RSA_free(rsa);
  50976. der = privDer;
  50977. rsa = NULL;
  50978. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  50979. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  50980. &plen), 0);
  50981. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  50982. &plen), 0);
  50983. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50984. NULL), 0);
  50985. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  50986. &plen), 1);
  50987. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50988. pem = NULL;
  50989. #ifndef NO_AES
  50990. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50991. NULL, 0, &pem, &plen), 1);
  50992. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50993. pem = NULL;
  50994. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  50995. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  50996. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  50997. #endif
  50998. RSA_free(rsa);
  50999. #endif
  51000. return EXPECT_RESULT();
  51001. }
  51002. static int test_wolfSSL_DH(void)
  51003. {
  51004. EXPECT_DECLS;
  51005. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51006. DH *dh = NULL;
  51007. BIGNUM* p;
  51008. BIGNUM* q;
  51009. BIGNUM* g;
  51010. BIGNUM* pub = NULL;
  51011. BIGNUM* priv = NULL;
  51012. #if defined(OPENSSL_ALL)
  51013. #if !defined(HAVE_FIPS) || \
  51014. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  51015. FILE* f = NULL;
  51016. unsigned char buf[268];
  51017. const unsigned char* pt = buf;
  51018. long len = 0;
  51019. dh = NULL;
  51020. XMEMSET(buf, 0, sizeof(buf));
  51021. /* Test 2048 bit parameters */
  51022. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  51023. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51024. if (f != XBADFILE)
  51025. XFCLOSE(f);
  51026. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  51027. ExpectNotNull(dh->p);
  51028. ExpectNotNull(dh->g);
  51029. ExpectTrue(pt == buf);
  51030. ExpectIntEQ(DH_generate_key(dh), 1);
  51031. ExpectIntEQ(DH_generate_key(dh), 1);
  51032. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  51033. ExpectNotNull(pub = BN_new());
  51034. ExpectIntEQ(BN_set_word(pub, 1), 1);
  51035. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  51036. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  51037. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  51038. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  51039. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  51040. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  51041. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  51042. BN_free(pub);
  51043. pub = NULL;
  51044. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51045. (const BIGNUM**)&q,
  51046. (const BIGNUM**)&g);
  51047. ExpectPtrEq(p, dh->p);
  51048. ExpectPtrEq(q, dh->q);
  51049. ExpectPtrEq(g, dh->g);
  51050. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51051. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  51052. ExpectPtrEq(pub, dh->pub_key);
  51053. ExpectPtrEq(priv, dh->priv_key);
  51054. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  51055. ExpectPtrEq(pub, dh->pub_key);
  51056. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  51057. ExpectPtrEq(priv, dh->priv_key);
  51058. pub = NULL;
  51059. priv = NULL;
  51060. ExpectNotNull(pub = BN_new());
  51061. ExpectNotNull(priv = BN_new());
  51062. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  51063. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  51064. if (EXPECT_FAIL()) {
  51065. BN_free(pub);
  51066. BN_free(priv);
  51067. }
  51068. pub = NULL;
  51069. priv = NULL;
  51070. ExpectNotNull(pub = BN_new());
  51071. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  51072. if (EXPECT_FAIL()) {
  51073. BN_free(pub);
  51074. }
  51075. ExpectNotNull(priv = BN_new());
  51076. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  51077. if (EXPECT_FAIL()) {
  51078. BN_free(priv);
  51079. }
  51080. ExpectPtrEq(pub, dh->pub_key);
  51081. ExpectPtrEq(priv, dh->priv_key);
  51082. pub = NULL;
  51083. priv = NULL;
  51084. DH_free(dh);
  51085. dh = NULL;
  51086. ExpectNotNull(dh = DH_new());
  51087. p = NULL;
  51088. ExpectNotNull(p = BN_new());
  51089. ExpectIntEQ(BN_set_word(p, 1), 1);
  51090. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  51091. ExpectNotNull(pub = BN_new());
  51092. ExpectNotNull(priv = BN_new());
  51093. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  51094. if (EXPECT_FAIL()) {
  51095. BN_free(pub);
  51096. BN_free(priv);
  51097. }
  51098. pub = NULL;
  51099. priv = NULL;
  51100. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  51101. BN_free(p);
  51102. p = NULL;
  51103. DH_free(dh);
  51104. dh = NULL;
  51105. #ifdef WOLFSSL_KEY_GEN
  51106. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  51107. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  51108. DH_free(dh);
  51109. #endif
  51110. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  51111. #endif /* OPENSSL_ALL */
  51112. (void)dh;
  51113. (void)p;
  51114. (void)q;
  51115. (void)g;
  51116. (void)pub;
  51117. (void)priv;
  51118. ExpectNotNull(dh = wolfSSL_DH_new());
  51119. /* invalid parameters test */
  51120. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  51121. (const BIGNUM**)&q,
  51122. (const BIGNUM**)&g);
  51123. DH_get0_pqg(dh, NULL,
  51124. (const BIGNUM**)&q,
  51125. (const BIGNUM**)&g);
  51126. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  51127. DH_get0_pqg(dh, NULL, NULL, NULL);
  51128. DH_get0_pqg(dh, (const BIGNUM**)&p,
  51129. (const BIGNUM**)&q,
  51130. (const BIGNUM**)&g);
  51131. ExpectPtrEq(p, NULL);
  51132. ExpectPtrEq(q, NULL);
  51133. ExpectPtrEq(g, NULL);
  51134. DH_free(dh);
  51135. dh = NULL;
  51136. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  51137. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  51138. #if defined(OPENSSL_ALL) || \
  51139. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51140. dh = wolfSSL_DH_new();
  51141. ExpectNotNull(dh);
  51142. p = wolfSSL_BN_new();
  51143. ExpectNotNull(p);
  51144. ExpectIntEQ(BN_set_word(p, 11), 1);
  51145. g = wolfSSL_BN_new();
  51146. ExpectNotNull(g);
  51147. ExpectIntEQ(BN_set_word(g, 2), 1);
  51148. q = wolfSSL_BN_new();
  51149. ExpectNotNull(q);
  51150. ExpectIntEQ(BN_set_word(q, 5), 1);
  51151. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  51152. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  51153. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  51154. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  51155. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  51156. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  51157. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  51158. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  51159. /* Don't need q. */
  51160. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51161. if (EXPECT_FAIL()) {
  51162. BN_free(p);
  51163. BN_free(g);
  51164. }
  51165. p = NULL;
  51166. g = NULL;
  51167. /* Setting again will free the p and g. */
  51168. wolfSSL_BN_free(q);
  51169. q = NULL;
  51170. DH_free(dh);
  51171. dh = NULL;
  51172. dh = wolfSSL_DH_new();
  51173. ExpectNotNull(dh);
  51174. p = wolfSSL_BN_new();
  51175. ExpectNotNull(p);
  51176. ExpectIntEQ(BN_set_word(p, 11), 1);
  51177. g = wolfSSL_BN_new();
  51178. ExpectNotNull(g);
  51179. ExpectIntEQ(BN_set_word(g, 2), 1);
  51180. q = wolfSSL_BN_new();
  51181. ExpectNotNull(q);
  51182. ExpectIntEQ(BN_set_word(q, 5), 1);
  51183. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  51184. /* p, q and g are now owned by dh - don't free. */
  51185. if (EXPECT_FAIL()) {
  51186. BN_free(p);
  51187. BN_free(q);
  51188. BN_free(g);
  51189. }
  51190. p = NULL;
  51191. q = NULL;
  51192. g = NULL;
  51193. p = wolfSSL_BN_new();
  51194. ExpectNotNull(p);
  51195. ExpectIntEQ(BN_set_word(p, 11), 1);
  51196. g = wolfSSL_BN_new();
  51197. ExpectNotNull(g);
  51198. ExpectIntEQ(BN_set_word(g, 2), 1);
  51199. q = wolfSSL_BN_new();
  51200. ExpectNotNull(q);
  51201. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  51202. if (EXPECT_FAIL()) {
  51203. BN_free(p);
  51204. }
  51205. p = NULL;
  51206. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  51207. if (EXPECT_FAIL()) {
  51208. BN_free(q);
  51209. }
  51210. q = NULL;
  51211. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  51212. if (EXPECT_FAIL()) {
  51213. BN_free(g);
  51214. }
  51215. g = NULL;
  51216. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  51217. /* p, q and g are now owned by dh - don't free. */
  51218. DH_free(dh);
  51219. dh = NULL;
  51220. ExpectIntEQ(DH_generate_key(NULL), 0);
  51221. ExpectNotNull(dh = DH_new());
  51222. ExpectIntEQ(DH_generate_key(dh), 0);
  51223. p = wolfSSL_BN_new();
  51224. ExpectNotNull(p);
  51225. ExpectIntEQ(BN_set_word(p, 0), 1);
  51226. g = wolfSSL_BN_new();
  51227. ExpectNotNull(g);
  51228. ExpectIntEQ(BN_set_word(g, 2), 1);
  51229. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51230. if (EXPECT_FAIL()) {
  51231. BN_free(p);
  51232. BN_free(g);
  51233. }
  51234. p = NULL;
  51235. g = NULL;
  51236. ExpectIntEQ(DH_generate_key(dh), 0);
  51237. DH_free(dh);
  51238. dh = NULL;
  51239. #endif
  51240. #endif
  51241. /* Test DH_up_ref() */
  51242. dh = wolfSSL_DH_new();
  51243. ExpectNotNull(dh);
  51244. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  51245. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  51246. DH_free(dh); /* decrease ref count */
  51247. DH_free(dh); /* free WOLFSSL_DH */
  51248. q = NULL;
  51249. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  51250. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  51251. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  51252. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  51253. #ifdef HAVE_FFDHE_2048
  51254. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  51255. DH_free(dh);
  51256. q = NULL;
  51257. #endif
  51258. #ifdef HAVE_FFDHE_3072
  51259. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  51260. DH_free(dh);
  51261. q = NULL;
  51262. #endif
  51263. #ifdef HAVE_FFDHE_4096
  51264. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  51265. DH_free(dh);
  51266. q = NULL;
  51267. #endif
  51268. #else
  51269. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  51270. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  51271. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  51272. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  51273. #endif /* OPENSSL_EXTRA && !NO_DH */
  51274. return EXPECT_RESULT();
  51275. }
  51276. static int test_wolfSSL_DH_dup(void)
  51277. {
  51278. EXPECT_DECLS;
  51279. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  51280. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  51281. defined(OPENSSL_EXTRA)
  51282. DH *dh = NULL;
  51283. DH *dhDup = NULL;
  51284. ExpectNotNull(dh = wolfSSL_DH_new());
  51285. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  51286. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  51287. #if defined(OPENSSL_ALL) || \
  51288. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  51289. {
  51290. WOLFSSL_BIGNUM* p = NULL;
  51291. WOLFSSL_BIGNUM* g = NULL;
  51292. ExpectNotNull(p = wolfSSL_BN_new());
  51293. ExpectNotNull(g = wolfSSL_BN_new());
  51294. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  51295. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  51296. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  51297. if (EXPECT_FAIL()) {
  51298. wolfSSL_BN_free(p);
  51299. wolfSSL_BN_free(g);
  51300. }
  51301. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  51302. wolfSSL_DH_free(dhDup);
  51303. }
  51304. #endif
  51305. wolfSSL_DH_free(dh);
  51306. #endif
  51307. #endif
  51308. return EXPECT_RESULT();
  51309. }
  51310. static int test_wolfSSL_DH_check(void)
  51311. {
  51312. EXPECT_DECLS;
  51313. #ifdef OPENSSL_ALL
  51314. #ifndef NO_DH
  51315. #ifndef NO_BIO
  51316. #ifndef NO_DSA
  51317. byte buf[6000];
  51318. char file[] = "./certs/dsaparams.pem";
  51319. XFILE f = XBADFILE;
  51320. int bytes;
  51321. BIO* bio = NULL;
  51322. DSA* dsa = NULL;
  51323. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  51324. static const byte dh2048[] = {
  51325. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51326. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51327. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51328. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51329. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51330. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51331. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51332. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51333. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51334. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51335. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51336. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51337. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51338. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51339. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51340. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51341. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51342. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51343. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51344. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51345. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51346. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51347. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51348. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51349. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51350. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51351. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51352. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51353. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51354. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51355. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51356. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51357. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51358. 0x93, 0x02, 0x01, 0x02
  51359. };
  51360. const byte* params;
  51361. #endif
  51362. DH* dh = NULL;
  51363. WOLFSSL_BIGNUM* p = NULL;
  51364. WOLFSSL_BIGNUM* g = NULL;
  51365. WOLFSSL_BIGNUM* pTmp = NULL;
  51366. WOLFSSL_BIGNUM* gTmp = NULL;
  51367. int codes = -1;
  51368. #ifndef NO_DSA
  51369. /* Initialize DH */
  51370. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  51371. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  51372. if (f != XBADFILE)
  51373. XFCLOSE(f);
  51374. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  51375. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  51376. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  51377. ExpectNotNull(dh);
  51378. BIO_free(bio);
  51379. DSA_free(dsa);
  51380. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  51381. params = dh2048;
  51382. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  51383. (long)sizeof(dh2048)));
  51384. #else
  51385. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  51386. #endif
  51387. /* Test assumed to be valid dh.
  51388. * Should return WOLFSSL_SUCCESS
  51389. * codes should be 0
  51390. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  51391. */
  51392. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51393. ExpectIntEQ(codes, 0);
  51394. /* Test NULL dh: expected BAD_FUNC_ARG */
  51395. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  51396. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  51397. if (dh != NULL) {
  51398. pTmp = dh->p;
  51399. dh->p = NULL;
  51400. }
  51401. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51402. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51403. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51404. /* set dh->p back to normal so it won't fail on next tests */
  51405. if (dh != NULL) {
  51406. dh->p = pTmp;
  51407. pTmp = NULL;
  51408. }
  51409. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  51410. if (dh != NULL) {
  51411. gTmp = dh->g;
  51412. dh->g = NULL;
  51413. }
  51414. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51415. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51416. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  51417. if (dh != NULL) {
  51418. dh->g = gTmp;
  51419. gTmp = NULL;
  51420. }
  51421. /* Cleanup */
  51422. DH_free(dh);
  51423. dh = NULL;
  51424. dh = DH_new();
  51425. ExpectNotNull(dh);
  51426. /* Check empty DH. */
  51427. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51428. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51429. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  51430. /* Check non-prime valued p. */
  51431. ExpectNotNull(p = BN_new());
  51432. ExpectIntEQ(BN_set_word(p, 4), 1);
  51433. ExpectNotNull(g = BN_new());
  51434. ExpectIntEQ(BN_set_word(g, 2), 1);
  51435. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  51436. if (EXPECT_FAIL()) {
  51437. wolfSSL_BN_free(p);
  51438. wolfSSL_BN_free(g);
  51439. }
  51440. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  51441. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  51442. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  51443. DH_free(dh);
  51444. #endif
  51445. #endif /* !NO_DH && !NO_DSA */
  51446. #endif
  51447. return EXPECT_RESULT();
  51448. }
  51449. static int test_wolfSSL_DH_prime(void)
  51450. {
  51451. EXPECT_DECLS;
  51452. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51453. WOLFSSL_BIGNUM* bn = NULL;
  51454. #if WOLFSSL_MAX_BN_BITS >= 768
  51455. WOLFSSL_BIGNUM* bn2 = NULL;
  51456. #endif
  51457. bn = wolfSSL_DH_768_prime(NULL);
  51458. #if WOLFSSL_MAX_BN_BITS >= 768
  51459. ExpectNotNull(bn);
  51460. bn2 = wolfSSL_DH_768_prime(bn);
  51461. ExpectNotNull(bn2);
  51462. ExpectTrue(bn == bn2);
  51463. wolfSSL_BN_free(bn);
  51464. bn = NULL;
  51465. #else
  51466. ExpectNull(bn);
  51467. #endif
  51468. bn = wolfSSL_DH_1024_prime(NULL);
  51469. #if WOLFSSL_MAX_BN_BITS >= 1024
  51470. ExpectNotNull(bn);
  51471. wolfSSL_BN_free(bn);
  51472. bn = NULL;
  51473. #else
  51474. ExpectNull(bn);
  51475. #endif
  51476. bn = wolfSSL_DH_2048_prime(NULL);
  51477. #if WOLFSSL_MAX_BN_BITS >= 2048
  51478. ExpectNotNull(bn);
  51479. wolfSSL_BN_free(bn);
  51480. bn = NULL;
  51481. #else
  51482. ExpectNull(bn);
  51483. #endif
  51484. bn = wolfSSL_DH_3072_prime(NULL);
  51485. #if WOLFSSL_MAX_BN_BITS >= 3072
  51486. ExpectNotNull(bn);
  51487. wolfSSL_BN_free(bn);
  51488. bn = NULL;
  51489. #else
  51490. ExpectNull(bn);
  51491. #endif
  51492. bn = wolfSSL_DH_4096_prime(NULL);
  51493. #if WOLFSSL_MAX_BN_BITS >= 4096
  51494. ExpectNotNull(bn);
  51495. wolfSSL_BN_free(bn);
  51496. bn = NULL;
  51497. #else
  51498. ExpectNull(bn);
  51499. #endif
  51500. bn = wolfSSL_DH_6144_prime(NULL);
  51501. #if WOLFSSL_MAX_BN_BITS >= 6144
  51502. ExpectNotNull(bn);
  51503. wolfSSL_BN_free(bn);
  51504. bn = NULL;
  51505. #else
  51506. ExpectNull(bn);
  51507. #endif
  51508. bn = wolfSSL_DH_8192_prime(NULL);
  51509. #if WOLFSSL_MAX_BN_BITS >= 8192
  51510. ExpectNotNull(bn);
  51511. wolfSSL_BN_free(bn);
  51512. bn = NULL;
  51513. #else
  51514. ExpectNull(bn);
  51515. #endif
  51516. #endif
  51517. return EXPECT_RESULT();
  51518. }
  51519. static int test_wolfSSL_DH_1536_prime(void)
  51520. {
  51521. EXPECT_DECLS;
  51522. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51523. BIGNUM* bn = NULL;
  51524. unsigned char bits[200];
  51525. int sz = 192; /* known binary size */
  51526. const byte expected[] = {
  51527. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51528. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  51529. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  51530. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  51531. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  51532. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  51533. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  51534. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  51535. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  51536. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  51537. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  51538. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  51539. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  51540. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  51541. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  51542. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  51543. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  51544. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  51545. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  51546. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  51547. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  51548. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  51549. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  51550. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51551. };
  51552. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  51553. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  51554. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  51555. BN_free(bn);
  51556. #endif
  51557. return EXPECT_RESULT();
  51558. }
  51559. static int test_wolfSSL_DH_get_2048_256(void)
  51560. {
  51561. EXPECT_DECLS;
  51562. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51563. WOLFSSL_DH* dh = NULL;
  51564. const WOLFSSL_BIGNUM* pBn;
  51565. const WOLFSSL_BIGNUM* gBn;
  51566. const WOLFSSL_BIGNUM* qBn;
  51567. const byte pExpected[] = {
  51568. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  51569. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  51570. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  51571. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  51572. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  51573. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  51574. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  51575. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  51576. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  51577. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  51578. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  51579. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  51580. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  51581. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  51582. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  51583. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  51584. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  51585. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  51586. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  51587. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  51588. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  51589. 0x1E, 0x1A, 0x15, 0x97
  51590. };
  51591. const byte gExpected[] = {
  51592. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  51593. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  51594. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  51595. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  51596. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  51597. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  51598. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  51599. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  51600. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  51601. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  51602. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  51603. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  51604. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  51605. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  51606. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  51607. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  51608. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  51609. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  51610. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  51611. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  51612. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  51613. 0x6C, 0xC4, 0x16, 0x59
  51614. };
  51615. const byte qExpected[] = {
  51616. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  51617. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  51618. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  51619. };
  51620. int pSz;
  51621. int qSz;
  51622. int gSz;
  51623. byte* pReturned = NULL;
  51624. byte* qReturned = NULL;
  51625. byte* gReturned = NULL;
  51626. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  51627. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  51628. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  51629. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  51630. DYNAMIC_TYPE_TMP_BUFFER));
  51631. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  51632. ExpectIntEQ(pSz, sizeof(pExpected));
  51633. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  51634. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  51635. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  51636. DYNAMIC_TYPE_TMP_BUFFER));
  51637. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  51638. ExpectIntEQ(qSz, sizeof(qExpected));
  51639. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  51640. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  51641. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  51642. DYNAMIC_TYPE_TMP_BUFFER));
  51643. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  51644. ExpectIntEQ(gSz, sizeof(gExpected));
  51645. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  51646. wolfSSL_DH_free(dh);
  51647. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51648. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51649. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51650. #endif
  51651. return EXPECT_RESULT();
  51652. }
  51653. static int test_wolfSSL_PEM_write_DHparams(void)
  51654. {
  51655. EXPECT_DECLS;
  51656. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  51657. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  51658. DH* dh = NULL;
  51659. BIO* bio = NULL;
  51660. XFILE fp = XBADFILE;
  51661. byte pem[2048];
  51662. int pemSz = 0;
  51663. const char expected[] =
  51664. "-----BEGIN DH PARAMETERS-----\n"
  51665. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  51666. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  51667. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  51668. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  51669. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  51670. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  51671. "-----END DH PARAMETERS-----\n";
  51672. const char badPem[] =
  51673. "-----BEGIN DH PARAMETERS-----\n"
  51674. "-----END DH PARAMETERS-----\n";
  51675. const char emptySeqPem[] =
  51676. "-----BEGIN DH PARAMETERS-----\n"
  51677. "MAA=\n"
  51678. "-----END DH PARAMETERS-----\n";
  51679. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  51680. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51681. if (fp != XBADFILE) {
  51682. XFCLOSE(fp);
  51683. fp = XBADFILE;
  51684. }
  51685. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  51686. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51687. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51688. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  51689. (int)sizeof(badPem));
  51690. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51691. BIO_free(bio);
  51692. bio = NULL;
  51693. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51694. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51695. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  51696. (int)sizeof(emptySeqPem));
  51697. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51698. BIO_free(bio);
  51699. bio = NULL;
  51700. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51701. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  51702. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  51703. BIO_free(bio);
  51704. bio = NULL;
  51705. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  51706. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  51707. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  51708. DH_free(dh);
  51709. dh = NULL;
  51710. dh = wolfSSL_DH_new();
  51711. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  51712. if (fp != XBADFILE) {
  51713. XFCLOSE(fp);
  51714. fp = XBADFILE;
  51715. }
  51716. wolfSSL_DH_free(dh);
  51717. dh = NULL;
  51718. /* check results */
  51719. XMEMSET(pem, 0, sizeof(pem));
  51720. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  51721. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  51722. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  51723. if (fp != XBADFILE)
  51724. XFCLOSE(fp);
  51725. #endif
  51726. return EXPECT_RESULT();
  51727. }
  51728. static int test_wolfSSL_d2i_DHparams(void)
  51729. {
  51730. EXPECT_DECLS;
  51731. #ifdef OPENSSL_ALL
  51732. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51733. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51734. XFILE f = XBADFILE;
  51735. unsigned char buf[4096];
  51736. const unsigned char* pt = buf;
  51737. #ifdef HAVE_FFDHE_2048
  51738. const char* params1 = "./certs/dh2048.der";
  51739. #endif
  51740. #ifdef HAVE_FFDHE_3072
  51741. const char* params2 = "./certs/dh3072.der";
  51742. #endif
  51743. long len = 0;
  51744. WOLFSSL_DH* dh = NULL;
  51745. XMEMSET(buf, 0, sizeof(buf));
  51746. /* Test 2048 bit parameters */
  51747. #ifdef HAVE_FFDHE_2048
  51748. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  51749. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51750. if (f != XBADFILE) {
  51751. XFCLOSE(f);
  51752. f = XBADFILE;
  51753. }
  51754. /* Valid case */
  51755. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51756. ExpectNotNull(dh->p);
  51757. ExpectNotNull(dh->g);
  51758. ExpectTrue(pt == buf);
  51759. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  51760. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  51761. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  51762. /* Invalid cases */
  51763. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51764. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51765. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  51766. DH_free(dh);
  51767. dh = NULL;
  51768. *buf = 0;
  51769. pt = buf;
  51770. #endif /* HAVE_FFDHE_2048 */
  51771. /* Test 3072 bit parameters */
  51772. #ifdef HAVE_FFDHE_3072
  51773. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  51774. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51775. if (f != XBADFILE) {
  51776. XFCLOSE(f);
  51777. f = XBADFILE;
  51778. }
  51779. /* Valid case */
  51780. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  51781. ExpectNotNull(dh->p);
  51782. ExpectNotNull(dh->g);
  51783. ExpectTrue(pt != buf);
  51784. ExpectIntEQ(DH_generate_key(dh), 1);
  51785. /* Invalid cases */
  51786. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  51787. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  51788. DH_free(dh);
  51789. dh = NULL;
  51790. #endif /* HAVE_FFDHE_3072 */
  51791. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51792. #endif /* !NO_DH */
  51793. #endif
  51794. return EXPECT_RESULT();
  51795. }
  51796. static int test_wolfSSL_DH_LoadDer(void)
  51797. {
  51798. EXPECT_DECLS;
  51799. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  51800. defined(OPENSSL_EXTRA)
  51801. static const byte dh2048[] = {
  51802. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  51803. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  51804. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  51805. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  51806. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  51807. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  51808. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  51809. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  51810. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  51811. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  51812. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  51813. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  51814. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  51815. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  51816. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  51817. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  51818. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  51819. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  51820. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  51821. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  51822. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  51823. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  51824. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  51825. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  51826. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  51827. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  51828. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  51829. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  51830. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  51831. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  51832. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  51833. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  51834. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  51835. 0x93, 0x02, 0x01, 0x02
  51836. };
  51837. WOLFSSL_DH* dh = NULL;
  51838. ExpectNotNull(dh = wolfSSL_DH_new());
  51839. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  51840. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  51841. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  51842. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  51843. wolfSSL_DH_free(dh);
  51844. #endif
  51845. return EXPECT_RESULT();
  51846. }
  51847. static int test_wolfSSL_i2d_DHparams(void)
  51848. {
  51849. EXPECT_DECLS;
  51850. #ifdef OPENSSL_ALL
  51851. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  51852. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51853. XFILE f = XBADFILE;
  51854. unsigned char buf[4096];
  51855. const unsigned char* pt;
  51856. unsigned char* pt2;
  51857. #ifdef HAVE_FFDHE_2048
  51858. const char* params1 = "./certs/dh2048.der";
  51859. #endif
  51860. #ifdef HAVE_FFDHE_3072
  51861. const char* params2 = "./certs/dh3072.der";
  51862. #endif
  51863. long len;
  51864. WOLFSSL_DH* dh = NULL;
  51865. /* Test 2048 bit parameters */
  51866. #ifdef HAVE_FFDHE_2048
  51867. pt = buf;
  51868. pt2 = buf;
  51869. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  51870. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51871. if (f != XBADFILE) {
  51872. XFCLOSE(f);
  51873. f = XBADFILE;
  51874. }
  51875. /* Valid case */
  51876. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51877. ExpectTrue(pt == buf);
  51878. ExpectIntEQ(DH_generate_key(dh), 1);
  51879. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  51880. /* Invalid case */
  51881. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51882. /* Return length only */
  51883. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  51884. DH_free(dh);
  51885. *buf = 0;
  51886. #endif
  51887. /* Test 3072 bit parameters */
  51888. #ifdef HAVE_FFDHE_3072
  51889. pt = buf;
  51890. pt2 = buf;
  51891. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  51892. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51893. if (f != XBADFILE) {
  51894. XFCLOSE(f);
  51895. f = XBADFILE;
  51896. }
  51897. /* Valid case */
  51898. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  51899. ExpectTrue(pt == buf);
  51900. ExpectIntEQ(DH_generate_key(dh), 1);
  51901. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  51902. /* Invalid case */
  51903. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  51904. /* Return length only */
  51905. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  51906. DH_free(dh);
  51907. #endif
  51908. dh = DH_new();
  51909. ExpectNotNull(dh);
  51910. pt2 = buf;
  51911. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  51912. DH_free(dh);
  51913. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51914. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  51915. #endif
  51916. return EXPECT_RESULT();
  51917. }
  51918. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  51919. /*----------------------------------------------------------------------------*
  51920. | EC
  51921. *----------------------------------------------------------------------------*/
  51922. static int test_wolfSSL_EC_GROUP(void)
  51923. {
  51924. EXPECT_DECLS;
  51925. #ifdef OPENSSL_EXTRA
  51926. EC_GROUP *group = NULL;
  51927. EC_GROUP *group2 = NULL;
  51928. EC_GROUP *group3 = NULL;
  51929. #ifndef HAVE_ECC_BRAINPOOL
  51930. EC_GROUP *group4 = NULL;
  51931. #endif
  51932. WOLFSSL_BIGNUM* order = NULL;
  51933. int group_bits;
  51934. int i;
  51935. static const int knownEccNids[] = {
  51936. NID_X9_62_prime192v1,
  51937. NID_X9_62_prime192v2,
  51938. NID_X9_62_prime192v3,
  51939. NID_X9_62_prime239v1,
  51940. NID_X9_62_prime239v2,
  51941. NID_X9_62_prime239v3,
  51942. NID_X9_62_prime256v1,
  51943. NID_secp112r1,
  51944. NID_secp112r2,
  51945. NID_secp128r1,
  51946. NID_secp128r2,
  51947. NID_secp160r1,
  51948. NID_secp160r2,
  51949. NID_secp224r1,
  51950. NID_secp384r1,
  51951. NID_secp521r1,
  51952. NID_secp160k1,
  51953. NID_secp192k1,
  51954. NID_secp224k1,
  51955. NID_secp256k1,
  51956. NID_brainpoolP160r1,
  51957. NID_brainpoolP192r1,
  51958. NID_brainpoolP224r1,
  51959. NID_brainpoolP256r1,
  51960. NID_brainpoolP320r1,
  51961. NID_brainpoolP384r1,
  51962. NID_brainpoolP512r1,
  51963. };
  51964. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  51965. static const int knownEccEnums[] = {
  51966. ECC_SECP192R1,
  51967. ECC_PRIME192V2,
  51968. ECC_PRIME192V3,
  51969. ECC_PRIME239V1,
  51970. ECC_PRIME239V2,
  51971. ECC_PRIME239V3,
  51972. ECC_SECP256R1,
  51973. ECC_SECP112R1,
  51974. ECC_SECP112R2,
  51975. ECC_SECP128R1,
  51976. ECC_SECP128R2,
  51977. ECC_SECP160R1,
  51978. ECC_SECP160R2,
  51979. ECC_SECP224R1,
  51980. ECC_SECP384R1,
  51981. ECC_SECP521R1,
  51982. ECC_SECP160K1,
  51983. ECC_SECP192K1,
  51984. ECC_SECP224K1,
  51985. ECC_SECP256K1,
  51986. ECC_BRAINPOOLP160R1,
  51987. ECC_BRAINPOOLP192R1,
  51988. ECC_BRAINPOOLP224R1,
  51989. ECC_BRAINPOOLP256R1,
  51990. ECC_BRAINPOOLP320R1,
  51991. ECC_BRAINPOOLP384R1,
  51992. ECC_BRAINPOOLP512R1,
  51993. };
  51994. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  51995. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51996. ExpectNotNull(group2 = EC_GROUP_dup(group));
  51997. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  51998. #ifndef HAVE_ECC_BRAINPOOL
  51999. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  52000. NID_brainpoolP256r1));
  52001. #endif
  52002. ExpectNull(EC_GROUP_dup(NULL));
  52003. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  52004. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52005. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  52006. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  52007. #ifndef HAVE_ECC_BRAINPOOL
  52008. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  52009. #endif
  52010. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  52011. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  52012. ExpectNotNull(order = BN_new());
  52013. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  52014. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  52015. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  52016. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  52017. wolfSSL_BN_free(order);
  52018. ExpectNotNull(EC_GROUP_method_of(group));
  52019. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  52020. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  52021. NID_X9_62_prime_field);
  52022. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  52023. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  52024. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  52025. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  52026. #ifndef NO_WOLFSSL_STUB
  52027. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  52028. #endif
  52029. #ifndef HAVE_ECC_BRAINPOOL
  52030. EC_GROUP_free(group4);
  52031. #endif
  52032. EC_GROUP_free(group3);
  52033. EC_GROUP_free(group2);
  52034. EC_GROUP_free(group);
  52035. for (i = 0; i < knowEccNidsLen; i++) {
  52036. group = NULL;
  52037. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  52038. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  52039. EC_GROUP_free(group);
  52040. }
  52041. for (i = 0; i < knowEccEnumsLen; i++) {
  52042. group = NULL;
  52043. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  52044. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  52045. EC_GROUP_free(group);
  52046. }
  52047. #endif
  52048. return EXPECT_RESULT();
  52049. }
  52050. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  52051. {
  52052. EXPECT_DECLS;
  52053. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52054. EC_GROUP *group = NULL;
  52055. BIO* bio = NULL;
  52056. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  52057. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  52058. EC_GROUP *ret = NULL;
  52059. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  52060. "BgUrgQQAIg==\n"
  52061. "-----END EC PARAMETERS-----";
  52062. #endif
  52063. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  52064. "MAA=\n"
  52065. "-----END EC PARAMETERS-----";
  52066. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  52067. "BgA=\n"
  52068. "-----END EC PARAMETERS-----";
  52069. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  52070. "BgE=\n"
  52071. "-----END EC PARAMETERS-----";
  52072. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  52073. "BgE*\n"
  52074. "-----END EC PARAMETERS-----";
  52075. /* Test that first parameter, bio, being NULL fails. */
  52076. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  52077. /* Test that reading named parameters works. */
  52078. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  52079. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  52080. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52081. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52082. BIO_free(bio);
  52083. bio = NULL;
  52084. EC_GROUP_free(group);
  52085. group = NULL;
  52086. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  52087. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  52088. /* Test that reusing group works. */
  52089. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  52090. sizeof(ec_nc_p384)));
  52091. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  52092. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  52093. BIO_free(bio);
  52094. bio = NULL;
  52095. EC_GROUP_free(group);
  52096. group = NULL;
  52097. /* Test that returning through group works. */
  52098. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  52099. sizeof(ec_nc_p384)));
  52100. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  52101. ExpectIntEQ(group == ret, 1);
  52102. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  52103. BIO_free(bio);
  52104. bio = NULL;
  52105. EC_GROUP_free(group);
  52106. group = NULL;
  52107. #endif
  52108. /* Test 0x30, 0x00 (not and object id) fails. */
  52109. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  52110. sizeof(ec_nc_bad_1)));
  52111. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52112. BIO_free(bio);
  52113. bio = NULL;
  52114. /* Test 0x06, 0x00 (empty object id) fails. */
  52115. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  52116. sizeof(ec_nc_bad_2)));
  52117. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52118. BIO_free(bio);
  52119. bio = NULL;
  52120. /* Test 0x06, 0x01 (badly formed object id) fails. */
  52121. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  52122. sizeof(ec_nc_bad_3)));
  52123. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52124. BIO_free(bio);
  52125. bio = NULL;
  52126. /* Test invalid PEM encoding - invalid character. */
  52127. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  52128. sizeof(ec_nc_bad_4)));
  52129. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  52130. BIO_free(bio);
  52131. #endif
  52132. return EXPECT_RESULT();
  52133. }
  52134. static int test_wolfSSL_EC_POINT(void)
  52135. {
  52136. EXPECT_DECLS;
  52137. #if !defined(WOLFSSL_SP_MATH) && \
  52138. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  52139. #ifdef OPENSSL_EXTRA
  52140. BN_CTX* ctx = NULL;
  52141. EC_GROUP* group = NULL;
  52142. #ifndef HAVE_ECC_BRAINPOOL
  52143. EC_GROUP* group2 = NULL;
  52144. #endif
  52145. EC_POINT* Gxy = NULL;
  52146. EC_POINT* new_point = NULL;
  52147. EC_POINT* set_point = NULL;
  52148. EC_POINT* infinity = NULL;
  52149. BIGNUM* k = NULL;
  52150. BIGNUM* Gx = NULL;
  52151. BIGNUM* Gy = NULL;
  52152. BIGNUM* Gz = NULL;
  52153. BIGNUM* X = NULL;
  52154. BIGNUM* Y = NULL;
  52155. BIGNUM* set_point_bn = NULL;
  52156. char* hexStr = NULL;
  52157. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  52158. "17AF381913FF7A96314EA47055EA0FD0";
  52159. /* NISTP256R1 Gx/Gy */
  52160. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  52161. "77037D812DEB33A0F4A13945D898C296";
  52162. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  52163. "2BCE33576B315ECECBB6406837BF51F5";
  52164. #ifndef HAVE_SELFTEST
  52165. EC_POINT *tmp = NULL;
  52166. size_t bin_len;
  52167. unsigned int blen = 0;
  52168. unsigned char* buf = NULL;
  52169. unsigned char bufInf[1] = { 0x00 };
  52170. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  52171. "77037D812DEB33A0F4A13945D898C296"
  52172. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  52173. "2BCE33576B315ECECBB6406837BF51F5";
  52174. const unsigned char binUncompG[] = {
  52175. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52176. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52177. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52178. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52179. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52180. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52181. };
  52182. const unsigned char binUncompGBad[] = {
  52183. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52184. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52185. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52186. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52187. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52188. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52189. };
  52190. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  52191. "77037D812DEB33A0F4A13945D898C296";
  52192. #ifdef HAVE_COMP_KEY
  52193. const unsigned char binCompG[] = {
  52194. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52195. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52196. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52197. };
  52198. #endif
  52199. #endif
  52200. ExpectNotNull(ctx = BN_CTX_new());
  52201. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52202. #ifndef HAVE_ECC_BRAINPOOL
  52203. /* Used to make groups curve_idx == -1. */
  52204. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  52205. #endif
  52206. ExpectNull(EC_POINT_new(NULL));
  52207. ExpectNotNull(Gxy = EC_POINT_new(group));
  52208. ExpectNotNull(new_point = EC_POINT_new(group));
  52209. ExpectNotNull(set_point = EC_POINT_new(group));
  52210. ExpectNotNull(X = BN_new());
  52211. ExpectNotNull(Y = BN_new());
  52212. ExpectNotNull(set_point_bn = BN_new());
  52213. ExpectNotNull(infinity = EC_POINT_new(group));
  52214. /* load test values */
  52215. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  52216. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  52217. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  52218. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  52219. /* populate coordinates for input point */
  52220. if (Gxy != NULL) {
  52221. Gxy->X = Gx;
  52222. Gxy->Y = Gy;
  52223. Gxy->Z = Gz;
  52224. }
  52225. /* Test handling of NULL point. */
  52226. EC_POINT_clear_free(NULL);
  52227. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52228. NULL, NULL, ctx), 0);
  52229. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  52230. NULL, NULL, ctx), 0);
  52231. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  52232. NULL, NULL, ctx), 0);
  52233. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52234. X, NULL, ctx), 0);
  52235. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  52236. NULL, Y, ctx), 0);
  52237. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  52238. X, Y, ctx), 0);
  52239. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  52240. X, Y, ctx), 0);
  52241. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  52242. NULL, Y, ctx), 0);
  52243. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  52244. X, NULL, ctx), 0);
  52245. /* Getting point at infinity returns an error. */
  52246. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  52247. X, Y, ctx), 0);
  52248. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  52249. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  52250. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52251. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  52252. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  52253. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  52254. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  52255. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  52256. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  52257. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  52258. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  52259. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  52260. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  52261. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  52262. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  52263. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  52264. /* perform point multiplication */
  52265. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  52266. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52267. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52268. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52269. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  52270. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52271. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52272. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52273. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  52274. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52275. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52276. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52277. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  52278. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  52279. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  52280. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  52281. /* Set point to something. */
  52282. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  52283. #else
  52284. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  52285. ctx), 1);
  52286. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52287. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  52288. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  52289. #endif
  52290. /* check if point X coordinate is zero */
  52291. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  52292. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  52293. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  52294. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  52295. #endif
  52296. /* extract the coordinates from point */
  52297. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  52298. ctx), WOLFSSL_SUCCESS);
  52299. /* check if point X coordinate is zero */
  52300. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  52301. /* set the same X and Y points in another object */
  52302. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  52303. ctx), WOLFSSL_SUCCESS);
  52304. /* compare points as they should be the same */
  52305. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  52306. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  52307. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  52308. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  52309. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  52310. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  52311. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  52312. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  52313. /* Test copying */
  52314. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  52315. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  52316. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  52317. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  52318. /* Test inverting */
  52319. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  52320. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  52321. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  52322. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  52323. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  52324. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  52325. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52326. {
  52327. EC_POINT* orig_point = NULL;
  52328. ExpectNotNull(orig_point = EC_POINT_new(group));
  52329. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  52330. 1);
  52331. /* new_point should be set_point inverted so adding it will revert
  52332. * the point back to set_point */
  52333. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  52334. NULL), 1);
  52335. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  52336. EC_POINT_free(orig_point);
  52337. }
  52338. #endif
  52339. /* Test getting affine converts from projective. */
  52340. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  52341. /* Force non-affine coordinates */
  52342. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52343. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  52344. if (new_point != NULL) {
  52345. new_point->inSet = 0;
  52346. }
  52347. /* extract the coordinates from point */
  52348. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  52349. ctx), WOLFSSL_SUCCESS);
  52350. /* check if point ordinates have changed. */
  52351. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  52352. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  52353. /* Test check for infinity */
  52354. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  52355. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  52356. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  52357. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  52358. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  52359. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  52360. #else
  52361. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  52362. #endif
  52363. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  52364. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  52365. /* check bn2hex */
  52366. hexStr = BN_bn2hex(k);
  52367. ExpectStrEQ(hexStr, kTest);
  52368. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  52369. BN_print_fp(stderr, k);
  52370. fprintf(stderr, "\n");
  52371. #endif
  52372. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52373. hexStr = BN_bn2hex(Gx);
  52374. ExpectStrEQ(hexStr, kGx);
  52375. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  52376. BN_print_fp(stderr, Gx);
  52377. fprintf(stderr, "\n");
  52378. #endif
  52379. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52380. hexStr = BN_bn2hex(Gy);
  52381. ExpectStrEQ(hexStr, kGy);
  52382. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  52383. BN_print_fp(stderr, Gy);
  52384. fprintf(stderr, "\n");
  52385. #endif
  52386. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52387. #ifndef HAVE_SELFTEST
  52388. /* Test point to hex */
  52389. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52390. ctx));
  52391. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52392. ctx));
  52393. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52394. ctx));
  52395. #ifndef HAVE_ECC_BRAINPOOL
  52396. /* Group not supported in wolfCrypt. */
  52397. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52398. ctx));
  52399. #endif
  52400. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  52401. ExpectNotNull(hexStr);
  52402. ExpectStrEQ(hexStr, uncompG);
  52403. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52404. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  52405. ExpectNotNull(hexStr);
  52406. ExpectStrEQ(hexStr, compG);
  52407. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  52408. /* Test point to oct */
  52409. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52410. NULL, 0, ctx), 0);
  52411. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52412. NULL, 0, ctx), 0);
  52413. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  52414. NULL, 0, ctx), 0);
  52415. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52416. NULL, 0, ctx);
  52417. ExpectIntEQ(bin_len, sizeof(binUncompG));
  52418. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52419. DYNAMIC_TYPE_ECC));
  52420. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  52421. buf, bin_len, ctx), bin_len);
  52422. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52423. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52424. /* Infinity (x=0, y=0) encodes as '0x00'. */
  52425. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52426. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  52427. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52428. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  52429. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  52430. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  52431. ExpectIntEQ(bufInf[0], 0);
  52432. wolfSSL_EC_POINT_dump(NULL, NULL);
  52433. /* Test point i2d */
  52434. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  52435. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  52436. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  52437. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  52438. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  52439. ExpectIntEQ(blen, sizeof(binUncompG));
  52440. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  52441. blen -= 1;
  52442. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  52443. blen += 1;
  52444. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  52445. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  52446. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52447. #ifdef HAVE_COMP_KEY
  52448. /* Test point to oct compressed */
  52449. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  52450. 0, ctx);
  52451. ExpectIntEQ(bin_len, sizeof(binCompG));
  52452. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  52453. DYNAMIC_TYPE_ECC));
  52454. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  52455. bin_len, ctx), bin_len);
  52456. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  52457. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  52458. #endif
  52459. /* Test point BN */
  52460. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  52461. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52462. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  52463. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52464. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  52465. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  52466. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  52467. /* Test oct to point */
  52468. ExpectNotNull(tmp = EC_POINT_new(group));
  52469. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  52470. ctx), 0);
  52471. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  52472. ctx), 0);
  52473. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  52474. ctx), 0);
  52475. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  52476. sizeof(binUncompGBad), ctx), 0);
  52477. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  52478. ctx), 1);
  52479. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52480. EC_POINT_free(tmp);
  52481. tmp = NULL;
  52482. /* Test setting BN ordinates. */
  52483. ExpectNotNull(tmp = EC_POINT_new(group));
  52484. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52485. NULL, ctx), 0);
  52486. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  52487. NULL, ctx), 0);
  52488. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  52489. NULL, ctx), 0);
  52490. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  52491. NULL, ctx), 0);
  52492. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  52493. Gy, ctx), 0);
  52494. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  52495. ctx), 0);
  52496. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  52497. ctx), 0);
  52498. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  52499. Gy, ctx), 0);
  52500. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  52501. NULL, ctx), 0);
  52502. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  52503. ctx), 1);
  52504. EC_POINT_free(tmp);
  52505. tmp = NULL;
  52506. /* Test point d2i */
  52507. ExpectNotNull(tmp = EC_POINT_new(group));
  52508. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  52509. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  52510. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  52511. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  52512. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  52513. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  52514. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  52515. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  52516. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  52517. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52518. EC_POINT_free(tmp);
  52519. tmp = NULL;
  52520. #ifdef HAVE_COMP_KEY
  52521. /* Test oct compressed to point */
  52522. ExpectNotNull(tmp = EC_POINT_new(group));
  52523. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  52524. 1);
  52525. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52526. EC_POINT_free(tmp);
  52527. tmp = NULL;
  52528. /* Test point d2i - compressed */
  52529. ExpectNotNull(tmp = EC_POINT_new(group));
  52530. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  52531. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  52532. EC_POINT_free(tmp);
  52533. tmp = NULL;
  52534. #endif
  52535. #endif
  52536. /* test BN_mod_add */
  52537. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  52538. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  52539. 1);
  52540. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  52541. /* cleanup */
  52542. BN_free(X);
  52543. BN_free(Y);
  52544. BN_free(k);
  52545. BN_free(set_point_bn);
  52546. EC_POINT_free(infinity);
  52547. EC_POINT_free(new_point);
  52548. EC_POINT_free(set_point);
  52549. EC_POINT_clear_free(Gxy);
  52550. #ifndef HAVE_ECC_BRAINPOOL
  52551. EC_GROUP_free(group2);
  52552. #endif
  52553. EC_GROUP_free(group);
  52554. BN_CTX_free(ctx);
  52555. #endif
  52556. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  52557. return EXPECT_RESULT();
  52558. }
  52559. static int test_wolfSSL_SPAKE(void)
  52560. {
  52561. EXPECT_DECLS;
  52562. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  52563. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  52564. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  52565. BIGNUM* x = NULL; /* kdc priv */
  52566. BIGNUM* y = NULL; /* client priv */
  52567. BIGNUM* w = NULL; /* shared value */
  52568. byte M_bytes[] = {
  52569. /* uncompressed */
  52570. 0x04,
  52571. /* x */
  52572. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  52573. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  52574. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  52575. /* y */
  52576. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  52577. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  52578. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  52579. };
  52580. EC_POINT* M = NULL; /* shared value */
  52581. byte N_bytes[] = {
  52582. /* uncompressed */
  52583. 0x04,
  52584. /* x */
  52585. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  52586. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  52587. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  52588. /* y */
  52589. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  52590. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  52591. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  52592. };
  52593. EC_POINT* N = NULL; /* shared value */
  52594. EC_POINT* T = NULL; /* kdc pub */
  52595. EC_POINT* tmp1 = NULL; /* kdc pub */
  52596. EC_POINT* tmp2 = NULL; /* kdc pub */
  52597. EC_POINT* S = NULL; /* client pub */
  52598. EC_POINT* client_secret = NULL;
  52599. EC_POINT* kdc_secret = NULL;
  52600. EC_GROUP* group = NULL;
  52601. BN_CTX* bn_ctx = NULL;
  52602. /* Values taken from a test run of Kerberos 5 */
  52603. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52604. ExpectNotNull(bn_ctx = BN_CTX_new());
  52605. ExpectNotNull(M = EC_POINT_new(group));
  52606. ExpectNotNull(N = EC_POINT_new(group));
  52607. ExpectNotNull(T = EC_POINT_new(group));
  52608. ExpectNotNull(tmp1 = EC_POINT_new(group));
  52609. ExpectNotNull(tmp2 = EC_POINT_new(group));
  52610. ExpectNotNull(S = EC_POINT_new(group));
  52611. ExpectNotNull(client_secret = EC_POINT_new(group));
  52612. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  52613. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  52614. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  52615. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  52616. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  52617. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  52618. "506AB395478F0AAB647752CF117B36250"), 1);
  52619. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  52620. 1);
  52621. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  52622. 1);
  52623. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  52624. /* kdc */
  52625. /* T=x*P+w*M */
  52626. /* All in one function call */
  52627. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  52628. /* Spread into separate calls */
  52629. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  52630. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  52631. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  52632. 1);
  52633. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  52634. /* client */
  52635. /* S=y*P+w*N */
  52636. /* All in one function call */
  52637. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  52638. /* Spread into separate calls */
  52639. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  52640. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  52641. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  52642. 1);
  52643. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  52644. /* K=y*(T-w*M) */
  52645. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  52646. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  52647. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  52648. 1);
  52649. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  52650. bn_ctx), 1);
  52651. /* kdc */
  52652. /* K=x*(S-w*N) */
  52653. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  52654. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  52655. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  52656. 1);
  52657. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  52658. 1);
  52659. /* kdc_secret == client_secret */
  52660. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  52661. BN_free(x);
  52662. BN_free(y);
  52663. BN_free(w);
  52664. EC_POINT_free(M);
  52665. EC_POINT_free(N);
  52666. EC_POINT_free(T);
  52667. EC_POINT_free(tmp1);
  52668. EC_POINT_free(tmp2);
  52669. EC_POINT_free(S);
  52670. EC_POINT_free(client_secret);
  52671. EC_POINT_free(kdc_secret);
  52672. EC_GROUP_free(group);
  52673. BN_CTX_free(bn_ctx);
  52674. #endif
  52675. return EXPECT_RESULT();
  52676. }
  52677. static int test_wolfSSL_EC_KEY_generate(void)
  52678. {
  52679. EXPECT_DECLS;
  52680. #ifdef OPENSSL_EXTRA
  52681. WOLFSSL_EC_KEY* key = NULL;
  52682. #ifndef HAVE_ECC_BRAINPOOL
  52683. WOLFSSL_EC_GROUP* group = NULL;
  52684. #endif
  52685. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52686. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  52687. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  52688. wolfSSL_EC_KEY_free(key);
  52689. key = NULL;
  52690. #ifndef HAVE_ECC_BRAINPOOL
  52691. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  52692. NID_brainpoolP256r1));
  52693. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  52694. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  52695. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  52696. wolfSSL_EC_KEY_free(key);
  52697. wolfSSL_EC_GROUP_free(group);
  52698. #endif
  52699. #endif
  52700. return EXPECT_RESULT();
  52701. }
  52702. static int test_EC_i2d(void)
  52703. {
  52704. EXPECT_DECLS;
  52705. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  52706. EC_KEY *key = NULL;
  52707. EC_KEY *copy = NULL;
  52708. int len = 0;
  52709. unsigned char *buf = NULL;
  52710. unsigned char *p = NULL;
  52711. const unsigned char *tmp = NULL;
  52712. const unsigned char octBad[] = {
  52713. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  52714. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  52715. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  52716. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  52717. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  52718. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  52719. };
  52720. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52721. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  52722. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  52723. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52724. DYNAMIC_TYPE_TMP_BUFFER));
  52725. p = buf;
  52726. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  52727. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  52728. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  52729. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  52730. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52731. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  52732. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  52733. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  52734. tmp = buf;
  52735. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  52736. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  52737. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  52738. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  52739. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  52740. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  52741. tmp = buf;
  52742. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  52743. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  52744. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  52745. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  52746. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  52747. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  52748. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  52749. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  52750. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  52751. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  52752. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  52753. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  52754. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  52755. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52756. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  52757. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52758. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  52759. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52760. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  52761. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52762. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  52763. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  52764. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52765. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52766. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  52767. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  52768. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  52769. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52770. buf = NULL;
  52771. buf = NULL;
  52772. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  52773. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52774. DYNAMIC_TYPE_TMP_BUFFER));
  52775. p = buf;
  52776. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  52777. p = NULL;
  52778. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  52779. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52780. p = NULL;
  52781. /* Bad point is also an invalid private key. */
  52782. tmp = octBad;
  52783. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  52784. tmp = buf;
  52785. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  52786. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52787. buf = NULL;
  52788. buf = NULL;
  52789. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  52790. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  52791. DYNAMIC_TYPE_TMP_BUFFER));
  52792. p = buf;
  52793. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52794. p = NULL;
  52795. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  52796. tmp = buf;
  52797. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  52798. tmp = octBad;
  52799. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  52800. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  52801. ExpectIntEQ(EC_KEY_check_key(key), 1);
  52802. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  52803. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  52804. EC_KEY_free(key);
  52805. EC_KEY_free(copy);
  52806. #endif
  52807. return EXPECT_RESULT();
  52808. }
  52809. static int test_wolfSSL_EC_curve(void)
  52810. {
  52811. EXPECT_DECLS;
  52812. #if defined(OPENSSL_EXTRA)
  52813. int nid = NID_secp160k1;
  52814. const char* nid_name = NULL;
  52815. ExpectNull(EC_curve_nid2nist(NID_sha256));
  52816. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  52817. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  52818. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  52819. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  52820. #endif
  52821. return EXPECT_RESULT();
  52822. }
  52823. static int test_wolfSSL_EC_KEY_dup(void)
  52824. {
  52825. EXPECT_DECLS;
  52826. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  52827. WOLFSSL_EC_KEY* ecKey = NULL;
  52828. WOLFSSL_EC_KEY* dupKey = NULL;
  52829. ecc_key* srcKey = NULL;
  52830. ecc_key* destKey = NULL;
  52831. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52832. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52833. /* Valid cases */
  52834. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52835. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  52836. /* Compare pubkey */
  52837. if (ecKey != NULL) {
  52838. srcKey = (ecc_key*)ecKey->internal;
  52839. }
  52840. if (dupKey != NULL) {
  52841. destKey = (ecc_key*)dupKey->internal;
  52842. }
  52843. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  52844. /* compare EC_GROUP */
  52845. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  52846. /* compare EC_POINT */
  52847. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  52848. dupKey->pub_key, NULL), MP_EQ);
  52849. /* compare BIGNUM */
  52850. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  52851. wolfSSL_EC_KEY_free(dupKey);
  52852. dupKey = NULL;
  52853. /* Invalid cases */
  52854. /* NULL key */
  52855. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  52856. /* NULL ecc_key */
  52857. if (ecKey != NULL) {
  52858. wc_ecc_free((ecc_key*)ecKey->internal);
  52859. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  52860. ecKey->internal = NULL; /* Set ecc_key to NULL */
  52861. }
  52862. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52863. wolfSSL_EC_KEY_free(ecKey);
  52864. ecKey = NULL;
  52865. wolfSSL_EC_KEY_free(dupKey);
  52866. dupKey = NULL;
  52867. /* NULL Group */
  52868. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52869. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52870. if (ecKey != NULL) {
  52871. wolfSSL_EC_GROUP_free(ecKey->group);
  52872. ecKey->group = NULL; /* Set group to NULL */
  52873. }
  52874. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52875. wolfSSL_EC_KEY_free(ecKey);
  52876. ecKey = NULL;
  52877. wolfSSL_EC_KEY_free(dupKey);
  52878. dupKey = NULL;
  52879. /* NULL public key */
  52880. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52881. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52882. if (ecKey != NULL) {
  52883. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  52884. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  52885. }
  52886. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52887. if (ecKey != NULL) {
  52888. wolfSSL_EC_POINT_free(ecKey->pub_key);
  52889. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  52890. }
  52891. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52892. wolfSSL_EC_KEY_free(ecKey);
  52893. ecKey = NULL;
  52894. wolfSSL_EC_KEY_free(dupKey);
  52895. dupKey = NULL;
  52896. /* NULL private key */
  52897. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52898. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  52899. if (ecKey != NULL) {
  52900. wolfSSL_BN_free(ecKey->priv_key);
  52901. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  52902. }
  52903. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52904. wolfSSL_EC_KEY_free(ecKey);
  52905. ecKey = NULL;
  52906. wolfSSL_EC_KEY_free(dupKey);
  52907. dupKey = NULL;
  52908. /* Test EC_KEY_up_ref */
  52909. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  52910. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  52911. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  52912. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  52913. /* reference count doesn't follow duplicate */
  52914. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  52915. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  52916. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  52917. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  52918. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  52919. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  52920. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  52921. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  52922. #endif
  52923. return EXPECT_RESULT();
  52924. }
  52925. static int test_wolfSSL_EC_KEY_set_group(void)
  52926. {
  52927. EXPECT_DECLS;
  52928. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  52929. defined(OPENSSL_EXTRA)
  52930. EC_KEY *key = NULL;
  52931. EC_GROUP *group = NULL;
  52932. const EC_GROUP *group2 = NULL;
  52933. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52934. ExpectNotNull(key = EC_KEY_new());
  52935. ExpectNull(EC_KEY_get0_group(NULL));
  52936. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  52937. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  52938. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  52939. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  52940. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  52941. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  52942. EC_GROUP_free(group);
  52943. EC_KEY_free(key);
  52944. #endif
  52945. return EXPECT_RESULT();
  52946. }
  52947. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  52948. {
  52949. EXPECT_DECLS;
  52950. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52951. BIO* bio = NULL;
  52952. EC_KEY* key = NULL;
  52953. /* Error condition: NULL key. */
  52954. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  52955. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  52956. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  52957. /* Conversion form defaults to uncompressed. */
  52958. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52959. #ifdef HAVE_COMP_KEY
  52960. /* Explicitly set to compressed. */
  52961. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52962. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  52963. #else
  52964. /* Will still work just won't change anything. */
  52965. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  52966. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52967. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  52968. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  52969. #endif
  52970. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  52971. BIO_free(bio);
  52972. EC_KEY_free(key);
  52973. #endif
  52974. return EXPECT_RESULT();
  52975. }
  52976. static int test_wolfSSL_EC_KEY_private_key(void)
  52977. {
  52978. EXPECT_DECLS;
  52979. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52980. WOLFSSL_EC_KEY* key = NULL;
  52981. WOLFSSL_BIGNUM* priv = NULL;
  52982. WOLFSSL_BIGNUM* priv2 = NULL;
  52983. WOLFSSL_BIGNUM* bn;
  52984. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52985. ExpectNotNull(priv = wolfSSL_BN_new());
  52986. ExpectNotNull(priv2 = wolfSSL_BN_new());
  52987. ExpectIntNE(BN_set_word(priv, 2), 0);
  52988. ExpectIntNE(BN_set_word(priv2, 2), 0);
  52989. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  52990. /* No private key set. */
  52991. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  52992. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  52993. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  52994. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  52995. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  52996. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  52997. ExpectPtrNE(bn, priv);
  52998. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  52999. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53000. ExpectPtrNE(bn, priv2);
  53001. wolfSSL_BN_free(priv2);
  53002. wolfSSL_BN_free(priv);
  53003. wolfSSL_EC_KEY_free(key);
  53004. #endif
  53005. return EXPECT_RESULT();
  53006. }
  53007. static int test_wolfSSL_EC_KEY_public_key(void)
  53008. {
  53009. EXPECT_DECLS;
  53010. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53011. WOLFSSL_EC_KEY* key = NULL;
  53012. WOLFSSL_EC_POINT* pub = NULL;
  53013. WOLFSSL_EC_POINT* point = NULL;
  53014. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53015. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  53016. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  53017. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53018. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  53019. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  53020. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  53021. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  53022. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  53023. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  53024. ExpectPtrEq(point, pub);
  53025. wolfSSL_EC_KEY_free(key);
  53026. #endif
  53027. return EXPECT_RESULT();
  53028. }
  53029. static int test_wolfSSL_EC_KEY_print_fp(void)
  53030. {
  53031. EXPECT_DECLS;
  53032. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  53033. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  53034. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  53035. !defined(NO_STDIO_FILESYSTEM)
  53036. EC_KEY* key = NULL;
  53037. /* Bad file pointer. */
  53038. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  53039. /* NULL key. */
  53040. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  53041. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  53042. /* Negative indent. */
  53043. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  53044. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53045. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  53046. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53047. wolfSSL_EC_KEY_free(key);
  53048. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  53049. NID_X9_62_prime256v1)));
  53050. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  53051. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  53052. wolfSSL_EC_KEY_free(key);
  53053. #endif
  53054. return EXPECT_RESULT();
  53055. }
  53056. static int test_wolfSSL_EC_get_builtin_curves(void)
  53057. {
  53058. EXPECT_DECLS;
  53059. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  53060. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53061. EC_builtin_curve* curves = NULL;
  53062. size_t crv_len = 0;
  53063. size_t i = 0;
  53064. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  53065. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  53066. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  53067. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  53068. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  53069. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  53070. if (curves[i].comment != NULL) {
  53071. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  53072. }
  53073. }
  53074. if (crv_len > 1) {
  53075. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  53076. }
  53077. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53078. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53079. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  53080. return EXPECT_RESULT();
  53081. }
  53082. static int test_wolfSSL_ECDSA_SIG(void)
  53083. {
  53084. EXPECT_DECLS;
  53085. #ifdef OPENSSL_EXTRA
  53086. WOLFSSL_ECDSA_SIG* sig = NULL;
  53087. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  53088. WOLFSSL_BIGNUM* r = NULL;
  53089. WOLFSSL_BIGNUM* s = NULL;
  53090. const WOLFSSL_BIGNUM* r2 = NULL;
  53091. const WOLFSSL_BIGNUM* s2 = NULL;
  53092. const unsigned char* cp = NULL;
  53093. unsigned char* p = NULL;
  53094. unsigned char outSig[8];
  53095. unsigned char sigData[8] =
  53096. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  53097. unsigned char sigDataBad[8] =
  53098. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  53099. wolfSSL_ECDSA_SIG_free(NULL);
  53100. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  53101. ExpectNotNull(r = wolfSSL_BN_new());
  53102. ExpectNotNull(s = wolfSSL_BN_new());
  53103. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  53104. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  53105. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  53106. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  53107. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  53108. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  53109. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  53110. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  53111. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  53112. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  53113. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  53114. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  53115. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  53116. r2 = NULL;
  53117. s2 = NULL;
  53118. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  53119. ExpectNull(r2);
  53120. ExpectNull(s2);
  53121. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  53122. if (EXPECT_FAIL()) {
  53123. wolfSSL_BN_free(r);
  53124. wolfSSL_BN_free(s);
  53125. }
  53126. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  53127. ExpectPtrEq(r2, r);
  53128. ExpectPtrEq(s2, s);
  53129. r2 = NULL;
  53130. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  53131. ExpectPtrEq(r2, r);
  53132. s2 = NULL;
  53133. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  53134. ExpectPtrEq(s2, s);
  53135. /* r and s are freed when sig is freed. */
  53136. wolfSSL_ECDSA_SIG_free(sig);
  53137. sig = NULL;
  53138. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  53139. cp = sigDataBad;
  53140. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  53141. cp = sigData;
  53142. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  53143. ExpectIntEQ((cp == sigData + 8), 1);
  53144. cp = sigData;
  53145. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  53146. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  53147. ExpectIntEQ((sig == sig2), 1);
  53148. cp = outSig;
  53149. p = outSig;
  53150. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  53151. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  53152. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  53153. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  53154. ExpectIntEQ((p == outSig + 8), 1);
  53155. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  53156. wolfSSL_ECDSA_SIG_free(sig);
  53157. #endif
  53158. return EXPECT_RESULT();
  53159. }
  53160. static int test_ECDSA_size_sign(void)
  53161. {
  53162. EXPECT_DECLS;
  53163. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  53164. EC_KEY* key = NULL;
  53165. ECDSA_SIG* ecdsaSig = NULL;
  53166. int id;
  53167. byte hash[WC_MAX_DIGEST_SIZE];
  53168. byte hash2[WC_MAX_DIGEST_SIZE];
  53169. byte sig[ECC_MAX_SIG_SIZE];
  53170. unsigned int sigSz = sizeof(sig);
  53171. XMEMSET(hash, 123, sizeof(hash));
  53172. XMEMSET(hash2, 234, sizeof(hash2));
  53173. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  53174. ExpectIntEQ(id, ECC_SECP256R1);
  53175. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53176. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  53177. ExpectIntGE(ECDSA_size(NULL), 0);
  53178. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  53179. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  53180. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  53181. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  53182. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  53183. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  53184. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  53185. ExpectIntGE(ECDSA_size(key), sigSz);
  53186. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  53187. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  53188. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  53189. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  53190. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  53191. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  53192. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  53193. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  53194. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  53195. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  53196. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  53197. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  53198. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  53199. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  53200. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  53201. ECDSA_SIG_free(ecdsaSig);
  53202. EC_KEY_free(key);
  53203. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  53204. return EXPECT_RESULT();
  53205. }
  53206. static int test_ECDH_compute_key(void)
  53207. {
  53208. EXPECT_DECLS;
  53209. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53210. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53211. EC_KEY* key1 = NULL;
  53212. EC_KEY* key2 = NULL;
  53213. EC_POINT* pub1 = NULL;
  53214. EC_POINT* pub2 = NULL;
  53215. byte secret1[32];
  53216. byte secret2[32];
  53217. int i;
  53218. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53219. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  53220. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  53221. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53222. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  53223. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  53224. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  53225. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  53226. 0);
  53227. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  53228. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  53229. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  53230. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  53231. 0);
  53232. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  53233. 0);
  53234. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  53235. NULL), 0);
  53236. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  53237. sizeof(secret1));
  53238. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  53239. sizeof(secret2));
  53240. for (i = 0; i < (int)sizeof(secret1); i++) {
  53241. ExpectIntEQ(secret1[i], secret2[i]);
  53242. }
  53243. EC_KEY_free(key2);
  53244. EC_KEY_free(key1);
  53245. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  53246. * !WOLF_CRYPTO_CB_ONLY_ECC */
  53247. return EXPECT_RESULT();
  53248. }
  53249. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  53250. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  53251. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  53252. !defined(NO_ASN_TIME)
  53253. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  53254. int expectedDerSz)
  53255. {
  53256. EXPECT_DECLS;
  53257. X509* x509 = NULL;
  53258. BIGNUM* serial_number = NULL;
  53259. X509_NAME* name = NULL;
  53260. time_t epoch_off = 0;
  53261. ASN1_INTEGER* asn1_serial_number;
  53262. long not_before, not_after;
  53263. int derSz;
  53264. ExpectNotNull(x509 = X509_new());
  53265. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  53266. ExpectNotNull(serial_number = BN_new());
  53267. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  53268. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  53269. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  53270. /* version 3 */
  53271. ExpectIntNE(X509_set_version(x509, 2L), 0);
  53272. ExpectNotNull(name = X509_NAME_new());
  53273. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  53274. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  53275. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  53276. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  53277. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  53278. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  53279. not_before = (long)wc_Time(NULL);
  53280. not_after = not_before + (365 * 24 * 60 * 60);
  53281. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  53282. &epoch_off));
  53283. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  53284. &epoch_off));
  53285. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  53286. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  53287. ExpectIntGE(derSz, expectedDerSz);
  53288. BN_free(serial_number);
  53289. X509_NAME_free(name);
  53290. X509_free(x509);
  53291. return EXPECT_RESULT();
  53292. }
  53293. #endif
  53294. static int test_openssl_generate_key_and_cert(void)
  53295. {
  53296. EXPECT_DECLS;
  53297. #if defined(OPENSSL_EXTRA)
  53298. int expectedDerSz;
  53299. EVP_PKEY* pkey = NULL;
  53300. #ifdef HAVE_ECC
  53301. EC_KEY* ec_key = NULL;
  53302. #endif
  53303. #if !defined(NO_RSA)
  53304. int key_length = 2048;
  53305. BIGNUM* exponent = NULL;
  53306. RSA* rsa = NULL;
  53307. ExpectNotNull(pkey = EVP_PKEY_new());
  53308. ExpectNotNull(exponent = BN_new());
  53309. ExpectNotNull(rsa = RSA_new());
  53310. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  53311. #ifndef WOLFSSL_KEY_GEN
  53312. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  53313. #if defined(USE_CERT_BUFFERS_1024)
  53314. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  53315. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  53316. key_length = 1024;
  53317. #elif defined(USE_CERT_BUFFERS_2048)
  53318. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  53319. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  53320. #else
  53321. RSA_free(rsa);
  53322. rsa = NULL;
  53323. #endif
  53324. #else
  53325. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  53326. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  53327. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  53328. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  53329. #endif
  53330. if (rsa) {
  53331. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  53332. if (EXPECT_FAIL()) {
  53333. RSA_free(rsa);
  53334. }
  53335. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  53336. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53337. expectedDerSz = 743;
  53338. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  53339. expectedDerSz), TEST_SUCCESS);
  53340. #endif
  53341. }
  53342. EVP_PKEY_free(pkey);
  53343. pkey = NULL;
  53344. BN_free(exponent);
  53345. #endif /* !NO_RSA */
  53346. #ifdef HAVE_ECC
  53347. ExpectNotNull(pkey = EVP_PKEY_new());
  53348. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53349. #ifndef NO_WOLFSSL_STUB
  53350. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  53351. #endif
  53352. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  53353. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  53354. if (EXPECT_FAIL()) {
  53355. EC_KEY_free(ec_key);
  53356. }
  53357. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  53358. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  53359. expectedDerSz = 344;
  53360. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  53361. TEST_SUCCESS);
  53362. #endif
  53363. EVP_PKEY_free(pkey);
  53364. #endif /* HAVE_ECC */
  53365. (void)pkey;
  53366. (void)expectedDerSz;
  53367. #endif /* OPENSSL_EXTRA */
  53368. return EXPECT_RESULT();
  53369. }
  53370. static int test_stubs_are_stubs(void)
  53371. {
  53372. EXPECT_DECLS;
  53373. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  53374. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  53375. WOLFSSL_CTX* ctx = NULL;
  53376. WOLFSSL_CTX* ctxN = NULL;
  53377. #ifndef NO_WOLFSSL_CLIENT
  53378. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53379. #elif !defined(NO_WOLFSSL_SERVER)
  53380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53381. #endif
  53382. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  53383. ExpectIntEQ((int) x(z), 0)
  53384. /* test logic, all stubs return same result regardless of ctx being NULL
  53385. * as there are no sanity checks, it's just a stub! If at some
  53386. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  53387. * if invalid inputs are supplied. Test calling both
  53388. * with and without valid inputs, if a stub functionality remains unchanged.
  53389. */
  53390. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  53391. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  53392. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  53393. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  53394. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  53395. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  53396. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  53397. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  53398. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  53399. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  53400. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  53401. wolfSSL_CTX_free(ctx);
  53402. ctx = NULL;
  53403. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  53404. * !NO_WOLFSSL_SERVER) */
  53405. return EXPECT_RESULT();
  53406. }
  53407. static int test_CONF_modules_xxx(void)
  53408. {
  53409. int res = TEST_SKIPPED;
  53410. #if defined(OPENSSL_EXTRA)
  53411. CONF_modules_free();
  53412. CONF_modules_unload(0);
  53413. CONF_modules_unload(1);
  53414. CONF_modules_unload(-1);
  53415. res = TEST_SUCCESS;
  53416. #endif /* OPENSSL_EXTRA */
  53417. return res;
  53418. }
  53419. static int test_CRYPTO_set_dynlock_xxx(void)
  53420. {
  53421. int res = TEST_SKIPPED;
  53422. #if defined(OPENSSL_EXTRA)
  53423. CRYPTO_set_dynlock_create_callback(
  53424. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  53425. CRYPTO_set_dynlock_create_callback(
  53426. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  53427. CRYPTO_set_dynlock_destroy_callback(
  53428. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  53429. CRYPTO_set_dynlock_destroy_callback(
  53430. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  53431. CRYPTO_set_dynlock_lock_callback(
  53432. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  53433. CRYPTO_set_dynlock_lock_callback(
  53434. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  53435. res = TEST_SUCCESS;
  53436. #endif /* OPENSSL_EXTRA */
  53437. return res;
  53438. }
  53439. static int test_CRYPTO_THREADID_xxx(void)
  53440. {
  53441. EXPECT_DECLS;
  53442. #if defined(OPENSSL_EXTRA)
  53443. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  53444. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  53445. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  53446. #endif /* OPENSSL_EXTRA */
  53447. return EXPECT_RESULT();
  53448. }
  53449. static int test_ENGINE_cleanup(void)
  53450. {
  53451. int res = TEST_SKIPPED;
  53452. #if defined(OPENSSL_EXTRA)
  53453. ENGINE_cleanup();
  53454. res = TEST_SUCCESS;
  53455. #endif /* OPENSSL_EXTRA */
  53456. return res;
  53457. }
  53458. static int test_wolfSSL_CTX_LoadCRL(void)
  53459. {
  53460. EXPECT_DECLS;
  53461. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  53462. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  53463. WOLFSSL_CTX* ctx = NULL;
  53464. WOLFSSL* ssl = NULL;
  53465. const char* badPath = "dummypath";
  53466. const char* validPath = "./certs/crl";
  53467. const char* validFilePath = "./certs/crl/cliCrl.pem";
  53468. const char* issuerCert = "./certs/client-cert.pem";
  53469. int derType = WOLFSSL_FILETYPE_ASN1;
  53470. int pemType = WOLFSSL_FILETYPE_PEM;
  53471. #ifdef HAVE_CRL_MONITOR
  53472. int monitor = WOLFSSL_CRL_MONITOR;
  53473. #else
  53474. int monitor = 0;
  53475. #endif
  53476. WOLFSSL_CERT_MANAGER* cm = NULL;
  53477. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53478. BAD_FUNC_ARG)
  53479. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53480. NOT_COMPILED_IN)
  53481. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  53482. WOLFSSL_SUCCESS)
  53483. #ifndef NO_WOLFSSL_CLIENT
  53484. #define NEW_CTX(ctx) ExpectNotNull( \
  53485. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  53486. #elif !defined(NO_WOLFSSL_SERVER)
  53487. #define NEW_CTX(ctx) ExpectNotNull( \
  53488. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  53489. #else
  53490. #define NEW_CTX(ctx) return
  53491. #endif
  53492. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53493. NEW_CTX(ctx);
  53494. #ifndef HAVE_CRL_MONITOR
  53495. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  53496. wolfSSL_CTX_free(ctx);
  53497. NEW_CTX(ctx);
  53498. #endif
  53499. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  53500. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  53501. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  53502. wolfSSL_CTX_free(ctx);
  53503. ctx = NULL;
  53504. NEW_CTX(ctx);
  53505. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53506. WOLFSSL_SUCCESS);
  53507. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  53508. wolfSSL_CTX_free(ctx);
  53509. ctx = NULL;
  53510. NEW_CTX(ctx);
  53511. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  53512. WOLFSSL_SUCCESS);
  53513. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53514. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  53515. wolfSSL_free(ssl);
  53516. ssl = NULL;
  53517. wolfSSL_CTX_free(ctx);
  53518. ctx = NULL;
  53519. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53520. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  53521. WOLFSSL_SUCCESS);
  53522. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  53523. WOLFSSL_SUCCESS);
  53524. wolfSSL_CertManagerFree(cm);
  53525. #endif
  53526. return EXPECT_RESULT();
  53527. }
  53528. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53529. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  53530. {
  53531. EXPECT_DECLS;
  53532. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53533. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  53534. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53535. return EXPECT_RESULT();
  53536. }
  53537. #endif
  53538. static int test_multiple_crls_same_issuer(void)
  53539. {
  53540. EXPECT_DECLS;
  53541. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  53542. test_ssl_cbf client_cbs, server_cbs;
  53543. struct {
  53544. const char* server_cert;
  53545. const char* server_key;
  53546. } test_params[] = {
  53547. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  53548. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  53549. };
  53550. size_t i;
  53551. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  53552. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53553. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53554. server_cbs.certPemFile = test_params[i].server_cert;
  53555. server_cbs.keyPemFile = test_params[i].server_key;
  53556. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  53557. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  53558. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  53559. &server_cbs, NULL), TEST_FAIL);
  53560. }
  53561. #endif
  53562. return EXPECT_RESULT();
  53563. }
  53564. static int test_SetTmpEC_DHE_Sz(void)
  53565. {
  53566. EXPECT_DECLS;
  53567. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  53568. WOLFSSL_CTX *ctx = NULL;
  53569. WOLFSSL *ssl = NULL;
  53570. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53571. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  53572. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53573. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  53574. wolfSSL_free(ssl);
  53575. wolfSSL_CTX_free(ctx);
  53576. #endif
  53577. return EXPECT_RESULT();
  53578. }
  53579. static int test_wolfSSL_CTX_get0_privatekey(void)
  53580. {
  53581. EXPECT_DECLS;
  53582. #ifdef OPENSSL_ALL
  53583. WOLFSSL_CTX* ctx = NULL;
  53584. (void)ctx;
  53585. #ifndef NO_RSA
  53586. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53587. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53588. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53589. WOLFSSL_FILETYPE_PEM));
  53590. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53591. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53592. WOLFSSL_FILETYPE_PEM));
  53593. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53594. wolfSSL_CTX_free(ctx);
  53595. ctx = NULL;
  53596. #endif
  53597. #ifdef HAVE_ECC
  53598. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53599. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53600. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  53601. WOLFSSL_FILETYPE_PEM));
  53602. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  53603. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  53604. WOLFSSL_FILETYPE_PEM));
  53605. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  53606. wolfSSL_CTX_free(ctx);
  53607. #endif
  53608. #endif
  53609. return EXPECT_RESULT();
  53610. }
  53611. static int test_wolfSSL_dtls_set_mtu(void)
  53612. {
  53613. EXPECT_DECLS;
  53614. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  53615. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  53616. !defined(WOLFSSL_NO_TLS12)
  53617. WOLFSSL_CTX* ctx = NULL;
  53618. WOLFSSL* ssl = NULL;
  53619. const char* testCertFile;
  53620. const char* testKeyFile;
  53621. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  53622. #ifndef NO_RSA
  53623. testCertFile = svrCertFile;
  53624. testKeyFile = svrKeyFile;
  53625. #elif defined(HAVE_ECC)
  53626. testCertFile = eccCertFile;
  53627. testKeyFile = eccKeyFile;
  53628. #endif
  53629. if (testCertFile != NULL && testKeyFile != NULL) {
  53630. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  53631. WOLFSSL_FILETYPE_PEM));
  53632. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  53633. WOLFSSL_FILETYPE_PEM));
  53634. }
  53635. ExpectNotNull(ssl = wolfSSL_new(ctx));
  53636. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53637. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  53638. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  53639. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  53640. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  53641. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  53642. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  53643. wolfSSL_free(ssl);
  53644. wolfSSL_CTX_free(ctx);
  53645. #endif
  53646. return EXPECT_RESULT();
  53647. }
  53648. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53649. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53650. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  53651. enum HandShakeType hsType, word16 length)
  53652. {
  53653. size_t idx = 0;
  53654. byte* l;
  53655. /* record layer */
  53656. /* handshake type */
  53657. out[idx++] = handshake;
  53658. /* protocol version */
  53659. out[idx++] = 0xfe;
  53660. out[idx++] = 0xfd; /* DTLS 1.2 */
  53661. /* epoch 0 */
  53662. XMEMSET(out + idx, 0, 2);
  53663. idx += 2;
  53664. /* sequence number */
  53665. XMEMSET(out + idx, 0, 6);
  53666. c32toa(seq, out + idx + 2);
  53667. idx += 6;
  53668. /* length in BE */
  53669. if (length)
  53670. c16toa(length, out + idx);
  53671. else
  53672. c16toa(outSz - idx - 2, out + idx);
  53673. idx += 2;
  53674. /* handshake layer */
  53675. /* handshake type */
  53676. out[idx++] = (byte)hsType;
  53677. /* length */
  53678. l = out + idx;
  53679. idx += 3;
  53680. /* message seq */
  53681. c16toa(0, out + idx);
  53682. idx += 2;
  53683. /* frag offset */
  53684. c32to24(0, out + idx);
  53685. idx += 3;
  53686. /* frag length */
  53687. c32to24((word32)outSz - (word32)idx - 3, l);
  53688. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  53689. idx += 3;
  53690. XMEMSET(out + idx, 0, outSz - idx);
  53691. }
  53692. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  53693. {
  53694. byte msg[] = "This is a msg for the client";
  53695. byte reply[40];
  53696. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53697. reply[sizeof(reply) - 1] = '\0';
  53698. fprintf(stderr, "Client message: %s\n", reply);
  53699. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53700. }
  53701. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  53702. {
  53703. byte ch[50];
  53704. int fd = wolfSSL_get_fd(ssl);
  53705. byte msg[] = "This is a msg for the server";
  53706. byte reply[40];
  53707. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  53708. /* Server should ignore this datagram */
  53709. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53710. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  53711. /* Server should ignore this datagram */
  53712. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  53713. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  53714. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  53715. reply[sizeof(reply) - 1] = '\0';
  53716. fprintf(stderr, "Server response: %s\n", reply);
  53717. }
  53718. static int test_wolfSSL_dtls_plaintext(void)
  53719. {
  53720. callback_functions func_cb_client;
  53721. callback_functions func_cb_server;
  53722. size_t i;
  53723. struct test_params {
  53724. method_provider client_meth;
  53725. method_provider server_meth;
  53726. ssl_callback on_result_server;
  53727. ssl_callback on_result_client;
  53728. } params[] = {
  53729. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53730. test_wolfSSL_dtls_plaintext_server,
  53731. test_wolfSSL_dtls_plaintext_client},
  53732. };
  53733. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53734. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53735. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53736. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53737. func_cb_server.method = params[i].server_meth;
  53738. func_cb_client.method = params[i].client_meth;
  53739. func_cb_client.on_result = params[i].on_result_client;
  53740. func_cb_server.on_result = params[i].on_result_server;
  53741. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53742. if (!func_cb_client.return_code)
  53743. return TEST_FAIL;
  53744. if (!func_cb_server.return_code)
  53745. return TEST_FAIL;
  53746. }
  53747. return TEST_RES_CHECK(1);
  53748. }
  53749. #else
  53750. static int test_wolfSSL_dtls_plaintext(void) {
  53751. return TEST_SKIPPED;
  53752. }
  53753. #endif
  53754. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  53755. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53756. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  53757. {
  53758. byte b[1100]; /* buffer for the messages to send */
  53759. size_t idx = 0;
  53760. size_t seq_offset = 0;
  53761. size_t msg_offset = 0;
  53762. int i;
  53763. int fd = wolfSSL_get_fd(ssl);
  53764. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53765. word32 seq_number = 100; /* start high so server definitely reads this */
  53766. word16 msg_number = 50; /* start high so server has to buffer this */
  53767. AssertIntEQ(ret, 1);
  53768. /* Now let's start spamming the peer with fragments it needs to store */
  53769. XMEMSET(b, -1, sizeof(b));
  53770. /* record layer */
  53771. /* handshake type */
  53772. b[idx++] = 22;
  53773. /* protocol version */
  53774. b[idx++] = 0xfe;
  53775. b[idx++] = 0xfd; /* DTLS 1.2 */
  53776. /* epoch 0 */
  53777. XMEMSET(b + idx, 0, 2);
  53778. idx += 2;
  53779. /* sequence number */
  53780. XMEMSET(b + idx, 0, 6);
  53781. seq_offset = idx + 2; /* increment only the low 32 bits */
  53782. idx += 6;
  53783. /* static length in BE */
  53784. c16toa(42, b + idx);
  53785. idx += 2;
  53786. /* handshake layer */
  53787. /* cert type */
  53788. b[idx++] = 11;
  53789. /* length */
  53790. c32to24(1000, b + idx);
  53791. idx += 3;
  53792. /* message seq */
  53793. c16toa(0, b + idx);
  53794. msg_offset = idx;
  53795. idx += 2;
  53796. /* frag offset */
  53797. c32to24(500, b + idx);
  53798. idx += 3;
  53799. /* frag length */
  53800. c32to24(30, b + idx);
  53801. idx += 3;
  53802. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  53803. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  53804. seq_number++, msg_number++, i++) {
  53805. struct timespec delay;
  53806. XMEMSET(&delay, 0, sizeof(delay));
  53807. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53808. c32toa(seq_number, b + seq_offset);
  53809. c16toa(msg_number, b + msg_offset);
  53810. ret = (int)send(fd, b, 55, 0);
  53811. nanosleep(&delay, NULL);
  53812. }
  53813. }
  53814. #ifdef WOLFSSL_DTLS13
  53815. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  53816. {
  53817. const word16 sendCountMax = 100;
  53818. byte b[150]; /* buffer for the messages to send */
  53819. size_t idx = 0;
  53820. size_t msg_offset = 0;
  53821. int fd = wolfSSL_get_fd(ssl);
  53822. word16 msg_number = 10; /* start high so server has to buffer this */
  53823. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  53824. AssertIntEQ(ret, 1);
  53825. /* Now let's start spamming the peer with fragments it needs to store */
  53826. XMEMSET(b, -1, sizeof(b));
  53827. /* handshake type */
  53828. b[idx++] = 11;
  53829. /* length */
  53830. c32to24(10000, b + idx);
  53831. idx += 3;
  53832. /* message_seq */
  53833. msg_offset = idx;
  53834. idx += 2;
  53835. /* fragment_offset */
  53836. c32to24(5000, b + idx);
  53837. idx += 3;
  53838. /* fragment_length */
  53839. c32to24(100, b + idx);
  53840. idx += 3;
  53841. /* fragment contents */
  53842. idx += 100;
  53843. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  53844. byte sendBuf[150];
  53845. int sendSz = sizeof(sendBuf);
  53846. struct timespec delay;
  53847. XMEMSET(&delay, 0, sizeof(delay));
  53848. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  53849. c16toa(msg_number, b + msg_offset);
  53850. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  53851. (int)idx, handshake, 0, 0, 0);
  53852. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  53853. nanosleep(&delay, NULL);
  53854. }
  53855. }
  53856. #endif
  53857. static int test_wolfSSL_dtls_fragments(void)
  53858. {
  53859. EXPECT_DECLS;
  53860. callback_functions func_cb_client;
  53861. callback_functions func_cb_server;
  53862. size_t i;
  53863. struct test_params {
  53864. method_provider client_meth;
  53865. method_provider server_meth;
  53866. ssl_callback spammer;
  53867. } params[] = {
  53868. #if !defined(WOLFSSL_NO_TLS12)
  53869. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53870. test_wolfSSL_dtls12_fragments_spammer},
  53871. #endif
  53872. #ifdef WOLFSSL_DTLS13
  53873. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53874. test_wolfSSL_dtls13_fragments_spammer},
  53875. #endif
  53876. };
  53877. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  53878. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  53879. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  53880. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  53881. func_cb_server.method = params[i].server_meth;
  53882. func_cb_client.method = params[i].client_meth;
  53883. func_cb_client.ssl_ready = params[i].spammer;
  53884. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  53885. ExpectFalse(func_cb_client.return_code);
  53886. ExpectFalse(func_cb_server.return_code);
  53887. /* The socket should be closed by the server resulting in a
  53888. * socket error, fatal error or reading a close notify alert */
  53889. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  53890. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  53891. func_cb_client.last_err != FATAL_ERROR) {
  53892. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  53893. }
  53894. /* Check the server returned an error indicating the msg buffer
  53895. * was full */
  53896. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  53897. if (EXPECT_FAIL())
  53898. break;
  53899. }
  53900. return EXPECT_RESULT();
  53901. }
  53902. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  53903. {
  53904. int fd, ret;
  53905. byte alert_msg[] = {
  53906. 0x15, /* alert type */
  53907. 0xfe, 0xfd, /* version */
  53908. 0x00, 0x00, /* epoch */
  53909. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  53910. 0x00, 0x02, /* length */
  53911. 0x02, /* level: fatal */
  53912. 0x46 /* protocol version */
  53913. };
  53914. fd = wolfSSL_get_fd(ssl);
  53915. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  53916. AssertIntGT(ret, 0);
  53917. }
  53918. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  53919. {
  53920. callback_functions client_cbs, server_cbs;
  53921. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53922. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53923. client_cbs.doUdp = server_cbs.doUdp = 1;
  53924. if (version12) {
  53925. #if !defined(WOLFSSL_NO_TLS12)
  53926. client_cbs.method = wolfDTLSv1_2_client_method;
  53927. server_cbs.method = wolfDTLSv1_2_server_method;
  53928. #else
  53929. return TEST_SKIPPED;
  53930. #endif
  53931. }
  53932. else
  53933. {
  53934. #ifdef WOLFSSL_DTLS13
  53935. client_cbs.method = wolfDTLSv1_3_client_method;
  53936. server_cbs.method = wolfDTLSv1_3_server_method;
  53937. #else
  53938. return TEST_SKIPPED;
  53939. #endif /* WOLFSSL_DTLS13 */
  53940. }
  53941. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  53942. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53943. if (!client_cbs.return_code)
  53944. return TEST_FAIL;
  53945. if (!server_cbs.return_code)
  53946. return TEST_FAIL;
  53947. return TEST_SUCCESS;
  53948. }
  53949. static int test_wolfSSL_ignore_alert_before_cookie(void)
  53950. {
  53951. int ret;
  53952. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  53953. if (ret != 0)
  53954. return ret;
  53955. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  53956. if (ret != 0)
  53957. return ret;
  53958. return 0;
  53959. }
  53960. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  53961. {
  53962. int ret;
  53963. int fd;
  53964. byte bad_msg[] = {
  53965. 0x17, /* app data */
  53966. 0xaa, 0xfd, /* bad version */
  53967. 0x00, 0x01, /* epoch 1 */
  53968. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  53969. 0x00, 0x26, /* length: 38 bytes */
  53970. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  53971. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  53972. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  53973. 0x03, 0x18, 0x72
  53974. };
  53975. fd = wolfSSL_get_fd(ssl);
  53976. AssertIntGE(fd, 0);
  53977. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  53978. AssertIntEQ(ret, sizeof(bad_msg));
  53979. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  53980. AssertIntEQ(ret, sizeof("badrecordtest"));
  53981. }
  53982. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  53983. {
  53984. byte buf[100];
  53985. int ret;
  53986. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  53987. AssertIntGT(ret, 0);
  53988. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  53989. }
  53990. static int _test_wolfSSL_dtls_bad_record(
  53991. method_provider client_method, method_provider server_method)
  53992. {
  53993. callback_functions client_cbs, server_cbs;
  53994. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53995. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53996. client_cbs.doUdp = server_cbs.doUdp = 1;
  53997. client_cbs.method = client_method;
  53998. server_cbs.method = server_method;
  53999. client_cbs.on_result = test_wolfSSL_send_bad_record;
  54000. server_cbs.on_result = test_wolfSSL_read_string;
  54001. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54002. if (!client_cbs.return_code)
  54003. return TEST_FAIL;
  54004. if (!server_cbs.return_code)
  54005. return TEST_FAIL;
  54006. return TEST_SUCCESS;
  54007. }
  54008. static int test_wolfSSL_dtls_bad_record(void)
  54009. {
  54010. int ret = TEST_SUCCESS;
  54011. #if !defined(WOLFSSL_NO_TLS12)
  54012. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  54013. wolfDTLSv1_2_server_method);
  54014. #endif
  54015. #ifdef WOLFSSL_DTLS13
  54016. if (ret == TEST_SUCCESS) {
  54017. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  54018. wolfDTLSv1_3_server_method);
  54019. }
  54020. #endif /* WOLFSSL_DTLS13 */
  54021. return ret;
  54022. }
  54023. #else
  54024. static int test_wolfSSL_dtls_fragments(void) {
  54025. return TEST_SKIPPED;
  54026. }
  54027. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  54028. return TEST_SKIPPED;
  54029. }
  54030. static int test_wolfSSL_dtls_bad_record(void) {
  54031. return TEST_SKIPPED;
  54032. }
  54033. #endif
  54034. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  54035. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54036. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54037. static byte test_AEAD_fail_decryption = 0;
  54038. static byte test_AEAD_seq_num = 0;
  54039. static byte test_AEAD_done = 0;
  54040. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  54041. {
  54042. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  54043. if (ret > 0) {
  54044. if (test_AEAD_fail_decryption) {
  54045. /* Modify the packet to trigger a decryption failure */
  54046. buf[ret/2] ^= 0xFF;
  54047. if (test_AEAD_fail_decryption == 1)
  54048. test_AEAD_fail_decryption = 0;
  54049. }
  54050. }
  54051. (void)ctx;
  54052. return ret;
  54053. }
  54054. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  54055. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  54056. {
  54057. if (sendLimit)
  54058. w64Zero(sendLimit);
  54059. switch (ssl->specs.bulk_cipher_algorithm) {
  54060. case wolfssl_aes_gcm:
  54061. if (sendLimit)
  54062. *sendLimit = AEAD_AES_LIMIT;
  54063. FALL_THROUGH;
  54064. case wolfssl_chacha:
  54065. if (hardLimit)
  54066. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  54067. if (keyUpdateLimit)
  54068. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  54069. break;
  54070. case wolfssl_aes_ccm:
  54071. if (sendLimit)
  54072. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  54073. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  54074. if (hardLimit)
  54075. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  54076. if (keyUpdateLimit)
  54077. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  54078. }
  54079. else {
  54080. if (hardLimit)
  54081. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  54082. if (keyUpdateLimit)
  54083. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  54084. }
  54085. break;
  54086. default:
  54087. fprintf(stderr, "Unrecognized bulk cipher");
  54088. AssertFalse(1);
  54089. break;
  54090. }
  54091. }
  54092. static void test_AEAD_limit_client(WOLFSSL* ssl)
  54093. {
  54094. int ret;
  54095. int i;
  54096. int didReKey = 0;
  54097. char msgBuf[20];
  54098. w64wrapper hardLimit;
  54099. w64wrapper keyUpdateLimit;
  54100. w64wrapper counter;
  54101. w64wrapper sendLimit;
  54102. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  54103. w64Zero(&counter);
  54104. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  54105. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  54106. for (i = 0; i < 10; i++) {
  54107. /* Test some failed decryptions */
  54108. test_AEAD_fail_decryption = 1;
  54109. w64Increment(&counter);
  54110. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54111. /* Should succeed since decryption failures are dropped */
  54112. AssertIntGT(ret, 0);
  54113. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  54114. }
  54115. test_AEAD_fail_decryption = 1;
  54116. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  54117. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  54118. /* 100 read calls should be enough to complete the key update */
  54119. w64Zero(&counter);
  54120. for (i = 0; i < 100; i++) {
  54121. /* Key update should be sent and negotiated */
  54122. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54123. AssertIntGT(ret, 0);
  54124. /* Epoch after one key update is 4 */
  54125. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  54126. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  54127. didReKey = 1;
  54128. break;
  54129. }
  54130. }
  54131. AssertTrue(didReKey);
  54132. if (!w64IsZero(sendLimit)) {
  54133. /* Test the sending limit for AEAD ciphers */
  54134. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  54135. test_AEAD_seq_num = 1;
  54136. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  54137. AssertIntGT(ret, 0);
  54138. didReKey = 0;
  54139. w64Zero(&counter);
  54140. /* 100 read calls should be enough to complete the key update */
  54141. for (i = 0; i < 100; i++) {
  54142. /* Key update should be sent and negotiated */
  54143. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54144. AssertIntGT(ret, 0);
  54145. /* Epoch after another key update is 5 */
  54146. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  54147. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  54148. didReKey = 1;
  54149. break;
  54150. }
  54151. }
  54152. AssertTrue(didReKey);
  54153. }
  54154. test_AEAD_fail_decryption = 2;
  54155. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  54156. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  54157. /* Connection should fail with a DECRYPT_ERROR */
  54158. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54159. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  54160. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  54161. test_AEAD_done = 1;
  54162. }
  54163. int counter = 0;
  54164. static void test_AEAD_limit_server(WOLFSSL* ssl)
  54165. {
  54166. char msgBuf[] = "Sending data";
  54167. int ret = WOLFSSL_SUCCESS;
  54168. w64wrapper sendLimit;
  54169. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54170. struct timespec delay;
  54171. XMEMSET(&delay, 0, sizeof(delay));
  54172. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  54173. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  54174. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54175. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  54176. while (!test_AEAD_done && ret > 0) {
  54177. counter++;
  54178. if (test_AEAD_seq_num) {
  54179. /* We need to update the seq number so that we can understand the
  54180. * peer. Otherwise we will incorrectly interpret the seq number. */
  54181. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  54182. AssertNotNull(e);
  54183. e->nextPeerSeqNumber = sendLimit;
  54184. test_AEAD_seq_num = 0;
  54185. }
  54186. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  54187. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  54188. nanosleep(&delay, NULL);
  54189. }
  54190. }
  54191. static int test_wolfSSL_dtls_AEAD_limit(void)
  54192. {
  54193. callback_functions func_cb_client;
  54194. callback_functions func_cb_server;
  54195. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54196. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54197. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54198. func_cb_server.method = wolfDTLSv1_3_server_method;
  54199. func_cb_client.method = wolfDTLSv1_3_client_method;
  54200. func_cb_server.on_result = test_AEAD_limit_server;
  54201. func_cb_client.on_result = test_AEAD_limit_client;
  54202. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54203. if (!func_cb_client.return_code)
  54204. return TEST_FAIL;
  54205. if (!func_cb_server.return_code)
  54206. return TEST_FAIL;
  54207. return TEST_SUCCESS;
  54208. }
  54209. #else
  54210. static int test_wolfSSL_dtls_AEAD_limit(void)
  54211. {
  54212. return TEST_SKIPPED;
  54213. }
  54214. #endif
  54215. #if defined(WOLFSSL_DTLS) && \
  54216. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  54217. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  54218. {
  54219. int fd, ret;
  54220. byte ch_msg[] = {
  54221. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  54222. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  54223. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  54224. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  54225. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  54226. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  54227. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  54228. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  54229. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  54230. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  54231. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  54232. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  54233. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  54234. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  54235. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  54236. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  54237. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  54238. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  54239. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  54240. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  54241. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  54242. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  54243. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  54244. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  54245. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  54246. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  54247. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  54248. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  54249. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  54250. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  54251. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  54252. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  54253. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  54254. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  54255. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  54256. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  54257. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  54258. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  54259. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  54260. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  54261. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  54262. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  54263. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  54264. 0x31, 0x68, 0x4c
  54265. };
  54266. fd = wolfSSL_get_fd(ssl);
  54267. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  54268. AssertIntGT(ret, 0);
  54269. /* consume the HRR otherwise handshake will fail */
  54270. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  54271. AssertIntGT(ret, 0);
  54272. }
  54273. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54274. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  54275. {
  54276. int fd, ret;
  54277. byte ch_msh_invalid_cookie[] = {
  54278. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  54279. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  54280. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  54281. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  54282. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  54283. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  54284. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  54285. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  54286. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  54287. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  54288. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  54289. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  54290. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  54291. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  54292. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54293. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54294. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54295. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54296. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54297. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  54298. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  54299. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  54300. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  54301. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  54302. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  54303. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  54304. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  54305. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  54306. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  54307. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  54308. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  54309. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  54310. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  54311. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  54312. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  54313. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  54314. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  54315. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  54316. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  54317. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  54318. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  54319. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  54320. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  54321. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  54322. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  54323. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  54324. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  54325. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  54326. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  54327. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  54328. 0x23, 0x00, 0x00
  54329. };
  54330. byte alert_reply[50];
  54331. byte expected_alert_reply[] = {
  54332. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  54333. 0x02, 0x02, 0x2f
  54334. };
  54335. fd = wolfSSL_get_fd(ssl);
  54336. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  54337. AssertIntGT(ret, 0);
  54338. /* should reply with an illegal_parameter reply */
  54339. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  54340. AssertIntEQ(ret, sizeof(expected_alert_reply));
  54341. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  54342. }
  54343. #endif
  54344. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  54345. {
  54346. #ifndef NO_MD5
  54347. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  54348. #elif !defined(NO_SHA)
  54349. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  54350. #elif !defined(NO_SHA256)
  54351. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  54352. #else
  54353. #error "We need a digest to hash the WOLFSSL object"
  54354. #endif
  54355. byte hashBuf[WC_MAX_DIGEST_SIZE];
  54356. wc_HashAlg hash;
  54357. const TLSX* exts = ssl->extensions;
  54358. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  54359. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  54360. * InitHandshakeHashes */
  54361. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  54362. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  54363. /* Following fields are not important to compare */
  54364. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  54365. sslCopy.buffers.inputBuffer.buffer = NULL;
  54366. sslCopy.buffers.inputBuffer.bufferSize = 0;
  54367. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  54368. sslCopy.buffers.inputBuffer.offset = 0;
  54369. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  54370. sslCopy.buffers.outputBuffer.buffer = NULL;
  54371. sslCopy.buffers.outputBuffer.bufferSize = 0;
  54372. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  54373. sslCopy.buffers.outputBuffer.offset = 0;
  54374. sslCopy.error = 0;
  54375. sslCopy.curSize = 0;
  54376. sslCopy.curStartIdx = 0;
  54377. sslCopy.keys.curSeq_lo = 0;
  54378. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  54379. #ifdef WOLFSSL_DTLS13
  54380. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  54381. sslCopy.dtls13FastTimeout = 0;
  54382. #endif
  54383. sslCopy.keys.dtls_peer_handshake_number = 0;
  54384. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  54385. sslCopy.hsHashes = NULL;
  54386. #ifdef WOLFSSL_ASYNC_IO
  54387. #ifdef WOLFSSL_ASYNC_CRYPT
  54388. sslCopy.asyncDev = NULL;
  54389. #endif
  54390. sslCopy.async = NULL;
  54391. #endif
  54392. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  54393. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  54394. /* hash extension list */
  54395. while (exts != NULL) {
  54396. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  54397. exts = exts->next;
  54398. }
  54399. /* Hash suites */
  54400. if (sslCopy.suites != NULL) {
  54401. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  54402. sizeof(struct Suites)), 0);
  54403. }
  54404. /* Hash hsHashes */
  54405. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  54406. sizeof(*hsHashes)), 0);
  54407. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  54408. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  54409. return MakeWordFromHash(hashBuf);
  54410. }
  54411. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  54412. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  54413. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  54414. char *buf, int sz, void *ctx)
  54415. {
  54416. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  54417. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  54418. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  54419. }
  54420. else {
  54421. return WOLFSSL_CBIO_ERR_WANT_READ;
  54422. }
  54423. }
  54424. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  54425. {
  54426. /* Compare the ssl object before and after one ClientHello msg */
  54427. SOCKET_T fd = wolfSSL_get_fd(ssl);
  54428. int res;
  54429. int err;
  54430. word32 initHash;
  54431. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  54432. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  54433. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  54434. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  54435. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  54436. (void)initHash;
  54437. res = tcp_select(fd, 5);
  54438. /* We are expecting a msg. A timeout indicates failure. */
  54439. AssertIntEQ(res, TEST_RECV_READY);
  54440. res = wolfSSL_accept(ssl);
  54441. err = wolfSSL_get_error(ssl, res);
  54442. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  54443. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  54444. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  54445. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  54446. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  54447. }
  54448. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54449. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  54450. {
  54451. int ret;
  54452. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  54453. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  54454. test_wolfSSL_dtls_compare_stateless(ssl);
  54455. }
  54456. #endif
  54457. static int test_wolfSSL_dtls_stateless(void)
  54458. {
  54459. callback_functions client_cbs, server_cbs;
  54460. size_t i;
  54461. struct {
  54462. method_provider client_meth;
  54463. method_provider server_meth;
  54464. ssl_callback client_ssl_ready;
  54465. ssl_callback server_ssl_ready;
  54466. } test_params[] = {
  54467. #if !defined(WOLFSSL_NO_TLS12)
  54468. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54469. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  54470. #endif
  54471. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  54472. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54473. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  54474. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54475. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  54476. #endif
  54477. };
  54478. if (0 == sizeof(test_params)){
  54479. return TEST_SKIPPED;
  54480. }
  54481. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  54482. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54483. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54484. client_cbs.doUdp = server_cbs.doUdp = 1;
  54485. client_cbs.method = test_params[i].client_meth;
  54486. server_cbs.method = test_params[i].server_meth;
  54487. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  54488. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  54489. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54490. if (!client_cbs.return_code)
  54491. return TEST_FAIL;
  54492. if (!server_cbs.return_code)
  54493. return TEST_FAIL;
  54494. }
  54495. return TEST_SUCCESS;
  54496. }
  54497. #else
  54498. static int test_wolfSSL_dtls_stateless(void)
  54499. {
  54500. return TEST_SKIPPED;
  54501. }
  54502. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  54503. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  54504. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  54505. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  54506. !defined(WOLFSSL_NO_CLIENT_AUTH))
  54507. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54508. {
  54509. int ret;
  54510. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  54511. fprintf(stderr, "loading cert %s failed\n", certA);
  54512. fprintf(stderr, "Error: (%d): %s\n", ret,
  54513. wolfSSL_ERR_reason_error_string(ret));
  54514. return -1;
  54515. }
  54516. return 0;
  54517. }
  54518. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  54519. {
  54520. int ret;
  54521. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  54522. != WOLFSSL_SUCCESS) {
  54523. fprintf(stderr, "could not verify the cert: %s\n", certA);
  54524. fprintf(stderr, "Error: (%d): %s\n", ret,
  54525. wolfSSL_ERR_reason_error_string(ret));
  54526. return -1;
  54527. }
  54528. else {
  54529. fprintf(stderr, "successfully verified: %s\n", certA);
  54530. }
  54531. return 0;
  54532. }
  54533. #define LOAD_ONE_CA(a, b, c, d) \
  54534. do { \
  54535. (a) = load_ca_into_cm(c, d); \
  54536. if ((a) != 0) \
  54537. return (b); \
  54538. else \
  54539. (b)--; \
  54540. } while(0)
  54541. #define VERIFY_ONE_CERT(a, b, c, d) \
  54542. do { \
  54543. (a) = verify_cert_with_cm(c, d);\
  54544. if ((a) != 0) \
  54545. return (b); \
  54546. else \
  54547. (b)--; \
  54548. } while(0)
  54549. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  54550. {
  54551. int ret;
  54552. int i = -1;
  54553. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  54554. char chainGArr[9][50] = {"certs/ca-cert.pem",
  54555. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  54556. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  54557. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  54558. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  54559. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  54560. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  54561. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  54562. "certs/test-pathlen/chainG-entity.pem"};
  54563. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  54564. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  54565. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  54566. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  54567. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  54568. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  54569. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  54570. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  54571. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  54572. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  54573. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  54574. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  54575. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  54576. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  54577. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  54578. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  54579. /* test validating the entity twice, should have no effect on pathLen since
  54580. * entity/leaf cert */
  54581. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  54582. return ret;
  54583. }
  54584. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  54585. {
  54586. int ret;
  54587. int i = -1;
  54588. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  54589. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  54590. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  54591. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  54592. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  54593. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54594. */
  54595. char chainHArr[6][50] = {"certs/ca-cert.pem",
  54596. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  54597. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  54598. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  54599. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  54600. "certs/test-pathlen/chainH-entity.pem"};
  54601. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  54602. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  54603. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  54604. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  54605. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  54606. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  54607. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  54608. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  54609. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  54610. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  54611. return ret;
  54612. }
  54613. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  54614. {
  54615. int ret;
  54616. int i = -1;
  54617. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  54618. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  54619. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  54620. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  54621. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  54622. */
  54623. char chainIArr[5][50] = {"certs/ca-cert.pem",
  54624. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  54625. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  54626. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  54627. "certs/test-pathlen/chainI-entity.pem"};
  54628. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  54629. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  54630. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  54631. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  54632. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  54633. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  54634. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  54635. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  54636. return ret;
  54637. }
  54638. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  54639. {
  54640. int ret;
  54641. int i = -1;
  54642. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  54643. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  54644. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  54645. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  54646. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  54647. */
  54648. char chainJArr[6][50] = {"certs/ca-cert.pem",
  54649. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  54650. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  54651. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  54652. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  54653. "certs/test-pathlen/chainJ-entity.pem"};
  54654. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  54655. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  54656. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  54657. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  54658. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  54659. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  54660. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  54661. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  54662. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  54663. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  54664. return ret;
  54665. }
  54666. static int test_various_pathlen_chains(void)
  54667. {
  54668. EXPECT_DECLS;
  54669. WOLFSSL_CERT_MANAGER* cm = NULL;
  54670. /* Test chain G (large chain with varying pathLens) */
  54671. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54672. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54673. ExpectIntEQ(test_chainG(cm), -1);
  54674. #else
  54675. ExpectIntEQ(test_chainG(cm), 0);
  54676. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54677. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54678. wolfSSL_CertManagerFree(cm);
  54679. /* end test chain G */
  54680. /* Test chain H (5 chain with same pathLens) */
  54681. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54682. ExpectIntLT(test_chainH(cm), 0);
  54683. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54684. wolfSSL_CertManagerFree(cm);
  54685. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54686. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54687. wolfSSL_CertManagerFree(cm);
  54688. /* end test chain H */
  54689. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  54690. * followed by 2 ICA's, should pass) */
  54691. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54692. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  54693. ExpectIntEQ(test_chainI(cm), -1);
  54694. #else
  54695. ExpectIntEQ(test_chainI(cm), 0);
  54696. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  54697. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54698. wolfSSL_CertManagerFree(cm);
  54699. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54700. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54701. wolfSSL_CertManagerFree(cm);
  54702. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  54703. * this time followed by 3 ICA's, should fail */
  54704. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54705. ExpectIntLT(test_chainJ(cm), 0);
  54706. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54707. wolfSSL_CertManagerFree(cm);
  54708. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54709. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  54710. wolfSSL_CertManagerFree(cm);
  54711. return EXPECT_RESULT();
  54712. }
  54713. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  54714. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54715. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  54716. {
  54717. EXPECT_DECLS;
  54718. byte ekm[100] = {0};
  54719. (void)ctx;
  54720. /* Success Cases */
  54721. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54722. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  54723. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54724. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  54725. /* Use some random context */
  54726. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54727. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  54728. /* Failure cases */
  54729. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54730. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  54731. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54732. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  54733. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54734. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  54735. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54736. "extended master secret", XSTR_SIZEOF("extended master secret"),
  54737. NULL, 0, 0), 0);
  54738. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  54739. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  54740. return EXPECT_RESULT();
  54741. }
  54742. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  54743. {
  54744. wolfSSL_KeepArrays(ssl);
  54745. return TEST_SUCCESS;
  54746. }
  54747. static int test_export_keying_material(void)
  54748. {
  54749. EXPECT_DECLS;
  54750. test_ssl_cbf serverCb;
  54751. test_ssl_cbf clientCb;
  54752. XMEMSET(&serverCb, 0, sizeof(serverCb));
  54753. XMEMSET(&clientCb, 0, sizeof(clientCb));
  54754. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  54755. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  54756. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  54757. return EXPECT_RESULT();
  54758. }
  54759. #endif /* HAVE_KEYING_MATERIAL */
  54760. static int test_wolfSSL_THREADID_hash(void)
  54761. {
  54762. EXPECT_DECLS;
  54763. #if defined(OPENSSL_EXTRA)
  54764. CRYPTO_THREADID id;
  54765. CRYPTO_THREADID_current(NULL);
  54766. /* Hash result is unsigned long. */
  54767. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  54768. XMEMSET(&id, 0, sizeof(id));
  54769. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  54770. #endif /* OPENSSL_EXTRA */
  54771. return EXPECT_RESULT();
  54772. }
  54773. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  54774. {
  54775. EXPECT_DECLS;
  54776. #if defined(OPENSSL_EXTRA)
  54777. WOLFSSL_CTX* ctx = NULL;
  54778. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  54779. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  54780. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  54781. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  54782. #endif /* OPENSSL_EXTRA */
  54783. return EXPECT_RESULT();
  54784. }
  54785. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54786. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54787. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  54788. {
  54789. EXPECT_DECLS;
  54790. callback_functions* callbacks = NULL;
  54791. WOLFSSL_CTX* ctx = NULL;
  54792. WOLFSSL* ssl = NULL;
  54793. SOCKET_T sfd = 0;
  54794. SOCKET_T cfd = 0;
  54795. word16 port;
  54796. char msg[] = "I hear you fa shizzle!";
  54797. int len = (int) XSTRLEN(msg);
  54798. char input[1024];
  54799. int ret;
  54800. int err = 0;
  54801. if (!args)
  54802. WOLFSSL_RETURN_FROM_THREAD(0);
  54803. ((func_args*)args)->return_code = TEST_FAIL;
  54804. callbacks = ((func_args*)args)->callbacks;
  54805. ctx = wolfSSL_CTX_new(callbacks->method());
  54806. #if defined(USE_WINDOWS_API)
  54807. port = ((func_args*)args)->signal->port;
  54808. #else
  54809. /* Let tcp_listen assign port */
  54810. port = 0;
  54811. #endif
  54812. #ifdef WOLFSSL_TIRTOS
  54813. fdOpenSession(Task_self());
  54814. #endif
  54815. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  54816. caCertFile, 0));
  54817. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  54818. svrCertFile, WOLFSSL_FILETYPE_PEM));
  54819. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  54820. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  54821. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  54822. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  54823. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  54824. #elif !defined(NO_DH)
  54825. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  54826. #endif
  54827. if (callbacks->ctx_ready)
  54828. callbacks->ctx_ready(ctx);
  54829. ssl = wolfSSL_new(ctx);
  54830. ExpectNotNull(ssl);
  54831. /* listen and accept */
  54832. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  54833. CloseSocket(sfd);
  54834. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  54835. if (callbacks->ssl_ready)
  54836. callbacks->ssl_ready(ssl);
  54837. if (EXPECT_SUCCESS()) {
  54838. do {
  54839. err = 0; /* Reset error */
  54840. ret = wolfSSL_accept(ssl);
  54841. if (ret != WOLFSSL_SUCCESS) {
  54842. err = wolfSSL_get_error(ssl, 0);
  54843. }
  54844. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54845. }
  54846. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54847. /* read and write data */
  54848. XMEMSET(input, 0, sizeof(input));
  54849. while (EXPECT_SUCCESS()) {
  54850. ret = wolfSSL_read(ssl, input, sizeof(input));
  54851. if (ret > 0) {
  54852. break;
  54853. }
  54854. else {
  54855. err = wolfSSL_get_error(ssl,ret);
  54856. if (err == WOLFSSL_ERROR_WANT_READ) {
  54857. continue;
  54858. }
  54859. break;
  54860. }
  54861. }
  54862. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  54863. do {
  54864. ret = wolfSSL_write(ssl, msg, len);
  54865. if (ret > 0) {
  54866. break;
  54867. }
  54868. } while (ret < 0);
  54869. }
  54870. /* bidirectional shutdown */
  54871. while (EXPECT_SUCCESS()) {
  54872. ret = wolfSSL_shutdown(ssl);
  54873. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  54874. if (ret == WOLFSSL_SUCCESS) {
  54875. break;
  54876. }
  54877. }
  54878. if (EXPECT_SUCCESS()) {
  54879. /* wait for the peer to disconnect the tcp connection */
  54880. do {
  54881. ret = wolfSSL_read(ssl, input, sizeof(input));
  54882. err = wolfSSL_get_error(ssl, ret);
  54883. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  54884. }
  54885. /* detect TCP disconnect */
  54886. ExpectIntLE(ret,WOLFSSL_FAILURE);
  54887. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  54888. ((func_args*)args)->return_code = EXPECT_RESULT();
  54889. wolfSSL_free(ssl);
  54890. wolfSSL_CTX_free(ctx);
  54891. CloseSocket(cfd);
  54892. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54893. wc_ecc_fp_free(); /* free per thread cache */
  54894. #endif
  54895. WOLFSSL_RETURN_FROM_THREAD(0);
  54896. }
  54897. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  54898. {
  54899. EXPECT_DECLS;
  54900. callback_functions* callbacks = NULL;
  54901. WOLFSSL_CTX* ctx = NULL;
  54902. WOLFSSL* ssl = NULL;
  54903. SOCKET_T sfd = 0;
  54904. char msg[] = "hello wolfssl server!";
  54905. int len = (int) XSTRLEN(msg);
  54906. char input[1024];
  54907. int idx;
  54908. int ret, err;
  54909. if (!args)
  54910. WOLFSSL_RETURN_FROM_THREAD(0);
  54911. ((func_args*)args)->return_code = TEST_FAIL;
  54912. callbacks = ((func_args*)args)->callbacks;
  54913. ctx = wolfSSL_CTX_new(callbacks->method());
  54914. #ifdef WOLFSSL_TIRTOS
  54915. fdOpenSession(Task_self());
  54916. #endif
  54917. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  54918. caCertFile, 0));
  54919. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  54920. cliCertFile, WOLFSSL_FILETYPE_PEM));
  54921. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  54922. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  54923. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  54924. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  54925. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  54926. if (EXPECT_SUCCESS()) {
  54927. do {
  54928. err = 0; /* Reset error */
  54929. ret = wolfSSL_connect(ssl);
  54930. if (ret != WOLFSSL_SUCCESS) {
  54931. err = wolfSSL_get_error(ssl, 0);
  54932. }
  54933. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  54934. }
  54935. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  54936. if (EXPECT_SUCCESS()) {
  54937. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  54938. input[idx] = 0;
  54939. }
  54940. }
  54941. if (EXPECT_SUCCESS()) {
  54942. ret = wolfSSL_shutdown(ssl);
  54943. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  54944. ret = wolfSSL_shutdown(ssl);
  54945. }
  54946. }
  54947. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54948. ((func_args*)args)->return_code = EXPECT_RESULT();
  54949. wolfSSL_free(ssl);
  54950. wolfSSL_CTX_free(ctx);
  54951. CloseSocket(sfd);
  54952. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  54953. wc_ecc_fp_free(); /* free per thread cache */
  54954. #endif
  54955. WOLFSSL_RETURN_FROM_THREAD(0);
  54956. }
  54957. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  54958. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  54959. /* This test is to check wolfSSL_read behaves as same as
  54960. * openSSL when it is called after SSL_shutdown completes.
  54961. */
  54962. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  54963. {
  54964. EXPECT_DECLS;
  54965. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  54966. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  54967. tcp_ready ready;
  54968. func_args client_args;
  54969. func_args server_args;
  54970. THREAD_TYPE serverThread;
  54971. THREAD_TYPE clientThread;
  54972. callback_functions server_cbf;
  54973. callback_functions client_cbf;
  54974. #ifdef WOLFSSL_TIRTOS
  54975. fdOpenSession(Task_self());
  54976. #endif
  54977. StartTCP();
  54978. InitTcpReady(&ready);
  54979. #if defined(USE_WINDOWS_API)
  54980. /* use RNG to get random port if using windows */
  54981. ready.port = GetRandomPort();
  54982. #endif
  54983. XMEMSET(&client_args, 0, sizeof(func_args));
  54984. XMEMSET(&server_args, 0, sizeof(func_args));
  54985. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  54986. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  54987. server_cbf.method = wolfTLSv1_2_server_method;
  54988. client_cbf.method = wolfTLSv1_2_client_method;
  54989. server_args.callbacks = &server_cbf;
  54990. client_args.callbacks = &client_cbf;
  54991. server_args.signal = &ready;
  54992. client_args.signal = &ready;
  54993. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  54994. wait_tcp_ready(&server_args);
  54995. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  54996. join_thread(clientThread);
  54997. join_thread(serverThread);
  54998. ExpectTrue(client_args.return_code);
  54999. ExpectTrue(server_args.return_code);
  55000. FreeTcpReady(&ready);
  55001. #endif
  55002. return EXPECT_RESULT();
  55003. }
  55004. static int test_wolfSSL_CTX_get_min_proto_version(void)
  55005. {
  55006. EXPECT_DECLS;
  55007. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55008. WOLFSSL_CTX *ctx = NULL;
  55009. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55010. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  55011. WOLFSSL_SUCCESS);
  55012. #ifdef WOLFSSL_ALLOW_SSLV3
  55013. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55014. #else
  55015. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  55016. #endif
  55017. wolfSSL_CTX_free(ctx);
  55018. ctx = NULL;
  55019. #ifdef WOLFSSL_ALLOW_TLSV10
  55020. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  55021. #else
  55022. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55023. #endif
  55024. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  55025. WOLFSSL_SUCCESS);
  55026. #ifdef WOLFSSL_ALLOW_TLSV10
  55027. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55028. #else
  55029. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  55030. #endif
  55031. wolfSSL_CTX_free(ctx);
  55032. ctx = NULL;
  55033. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55034. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  55035. WOLFSSL_SUCCESS);
  55036. #ifndef NO_OLD_TLS
  55037. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55038. #else
  55039. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  55040. #endif
  55041. wolfSSL_CTX_free(ctx);
  55042. ctx = NULL;
  55043. #ifndef WOLFSSL_NO_TLS12
  55044. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  55045. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  55046. WOLFSSL_SUCCESS);
  55047. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  55048. wolfSSL_CTX_free(ctx);
  55049. ctx = NULL;
  55050. #endif
  55051. #ifdef WOLFSSL_TLS13
  55052. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  55053. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  55054. WOLFSSL_SUCCESS);
  55055. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  55056. wolfSSL_CTX_free(ctx);
  55057. ctx = NULL;
  55058. #endif
  55059. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  55060. return EXPECT_RESULT();
  55061. }
  55062. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55063. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55064. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55065. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  55066. static int test_wolfSSL_set_SSL_CTX(void)
  55067. {
  55068. EXPECT_DECLS;
  55069. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  55070. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  55071. !defined(NO_RSA)
  55072. WOLFSSL_CTX *ctx1 = NULL;
  55073. WOLFSSL_CTX *ctx2 = NULL;
  55074. WOLFSSL *ssl = NULL;
  55075. const byte *session_id1 = (const byte *)"CTX1";
  55076. const byte *session_id2 = (const byte *)"CTX2";
  55077. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  55078. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  55079. WOLFSSL_FILETYPE_PEM));
  55080. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  55081. WOLFSSL_FILETYPE_PEM));
  55082. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  55083. WOLFSSL_SUCCESS);
  55084. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  55085. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  55086. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  55087. WOLFSSL_SUCCESS);
  55088. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  55089. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  55090. WOLFSSL_FILETYPE_PEM));
  55091. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  55092. WOLFSSL_FILETYPE_PEM));
  55093. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  55094. WOLFSSL_SUCCESS);
  55095. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  55096. WOLFSSL_SUCCESS);
  55097. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  55098. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  55099. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  55100. WOLFSSL_SUCCESS);
  55101. #ifdef HAVE_SESSION_TICKET
  55102. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  55103. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  55104. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  55105. #endif
  55106. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  55107. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  55108. #ifdef WOLFSSL_INT_H
  55109. #ifdef WOLFSSL_SESSION_ID_CTX
  55110. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  55111. #endif
  55112. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  55113. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  55114. #endif
  55115. #ifdef HAVE_SESSION_TICKET
  55116. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  55117. #endif
  55118. /* Set the ctx1 that has TLSv1.3 as max proto version */
  55119. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  55120. /* MUST not change proto versions of ssl */
  55121. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  55122. #ifdef HAVE_SESSION_TICKET
  55123. /* MUST not change */
  55124. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  55125. #endif
  55126. /* MUST change */
  55127. #ifdef WOLFSSL_INT_H
  55128. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  55129. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  55130. #ifdef WOLFSSL_SESSION_ID_CTX
  55131. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  55132. #endif
  55133. #endif
  55134. wolfSSL_free(ssl);
  55135. wolfSSL_CTX_free(ctx1);
  55136. wolfSSL_CTX_free(ctx2);
  55137. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  55138. return EXPECT_RESULT();
  55139. }
  55140. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55141. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55142. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55143. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  55144. static int test_wolfSSL_security_level(void)
  55145. {
  55146. EXPECT_DECLS;
  55147. #if defined(OPENSSL_EXTRA)
  55148. SSL_CTX *ctx = NULL;
  55149. #ifdef WOLFSSL_TLS13
  55150. #ifdef NO_WOLFSSL_SERVER
  55151. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55152. #else
  55153. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55154. #endif
  55155. SSL_CTX_set_security_level(NULL, 1);
  55156. SSL_CTX_set_security_level(ctx, 1);
  55157. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  55158. /* Stub so nothing happens. */
  55159. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  55160. SSL_CTX_free(ctx);
  55161. #else
  55162. (void)ctx;
  55163. #endif
  55164. #endif
  55165. return EXPECT_RESULT();
  55166. }
  55167. static int test_wolfSSL_SSL_in_init(void)
  55168. {
  55169. EXPECT_DECLS;
  55170. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  55171. SSL_CTX* ctx = NULL;
  55172. SSL* ssl = NULL;
  55173. const char* testCertFile;
  55174. const char* testKeyFile;
  55175. #ifdef WOLFSSL_TLS13
  55176. #ifdef NO_WOLFSSL_SERVER
  55177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55178. #else
  55179. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55180. #endif
  55181. #else
  55182. #ifdef NO_WOLFSSL_SERVER
  55183. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55184. #else
  55185. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55186. #endif
  55187. #endif
  55188. #ifndef NO_RSA
  55189. testCertFile = svrCertFile;
  55190. testKeyFile = svrKeyFile;
  55191. #elif defined(HAVE_ECC)
  55192. testCertFile = eccCertFile;
  55193. testKeyFile = eccKeyFile;
  55194. #else
  55195. testCertFile = NULL;
  55196. testKeyFile = NULL;
  55197. #endif
  55198. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  55199. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55200. SSL_FILETYPE_PEM));
  55201. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55202. SSL_FILETYPE_PEM));
  55203. }
  55204. ExpectNotNull(ssl = SSL_new(ctx));
  55205. ExpectIntEQ(SSL_in_init(ssl), 1);
  55206. SSL_CTX_free(ctx);
  55207. SSL_free(ssl);
  55208. #endif
  55209. return EXPECT_RESULT();
  55210. }
  55211. static int test_wolfSSL_CTX_set_timeout(void)
  55212. {
  55213. EXPECT_DECLS;
  55214. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  55215. int timeout;
  55216. WOLFSSL_CTX* ctx = NULL;
  55217. (void)timeout;
  55218. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55219. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  55220. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  55221. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  55222. */
  55223. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  55224. /* giving 0 as timeout value sets default timeout */
  55225. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  55226. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  55227. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  55228. #else
  55229. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  55230. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  55231. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  55232. #endif
  55233. wolfSSL_CTX_free(ctx);
  55234. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  55235. return EXPECT_RESULT();
  55236. }
  55237. static int test_wolfSSL_OpenSSL_version(void)
  55238. {
  55239. EXPECT_DECLS;
  55240. #if defined(OPENSSL_EXTRA)
  55241. const char* ver;
  55242. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55243. ExpectNotNull(ver = OpenSSL_version(0));
  55244. #else
  55245. ExpectNotNull(ver = OpenSSL_version());
  55246. #endif
  55247. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  55248. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  55249. #endif
  55250. return EXPECT_RESULT();
  55251. }
  55252. static int test_CONF_CTX_CMDLINE(void)
  55253. {
  55254. EXPECT_DECLS;
  55255. #if defined(OPENSSL_ALL)
  55256. SSL_CTX* ctx = NULL;
  55257. SSL_CONF_CTX* cctx = NULL;
  55258. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  55259. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55260. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  55261. /* set flags */
  55262. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  55263. WOLFSSL_CONF_FLAG_CMDLINE);
  55264. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  55265. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  55266. /* cmd invalid command */
  55267. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  55268. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  55269. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  55270. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  55271. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  55272. /* cmd Certificate and Private Key*/
  55273. {
  55274. #if !defined(NO_CERTS) && !defined(NO_RSA)
  55275. const char* ourCert = svrCertFile;
  55276. const char* ourKey = svrKeyFile;
  55277. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  55278. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  55279. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  55280. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  55281. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55282. #endif
  55283. }
  55284. /* cmd curves */
  55285. {
  55286. #if defined(HAVE_ECC)
  55287. const char* curve = "secp256r1";
  55288. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  55289. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  55290. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55291. #endif
  55292. }
  55293. /* cmd CipherString */
  55294. {
  55295. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  55296. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  55297. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  55298. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55299. }
  55300. /* cmd DH parameter */
  55301. {
  55302. #if !defined(NO_DH) && !defined(NO_BIO)
  55303. const char* ourdhcert = "./certs/dh2048.pem";
  55304. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  55305. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  55306. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55307. #endif
  55308. }
  55309. SSL_CTX_free(ctx);
  55310. SSL_CONF_CTX_free(cctx);
  55311. #endif /* OPENSSL_EXTRA */
  55312. return EXPECT_RESULT();
  55313. }
  55314. static int test_CONF_CTX_FILE(void)
  55315. {
  55316. EXPECT_DECLS;
  55317. #if defined(OPENSSL_ALL)
  55318. SSL_CTX* ctx = NULL;
  55319. SSL_CONF_CTX* cctx = NULL;
  55320. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  55321. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55322. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  55323. /* set flags */
  55324. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  55325. WOLFSSL_CONF_FLAG_FILE);
  55326. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  55327. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  55328. /* sanity check */
  55329. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  55330. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  55331. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  55332. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  55333. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  55334. /* cmd Certificate and Private Key*/
  55335. {
  55336. #if !defined(NO_CERTS) && !defined(NO_RSA)
  55337. const char* ourCert = svrCertFile;
  55338. const char* ourKey = svrKeyFile;
  55339. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  55340. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  55341. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  55342. WOLFSSL_SUCCESS);
  55343. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  55344. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55345. #endif
  55346. }
  55347. /* cmd curves */
  55348. {
  55349. #if defined(HAVE_ECC)
  55350. const char* curve = "secp256r1";
  55351. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  55352. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  55353. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55354. #endif
  55355. }
  55356. /* cmd CipherString */
  55357. {
  55358. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  55359. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  55360. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  55361. WOLFSSL_SUCCESS);
  55362. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55363. }
  55364. /* cmd DH parameter */
  55365. {
  55366. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  55367. const char* ourdhcert = "./certs/dh3072.pem";
  55368. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  55369. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  55370. WOLFSSL_SUCCESS);
  55371. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  55372. #endif
  55373. }
  55374. SSL_CTX_free(ctx);
  55375. SSL_CONF_CTX_free(cctx);
  55376. #endif /* OPENSSL_EXTRA */
  55377. return EXPECT_RESULT();
  55378. }
  55379. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  55380. {
  55381. EXPECT_DECLS;
  55382. #ifdef HAVE_EX_DATA
  55383. int idx1, idx2;
  55384. /* test for unsupported class index */
  55385. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  55386. 0,NULL, NULL, NULL, NULL ), -1);
  55387. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  55388. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  55389. 0,NULL, NULL, NULL, NULL ), -1);
  55390. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  55391. 0,NULL, NULL, NULL, NULL ), -1);
  55392. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  55393. 0,NULL, NULL, NULL, NULL ), -1);
  55394. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  55395. 0,NULL, NULL, NULL, NULL ), -1);
  55396. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  55397. 0,NULL, NULL, NULL, NULL ), -1);
  55398. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  55399. 0,NULL, NULL, NULL, NULL ), -1);
  55400. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  55401. 0,NULL, NULL, NULL, NULL ), -1);
  55402. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  55403. 0,NULL, NULL, NULL, NULL ), -1);
  55404. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  55405. 0,NULL, NULL, NULL, NULL ), -1);
  55406. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  55407. 0,NULL, NULL, NULL, NULL ), -1);
  55408. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  55409. 0,NULL, NULL, NULL, NULL ), -1);
  55410. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  55411. 0,NULL, NULL, NULL, NULL ), -1);
  55412. /* test for supported class index */
  55413. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55414. 0,NULL, NULL, NULL, NULL );
  55415. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  55416. 0,NULL, NULL, NULL, NULL );
  55417. ExpectIntNE(idx1, -1);
  55418. ExpectIntNE(idx2, -1);
  55419. ExpectIntNE(idx1, idx2);
  55420. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55421. 0,NULL, NULL, NULL, NULL );
  55422. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  55423. 0,NULL, NULL, NULL, NULL );
  55424. ExpectIntNE(idx1, -1);
  55425. ExpectIntNE(idx2, -1);
  55426. ExpectIntNE(idx1, idx2);
  55427. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55428. 0,NULL, NULL, NULL, NULL );
  55429. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  55430. 0,NULL, NULL, NULL, NULL );
  55431. ExpectIntNE(idx1, -1);
  55432. ExpectIntNE(idx2, -1);
  55433. ExpectIntNE(idx1, idx2);
  55434. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55435. 0,NULL, NULL, NULL, NULL );
  55436. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  55437. 0,NULL, NULL, NULL, NULL );
  55438. ExpectIntNE(idx1, -1);
  55439. ExpectIntNE(idx2, -1);
  55440. ExpectIntNE(idx1, idx2);
  55441. #endif /* HAVE_EX_DATA */
  55442. return EXPECT_RESULT();
  55443. }
  55444. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  55445. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  55446. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  55447. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  55448. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  55449. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  55450. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  55451. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  55452. #define SESSION_NEW_IDX_PTR "Testing"
  55453. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  55454. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55455. {
  55456. AssertNotNull(p);
  55457. AssertNull(ptr);
  55458. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  55459. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55460. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  55461. }
  55462. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  55463. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  55464. void* arg)
  55465. {
  55466. EXPECT_DECLS;
  55467. ExpectNotNull(out);
  55468. ExpectNotNull(in);
  55469. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  55470. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  55471. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  55472. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  55473. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55474. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  55475. if (EXPECT_SUCCESS()) {
  55476. return SSL_SUCCESS;
  55477. }
  55478. else {
  55479. return SSL_FAILURE;
  55480. }
  55481. }
  55482. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  55483. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  55484. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  55485. {
  55486. EXPECT_DECLS;
  55487. ExpectNotNull(p);
  55488. ExpectNull(ptr);
  55489. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  55490. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  55491. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  55492. if (EXPECT_SUCCESS()) {
  55493. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  55494. }
  55495. }
  55496. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55497. {
  55498. EXPECT_DECLS;
  55499. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  55500. (void*)SESSION_NEW_IDX_PTR,
  55501. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  55502. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  55503. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  55504. SSL_SESSION* s = SSL_SESSION_new();
  55505. SSL_SESSION* d = NULL;
  55506. ExpectNotNull(s);
  55507. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  55508. ExpectNotNull(d = SSL_SESSION_dup(s));
  55509. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  55510. SSL_SESSION_free(s);
  55511. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  55512. SSL_SESSION_free(d);
  55513. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  55514. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  55515. crypto_ex_cb_ctx_session = NULL;
  55516. return EXPECT_RESULT();
  55517. }
  55518. #else
  55519. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  55520. {
  55521. return TEST_SKIPPED;
  55522. }
  55523. #endif
  55524. static int test_wolfSSL_set_psk_use_session_callback(void)
  55525. {
  55526. EXPECT_DECLS;
  55527. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  55528. SSL_CTX* ctx = NULL;
  55529. SSL* ssl = NULL;
  55530. const char* testCertFile;
  55531. const char* testKeyFile;
  55532. #ifdef WOLFSSL_TLS13
  55533. #ifdef NO_WOLFSSL_SERVER
  55534. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  55535. #else
  55536. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  55537. #endif
  55538. #else
  55539. #ifdef NO_WOLFSSL_SERVER
  55540. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55541. #else
  55542. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55543. #endif
  55544. #endif
  55545. #ifndef NO_RSA
  55546. testCertFile = svrCertFile;
  55547. testKeyFile = svrKeyFile;
  55548. #elif defined(HAVE_ECC)
  55549. testCertFile = eccCertFile;
  55550. testKeyFile = eccKeyFile;
  55551. #else
  55552. testCertFile = NULL;
  55553. testKeyFile = NULL;
  55554. #endif
  55555. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  55556. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55557. SSL_FILETYPE_PEM));
  55558. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55559. SSL_FILETYPE_PEM));
  55560. }
  55561. ExpectNotNull(ssl = SSL_new(ctx));
  55562. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  55563. SSL_CTX_free(ctx);
  55564. SSL_free(ssl);
  55565. #endif
  55566. return EXPECT_RESULT();
  55567. }
  55568. static int test_wolfSSL_ERR_strings(void)
  55569. {
  55570. EXPECT_DECLS;
  55571. #if !defined(NO_ERROR_STRINGS)
  55572. const char* err1 = "unsupported cipher suite";
  55573. const char* err2 = "wolfSSL PEM routines";
  55574. const char* err = NULL;
  55575. (void)err;
  55576. (void)err1;
  55577. (void)err2;
  55578. #if defined(OPENSSL_EXTRA)
  55579. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  55580. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55581. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  55582. ExpectIntEQ((*err == '\0'), 1);
  55583. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  55584. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  55585. #else
  55586. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  55587. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  55588. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  55589. ExpectIntEQ((*err == '\0'), 1);
  55590. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  55591. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  55592. ExpectIntEQ((*err == '\0'), 1);
  55593. #endif
  55594. #endif
  55595. return EXPECT_RESULT();
  55596. }
  55597. static int test_wolfSSL_EVP_shake128(void)
  55598. {
  55599. EXPECT_DECLS;
  55600. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55601. defined(WOLFSSL_SHAKE128)
  55602. const EVP_MD* md = NULL;
  55603. ExpectNotNull(md = EVP_shake128());
  55604. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  55605. #endif
  55606. return EXPECT_RESULT();
  55607. }
  55608. static int test_wolfSSL_EVP_shake256(void)
  55609. {
  55610. EXPECT_DECLS;
  55611. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  55612. defined(WOLFSSL_SHAKE256)
  55613. const EVP_MD* md = NULL;
  55614. ExpectNotNull(md = EVP_shake256());
  55615. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  55616. #endif
  55617. return EXPECT_RESULT();
  55618. }
  55619. /*
  55620. * Testing EVP digest API with SM3
  55621. */
  55622. static int test_wolfSSL_EVP_sm3(void)
  55623. {
  55624. int res = TEST_SKIPPED;
  55625. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  55626. EXPECT_DECLS;
  55627. const EVP_MD* md = NULL;
  55628. EVP_MD_CTX* mdCtx = NULL;
  55629. byte data[WC_SM3_BLOCK_SIZE * 4];
  55630. byte hash[WC_SM3_DIGEST_SIZE];
  55631. byte calcHash[WC_SM3_DIGEST_SIZE];
  55632. byte expHash[WC_SM3_DIGEST_SIZE] = {
  55633. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  55634. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  55635. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  55636. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  55637. };
  55638. word32 chunk;
  55639. word32 i;
  55640. unsigned int sz;
  55641. int ret;
  55642. XMEMSET(data, 0, sizeof(data));
  55643. md = EVP_sm3();
  55644. ExpectTrue(md != NULL);
  55645. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  55646. mdCtx = EVP_MD_CTX_new();
  55647. ExpectTrue(mdCtx != NULL);
  55648. /* Invalid Parameters */
  55649. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  55650. /* Valid Parameters */
  55651. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  55652. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  55653. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  55654. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  55655. /* Valid Parameters */
  55656. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  55657. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  55658. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  55659. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  55660. WOLFSSL_SUCCESS);
  55661. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  55662. WOLFSSL_SUCCESS);
  55663. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  55664. WOLFSSL_SUCCESS);
  55665. /* Ensure too many bytes for lengths. */
  55666. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  55667. WOLFSSL_SUCCESS);
  55668. /* Invalid Parameters */
  55669. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  55670. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  55671. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  55672. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  55673. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  55674. /* Valid Parameters */
  55675. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  55676. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  55677. /* Chunk tests. */
  55678. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  55679. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  55680. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  55681. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  55682. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  55683. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  55684. WOLFSSL_SUCCESS);
  55685. }
  55686. if (i < (word32)sizeof(data)) {
  55687. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  55688. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  55689. }
  55690. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  55691. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  55692. }
  55693. /* Not testing when the low 32-bit length overflows. */
  55694. ret = EVP_MD_CTX_cleanup(mdCtx);
  55695. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55696. wolfSSL_EVP_MD_CTX_free(mdCtx);
  55697. res = EXPECT_RESULT();
  55698. #endif
  55699. return res;
  55700. } /* END test_EVP_sm3 */
  55701. static int test_EVP_blake2(void)
  55702. {
  55703. EXPECT_DECLS;
  55704. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  55705. const EVP_MD* md = NULL;
  55706. (void)md;
  55707. #if defined(HAVE_BLAKE2)
  55708. ExpectNotNull(md = EVP_blake2b512());
  55709. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  55710. #endif
  55711. #if defined(HAVE_BLAKE2S)
  55712. ExpectNotNull(md = EVP_blake2s256());
  55713. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  55714. #endif
  55715. #endif
  55716. return EXPECT_RESULT();
  55717. }
  55718. #if defined(OPENSSL_EXTRA)
  55719. static void list_md_fn(const EVP_MD* m, const char* from,
  55720. const char* to, void* arg)
  55721. {
  55722. const char* mn;
  55723. BIO *bio;
  55724. (void) from;
  55725. (void) to;
  55726. (void) arg;
  55727. (void) mn;
  55728. (void) bio;
  55729. if (!m) {
  55730. /* alias */
  55731. AssertNull(m);
  55732. AssertNotNull(to);
  55733. }
  55734. else {
  55735. AssertNotNull(m);
  55736. AssertNull(to);
  55737. }
  55738. AssertNotNull(from);
  55739. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55740. mn = EVP_get_digestbyname(from);
  55741. /* print to stderr */
  55742. AssertNotNull(arg);
  55743. bio = BIO_new(BIO_s_file());
  55744. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55745. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  55746. BIO_free(bio);
  55747. #endif
  55748. }
  55749. #endif
  55750. static int test_EVP_MD_do_all(void)
  55751. {
  55752. int res = TEST_SKIPPED;
  55753. #if defined(OPENSSL_EXTRA)
  55754. EVP_MD_do_all(NULL, stderr);
  55755. EVP_MD_do_all(list_md_fn, stderr);
  55756. res = TEST_SUCCESS;
  55757. #endif
  55758. return res;
  55759. }
  55760. #if defined(OPENSSL_EXTRA)
  55761. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  55762. {
  55763. (void)arg;
  55764. (void)nm;
  55765. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  55766. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  55767. /* print to stderr */
  55768. AssertNotNull(arg);
  55769. BIO *bio = BIO_new(BIO_s_file());
  55770. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  55771. BIO_printf(bio, "%s\n", nm);
  55772. BIO_free(bio);
  55773. #endif
  55774. }
  55775. #endif
  55776. static int test_OBJ_NAME_do_all(void)
  55777. {
  55778. int res = TEST_SKIPPED;
  55779. #if defined(OPENSSL_EXTRA)
  55780. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  55781. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  55782. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  55783. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  55784. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  55785. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  55786. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  55787. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  55788. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  55789. res = TEST_SUCCESS;
  55790. #endif
  55791. return res;
  55792. }
  55793. static int test_SSL_CIPHER_get_xxx(void)
  55794. {
  55795. EXPECT_DECLS;
  55796. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  55797. !defined(NO_FILESYSTEM)
  55798. const SSL_CIPHER* cipher = NULL;
  55799. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  55800. int i, numCiphers = 0;
  55801. SSL_CTX* ctx = NULL;
  55802. SSL* ssl = NULL;
  55803. const char* testCertFile;
  55804. const char* testKeyFile;
  55805. char buf[256] = {0};
  55806. const char* cipher_id = NULL;
  55807. int expect_nid1 = NID_undef;
  55808. int expect_nid2 = NID_undef;
  55809. int expect_nid3 = NID_undef;
  55810. int expect_nid4 = NID_undef;
  55811. int expect_nid5 = 0;
  55812. const char* cipher_id2 = NULL;
  55813. int expect_nid21 = NID_undef;
  55814. int expect_nid22 = NID_undef;
  55815. int expect_nid23 = NID_undef;
  55816. int expect_nid24 = NID_undef;
  55817. int expect_nid25 = 0;
  55818. (void)cipher;
  55819. (void)supportedCiphers;
  55820. (void)i;
  55821. (void)numCiphers;
  55822. (void)ctx;
  55823. (void)ssl;
  55824. (void)testCertFile;
  55825. (void)testKeyFile;
  55826. #if defined(WOLFSSL_TLS13)
  55827. cipher_id = "TLS13-AES128-GCM-SHA256";
  55828. expect_nid1 = NID_auth_rsa;
  55829. expect_nid2 = NID_aes_128_gcm;
  55830. expect_nid3 = NID_sha256;
  55831. expect_nid4 = NID_kx_any;
  55832. expect_nid5 = 1;
  55833. #if !defined(WOLFSSL_NO_TLS12)
  55834. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  55835. expect_nid21 = NID_auth_rsa;
  55836. expect_nid22 = NID_aes_256_gcm;
  55837. expect_nid23 = NID_sha384;
  55838. expect_nid24 = NID_kx_ecdhe;
  55839. expect_nid25 = 1;
  55840. #endif
  55841. #endif
  55842. #ifdef NO_WOLFSSL_SERVER
  55843. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55844. #else
  55845. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55846. #endif
  55847. if (cipher_id) {
  55848. #ifndef NO_RSA
  55849. testCertFile = svrCertFile;
  55850. testKeyFile = svrKeyFile;
  55851. #elif defined(HAVE_ECC)
  55852. testCertFile = eccCertFile;
  55853. testKeyFile = eccKeyFile;
  55854. #else
  55855. testCertFile = NULL;
  55856. testKeyFile = NULL;
  55857. #endif
  55858. if (testCertFile != NULL && testKeyFile != NULL) {
  55859. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  55860. SSL_FILETYPE_PEM));
  55861. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55862. SSL_FILETYPE_PEM));
  55863. }
  55864. ExpectNotNull(ssl = SSL_new(ctx));
  55865. ExpectIntEQ(SSL_in_init(ssl), 1);
  55866. supportedCiphers = SSL_get_ciphers(ssl);
  55867. numCiphers = sk_num(supportedCiphers);
  55868. for (i = 0; i < numCiphers; ++i) {
  55869. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55870. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55871. }
  55872. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  55873. break;
  55874. }
  55875. }
  55876. /* test case for */
  55877. if (i != numCiphers) {
  55878. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  55879. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  55880. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  55881. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  55882. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  55883. }
  55884. if (cipher_id2) {
  55885. for (i = 0; i < numCiphers; ++i) {
  55886. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  55887. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  55888. }
  55889. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  55890. break;
  55891. }
  55892. }
  55893. /* test case for */
  55894. if (i != numCiphers) {
  55895. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  55896. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  55897. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  55898. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  55899. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  55900. }
  55901. }
  55902. }
  55903. SSL_CTX_free(ctx);
  55904. SSL_free(ssl);
  55905. #endif
  55906. return EXPECT_RESULT();
  55907. }
  55908. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  55909. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  55910. int* keyFormat)
  55911. {
  55912. int ret;
  55913. byte* key_buf = NULL;
  55914. size_t key_sz = 0;
  55915. EncryptedInfo encInfo;
  55916. XMEMSET(&encInfo, 0, sizeof(encInfo));
  55917. ret = load_file(privKeyFile, &key_buf, &key_sz);
  55918. if (ret == 0) {
  55919. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  55920. NULL, &encInfo, keyFormat);
  55921. }
  55922. if (key_buf != NULL) {
  55923. free(key_buf); key_buf = NULL;
  55924. }
  55925. (void)encInfo; /* not used in this test */
  55926. #ifdef DEBUG_WOLFSSL
  55927. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  55928. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  55929. (*pDer)->length);
  55930. #endif
  55931. return ret;
  55932. }
  55933. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  55934. {
  55935. int ret = CRYPTOCB_UNAVAILABLE;
  55936. const char* privKeyFile = (const char*)ctx;
  55937. DerBuffer* pDer = NULL;
  55938. int keyFormat = 0;
  55939. if (info->algo_type == WC_ALGO_TYPE_PK) {
  55940. #ifdef DEBUG_WOLFSSL
  55941. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  55942. #endif
  55943. #ifndef NO_RSA
  55944. if (info->pk.type == WC_PK_TYPE_RSA) {
  55945. switch (info->pk.rsa.type) {
  55946. case RSA_PUBLIC_ENCRYPT:
  55947. case RSA_PUBLIC_DECRYPT:
  55948. /* perform software based RSA public op */
  55949. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  55950. break;
  55951. case RSA_PRIVATE_ENCRYPT:
  55952. case RSA_PRIVATE_DECRYPT:
  55953. {
  55954. RsaKey key;
  55955. /* perform software based RSA private op */
  55956. #ifdef DEBUG_WOLFSSL
  55957. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  55958. #endif
  55959. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  55960. &keyFormat);
  55961. if (ret != 0) {
  55962. return ret;
  55963. }
  55964. ret = wc_InitRsaKey(&key, HEAP_HINT);
  55965. if (ret == 0) {
  55966. word32 keyIdx = 0;
  55967. /* load RSA private key and perform private transform */
  55968. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  55969. &key, pDer->length);
  55970. if (ret == 0) {
  55971. ret = wc_RsaFunction(
  55972. info->pk.rsa.in, info->pk.rsa.inLen,
  55973. info->pk.rsa.out, info->pk.rsa.outLen,
  55974. info->pk.rsa.type, &key, info->pk.rsa.rng);
  55975. }
  55976. else {
  55977. /* if decode fails, then fall-back to software based crypto */
  55978. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  55979. "key decode failed %d, falling back to "
  55980. "software\n", ret);
  55981. ret = CRYPTOCB_UNAVAILABLE;
  55982. }
  55983. wc_FreeRsaKey(&key);
  55984. }
  55985. wc_FreeDer(&pDer); pDer = NULL;
  55986. break;
  55987. }
  55988. }
  55989. #ifdef DEBUG_WOLFSSL
  55990. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  55991. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  55992. #endif
  55993. }
  55994. #endif /* !NO_RSA */
  55995. #ifdef HAVE_ECC
  55996. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  55997. /* mark this key as ephemeral */
  55998. if (info->pk.eckg.key != NULL) {
  55999. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  56000. sizeof(info->pk.eckg.key->label));
  56001. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  56002. }
  56003. }
  56004. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  56005. ecc_key key;
  56006. /* perform software based ECC sign */
  56007. #ifdef DEBUG_WOLFSSL
  56008. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  56009. #endif
  56010. if (info->pk.eccsign.key != NULL &&
  56011. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  56012. /* this is an empheral key */
  56013. #ifdef DEBUG_WOLFSSL
  56014. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  56015. "ephemeral key\n");
  56016. #endif
  56017. return CRYPTOCB_UNAVAILABLE;
  56018. }
  56019. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  56020. if (ret != 0) {
  56021. return ret;
  56022. }
  56023. ret = wc_ecc_init(&key);
  56024. if (ret == 0) {
  56025. word32 keyIdx = 0;
  56026. /* load ECC private key and perform private transform */
  56027. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  56028. &key, pDer->length);
  56029. if (ret == 0) {
  56030. ret = wc_ecc_sign_hash(
  56031. info->pk.eccsign.in, info->pk.eccsign.inlen,
  56032. info->pk.eccsign.out, info->pk.eccsign.outlen,
  56033. info->pk.eccsign.rng, &key);
  56034. }
  56035. else {
  56036. /* if decode fails, then fall-back to software based crypto */
  56037. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  56038. "decode failed %d, falling back to software\n", ret);
  56039. ret = CRYPTOCB_UNAVAILABLE;
  56040. }
  56041. wc_ecc_free(&key);
  56042. }
  56043. wc_FreeDer(&pDer); pDer = NULL;
  56044. #ifdef DEBUG_WOLFSSL
  56045. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  56046. ret, *info->pk.eccsign.outlen);
  56047. #endif
  56048. }
  56049. #endif /* HAVE_ECC */
  56050. #ifdef HAVE_ED25519
  56051. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  56052. ed25519_key key;
  56053. /* perform software based ED25519 sign */
  56054. #ifdef DEBUG_WOLFSSL
  56055. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  56056. #endif
  56057. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  56058. if (ret != 0) {
  56059. return ret;
  56060. }
  56061. ret = wc_ed25519_init(&key);
  56062. if (ret == 0) {
  56063. word32 keyIdx = 0;
  56064. /* load ED25519 private key and perform private transform */
  56065. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  56066. &key, pDer->length);
  56067. if (ret == 0) {
  56068. /* calculate public key */
  56069. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  56070. if (ret == 0) {
  56071. key.pubKeySet = 1;
  56072. ret = wc_ed25519_sign_msg_ex(
  56073. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  56074. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  56075. &key, info->pk.ed25519sign.type,
  56076. info->pk.ed25519sign.context,
  56077. info->pk.ed25519sign.contextLen);
  56078. }
  56079. }
  56080. else {
  56081. /* if decode fails, then fall-back to software based crypto */
  56082. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  56083. "decode failed %d, falling back to software\n", ret);
  56084. ret = CRYPTOCB_UNAVAILABLE;
  56085. }
  56086. wc_ed25519_free(&key);
  56087. }
  56088. wc_FreeDer(&pDer); pDer = NULL;
  56089. #ifdef DEBUG_WOLFSSL
  56090. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  56091. ret, *info->pk.ed25519sign.outLen);
  56092. #endif
  56093. }
  56094. #endif /* HAVE_ED25519 */
  56095. }
  56096. (void)thisDevId;
  56097. (void)keyFormat;
  56098. return ret;
  56099. }
  56100. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  56101. static int test_wc_CryptoCb_TLS(int tlsVer,
  56102. const char* cliCaPemFile, const char* cliCertPemFile,
  56103. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  56104. const char* svrCaPemFile, const char* svrCertPemFile,
  56105. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  56106. {
  56107. EXPECT_DECLS;
  56108. callback_functions client_cbf;
  56109. callback_functions server_cbf;
  56110. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  56111. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  56112. if (tlsVer == WOLFSSL_TLSV1_3) {
  56113. #ifdef WOLFSSL_TLS13
  56114. server_cbf.method = wolfTLSv1_3_server_method;
  56115. client_cbf.method = wolfTLSv1_3_client_method;
  56116. #endif
  56117. }
  56118. else if (tlsVer == WOLFSSL_TLSV1_2) {
  56119. #ifndef WOLFSSL_NO_TLS12
  56120. server_cbf.method = wolfTLSv1_2_server_method;
  56121. client_cbf.method = wolfTLSv1_2_client_method;
  56122. #endif
  56123. }
  56124. else if (tlsVer == WOLFSSL_TLSV1_1) {
  56125. #ifndef NO_OLD_TLS
  56126. server_cbf.method = wolfTLSv1_1_server_method;
  56127. client_cbf.method = wolfTLSv1_1_client_method;
  56128. #endif
  56129. }
  56130. else if (tlsVer == WOLFSSL_TLSV1) {
  56131. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  56132. server_cbf.method = wolfTLSv1_server_method;
  56133. client_cbf.method = wolfTLSv1_client_method;
  56134. #endif
  56135. }
  56136. else if (tlsVer == WOLFSSL_SSLV3) {
  56137. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  56138. defined(WOLFSSL_STATIC_RSA)
  56139. server_cbf.method = wolfSSLv3_server_method;
  56140. client_cbf.method = wolfSSLv3_client_method;
  56141. #endif
  56142. }
  56143. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  56144. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56145. server_cbf.method = wolfDTLSv1_2_server_method;
  56146. client_cbf.method = wolfDTLSv1_2_client_method;
  56147. #endif
  56148. }
  56149. else if (tlsVer == WOLFSSL_DTLSV1) {
  56150. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  56151. server_cbf.method = wolfDTLSv1_server_method;
  56152. client_cbf.method = wolfDTLSv1_client_method;
  56153. #endif
  56154. }
  56155. if (server_cbf.method == NULL) {
  56156. /* not enabled */
  56157. return TEST_SUCCESS;
  56158. }
  56159. /* Setup the keys for the TLS test */
  56160. client_cbf.certPemFile = cliCertPemFile;
  56161. client_cbf.keyPemFile = cliPubKeyPemFile;
  56162. client_cbf.caPemFile = cliCaPemFile;
  56163. server_cbf.certPemFile = svrCertPemFile;
  56164. server_cbf.keyPemFile = svrPubKeyPemFile;
  56165. server_cbf.caPemFile = svrCaPemFile;
  56166. /* Setup a crypto callback with pointer to private key file for testing */
  56167. client_cbf.devId = 1;
  56168. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  56169. (void*)cliPrivKeyPemFile);
  56170. server_cbf.devId = 2;
  56171. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  56172. (void*)svrPrivKeyPemFile);
  56173. /* Perform TLS server and client test */
  56174. /* First test is at WOLFSSL_CTX level */
  56175. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56176. /* Check for success */
  56177. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56178. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56179. if (EXPECT_SUCCESS()) {
  56180. /* Second test is a WOLFSSL object level */
  56181. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  56182. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56183. }
  56184. /* Check for success */
  56185. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56186. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56187. /* Un register the devId's */
  56188. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  56189. client_cbf.devId = INVALID_DEVID;
  56190. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  56191. server_cbf.devId = INVALID_DEVID;
  56192. return EXPECT_RESULT();
  56193. }
  56194. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  56195. static int test_wc_CryptoCb(void)
  56196. {
  56197. EXPECT_DECLS;
  56198. #ifdef WOLF_CRYPTO_CB
  56199. /* TODO: Add crypto callback API tests */
  56200. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56201. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  56202. int tlsVer;
  56203. #endif
  56204. #ifndef NO_RSA
  56205. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56206. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  56207. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  56208. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  56209. TEST_SUCCESS);
  56210. }
  56211. #endif
  56212. #ifdef HAVE_ECC
  56213. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56214. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  56215. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  56216. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  56217. TEST_SUCCESS);
  56218. }
  56219. #endif
  56220. #ifdef HAVE_ED25519
  56221. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56222. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56223. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  56224. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  56225. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  56226. TEST_SUCCESS);
  56227. }
  56228. #endif
  56229. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56230. #endif /* WOLF_CRYPTO_CB */
  56231. return EXPECT_RESULT();
  56232. }
  56233. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56234. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  56235. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  56236. const char* cliCaPemFile, const char* cliCertPemFile,
  56237. const char* cliPrivKeyPemFile,
  56238. const char* svrCaPemFile, const char* svrCertPemFile,
  56239. const char* svrPrivKeyPemFile,
  56240. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  56241. {
  56242. EXPECT_DECLS;
  56243. callback_functions client_cbf;
  56244. callback_functions server_cbf;
  56245. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  56246. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  56247. if (tlsVer == WOLFSSL_TLSV1_3) {
  56248. #ifdef WOLFSSL_TLS13
  56249. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  56250. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  56251. #endif
  56252. }
  56253. else if (tlsVer == WOLFSSL_TLSV1_2) {
  56254. #ifndef WOLFSSL_NO_TLS12
  56255. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  56256. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  56257. #endif
  56258. }
  56259. else if (tlsVer == WOLFSSL_TLSV1_1) {
  56260. #ifndef NO_OLD_TLS
  56261. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  56262. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  56263. #endif
  56264. }
  56265. else if (tlsVer == WOLFSSL_TLSV1) {
  56266. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  56267. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  56268. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  56269. #endif
  56270. }
  56271. else if (tlsVer == WOLFSSL_SSLV3) {
  56272. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  56273. defined(WOLFSSL_STATIC_RSA)
  56274. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  56275. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  56276. #endif
  56277. }
  56278. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  56279. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56280. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  56281. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  56282. #endif
  56283. }
  56284. else if (tlsVer == WOLFSSL_DTLSV1) {
  56285. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  56286. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  56287. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  56288. #endif
  56289. }
  56290. if (server_cbf.method_ex == NULL) {
  56291. /* not enabled */
  56292. return TEST_SUCCESS;
  56293. }
  56294. /* Setup the keys for the TLS test */
  56295. client_cbf.certPemFile = cliCertPemFile;
  56296. client_cbf.keyPemFile = cliPrivKeyPemFile;
  56297. client_cbf.caPemFile = cliCaPemFile;
  56298. server_cbf.certPemFile = svrCertPemFile;
  56299. server_cbf.keyPemFile = svrPrivKeyPemFile;
  56300. server_cbf.caPemFile = svrCaPemFile;
  56301. client_cbf.mem = cliMem;
  56302. client_cbf.memSz = cliMemSz;
  56303. server_cbf.mem = svrMem;
  56304. server_cbf.memSz = svrMemSz;
  56305. client_cbf.devId = INVALID_DEVID;
  56306. server_cbf.devId = INVALID_DEVID;
  56307. /* Perform TLS server and client test */
  56308. /* First test is at WOLFSSL_CTX level */
  56309. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56310. /* Check for success */
  56311. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56312. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56313. if (EXPECT_SUCCESS()) {
  56314. /* Second test is a WOLFSSL object level */
  56315. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  56316. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  56317. }
  56318. /* Check for success */
  56319. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  56320. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  56321. return EXPECT_RESULT();
  56322. }
  56323. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  56324. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  56325. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  56326. {
  56327. EXPECT_DECLS;
  56328. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  56329. WOLFSSL_MEM_STATS mem_stats;
  56330. WOLFSSL_MEM_CONN_STATS ssl_stats;
  56331. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  56332. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  56333. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56334. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  56335. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56336. #endif
  56337. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  56338. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  56339. /* this should fail because kMaxCtxClients == 2 */
  56340. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  56341. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  56342. #ifdef DEBUG_WOLFSSL
  56343. wolfSSL_PrintStatsConn(&ssl_stats);
  56344. #endif
  56345. (void)ssl_stats;
  56346. }
  56347. /* display collected statistics */
  56348. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  56349. #ifdef DEBUG_WOLFSSL
  56350. wolfSSL_PrintStats(&mem_stats);
  56351. #endif
  56352. (void)mem_stats;
  56353. }
  56354. wolfSSL_free(ssl1);
  56355. wolfSSL_free(ssl2);
  56356. return EXPECT_RESULT();
  56357. }
  56358. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56359. static int test_wolfSSL_CTX_StaticMemory(void)
  56360. {
  56361. EXPECT_DECLS;
  56362. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  56363. wolfSSL_method_func method_func;
  56364. WOLFSSL_CTX* ctx;
  56365. const int kMaxCtxClients = 2;
  56366. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56367. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  56368. int tlsVer;
  56369. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  56370. #endif
  56371. #endif
  56372. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  56373. #ifndef NO_WOLFSSL_SERVER
  56374. #ifndef WOLFSSL_NO_TLS12
  56375. method_func = wolfTLSv1_2_server_method_ex;
  56376. #else
  56377. method_func = wolfTLSv1_3_server_method_ex;
  56378. #endif
  56379. #else
  56380. #ifndef WOLFSSL_NO_TLS12
  56381. method_func = wolfTLSv1_2_client_method_ex;
  56382. #else
  56383. method_func = wolfTLSv1_3_client_method_ex;
  56384. #endif
  56385. #endif
  56386. /* Test creating CTX directly from static memory pool */
  56387. ctx = NULL;
  56388. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  56389. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  56390. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  56391. wolfSSL_CTX_free(ctx);
  56392. ctx = NULL;
  56393. /* Test for heap allocated CTX, then assigning static pool to it */
  56394. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  56395. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  56396. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  56397. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  56398. wolfSSL_CTX_free(ctx);
  56399. /* TLS Level Tests using static memory */
  56400. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56401. #ifndef NO_RSA
  56402. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56403. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56404. svrCertFile, cliCertFile, cliKeyFile,
  56405. cliCertFile, svrCertFile, svrKeyFile,
  56406. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56407. TEST_SUCCESS);
  56408. }
  56409. #endif
  56410. #ifdef HAVE_ECC
  56411. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  56412. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56413. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  56414. cliEccCertFile, eccCertFile, eccKeyFile,
  56415. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56416. TEST_SUCCESS);
  56417. }
  56418. #endif
  56419. #ifdef HAVE_ED25519
  56420. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  56421. if (tlsVer == WOLFSSL_DTLSV1) continue;
  56422. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  56423. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  56424. cliEdCertFile, edCertFile, edKeyFile,
  56425. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  56426. TEST_SUCCESS);
  56427. }
  56428. #endif
  56429. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  56430. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  56431. return EXPECT_RESULT();
  56432. }
  56433. static int test_openssl_FIPS_drbg(void)
  56434. {
  56435. EXPECT_DECLS;
  56436. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  56437. DRBG_CTX* dctx = NULL;
  56438. byte data1[32], data2[32], zeroData[32];
  56439. byte testSeed[16];
  56440. size_t dlen = sizeof(data1);
  56441. int i;
  56442. XMEMSET(data1, 0, dlen);
  56443. XMEMSET(data2, 0, dlen);
  56444. XMEMSET(zeroData, 0, sizeof(zeroData));
  56445. for (i = 0; i < (int)sizeof(testSeed); i++) {
  56446. testSeed[i] = (byte)i;
  56447. }
  56448. ExpectNotNull(dctx = FIPS_get_default_drbg());
  56449. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  56450. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  56451. WOLFSSL_SUCCESS);
  56452. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  56453. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  56454. WOLFSSL_SUCCESS);
  56455. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56456. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  56457. WOLFSSL_SUCCESS);
  56458. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  56459. WOLFSSL_SUCCESS);
  56460. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  56461. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  56462. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  56463. #ifndef HAVE_GLOBAL_RNG
  56464. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  56465. wolfSSL_FIPS_drbg_free(dctx);
  56466. #endif
  56467. #endif
  56468. return EXPECT_RESULT();
  56469. }
  56470. static int test_wolfSSL_FIPS_mode(void)
  56471. {
  56472. EXPECT_DECLS;
  56473. #if defined(OPENSSL_ALL)
  56474. #ifdef HAVE_FIPS
  56475. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  56476. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  56477. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  56478. #else
  56479. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  56480. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  56481. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  56482. #endif
  56483. #endif
  56484. return EXPECT_RESULT();
  56485. }
  56486. #ifdef WOLFSSL_DTLS
  56487. /* Prints out the current window */
  56488. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  56489. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56490. int i;
  56491. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  56492. word32 b = w[i];
  56493. int j;
  56494. /* Prints out a 32 bit binary number in big endian order */
  56495. for (j = 0; j < 32; j++, b <<= 1) {
  56496. if (b & (((word32)1) << 31))
  56497. fprintf(stderr, "1");
  56498. else
  56499. fprintf(stderr, "0");
  56500. }
  56501. fprintf(stderr, " ");
  56502. }
  56503. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  56504. #else
  56505. (void)h;
  56506. (void)l;
  56507. (void)w;
  56508. #endif
  56509. }
  56510. /* a - cur_hi
  56511. * b - cur_lo
  56512. * c - next_hi
  56513. * d - next_lo
  56514. * e - window
  56515. * f - expected next_hi
  56516. * g - expected next_lo
  56517. * h - expected window[1]
  56518. * i - expected window[0]
  56519. */
  56520. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  56521. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  56522. DUW_TEST_print_window_binary((a), (b), (e)); \
  56523. ExpectIntEQ((c), (f)); \
  56524. ExpectIntEQ((d), (g)); \
  56525. ExpectIntEQ((e)[1], (h)); \
  56526. ExpectIntEQ((e)[0], (i)); \
  56527. } while (0)
  56528. static int test_wolfSSL_DtlsUpdateWindow(void)
  56529. {
  56530. EXPECT_DECLS;
  56531. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  56532. word32 next_lo = 0;
  56533. word16 next_hi = 0;
  56534. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  56535. fprintf(stderr, "\n");
  56536. #endif
  56537. XMEMSET(window, 0, sizeof window);
  56538. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  56539. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  56540. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  56541. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  56542. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  56543. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  56544. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  56545. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  56546. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  56547. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  56548. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  56549. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  56550. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  56551. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  56552. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  56553. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  56554. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  56555. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  56556. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  56557. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  56558. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  56559. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56560. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  56561. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  56562. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  56563. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56564. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  56565. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56566. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56567. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  56568. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  56569. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  56570. return EXPECT_RESULT();
  56571. }
  56572. #endif /* WOLFSSL_DTLS */
  56573. #ifdef WOLFSSL_DTLS
  56574. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  56575. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  56576. {
  56577. DtlsMsg* cur;
  56578. static byte msg[100];
  56579. static byte msgInit = 0;
  56580. if (!msgInit) {
  56581. int i;
  56582. for (i = 0; i < 100; i++)
  56583. msg[i] = i + 1;
  56584. msgInit = 1;
  56585. }
  56586. /* Sanitize test parameters */
  56587. if (len > sizeof(msg))
  56588. return -1;
  56589. if (f_offset + f_len > sizeof(msg))
  56590. return -1;
  56591. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  56592. if (ssl->dtls_rx_msg_list == NULL)
  56593. return -100;
  56594. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  56595. return -200;
  56596. if (cur->fragBucketListCount != f_count)
  56597. return -300;
  56598. if (cur->ready != ready)
  56599. return -400;
  56600. if (cur->bytesReceived != bytesReceived)
  56601. return -500;
  56602. if (ready) {
  56603. if (cur->fragBucketList != NULL)
  56604. return -600;
  56605. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  56606. return -700;
  56607. }
  56608. else {
  56609. DtlsFragBucket* fb;
  56610. if (cur->fragBucketList == NULL)
  56611. return -800;
  56612. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  56613. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  56614. return -900;
  56615. }
  56616. }
  56617. return 0;
  56618. }
  56619. static int test_wolfSSL_DTLS_fragment_buckets(void)
  56620. {
  56621. EXPECT_DECLS;
  56622. WOLFSSL ssl[1];
  56623. XMEMSET(ssl, 0, sizeof(*ssl));
  56624. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  56625. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56626. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  56627. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  56628. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  56629. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  56630. /* Test all permutations of 3 regions */
  56631. /* 1 2 3 */
  56632. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56633. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  56634. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56635. /* 1 3 2 */
  56636. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56637. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  56638. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56639. /* 2 1 3 */
  56640. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56641. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  56642. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  56643. /* 2 3 1 */
  56644. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  56645. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  56646. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56647. /* 3 1 2 */
  56648. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56649. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  56650. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  56651. /* 3 2 1 */
  56652. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  56653. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  56654. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  56655. /* Test overlapping regions */
  56656. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  56657. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  56658. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  56659. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  56660. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  56661. /* Test overlapping multiple regions */
  56662. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56663. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  56664. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  56665. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  56666. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  56667. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  56668. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  56669. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  56670. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  56671. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  56672. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  56673. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  56674. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  56675. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  56676. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  56677. ssl->dtls_rx_msg_list = NULL;
  56678. ssl->dtls_rx_msg_list_sz = 0;
  56679. return EXPECT_RESULT();
  56680. }
  56681. #endif
  56682. #if !defined(NO_FILESYSTEM) && \
  56683. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56684. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56685. static int test_wolfSSL_dtls_stateless2(void)
  56686. {
  56687. EXPECT_DECLS;
  56688. WOLFSSL *ssl_c = NULL;
  56689. WOLFSSL *ssl_c2 = NULL;
  56690. WOLFSSL *ssl_s = NULL;
  56691. struct test_memio_ctx test_ctx;
  56692. WOLFSSL_CTX *ctx_c = NULL;
  56693. WOLFSSL_CTX *ctx_s = NULL;
  56694. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56695. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56696. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56697. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  56698. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56699. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56700. /* send CH */
  56701. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56702. (ssl_c2->error == WANT_READ));
  56703. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56704. (ssl_s->error == WANT_READ));
  56705. ExpectIntNE(test_ctx.c_len, 0);
  56706. /* consume HRR */
  56707. test_ctx.c_len = 0;
  56708. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56709. wolfSSL_free(ssl_c2);
  56710. wolfSSL_free(ssl_c);
  56711. wolfSSL_free(ssl_s);
  56712. wolfSSL_CTX_free(ctx_c);
  56713. wolfSSL_CTX_free(ctx_s);
  56714. return EXPECT_RESULT();
  56715. }
  56716. #ifdef HAVE_MAX_FRAGMENT
  56717. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  56718. {
  56719. EXPECT_DECLS;
  56720. WOLFSSL *ssl_c = NULL;
  56721. WOLFSSL *ssl_c2 = NULL;
  56722. WOLFSSL *ssl_s = NULL;
  56723. struct test_memio_ctx test_ctx;
  56724. WOLFSSL_CTX *ctx_c = NULL;
  56725. WOLFSSL_CTX *ctx_s = NULL;
  56726. word16 max_fragment = 0;
  56727. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56728. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56729. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56730. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  56731. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  56732. WOLFSSL_SUCCESS);
  56733. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56734. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56735. if (ssl_s != NULL) {
  56736. max_fragment = ssl_s->max_fragment;
  56737. }
  56738. /* send CH */
  56739. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56740. (ssl_c2->error == WANT_READ));
  56741. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56742. (ssl_s->error == WANT_READ));
  56743. /* CH without cookie shouldn't change state */
  56744. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  56745. ExpectIntNE(test_ctx.c_len, 0);
  56746. /* consume HRR from buffer */
  56747. test_ctx.c_len = 0;
  56748. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56749. wolfSSL_free(ssl_c2);
  56750. wolfSSL_free(ssl_c);
  56751. wolfSSL_free(ssl_s);
  56752. wolfSSL_CTX_free(ctx_c);
  56753. wolfSSL_CTX_free(ctx_s);
  56754. return EXPECT_RESULT();
  56755. }
  56756. #endif /* HAVE_MAX_FRAGMENT */
  56757. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  56758. #define ROUNDS_WITH_HVR 4
  56759. #define ROUNDS_WITHOUT_HVR 2
  56760. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  56761. static int buf_is_hvr(const byte *data, int len)
  56762. {
  56763. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  56764. return 0;
  56765. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  56766. }
  56767. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  56768. {
  56769. EXPECT_DECLS;
  56770. struct test_memio_ctx test_ctx;
  56771. WOLFSSL_CTX *ctx_c = NULL;
  56772. WOLFSSL_CTX *ctx_s = NULL;
  56773. WOLFSSL *ssl_c = NULL;
  56774. WOLFSSL *ssl_s = NULL;
  56775. WOLFSSL_SESSION *sess = NULL;
  56776. int round_trips;
  56777. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56778. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  56779. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56780. #ifdef HAVE_SESSION_TICKET
  56781. if (useticket) {
  56782. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  56783. }
  56784. #endif
  56785. round_trips = ROUNDS_WITH_HVR;
  56786. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  56787. &round_trips), 0);
  56788. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  56789. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56790. wolfSSL_shutdown(ssl_c);
  56791. wolfSSL_shutdown(ssl_s);
  56792. wolfSSL_free(ssl_c);
  56793. ssl_c = NULL;
  56794. wolfSSL_free(ssl_s);
  56795. ssl_s = NULL;
  56796. test_ctx.c_len = test_ctx.s_len = 0;
  56797. /* make resumption invalid */
  56798. if (bad && (sess != NULL)) {
  56799. if (useticket) {
  56800. #ifdef HAVE_SESSION_TICKET
  56801. if (sess->ticket != NULL) {
  56802. sess->ticket[0] = !sess->ticket[0];
  56803. }
  56804. #endif /* HAVE_SESSION_TICKET */
  56805. }
  56806. else {
  56807. sess->sessionID[0] = !sess->sessionID[0];
  56808. }
  56809. }
  56810. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  56811. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  56812. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  56813. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  56814. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  56815. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  56816. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  56817. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56818. (ssl_c->error == WANT_READ));
  56819. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56820. (ssl_s->error == WANT_READ));
  56821. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  56822. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  56823. if (!useticket) {
  56824. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  56825. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  56826. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  56827. }
  56828. wolfSSL_SESSION_free(sess);
  56829. wolfSSL_free(ssl_c);
  56830. wolfSSL_free(ssl_s);
  56831. wolfSSL_CTX_free(ctx_c);
  56832. wolfSSL_CTX_free(ctx_s);
  56833. return EXPECT_RESULT();
  56834. }
  56835. static int test_wolfSSL_dtls_stateless_resume(void)
  56836. {
  56837. EXPECT_DECLS;
  56838. #ifdef HAVE_SESSION_TICKET
  56839. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  56840. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  56841. #endif /* HAVE_SESION_TICKET */
  56842. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  56843. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  56844. return EXPECT_RESULT();
  56845. }
  56846. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  56847. #if !defined(NO_OLD_TLS)
  56848. static int test_wolfSSL_dtls_stateless_downgrade(void)
  56849. {
  56850. EXPECT_DECLS;
  56851. WOLFSSL_CTX *ctx_c = NULL;
  56852. WOLFSSL_CTX *ctx_c2 = NULL;
  56853. WOLFSSL_CTX *ctx_s = NULL;
  56854. WOLFSSL *ssl_c = NULL;
  56855. WOLFSSL *ssl_c2 = NULL;
  56856. WOLFSSL *ssl_s = NULL;
  56857. struct test_memio_ctx test_ctx;
  56858. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56859. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56860. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56861. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  56862. WOLFSSL_SUCCESS);
  56863. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  56864. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  56865. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  56866. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  56867. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  56868. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  56869. /* send CH */
  56870. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  56871. (ssl_c2->error == WANT_READ));
  56872. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56873. (ssl_s->error == WANT_READ));
  56874. ExpectIntNE(test_ctx.c_len, 0);
  56875. /* consume HRR */
  56876. test_ctx.c_len = 0;
  56877. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56878. wolfSSL_free(ssl_c2);
  56879. wolfSSL_free(ssl_c);
  56880. wolfSSL_free(ssl_s);
  56881. wolfSSL_CTX_free(ctx_c);
  56882. wolfSSL_CTX_free(ctx_c2);
  56883. wolfSSL_CTX_free(ctx_s);
  56884. return EXPECT_RESULT();
  56885. }
  56886. #endif /* !defined(NO_OLD_TLS) */
  56887. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56888. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  56889. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56890. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56891. static int test_WOLFSSL_dtls_version_alert(void)
  56892. {
  56893. EXPECT_DECLS;
  56894. struct test_memio_ctx test_ctx;
  56895. WOLFSSL_CTX *ctx_c = NULL;
  56896. WOLFSSL_CTX *ctx_s = NULL;
  56897. WOLFSSL *ssl_c = NULL;
  56898. WOLFSSL *ssl_s = NULL;
  56899. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56900. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56901. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  56902. /* client hello */
  56903. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56904. (ssl_c->error == WANT_READ));
  56905. /* hrr */
  56906. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56907. (ssl_s->error == WANT_READ));
  56908. /* client hello 1 */
  56909. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56910. (ssl_c->error == WANT_READ));
  56911. /* server hello */
  56912. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56913. (ssl_s->error == WANT_READ));
  56914. /* should fail */
  56915. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  56916. (ssl_c->error == VERSION_ERROR));
  56917. /* shuould fail */
  56918. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  56919. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  56920. wolfSSL_free(ssl_c);
  56921. wolfSSL_free(ssl_s);
  56922. wolfSSL_CTX_free(ctx_c);
  56923. wolfSSL_CTX_free(ctx_s);
  56924. return EXPECT_RESULT();
  56925. }
  56926. #else
  56927. static int test_WOLFSSL_dtls_version_alert(void)
  56928. {
  56929. return TEST_SKIPPED;
  56930. }
  56931. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  56932. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  56933. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  56934. */
  56935. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56936. && defined(WOLFSSL_TLS13) && \
  56937. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  56938. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56939. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  56940. {
  56941. struct test_memio_ctx *test_ctx;
  56942. byte buf[2048];
  56943. int idx, sz;
  56944. word32 tmp;
  56945. int ret;
  56946. idx = 5; /* space for record header */
  56947. buf[idx] = session_ticket; /* type */
  56948. idx++;
  56949. tmp = OPAQUE32_LEN +
  56950. OPAQUE32_LEN +
  56951. OPAQUE8_LEN + nonceLength +
  56952. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  56953. c32to24(tmp, buf + idx);
  56954. idx += OPAQUE24_LEN;
  56955. c32toa((word32)12345, buf+idx); /* lifetime */
  56956. idx += OPAQUE32_LEN;
  56957. c32toa((word32)12345, buf+idx); /* add */
  56958. idx += OPAQUE32_LEN;
  56959. buf[idx] = nonceLength; /* nonce length */
  56960. idx++;
  56961. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  56962. idx += nonceLength;
  56963. tmp = 1; /* ticket len */
  56964. c16toa((word16)tmp, buf+idx);
  56965. idx += 2;
  56966. buf[idx] = 0xFF; /* ticket */
  56967. idx++;
  56968. tmp = 0; /* ext len */
  56969. c16toa((word16)tmp, buf+idx);
  56970. idx += 2;
  56971. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  56972. handshake, 0, 0, 0);
  56973. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  56974. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  56975. return !(ret == sz);
  56976. }
  56977. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  56978. {
  56979. int ret = 0;
  56980. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  56981. ret = -1;
  56982. }
  56983. else {
  56984. int i;
  56985. for (i = 0; i < len; i++) {
  56986. if (sess->ticketNonce.data[i] != len) {
  56987. ret = -1;
  56988. break;
  56989. }
  56990. }
  56991. }
  56992. return ret;
  56993. }
  56994. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  56995. {
  56996. EXPECT_DECLS;
  56997. char *buf[1024];
  56998. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  56999. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  57000. (ssl_c->error == WANT_READ));
  57001. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  57002. return EXPECT_RESULT();
  57003. }
  57004. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57005. {
  57006. EXPECT_DECLS;
  57007. WOLFSSL_SESSION *sess = NULL;
  57008. WOLFSSL_SESSION *cached = NULL;
  57009. WOLFSSL_CTX *ctx = ssl_c->ctx;
  57010. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  57011. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57012. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  57013. NULL, ssl_c->options.side, 1,NULL), 0);
  57014. ExpectNotNull(cached = wolfSSL_SESSION_new());
  57015. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  57016. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  57017. wolfSSL_SESSION_free(cached);
  57018. wolfSSL_SESSION_free(sess);
  57019. return EXPECT_RESULT();
  57020. }
  57021. static int test_ticket_nonce_malloc(void)
  57022. {
  57023. EXPECT_DECLS;
  57024. struct test_memio_ctx test_ctx;
  57025. WOLFSSL_CTX *ctx_c = NULL;
  57026. WOLFSSL_CTX *ctx_s = NULL;
  57027. WOLFSSL *ssl_c = NULL;
  57028. WOLFSSL *ssl_s = NULL;
  57029. byte small;
  57030. byte medium;
  57031. byte big;
  57032. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57033. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57034. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  57035. /* will send ticket manually */
  57036. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  57037. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57038. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57039. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  57040. (ssl_s->options.handShakeDone == 0)) {
  57041. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  57042. (ssl_c->error == WANT_READ));
  57043. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  57044. (ssl_s->error == WANT_READ));
  57045. }
  57046. small = TLS13_TICKET_NONCE_STATIC_SZ;
  57047. medium = small + 20 <= 255 ? small + 20 : 255;
  57048. big = medium + 20 <= 255 ? small + 20 : 255;
  57049. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  57050. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  57051. ssl_c->session->ticketNonce.dataStatic);
  57052. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  57053. TEST_SUCCESS);
  57054. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  57055. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  57056. TEST_SUCCESS);
  57057. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  57058. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  57059. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  57060. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  57061. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  57062. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  57063. wolfSSL_free(ssl_c);
  57064. wolfSSL_free(ssl_s);
  57065. wolfSSL_CTX_free(ctx_c);
  57066. wolfSSL_CTX_free(ctx_s);
  57067. return EXPECT_RESULT();
  57068. }
  57069. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  57070. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  57071. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  57072. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57073. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  57074. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57075. static int test_ticket_ret_create(void)
  57076. {
  57077. EXPECT_DECLS;
  57078. WOLFSSL_CTX *ctx_c = NULL;
  57079. WOLFSSL_CTX *ctx_s = NULL;
  57080. WOLFSSL *ssl_c = NULL;
  57081. WOLFSSL *ssl_s = NULL;
  57082. byte ticket[SESSION_TICKET_LEN];
  57083. struct test_memio_ctx test_ctx;
  57084. WOLFSSL_SESSION *sess = NULL;
  57085. word16 ticketLen = 0;
  57086. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57087. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57088. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57089. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  57090. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  57091. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  57092. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57093. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57094. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  57095. if (sess != NULL) {
  57096. ticketLen = sess->ticketLen;
  57097. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  57098. }
  57099. wolfSSL_free(ssl_c);
  57100. ssl_c = NULL;
  57101. wolfSSL_free(ssl_s);
  57102. ssl_s = NULL;
  57103. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  57104. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  57105. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  57106. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  57107. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  57108. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  57109. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  57110. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57111. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  57112. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  57113. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  57114. wolfSSL_SESSION_free(sess);
  57115. wolfSSL_free(ssl_c);
  57116. wolfSSL_free(ssl_s);
  57117. wolfSSL_CTX_free(ctx_c);
  57118. wolfSSL_CTX_free(ctx_s);
  57119. return EXPECT_RESULT();
  57120. }
  57121. #else
  57122. static int test_ticket_ret_create(void)
  57123. {
  57124. return TEST_SKIPPED;
  57125. }
  57126. #endif
  57127. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  57128. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  57129. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57130. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57131. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  57132. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  57133. {
  57134. int ret;
  57135. WOLFSSL_SESSION* session = NULL;
  57136. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  57137. if (!wolfSSL_is_server(ssl)) {
  57138. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  57139. AssertNotNull(session);
  57140. }
  57141. do {
  57142. ret = wolfSSL_shutdown(ssl);
  57143. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  57144. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  57145. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  57146. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  57147. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  57148. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  57149. #endif
  57150. if (!wolfSSL_is_server(ssl)) {
  57151. /* client */
  57152. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57153. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57154. wolfSSL_set_session(ssl, session);
  57155. wolfSSL_SESSION_free(session);
  57156. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  57157. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  57158. }
  57159. else {
  57160. /* server */
  57161. /* Different ciphersuite so that the ticket will be invalidated based on
  57162. * the ciphersuite */
  57163. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  57164. WOLFSSL_SUCCESS);
  57165. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  57166. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  57167. }
  57168. }
  57169. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  57170. {
  57171. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  57172. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57173. WOLFSSL_SUCCESS);
  57174. }
  57175. static int test_ticket_and_psk_mixing(void)
  57176. {
  57177. EXPECT_DECLS;
  57178. /* Test mixing tickets and regular PSK */
  57179. callback_functions client_cbs, server_cbs;
  57180. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57181. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57182. client_cbs.method = wolfTLSv1_3_client_method;
  57183. server_cbs.method = wolfTLSv1_3_server_method;
  57184. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  57185. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  57186. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  57187. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57188. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  57189. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  57190. return EXPECT_RESULT();
  57191. }
  57192. #else
  57193. static int test_ticket_and_psk_mixing(void)
  57194. {
  57195. return TEST_SKIPPED;
  57196. }
  57197. #endif
  57198. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  57199. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  57200. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57201. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  57202. static int test_prioritize_psk_cb_called = FALSE;
  57203. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  57204. const char* identity, unsigned char* key, unsigned int key_max_len,
  57205. const char** ciphersuite)
  57206. {
  57207. test_prioritize_psk_cb_called = TRUE;
  57208. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  57209. }
  57210. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  57211. {
  57212. int ret;
  57213. WOLFSSL_SESSION* session = NULL;
  57214. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  57215. if (!wolfSSL_is_server(ssl)) {
  57216. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  57217. AssertNotNull(session);
  57218. }
  57219. do {
  57220. ret = wolfSSL_shutdown(ssl);
  57221. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  57222. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  57223. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  57224. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  57225. * important. */
  57226. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57227. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57228. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  57229. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  57230. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  57231. #endif
  57232. if (!wolfSSL_is_server(ssl)) {
  57233. /* client */
  57234. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  57235. wolfSSL_set_session(ssl, session);
  57236. wolfSSL_SESSION_free(session);
  57237. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  57238. }
  57239. else {
  57240. /* server */
  57241. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  57242. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  57243. #ifdef WOLFSSL_PRIORITIZE_PSK
  57244. /* The ticket should be first tried with all ciphersuites and chosen */
  57245. AssertFalse(test_prioritize_psk_cb_called);
  57246. #else
  57247. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  57248. * callback that sets this var. */
  57249. AssertTrue(test_prioritize_psk_cb_called);
  57250. #endif
  57251. }
  57252. }
  57253. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  57254. {
  57255. if (!wolfSSL_is_server(ssl))
  57256. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57257. WOLFSSL_SUCCESS);
  57258. else
  57259. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57260. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  57261. }
  57262. static int test_prioritize_psk(void)
  57263. {
  57264. EXPECT_DECLS;
  57265. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  57266. * of the PSK's will be followed instead of the ciphersuite. */
  57267. callback_functions client_cbs, server_cbs;
  57268. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57269. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57270. client_cbs.method = wolfTLSv1_3_client_method;
  57271. server_cbs.method = wolfTLSv1_3_server_method;
  57272. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  57273. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  57274. client_cbs.on_result = test_prioritize_psk_on_result;
  57275. server_cbs.on_result = test_prioritize_psk_on_result;
  57276. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57277. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  57278. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  57279. return EXPECT_RESULT();
  57280. }
  57281. #else
  57282. static int test_prioritize_psk(void)
  57283. {
  57284. return TEST_SKIPPED;
  57285. }
  57286. #endif
  57287. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  57288. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57289. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57290. !defined(WOLFSSL_NO_TLS12)
  57291. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  57292. {
  57293. EXPECT_DECLS;
  57294. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  57295. /* Set TLS 1.3 specific suite */
  57296. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  57297. return EXPECT_RESULT();
  57298. }
  57299. static int test_wolfSSL_CTX_set_ciphersuites(void)
  57300. {
  57301. EXPECT_DECLS;
  57302. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  57303. * do a 1.2 connection. */
  57304. test_ssl_cbf client_cbs, server_cbs;
  57305. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57306. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57307. client_cbs.method = wolfTLSv1_2_client_method;
  57308. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  57309. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  57310. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57311. &server_cbs, NULL), TEST_SUCCESS);
  57312. return EXPECT_RESULT();
  57313. }
  57314. #else
  57315. static int test_wolfSSL_CTX_set_ciphersuites(void)
  57316. {
  57317. return TEST_SKIPPED;
  57318. }
  57319. #endif
  57320. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  57321. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57322. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  57323. {
  57324. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57325. return TEST_SUCCESS;
  57326. }
  57327. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  57328. {
  57329. EXPECT_DECLS;
  57330. WOLFSSL_ALERT_HISTORY h;
  57331. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57332. ExpectIntEQ(h.last_rx.level, alert_fatal);
  57333. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  57334. return EXPECT_RESULT();
  57335. }
  57336. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  57337. {
  57338. EXPECT_DECLS;
  57339. test_ssl_cbf client_cbs, server_cbs;
  57340. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57341. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57342. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  57343. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  57344. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  57345. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  57346. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  57347. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57348. &server_cbs, NULL), TEST_FAIL);
  57349. return EXPECT_RESULT();
  57350. }
  57351. #else
  57352. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  57353. {
  57354. return TEST_SKIPPED;
  57355. }
  57356. #endif
  57357. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  57358. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  57359. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  57360. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  57361. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  57362. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57363. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  57364. static int test_TLS_13_ticket_different_ciphers_run = 0;
  57365. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  57366. {
  57367. EXPECT_DECLS;
  57368. switch (test_TLS_13_ticket_different_ciphers_run) {
  57369. case 0:
  57370. /* First run */
  57371. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  57372. WOLFSSL_SUCCESS);
  57373. if (wolfSSL_is_server(ssl)) {
  57374. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  57375. wolfSSL_get_SSL_CTX(ssl));
  57376. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  57377. test_TLS_13_ticket_different_ciphers_ctx));
  57378. }
  57379. break;
  57380. case 1:
  57381. /* Second run */
  57382. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  57383. "TLS13-AES128-GCM-SHA256"),
  57384. WOLFSSL_SUCCESS);
  57385. if (!wolfSSL_is_server(ssl)) {
  57386. ExpectIntEQ(wolfSSL_set_session(ssl,
  57387. test_TLS_13_ticket_different_ciphers_session),
  57388. WOLFSSL_SUCCESS);
  57389. }
  57390. break;
  57391. default:
  57392. /* Bad state? */
  57393. Fail(("Should not enter here"), ("Should not enter here"));
  57394. }
  57395. return EXPECT_RESULT();
  57396. }
  57397. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  57398. {
  57399. EXPECT_DECLS;
  57400. switch (test_TLS_13_ticket_different_ciphers_run) {
  57401. case 0:
  57402. /* First run */
  57403. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  57404. wolfSSL_get1_session(ssl));
  57405. break;
  57406. case 1:
  57407. /* Second run */
  57408. ExpectTrue(wolfSSL_session_reused(ssl));
  57409. break;
  57410. default:
  57411. /* Bad state? */
  57412. Fail(("Should not enter here"), ("Should not enter here"));
  57413. }
  57414. return EXPECT_RESULT();
  57415. }
  57416. static int test_TLS_13_ticket_different_ciphers(void)
  57417. {
  57418. EXPECT_DECLS;
  57419. /* Check that we handle the connection when the ticket doesn't match
  57420. * the first ciphersuite. */
  57421. test_ssl_cbf client_cbs, server_cbs;
  57422. struct test_params {
  57423. method_provider client_meth;
  57424. method_provider server_meth;
  57425. int doUdp;
  57426. } params[] = {
  57427. #ifdef WOLFSSL_DTLS13
  57428. /* Test that the stateless code handles sessions correctly */
  57429. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  57430. #endif
  57431. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  57432. };
  57433. size_t i;
  57434. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57435. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57436. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57437. test_TLS_13_ticket_different_ciphers_run = 0;
  57438. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  57439. client_cbs.method = params[i].client_meth;
  57440. server_cbs.method = params[i].server_meth;
  57441. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57442. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  57443. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  57444. server_cbs.ticNoInit = 1;
  57445. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57446. &server_cbs, NULL), TEST_SUCCESS);
  57447. test_TLS_13_ticket_different_ciphers_run++;
  57448. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  57449. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57450. &server_cbs, NULL), TEST_SUCCESS);
  57451. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  57452. test_TLS_13_ticket_different_ciphers_session = NULL;
  57453. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  57454. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  57455. }
  57456. return EXPECT_RESULT();
  57457. }
  57458. #else
  57459. static int test_TLS_13_ticket_different_ciphers(void)
  57460. {
  57461. return TEST_SKIPPED;
  57462. }
  57463. #endif
  57464. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  57465. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57466. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  57467. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  57468. byte test_extra_alerts_wrong_cs_sh[] = {
  57469. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  57470. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  57471. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  57472. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  57473. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  57474. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  57475. 0xbb, 0x07, 0x54, 0x1c,
  57476. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  57477. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  57478. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  57479. };
  57480. static int test_extra_alerts_wrong_cs(void)
  57481. {
  57482. EXPECT_DECLS;
  57483. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  57484. struct test_memio_ctx test_ctx;
  57485. WOLFSSL_CTX *ctx_c = NULL;
  57486. WOLFSSL_ALERT_HISTORY h;
  57487. WOLFSSL *ssl_c = NULL;
  57488. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57489. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  57490. wolfTLSv1_2_client_method, NULL), 0);
  57491. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  57492. WOLFSSL_SUCCESS);
  57493. /* CH */
  57494. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57495. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57496. WOLFSSL_ERROR_WANT_READ);
  57497. /* consume CH */
  57498. test_ctx.s_len = 0;
  57499. /* inject SH */
  57500. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  57501. sizeof(test_extra_alerts_wrong_cs_sh));
  57502. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  57503. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57504. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57505. WOLFSSL_ERROR_WANT_READ);
  57506. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57507. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  57508. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57509. wolfSSL_free(ssl_c);
  57510. wolfSSL_CTX_free(ctx_c);
  57511. #endif
  57512. return EXPECT_RESULT();
  57513. }
  57514. #else
  57515. static int test_extra_alerts_wrong_cs(void)
  57516. {
  57517. return TEST_SKIPPED;
  57518. }
  57519. #endif
  57520. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  57521. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  57522. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  57523. {
  57524. tail_len -= msg_length;
  57525. XMEMMOVE(msg, msg + msg_length, tail_len);
  57526. *len = *len - msg_length;
  57527. }
  57528. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  57529. byte *found)
  57530. {
  57531. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  57532. const unsigned int _RECORD_HEADER_SZ = 5;
  57533. const int _change_cipher_hs = 55;
  57534. const int _change_cipher = 20;
  57535. const int _handshake = 22;
  57536. unsigned int tail_len;
  57537. byte *idx, *curr;
  57538. word8 currType;
  57539. word16 rLength;
  57540. word32 hLength;
  57541. idx = buf;
  57542. tail_len = *len;
  57543. *found = 0;
  57544. while (tail_len > _RECORD_HEADER_SZ) {
  57545. curr = idx;
  57546. currType = *idx;
  57547. ato16(idx + 3, &rLength);
  57548. idx += _RECORD_HEADER_SZ;
  57549. tail_len -= _RECORD_HEADER_SZ;
  57550. if (tail_len < rLength)
  57551. return -1;
  57552. if (type == _change_cipher_hs && currType == _change_cipher) {
  57553. if (rLength != 1)
  57554. return -1;
  57555. /* match */
  57556. test_remove_msg(curr, *len - (int)(curr - buf),
  57557. len, _RECORD_HEADER_SZ + 1);
  57558. *found = 1;
  57559. return 0;
  57560. }
  57561. if (currType != _handshake) {
  57562. idx += rLength;
  57563. tail_len -= rLength;
  57564. continue;
  57565. }
  57566. if (rLength < _HANDSHAKE_HEADER_SZ)
  57567. return -1;
  57568. currType = *idx;
  57569. ato24(idx+1, &hLength);
  57570. hLength += _HANDSHAKE_HEADER_SZ;
  57571. if (tail_len < hLength)
  57572. return -1;
  57573. if (currType != type) {
  57574. idx += hLength;
  57575. tail_len -= hLength;
  57576. continue;
  57577. }
  57578. /* match */
  57579. test_remove_msg(curr, *len - (int)(curr - buf), len,
  57580. hLength + _RECORD_HEADER_SZ);
  57581. *found = 1;
  57582. return 0;
  57583. }
  57584. /* not found */
  57585. return 0;
  57586. }
  57587. static int test_remove_hs_message(byte hs_message_type,
  57588. int extra_round, byte alert_type)
  57589. {
  57590. EXPECT_DECLS;
  57591. WOLFSSL_CTX *ctx_c = NULL;
  57592. WOLFSSL_CTX *ctx_s = NULL;
  57593. WOLFSSL *ssl_c = NULL;
  57594. WOLFSSL *ssl_s = NULL;
  57595. struct test_memio_ctx test_ctx;
  57596. WOLFSSL_ALERT_HISTORY h;
  57597. byte found = 0;
  57598. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57599. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57600. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57601. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57602. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57603. WOLFSSL_ERROR_WANT_READ);
  57604. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57605. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57606. WOLFSSL_ERROR_WANT_READ);
  57607. if (extra_round) {
  57608. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57609. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57610. WOLFSSL_ERROR_WANT_READ);
  57611. /* this will complete handshake from server side */
  57612. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57613. }
  57614. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  57615. &test_ctx.c_len, hs_message_type, &found), 0);
  57616. if (!found) {
  57617. wolfSSL_free(ssl_c);
  57618. wolfSSL_CTX_free(ctx_c);
  57619. wolfSSL_free(ssl_s);
  57620. wolfSSL_CTX_free(ctx_s);
  57621. return TEST_SKIPPED;
  57622. }
  57623. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57624. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57625. WOLFSSL_ERROR_WANT_READ);
  57626. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57627. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  57628. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57629. wolfSSL_free(ssl_c);
  57630. wolfSSL_CTX_free(ctx_c);
  57631. wolfSSL_free(ssl_s);
  57632. wolfSSL_CTX_free(ctx_s);
  57633. return EXPECT_RESULT();
  57634. }
  57635. static int test_extra_alerts_skip_hs(void)
  57636. {
  57637. EXPECT_DECLS;
  57638. const byte _server_key_exchange = 12;
  57639. const byte _server_hello = 2;
  57640. const byte _certificate = 11;
  57641. /* server_hello */
  57642. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  57643. unexpected_message), TEST_FAIL);
  57644. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  57645. 0xff), TEST_FAIL);
  57646. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  57647. unexpected_message), TEST_FAIL);
  57648. return EXPECT_RESULT();
  57649. }
  57650. #else
  57651. static int test_extra_alerts_skip_hs(void)
  57652. {
  57653. return TEST_SKIPPED;
  57654. }
  57655. #endif
  57656. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  57657. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  57658. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  57659. unsigned char* key, unsigned int key_max_len)
  57660. {
  57661. (void)ssl;
  57662. (void)id;
  57663. (void)key_max_len;
  57664. /* zero means error */
  57665. key[0] = 0x10;
  57666. return 1;
  57667. }
  57668. static int test_extra_alerts_bad_psk(void)
  57669. {
  57670. EXPECT_DECLS;
  57671. WOLFSSL_CTX *ctx_c = NULL;
  57672. WOLFSSL_CTX *ctx_s = NULL;
  57673. WOLFSSL *ssl_c = NULL;
  57674. WOLFSSL *ssl_s = NULL;
  57675. struct test_memio_ctx test_ctx;
  57676. WOLFSSL_ALERT_HISTORY h;
  57677. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57678. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57679. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  57680. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  57681. WOLFSSL_SUCCESS);
  57682. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  57683. WOLFSSL_SUCCESS);
  57684. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  57685. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57686. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57687. WOLFSSL_ERROR_WANT_READ);
  57688. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57689. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  57690. WOLFSSL_ERROR_WANT_READ);
  57691. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57692. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57693. WOLFSSL_ERROR_WANT_READ);
  57694. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  57695. ExpectIntEQ(h.last_tx.code, handshake_failure);
  57696. ExpectIntEQ(h.last_tx.level, alert_fatal);
  57697. wolfSSL_free(ssl_c);
  57698. wolfSSL_CTX_free(ctx_c);
  57699. wolfSSL_free(ssl_s);
  57700. wolfSSL_CTX_free(ctx_s);
  57701. return EXPECT_RESULT();
  57702. }
  57703. #else
  57704. static int test_extra_alerts_bad_psk(void)
  57705. {
  57706. return TEST_SKIPPED;
  57707. }
  57708. #endif
  57709. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  57710. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57711. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  57712. int sz, void *ctx)
  57713. {
  57714. static int sentServerHello = FALSE;
  57715. if (!sentServerHello) {
  57716. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  57717. size_t i;
  57718. if (sz < (int)sizeof(renegExt))
  57719. return WOLFSSL_CBIO_ERR_GENERAL;
  57720. /* Remove SCR from ServerHello */
  57721. for (i = 0; i < sz - sizeof(renegExt); i++) {
  57722. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  57723. /* Found the extension. Change it to something unrecognized. */
  57724. buf[i+1] = 0x11;
  57725. break;
  57726. }
  57727. }
  57728. sentServerHello = TRUE;
  57729. }
  57730. return EmbedSend(ssl, buf, sz, ctx);
  57731. }
  57732. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  57733. {
  57734. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  57735. }
  57736. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  57737. {
  57738. WOLFSSL_ALERT_HISTORY h;
  57739. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  57740. AssertIntEQ(h.last_rx.code, handshake_failure);
  57741. AssertIntEQ(h.last_rx.level, alert_fatal);
  57742. }
  57743. static int test_harden_no_secure_renegotiation(void)
  57744. {
  57745. EXPECT_DECLS;
  57746. callback_functions client_cbs, server_cbs;
  57747. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57748. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57749. client_cbs.method = wolfTLSv1_2_client_method;
  57750. server_cbs.method = wolfTLSv1_2_server_method;
  57751. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  57752. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  57753. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57754. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  57755. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  57756. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  57757. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  57758. server_cbs.last_err == FATAL_ERROR);
  57759. return EXPECT_RESULT();
  57760. }
  57761. #else
  57762. static int test_harden_no_secure_renegotiation(void)
  57763. {
  57764. return TEST_SKIPPED;
  57765. }
  57766. #endif
  57767. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57768. static int test_override_alt_cert_chain_cert_cb(int preverify,
  57769. WOLFSSL_X509_STORE_CTX* store)
  57770. {
  57771. fprintf(stderr, "preverify: %d\n", preverify);
  57772. fprintf(stderr, "store->error: %d\n", store->error);
  57773. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  57774. if (store->error == OCSP_INVALID_STATUS) {
  57775. fprintf(stderr, "Overriding OCSP error\n");
  57776. return 1;
  57777. }
  57778. #ifndef WOLFSSL_ALT_CERT_CHAINS
  57779. else if ((store->error == ASN_NO_SIGNER_E ||
  57780. store->error == ASN_SELF_SIGNED_E
  57781. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  57782. defined(HAVE_WEBSERVER)
  57783. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  57784. #endif
  57785. ) && store->error_depth == store->totalCerts - 1) {
  57786. fprintf(stderr, "Overriding no signer error only for root cert\n");
  57787. return 1;
  57788. }
  57789. #endif
  57790. else
  57791. return preverify;
  57792. }
  57793. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  57794. int urlSz, unsigned char* request, int requestSz,
  57795. unsigned char** response)
  57796. {
  57797. (void)ioCtx;
  57798. (void)url;
  57799. (void)urlSz;
  57800. (void)request;
  57801. (void)requestSz;
  57802. (void)response;
  57803. return -1;
  57804. }
  57805. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  57806. {
  57807. EXPECT_DECLS;
  57808. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  57809. test_override_alt_cert_chain_cert_cb);
  57810. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57811. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57812. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57813. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57814. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57815. WOLFSSL_SUCCESS);
  57816. return EXPECT_RESULT();
  57817. }
  57818. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  57819. {
  57820. EXPECT_DECLS;
  57821. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57822. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  57823. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  57824. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  57825. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  57826. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  57827. WOLFSSL_SUCCESS);
  57828. return EXPECT_RESULT();
  57829. }
  57830. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  57831. {
  57832. EXPECT_DECLS;
  57833. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  57834. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  57835. return EXPECT_RESULT();
  57836. }
  57837. static int test_override_alt_cert_chain(void)
  57838. {
  57839. EXPECT_DECLS;
  57840. size_t i;
  57841. struct test_params {
  57842. ctx_cb client_ctx_cb;
  57843. ctx_cb server_ctx_cb;
  57844. int result;
  57845. } params[] = {
  57846. {test_override_alt_cert_chain_client_ctx_ready,
  57847. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  57848. {test_override_alt_cert_chain_client_ctx_ready2,
  57849. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  57850. };
  57851. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57852. test_ssl_cbf client_cbs, server_cbs;
  57853. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57854. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57855. fprintf(stderr, "test config: %d\n", (int)i);
  57856. client_cbs.ctx_ready = params[i].client_ctx_cb;
  57857. server_cbs.ctx_ready = params[i].server_ctx_cb;
  57858. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57859. &server_cbs, NULL), params[i].result);
  57860. ExpectIntEQ(client_cbs.return_code, params[i].result);
  57861. ExpectIntEQ(server_cbs.return_code, params[i].result);
  57862. }
  57863. return EXPECT_RESULT();
  57864. }
  57865. #else
  57866. static int test_override_alt_cert_chain(void)
  57867. {
  57868. return TEST_SKIPPED;
  57869. }
  57870. #endif
  57871. #if defined(HAVE_RPK)
  57872. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  57873. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  57874. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  57875. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  57876. {
  57877. int ret = WOLFSSL_SUCCESS;
  57878. (void)mode;
  57879. (void)strctx;
  57880. WOLFSSL_ENTER("MyRpkVerifyCb");
  57881. return ret;
  57882. }
  57883. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  57884. static WC_INLINE int test_rpk_memio_setup(
  57885. struct test_memio_ctx *ctx,
  57886. WOLFSSL_CTX **ctx_c,
  57887. WOLFSSL_CTX **ctx_s,
  57888. WOLFSSL **ssl_c,
  57889. WOLFSSL **ssl_s,
  57890. method_provider method_c,
  57891. method_provider method_s,
  57892. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  57893. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  57894. const char* pkey_c, int fmt_kc, /* client private key and format */
  57895. const char* pkey_s, int fmt_ks /* server private key and format */
  57896. )
  57897. {
  57898. int ret;
  57899. if (ctx_c != NULL && *ctx_c == NULL) {
  57900. *ctx_c = wolfSSL_CTX_new(method_c());
  57901. if (*ctx_c == NULL) {
  57902. return -1;
  57903. }
  57904. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  57905. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  57906. if (ret != WOLFSSL_SUCCESS) {
  57907. return -1;
  57908. }
  57909. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  57910. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  57911. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  57912. if (ret != WOLFSSL_SUCCESS) {
  57913. return -1;
  57914. }
  57915. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  57916. if (ret != WOLFSSL_SUCCESS) {
  57917. return -1;
  57918. }
  57919. }
  57920. if (ctx_s != NULL && *ctx_s == NULL) {
  57921. *ctx_s = wolfSSL_CTX_new(method_s());
  57922. if (*ctx_s == NULL) {
  57923. return -1;
  57924. }
  57925. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  57926. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  57927. if (ret != WOLFSSL_SUCCESS) {
  57928. return -1;
  57929. }
  57930. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  57931. if (ret != WOLFSSL_SUCCESS) {
  57932. return -1;
  57933. }
  57934. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  57935. if (ret != WOLFSSL_SUCCESS) {
  57936. return -1;
  57937. }
  57938. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  57939. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  57940. if (ctx->s_ciphers != NULL) {
  57941. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  57942. if (ret != WOLFSSL_SUCCESS) {
  57943. return -1;
  57944. }
  57945. }
  57946. }
  57947. if (ctx_c != NULL && ssl_c != NULL) {
  57948. *ssl_c = wolfSSL_new(*ctx_c);
  57949. if (*ssl_c == NULL) {
  57950. return -1;
  57951. }
  57952. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  57953. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  57954. }
  57955. if (ctx_s != NULL && ssl_s != NULL) {
  57956. *ssl_s = wolfSSL_new(*ctx_s);
  57957. if (*ssl_s == NULL) {
  57958. return -1;
  57959. }
  57960. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  57961. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  57962. #if !defined(NO_DH)
  57963. SetDH(*ssl_s);
  57964. #endif
  57965. }
  57966. return 0;
  57967. }
  57968. #endif /* HAVE_RPK */
  57969. static int test_rpk_set_xxx_cert_type(void)
  57970. {
  57971. EXPECT_DECLS;
  57972. #if defined(HAVE_RPK)
  57973. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  57974. WOLFSSL_CTX* ctx = NULL;
  57975. WOLFSSL* ssl = NULL;
  57976. int tp;
  57977. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  57978. ExpectNotNull(ctx);
  57979. ssl = wolfSSL_new(ctx);
  57980. ExpectNotNull(ssl);
  57981. /*--------------------------------------------*/
  57982. /* tests for wolfSSL_CTX_set_client_cert_type */
  57983. /*--------------------------------------------*/
  57984. /* illegal parameter test caces */
  57985. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  57986. MAX_CLIENT_CERT_TYPE_CNT),
  57987. BAD_FUNC_ARG);
  57988. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57989. sizeof(ctype)),
  57990. BAD_FUNC_ARG);
  57991. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  57992. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  57993. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57994. MAX_CLIENT_CERT_TYPE_CNT),
  57995. BAD_FUNC_ARG);
  57996. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  57997. ctype[1] = 10; /* set unknown cert type */
  57998. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  57999. MAX_CLIENT_CERT_TYPE_CNT),
  58000. BAD_FUNC_ARG);
  58001. /* pass larger type count */
  58002. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58003. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58004. ctype[2] = 1; /* pass unacceptable type count */
  58005. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58006. MAX_CLIENT_CERT_TYPE_CNT + 1),
  58007. BAD_FUNC_ARG);
  58008. /* should accept NULL for type buffer */
  58009. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  58010. MAX_CLIENT_CERT_TYPE_CNT),
  58011. WOLFSSL_SUCCESS);
  58012. /* should accept zero for type count */
  58013. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58014. 0),
  58015. WOLFSSL_SUCCESS);
  58016. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  58017. MAX_CLIENT_CERT_TYPE_CNT),
  58018. WOLFSSL_SUCCESS);
  58019. /*--------------------------------------------*/
  58020. /* tests for wolfSSL_CTX_set_server_cert_type */
  58021. /*--------------------------------------------*/
  58022. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  58023. MAX_SERVER_CERT_TYPE_CNT),
  58024. BAD_FUNC_ARG);
  58025. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58026. sizeof(ctype)),
  58027. BAD_FUNC_ARG);
  58028. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58029. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58030. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58031. MAX_SERVER_CERT_TYPE_CNT),
  58032. BAD_FUNC_ARG);
  58033. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58034. ctype[1] = 10; /* set unknown cert type */
  58035. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58036. MAX_SERVER_CERT_TYPE_CNT),
  58037. BAD_FUNC_ARG);
  58038. /* pass larger type count */
  58039. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58040. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58041. ctype[2] = 1; /* pass unacceptable type count */
  58042. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58043. MAX_SERVER_CERT_TYPE_CNT + 1),
  58044. BAD_FUNC_ARG);
  58045. /* should accept NULL for type buffer */
  58046. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  58047. MAX_SERVER_CERT_TYPE_CNT),
  58048. WOLFSSL_SUCCESS);
  58049. /* should accept zero for type count */
  58050. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58051. 0),
  58052. WOLFSSL_SUCCESS);
  58053. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  58054. MAX_CLIENT_CERT_TYPE_CNT),
  58055. WOLFSSL_SUCCESS);
  58056. /*--------------------------------------------*/
  58057. /* tests for wolfSSL_set_client_cert_type */
  58058. /*--------------------------------------------*/
  58059. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  58060. MAX_CLIENT_CERT_TYPE_CNT),
  58061. BAD_FUNC_ARG);
  58062. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58063. sizeof(ctype)),
  58064. BAD_FUNC_ARG);
  58065. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58066. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58067. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58068. MAX_CLIENT_CERT_TYPE_CNT),
  58069. BAD_FUNC_ARG);
  58070. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58071. ctype[1] = 10; /* set unknown cert type */
  58072. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58073. MAX_CLIENT_CERT_TYPE_CNT),
  58074. BAD_FUNC_ARG);
  58075. /* pass larger type count */
  58076. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58077. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58078. ctype[2] = 1; /* pass unacceptable type count */
  58079. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58080. MAX_CLIENT_CERT_TYPE_CNT + 1),
  58081. BAD_FUNC_ARG);
  58082. /* should accept NULL for type buffer */
  58083. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  58084. MAX_CLIENT_CERT_TYPE_CNT),
  58085. WOLFSSL_SUCCESS);
  58086. /* should accept zero for type count */
  58087. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58088. 0),
  58089. WOLFSSL_SUCCESS);
  58090. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  58091. MAX_CLIENT_CERT_TYPE_CNT),
  58092. WOLFSSL_SUCCESS);
  58093. /*--------------------------------------------*/
  58094. /* tests for wolfSSL_CTX_set_server_cert_type */
  58095. /*--------------------------------------------*/
  58096. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  58097. MAX_SERVER_CERT_TYPE_CNT),
  58098. BAD_FUNC_ARG);
  58099. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58100. sizeof(ctype)),
  58101. BAD_FUNC_ARG);
  58102. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  58103. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  58104. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58105. MAX_SERVER_CERT_TYPE_CNT),
  58106. BAD_FUNC_ARG);
  58107. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  58108. ctype[1] = 10; /* set unknown cert type */
  58109. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58110. MAX_SERVER_CERT_TYPE_CNT),
  58111. BAD_FUNC_ARG);
  58112. /* pass larger type count */
  58113. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  58114. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  58115. ctype[2] = 1; /* pass unacceptable type count */
  58116. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58117. MAX_SERVER_CERT_TYPE_CNT + 1),
  58118. BAD_FUNC_ARG);
  58119. /* should accept NULL for type buffer */
  58120. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  58121. MAX_SERVER_CERT_TYPE_CNT),
  58122. WOLFSSL_SUCCESS);
  58123. /* should accept zero for type count */
  58124. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58125. 0),
  58126. WOLFSSL_SUCCESS);
  58127. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  58128. MAX_SERVER_CERT_TYPE_CNT),
  58129. WOLFSSL_SUCCESS);
  58130. /*------------------------------------------------*/
  58131. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  58132. /*------------------------------------------------*/
  58133. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  58134. BAD_FUNC_ARG);
  58135. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  58136. BAD_FUNC_ARG);
  58137. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  58138. BAD_FUNC_ARG);
  58139. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  58140. BAD_FUNC_ARG);
  58141. /* clean up */
  58142. wolfSSL_free(ssl);
  58143. wolfSSL_CTX_free(ctx);
  58144. #endif
  58145. return EXPECT_RESULT();
  58146. }
  58147. static int test_tls13_rpk_handshake(void)
  58148. {
  58149. EXPECT_DECLS;
  58150. #if defined(HAVE_RPK)
  58151. int ret = 0;
  58152. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  58153. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  58154. struct test_memio_ctx test_ctx;
  58155. int err;
  58156. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  58157. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  58158. int typeCnt_c;
  58159. int typeCnt_s;
  58160. int tp;
  58161. (void)err;
  58162. (void)typeCnt_c;
  58163. (void)typeCnt_s;
  58164. (void)certType_c;
  58165. (void)certType_s;
  58166. /* TLS1.2
  58167. * Both client and server load x509 cert and start handshaking.
  58168. * Check no negotiation occurred.
  58169. */
  58170. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58171. ExpectIntEQ(
  58172. test_rpk_memio_setup(
  58173. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58174. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  58175. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58176. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58177. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58178. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  58179. , 0);
  58180. /* set client certificate type in client end */
  58181. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58182. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58183. typeCnt_c = 2;
  58184. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58185. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58186. typeCnt_s = 2;
  58187. /* both clien and server do not call client/server_cert_type APIs,
  58188. * expecting default settings works and no negotiation performed.
  58189. */
  58190. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58191. return TEST_FAIL;
  58192. /* confirm no negotiation occurred */
  58193. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58194. WOLFSSL_SUCCESS);
  58195. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58196. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58197. WOLFSSL_SUCCESS);
  58198. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58199. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58200. WOLFSSL_SUCCESS);
  58201. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58202. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58203. WOLFSSL_SUCCESS);
  58204. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58205. wolfSSL_free(ssl_c);
  58206. wolfSSL_CTX_free(ctx_c);
  58207. wolfSSL_free(ssl_s);
  58208. wolfSSL_CTX_free(ctx_s);
  58209. ssl_c = ssl_s = NULL;
  58210. ctx_c = ctx_s = NULL;
  58211. /* Both client and server load x509 cert and start handshaking.
  58212. * Check no negotiation occurred.
  58213. */
  58214. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58215. ExpectIntEQ(
  58216. test_rpk_memio_setup(
  58217. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58218. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58219. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58220. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58221. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58222. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58223. , 0);
  58224. /* set client certificate type in client end */
  58225. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58226. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58227. typeCnt_c = 2;
  58228. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58229. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58230. typeCnt_s = 2;
  58231. /* both clien and server do not call client/server_cert_type APIs,
  58232. * expecting default settings works and no negotiation performed.
  58233. */
  58234. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58235. return TEST_FAIL;
  58236. /* confirm no negotiation occurred */
  58237. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58238. WOLFSSL_SUCCESS);
  58239. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58240. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58241. WOLFSSL_SUCCESS);
  58242. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58243. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58244. WOLFSSL_SUCCESS);
  58245. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58246. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58247. WOLFSSL_SUCCESS);
  58248. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58249. wolfSSL_free(ssl_c);
  58250. wolfSSL_CTX_free(ctx_c);
  58251. wolfSSL_free(ssl_s);
  58252. wolfSSL_CTX_free(ctx_s);
  58253. ssl_c = ssl_s = NULL;
  58254. ctx_c = ctx_s = NULL;
  58255. /* Both client and server load RPK cert and start handshaking.
  58256. * Confirm negotiated cert types match as expected.
  58257. */
  58258. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58259. ExpectIntEQ(
  58260. test_rpk_memio_setup(
  58261. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58262. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58263. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58264. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58265. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58266. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58267. , 0);
  58268. /* set client certificate type in client end */
  58269. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58270. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58271. typeCnt_c = 2;
  58272. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58273. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58274. typeCnt_s = 2;
  58275. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58276. WOLFSSL_SUCCESS);
  58277. /* set server certificate type in client end */
  58278. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58279. WOLFSSL_SUCCESS);
  58280. /* set client certificate type in server end */
  58281. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58282. WOLFSSL_SUCCESS);
  58283. /* set server certificate type in server end */
  58284. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58285. WOLFSSL_SUCCESS);
  58286. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58287. return TEST_FAIL;
  58288. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58289. WOLFSSL_SUCCESS);
  58290. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58291. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58292. WOLFSSL_SUCCESS);
  58293. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58294. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58295. WOLFSSL_SUCCESS);
  58296. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58297. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58298. WOLFSSL_SUCCESS);
  58299. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58300. wolfSSL_free(ssl_c);
  58301. wolfSSL_CTX_free(ctx_c);
  58302. wolfSSL_free(ssl_s);
  58303. wolfSSL_CTX_free(ctx_s);
  58304. ssl_c = ssl_s = NULL;
  58305. ctx_c = ctx_s = NULL;
  58306. /* TLS1.2
  58307. * Both client and server load RPK cert and start handshaking.
  58308. * Confirm negotiated cert types match as expected.
  58309. */
  58310. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58311. ExpectIntEQ(
  58312. test_rpk_memio_setup(
  58313. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58314. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  58315. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58316. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58317. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58318. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58319. , 0);
  58320. /* set client certificate type in client end */
  58321. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58322. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58323. typeCnt_c = 2;
  58324. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58325. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58326. typeCnt_s = 2;
  58327. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58328. WOLFSSL_SUCCESS);
  58329. /* set server certificate type in client end */
  58330. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58331. WOLFSSL_SUCCESS);
  58332. /* set client certificate type in server end */
  58333. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58334. WOLFSSL_SUCCESS);
  58335. /* set server certificate type in server end */
  58336. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58337. WOLFSSL_SUCCESS);
  58338. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58339. return TEST_FAIL;
  58340. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58341. WOLFSSL_SUCCESS);
  58342. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58343. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58344. WOLFSSL_SUCCESS);
  58345. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58346. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58347. WOLFSSL_SUCCESS);
  58348. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58349. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58350. WOLFSSL_SUCCESS);
  58351. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58352. wolfSSL_free(ssl_c);
  58353. wolfSSL_CTX_free(ctx_c);
  58354. wolfSSL_free(ssl_s);
  58355. wolfSSL_CTX_free(ctx_s);
  58356. ssl_c = ssl_s = NULL;
  58357. ctx_c = ctx_s = NULL;
  58358. /* Both client and server load x509 cert.
  58359. * Have client call set_client_cert_type with both RPK and x509.
  58360. * This doesn't makes client add client cert type extension to ClientHello,
  58361. * since it does not load RPK cert actually.
  58362. */
  58363. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58364. ExpectIntEQ(
  58365. test_rpk_memio_setup(
  58366. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58367. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58368. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58369. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58370. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58371. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58372. , 0);
  58373. /* set client certificate type in client end */
  58374. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58375. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58376. typeCnt_c = 2;
  58377. /* client indicates both RPK and x509 certs are available but loaded RPK
  58378. * cert only. It does not have client add client-cert-type extension in CH.
  58379. */
  58380. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58381. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58382. typeCnt_c = 2;
  58383. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58384. WOLFSSL_SUCCESS);
  58385. /* client indicates both RPK and x509 certs are acceptable */
  58386. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58387. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58388. typeCnt_s = 2;
  58389. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58390. WOLFSSL_SUCCESS);
  58391. /* server indicates both RPK and x509 certs are acceptable */
  58392. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58393. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58394. typeCnt_c = 2;
  58395. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58396. WOLFSSL_SUCCESS);
  58397. /* server should indicate only RPK cert is available */
  58398. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58399. certType_s[1] = -1;
  58400. typeCnt_s = 1;
  58401. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58402. WOLFSSL_SUCCESS);
  58403. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58404. return TEST_FAIL;
  58405. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  58406. * be returned as cert type.
  58407. */
  58408. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58409. WOLFSSL_SUCCESS);
  58410. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58411. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58412. WOLFSSL_SUCCESS);
  58413. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58414. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58415. WOLFSSL_SUCCESS);
  58416. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58417. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58418. WOLFSSL_SUCCESS);
  58419. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58420. wolfSSL_free(ssl_c);
  58421. wolfSSL_CTX_free(ctx_c);
  58422. wolfSSL_free(ssl_s);
  58423. wolfSSL_CTX_free(ctx_s);
  58424. ssl_c = ssl_s = NULL;
  58425. ctx_c = ctx_s = NULL;
  58426. /* Have client load RPK cert and have server load x509 cert.
  58427. * Check the negotiation result from both ends.
  58428. */
  58429. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58430. ExpectIntEQ(
  58431. test_rpk_memio_setup(
  58432. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58433. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58434. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58435. svrCertFile, WOLFSSL_FILETYPE_PEM,
  58436. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58437. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58438. , 0);
  58439. /* have client tell to use RPK cert */
  58440. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58441. certType_c[1] = -1;
  58442. typeCnt_c = 1;
  58443. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58444. WOLFSSL_SUCCESS);
  58445. /* have client tell to accept both RPK and x509 cert */
  58446. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58447. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58448. typeCnt_s = 2;
  58449. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58450. WOLFSSL_SUCCESS);
  58451. /* have server accept to both RPK and x509 cert */
  58452. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58453. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58454. typeCnt_c = 2;
  58455. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58456. WOLFSSL_SUCCESS);
  58457. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  58458. * end, expecting the default setting works.
  58459. */
  58460. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  58461. return TEST_FAIL;
  58462. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58463. WOLFSSL_SUCCESS);
  58464. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58465. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58466. WOLFSSL_SUCCESS);
  58467. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58468. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58469. WOLFSSL_SUCCESS);
  58470. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58471. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58472. WOLFSSL_SUCCESS);
  58473. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58474. wolfSSL_free(ssl_c);
  58475. wolfSSL_CTX_free(ctx_c);
  58476. wolfSSL_free(ssl_s);
  58477. wolfSSL_CTX_free(ctx_s);
  58478. ssl_c = ssl_s = NULL;
  58479. ctx_c = ctx_s = NULL;
  58480. /* Have both client and server load RPK cert, however, have server
  58481. * indicate its cert type x509.
  58482. * Client is expected to detect the cert type mismatch then to send alert
  58483. * with "unsupported_certificate".
  58484. */
  58485. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58486. ExpectIntEQ(
  58487. test_rpk_memio_setup(
  58488. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58489. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58490. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58491. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  58492. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58493. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58494. , 0);
  58495. /* have client tell to use RPK cert */
  58496. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58497. certType_c[1] = -1;
  58498. typeCnt_c = 1;
  58499. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58500. WOLFSSL_SUCCESS);
  58501. /* have client tell to accept both RPK and x509 cert */
  58502. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58503. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58504. typeCnt_s = 2;
  58505. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58506. WOLFSSL_SUCCESS);
  58507. /* have server accept to both RPK and x509 cert */
  58508. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58509. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58510. typeCnt_c = 2;
  58511. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58512. WOLFSSL_SUCCESS);
  58513. /* have server tell to use x509 cert intentionally. This will bring
  58514. * certificate type mismatch in client side.
  58515. */
  58516. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58517. certType_s[1] = -1;
  58518. typeCnt_s = 1;
  58519. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58520. WOLFSSL_SUCCESS);
  58521. /* expect client detect cert type mismatch then send Alert */
  58522. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58523. if (ret != -1)
  58524. return TEST_FAIL;
  58525. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  58526. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58527. WOLFSSL_SUCCESS);
  58528. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58529. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58530. WOLFSSL_SUCCESS);
  58531. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58532. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58533. WOLFSSL_SUCCESS);
  58534. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58535. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58536. WOLFSSL_SUCCESS);
  58537. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58538. wolfSSL_free(ssl_c);
  58539. wolfSSL_CTX_free(ctx_c);
  58540. wolfSSL_free(ssl_s);
  58541. wolfSSL_CTX_free(ctx_s);
  58542. ssl_c = ssl_s = NULL;
  58543. ctx_c = ctx_s = NULL;
  58544. /* Have client load x509 cert and server load RPK cert,
  58545. * however, have client indicate its cert type RPK.
  58546. * Server is expected to detect the cert type mismatch then to send alert
  58547. * with "unsupported_certificate".
  58548. */
  58549. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58550. ExpectIntEQ(
  58551. test_rpk_memio_setup(
  58552. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58553. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58554. cliCertFile, WOLFSSL_FILETYPE_PEM,
  58555. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58556. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58557. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58558. , 0);
  58559. /* have client tell to use RPK cert intentionally */
  58560. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58561. certType_c[1] = -1;
  58562. typeCnt_c = 1;
  58563. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58564. WOLFSSL_SUCCESS);
  58565. /* have client tell to accept both RPK and x509 cert */
  58566. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58567. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  58568. typeCnt_s = 2;
  58569. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58570. WOLFSSL_SUCCESS);
  58571. /* have server accept to both RPK and x509 cert */
  58572. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  58573. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  58574. typeCnt_c = 2;
  58575. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58576. WOLFSSL_SUCCESS);
  58577. /* have server tell to use x509 cert intentionally. This will bring
  58578. * certificate type mismatch in client side.
  58579. */
  58580. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  58581. certType_s[1] = -1;
  58582. typeCnt_s = 1;
  58583. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58584. WOLFSSL_SUCCESS);
  58585. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58586. /* expect server detect cert type mismatch then send Alert */
  58587. ExpectIntNE(ret, 0);
  58588. err = wolfSSL_get_error(ssl_c, ret);
  58589. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  58590. /* client did not load RPK cert actually, so negotiation did not happen */
  58591. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58592. WOLFSSL_SUCCESS);
  58593. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58594. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58595. WOLFSSL_SUCCESS);
  58596. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58597. /* client did not load RPK cert actually, so negotiation did not happen */
  58598. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58599. WOLFSSL_SUCCESS);
  58600. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  58601. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58602. WOLFSSL_SUCCESS);
  58603. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  58604. wolfSSL_free(ssl_c);
  58605. wolfSSL_CTX_free(ctx_c);
  58606. wolfSSL_free(ssl_s);
  58607. wolfSSL_CTX_free(ctx_s);
  58608. ssl_c = ssl_s = NULL;
  58609. ctx_c = ctx_s = NULL;
  58610. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  58611. /* Both client and server load RPK cert and set certificate verify
  58612. * callbacks then start handshaking.
  58613. * Confirm both side can refer the peer's cert.
  58614. */
  58615. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58616. ExpectIntEQ(
  58617. test_rpk_memio_setup(
  58618. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58619. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  58620. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58621. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  58622. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  58623. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  58624. , 0);
  58625. /* set client certificate type in client end */
  58626. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  58627. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  58628. typeCnt_c = 2;
  58629. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  58630. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  58631. typeCnt_s = 2;
  58632. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  58633. WOLFSSL_SUCCESS);
  58634. /* set server certificate type in client end */
  58635. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  58636. WOLFSSL_SUCCESS);
  58637. /* set client certificate type in server end */
  58638. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  58639. WOLFSSL_SUCCESS);
  58640. /* set server certificate type in server end */
  58641. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  58642. WOLFSSL_SUCCESS);
  58643. /* set certificate verify callback to both client and server */
  58644. int isServer = 0;
  58645. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  58646. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  58647. isServer = 1;
  58648. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  58649. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  58650. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  58651. if (ret != 0)
  58652. return TEST_FAIL;
  58653. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  58654. WOLFSSL_SUCCESS);
  58655. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58656. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  58657. WOLFSSL_SUCCESS);
  58658. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58659. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  58660. WOLFSSL_SUCCESS);
  58661. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58662. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  58663. WOLFSSL_SUCCESS);
  58664. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  58665. wolfSSL_free(ssl_c);
  58666. wolfSSL_CTX_free(ctx_c);
  58667. wolfSSL_free(ssl_s);
  58668. wolfSSL_CTX_free(ctx_s);
  58669. ssl_c = ssl_s = NULL;
  58670. ctx_c = ctx_s = NULL;
  58671. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  58672. #endif /* HAVE_RPK */
  58673. return EXPECT_RESULT();
  58674. }
  58675. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58676. static int test_dtls13_bad_epoch_ch(void)
  58677. {
  58678. EXPECT_DECLS;
  58679. WOLFSSL_CTX *ctx_c = NULL;
  58680. WOLFSSL_CTX *ctx_s = NULL;
  58681. WOLFSSL *ssl_c = NULL;
  58682. WOLFSSL *ssl_s = NULL;
  58683. struct test_memio_ctx test_ctx;
  58684. const int EPOCH_OFF = 3;
  58685. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58686. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58687. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58688. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  58689. * with just one message */
  58690. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  58691. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58692. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58693. WOLFSSL_ERROR_WANT_READ);
  58694. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  58695. /* first CH should use epoch 0x0 */
  58696. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  58697. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  58698. /* change epoch to 2 */
  58699. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  58700. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58701. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58702. WOLFSSL_ERROR_WANT_READ);
  58703. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  58704. /* resend the CH */
  58705. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58706. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58707. wolfSSL_free(ssl_c);
  58708. wolfSSL_CTX_free(ctx_c);
  58709. wolfSSL_free(ssl_s);
  58710. wolfSSL_CTX_free(ctx_s);
  58711. return EXPECT_RESULT();
  58712. }
  58713. #else
  58714. static int test_dtls13_bad_epoch_ch(void)
  58715. {
  58716. return TEST_SKIPPED;
  58717. }
  58718. #endif
  58719. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  58720. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  58721. {
  58722. EXPECT_DECLS;
  58723. WOLFSSL_SESSION *sess = NULL;
  58724. /* Setup the session to avoid errors */
  58725. ssl->session->timeout = -1;
  58726. ssl->session->side = WOLFSSL_CLIENT_END;
  58727. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  58728. defined(HAVE_SESSION_TICKET))
  58729. ssl->session->version = ssl->version;
  58730. #endif
  58731. /* Force a short session ID to be sent */
  58732. ssl->session->sessionIDSz = 4;
  58733. #ifndef NO_SESSION_CACHE_REF
  58734. /* Allow the client cache to be used */
  58735. ssl->session->idLen = 4;
  58736. #endif
  58737. ssl->session->isSetup = 1;
  58738. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  58739. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  58740. return EXPECT_RESULT();
  58741. }
  58742. static int test_short_session_id(void)
  58743. {
  58744. EXPECT_DECLS;
  58745. test_ssl_cbf client_cbf;
  58746. test_ssl_cbf server_cbf;
  58747. size_t i;
  58748. struct {
  58749. method_provider client_meth;
  58750. method_provider server_meth;
  58751. const char* tls_version;
  58752. } params[] = {
  58753. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  58754. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  58755. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  58756. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  58757. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  58758. #ifdef WOLFSSL_DTLS13
  58759. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  58760. #endif
  58761. #endif
  58762. #ifndef WOLFSSL_NO_TLS12
  58763. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  58764. #ifdef WOLFSSL_DTLS
  58765. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  58766. #endif
  58767. #endif
  58768. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  58769. !defined(NO_DES3))
  58770. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  58771. #ifdef WOLFSSL_DTLS
  58772. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  58773. #endif
  58774. #endif
  58775. };
  58776. fprintf(stderr, "\n");
  58777. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  58778. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58779. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58780. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  58781. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  58782. client_cbf.method = params[i].client_meth;
  58783. server_cbf.method = params[i].server_meth;
  58784. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  58785. &server_cbf, NULL), TEST_SUCCESS);
  58786. }
  58787. return EXPECT_RESULT();
  58788. }
  58789. #else
  58790. static int test_short_session_id(void)
  58791. {
  58792. return TEST_SKIPPED;
  58793. }
  58794. #endif
  58795. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  58796. && defined(WOLFSSL_DTLS13)
  58797. static byte* test_find_string(const char *string,
  58798. byte *buf, int buf_size)
  58799. {
  58800. int string_size, i;
  58801. string_size = (int)XSTRLEN(string);
  58802. for (i = 0; i < buf_size - string_size - 1; i++) {
  58803. if (XSTRCMP((char*)&buf[i], string) == 0)
  58804. return &buf[i];
  58805. }
  58806. return NULL;
  58807. }
  58808. static int test_wolfSSL_dtls13_null_cipher(void)
  58809. {
  58810. EXPECT_DECLS;
  58811. WOLFSSL_CTX *ctx_c = NULL;
  58812. WOLFSSL_CTX *ctx_s = NULL;
  58813. WOLFSSL *ssl_c = NULL;
  58814. WOLFSSL *ssl_s = NULL;
  58815. struct test_memio_ctx test_ctx;
  58816. const char *test_str = "test";
  58817. int test_str_size;
  58818. byte buf[255], *ptr = NULL;
  58819. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58820. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  58821. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58822. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58823. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58824. test_str_size = XSTRLEN("test") + 1;
  58825. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58826. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  58827. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  58828. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58829. /* check that the packet was sent cleartext */
  58830. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  58831. test_ctx.s_len));
  58832. if (ptr != NULL) {
  58833. /* modify the message */
  58834. *ptr = 'H';
  58835. /* bad messages should be ignored in DTLS */
  58836. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  58837. ExpectIntEQ(ssl_s->error, WANT_READ);
  58838. }
  58839. wolfSSL_free(ssl_c);
  58840. wolfSSL_free(ssl_s);
  58841. wolfSSL_CTX_free(ctx_c);
  58842. wolfSSL_CTX_free(ctx_s);
  58843. return TEST_SUCCESS;
  58844. }
  58845. #else
  58846. static int test_wolfSSL_dtls13_null_cipher(void)
  58847. {
  58848. return TEST_SKIPPED;
  58849. }
  58850. #endif
  58851. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58852. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58853. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  58854. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  58855. {
  58856. SOCKADDR_S peer;
  58857. XSOCKLENT len;
  58858. int ret;
  58859. XMEMSET((byte*)&peer, 0, sizeof(peer));
  58860. len = sizeof(peer);
  58861. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  58862. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  58863. return -1;
  58864. switch (peer.ss_family) {
  58865. #ifdef WOLFSSL_IPV6
  58866. case WOLFSSL_IP6: {
  58867. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  58868. break;
  58869. }
  58870. #endif /* WOLFSSL_IPV6 */
  58871. case WOLFSSL_IP4:
  58872. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  58873. break;
  58874. default:
  58875. return -1;
  58876. }
  58877. return 0;
  58878. }
  58879. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  58880. {
  58881. char buf[1] = {'t'};
  58882. SOCKADDR_IN_T addr;
  58883. int sock_fd;
  58884. word16 port;
  58885. int err;
  58886. (void)ssl;
  58887. (void)ctx;
  58888. if (ssl == NULL)
  58889. return -1;
  58890. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  58891. if (err != 0)
  58892. return -1;
  58893. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  58894. if (sock_fd == -1)
  58895. return -1;
  58896. build_addr(&addr, wolfSSLIP, port, 1, 0);
  58897. /* send a packet to the server. Being another socket, the kernel will ensure
  58898. * the source port will be different. */
  58899. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  58900. sizeof(addr));
  58901. close(sock_fd);
  58902. if (err == -1)
  58903. return -1;
  58904. return 0;
  58905. }
  58906. /* setup a SSL session but just after the handshake send a packet to the server
  58907. * with a source address different than the one of the connected client. The I/O
  58908. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  58909. * is done in test_dtls_msg_from_other_peer_cb */
  58910. static int test_dtls_msg_from_other_peer(void)
  58911. {
  58912. EXPECT_DECLS;
  58913. callback_functions client_cbs;
  58914. callback_functions server_cbs;
  58915. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  58916. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  58917. client_cbs.method = wolfDTLSv1_2_client_method;
  58918. server_cbs.method = wolfDTLSv1_2_server_method;
  58919. client_cbs.doUdp = 1;
  58920. server_cbs.doUdp = 1;
  58921. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  58922. test_dtls_msg_from_other_peer_cb);
  58923. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  58924. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  58925. return EXPECT_RESULT();
  58926. }
  58927. #else
  58928. static int test_dtls_msg_from_other_peer(void)
  58929. {
  58930. return TEST_SKIPPED;
  58931. }
  58932. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58933. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58934. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  58935. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  58936. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58937. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  58938. static int test_dtls_ipv6_check(void)
  58939. {
  58940. EXPECT_DECLS;
  58941. WOLFSSL_CTX *ctx_c = NULL;
  58942. WOLFSSL_CTX *ctx_s = NULL;
  58943. WOLFSSL *ssl_c = NULL;
  58944. WOLFSSL *ssl_s = NULL;
  58945. SOCKADDR_IN fake_addr6;
  58946. int sockfd = -1;
  58947. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  58948. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58949. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  58950. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  58951. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58952. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  58953. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58954. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58955. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  58956. /* mimic a sockaddr_in6 struct, this way we can't test without
  58957. * WOLFSSL_IPV6 */
  58958. fake_addr6.sin_family = WOLFSSL_IP6;
  58959. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  58960. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  58961. /* can't return error here, as the peer is opaque for wolfssl library at
  58962. * this point */
  58963. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  58964. WOLFSSL_SUCCESS);
  58965. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  58966. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  58967. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58968. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  58969. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  58970. WOLFSSL_SUCCESS);
  58971. /* reuse the socket */
  58972. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  58973. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  58974. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58975. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  58976. if (sockfd != -1)
  58977. close(sockfd);
  58978. wolfSSL_free(ssl_c);
  58979. wolfSSL_CTX_free(ctx_c);
  58980. wolfSSL_free(ssl_s);
  58981. wolfSSL_CTX_free(ctx_s);
  58982. return EXPECT_RESULT();
  58983. }
  58984. #else
  58985. static int test_dtls_ipv6_check(void)
  58986. {
  58987. return TEST_SKIPPED;
  58988. }
  58989. #endif
  58990. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58991. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58992. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  58993. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  58994. {
  58995. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58996. }
  58997. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  58998. {
  58999. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  59000. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  59001. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  59002. }
  59003. else {
  59004. char testMsg[] = "Message after SCR";
  59005. char msgBuf[sizeof(testMsg)];
  59006. int ret;
  59007. if (!wolfSSL_is_server(ssl)) {
  59008. AssertIntEQ(WOLFSSL_SUCCESS,
  59009. wolfSSL_set_session(ssl,
  59010. test_wolfSSL_SCR_after_resumption_session));
  59011. }
  59012. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59013. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59014. sizeof(testMsg));
  59015. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  59016. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  59017. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  59018. AssertIntEQ(ret, sizeof(msgBuf));
  59019. }
  59020. }
  59021. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  59022. {
  59023. AssertIntEQ(WOLFSSL_SUCCESS,
  59024. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  59025. }
  59026. static int test_wolfSSL_SCR_after_resumption(void)
  59027. {
  59028. EXPECT_DECLS;
  59029. callback_functions func_cb_client;
  59030. callback_functions func_cb_server;
  59031. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  59032. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  59033. func_cb_client.method = wolfTLSv1_2_client_method;
  59034. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  59035. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  59036. func_cb_server.method = wolfTLSv1_2_server_method;
  59037. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  59038. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59039. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59040. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59041. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  59042. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  59043. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59044. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59045. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59046. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  59047. return EXPECT_RESULT();
  59048. }
  59049. #else
  59050. static int test_wolfSSL_SCR_after_resumption(void)
  59051. {
  59052. return TEST_SKIPPED;
  59053. }
  59054. #endif
  59055. static int test_wolfSSL_configure_args(void)
  59056. {
  59057. EXPECT_DECLS;
  59058. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  59059. ExpectNotNull(wolfSSL_configure_args());
  59060. #endif
  59061. return EXPECT_RESULT();
  59062. }
  59063. static int test_dtls_no_extensions(void)
  59064. {
  59065. EXPECT_DECLS;
  59066. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  59067. !defined(WOLFSSL_NO_TLS12)
  59068. WOLFSSL *ssl_s = NULL;
  59069. WOLFSSL_CTX *ctx_s = NULL;
  59070. struct test_memio_ctx test_ctx;
  59071. const byte chNoExtensions[] = {
  59072. /* Handshake type */
  59073. 0x16,
  59074. /* Version */
  59075. 0xfe, 0xff,
  59076. /* Epoch */
  59077. 0x00, 0x00,
  59078. /* Seq number */
  59079. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59080. /* Length */
  59081. 0x00, 0x40,
  59082. /* CH type */
  59083. 0x01,
  59084. /* Length */
  59085. 0x00, 0x00, 0x34,
  59086. /* Msg Seq */
  59087. 0x00, 0x00,
  59088. /* Frag offset */
  59089. 0x00, 0x00, 0x00,
  59090. /* Frag length */
  59091. 0x00, 0x00, 0x34,
  59092. /* Version */
  59093. 0xfe, 0xff,
  59094. /* Random */
  59095. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  59096. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  59097. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  59098. /* Session Length */
  59099. 0x00,
  59100. /* Cookie Length */
  59101. 0x00,
  59102. /* CS Length */
  59103. 0x00, 0x0c,
  59104. /* CS */
  59105. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  59106. /* Comp Meths Length */
  59107. 0x01,
  59108. /* Comp Meths */
  59109. 0x00
  59110. /* And finally... no extensions */
  59111. };
  59112. int i;
  59113. #ifdef OPENSSL_EXTRA
  59114. int repeats = 2;
  59115. #else
  59116. int repeats = 1;
  59117. #endif
  59118. for (i = 0; i < repeats; i++) {
  59119. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59120. ssl_s = NULL;
  59121. ctx_s = NULL;
  59122. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  59123. NULL, wolfDTLS_server_method), 0);
  59124. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  59125. test_ctx.s_len = sizeof(chNoExtensions);
  59126. #ifdef OPENSSL_EXTRA
  59127. if (i > 0) {
  59128. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  59129. WOLFSSL_SUCCESS);
  59130. }
  59131. #endif
  59132. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  59133. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59134. /* Expecting a handshake msg. Either HVR or SH. */
  59135. ExpectIntGT(test_ctx.c_len, 0);
  59136. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  59137. wolfSSL_free(ssl_s);
  59138. wolfSSL_CTX_free(ctx_s);
  59139. }
  59140. #endif
  59141. return EXPECT_RESULT();
  59142. }
  59143. static int test_TLSX_CA_NAMES_bad_extension(void)
  59144. {
  59145. EXPECT_DECLS;
  59146. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59147. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  59148. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  59149. defined(HAVE_NULL_CIPHER)
  59150. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  59151. * parse the CA Names extension. Otherwise it will return other non-related
  59152. * errors. If CA Names will be parsed in more configurations, that should
  59153. * be reflected in the macro guard above. */
  59154. WOLFSSL *ssl_c = NULL;
  59155. WOLFSSL_CTX *ctx_c = NULL;
  59156. struct test_memio_ctx test_ctx;
  59157. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  59158. const byte shBadCaNamesExt[] = {
  59159. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  59160. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  59161. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  59162. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  59163. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  59164. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  59165. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  59166. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  59167. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  59168. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  59169. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  59170. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  59171. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  59172. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  59173. };
  59174. const byte shBadCaNamesExt2[] = {
  59175. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  59176. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  59177. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  59178. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  59179. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  59180. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  59181. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  59182. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  59183. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  59184. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  59185. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  59186. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  59187. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  59188. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  59189. };
  59190. int i = 0;
  59191. for (i = 0; i < 2; i++) {
  59192. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59193. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59194. wolfTLSv1_3_client_method, NULL), 0);
  59195. switch (i) {
  59196. case 0:
  59197. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  59198. sizeof(shBadCaNamesExt));
  59199. test_ctx.c_len = sizeof(shBadCaNamesExt);
  59200. break;
  59201. case 1:
  59202. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  59203. sizeof(shBadCaNamesExt2));
  59204. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  59205. break;
  59206. }
  59207. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59208. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  59209. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  59210. #else
  59211. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  59212. #endif
  59213. wolfSSL_free(ssl_c);
  59214. ssl_c = NULL;
  59215. wolfSSL_CTX_free(ctx_c);
  59216. ctx_c = NULL;
  59217. }
  59218. #endif
  59219. return EXPECT_RESULT();
  59220. }
  59221. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59222. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59223. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  59224. {
  59225. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59226. WOLFSSL_SUCCESS);
  59227. }
  59228. static int test_dtls_1_0_hvr_downgrade(void)
  59229. {
  59230. EXPECT_DECLS;
  59231. callback_functions func_cb_client;
  59232. callback_functions func_cb_server;
  59233. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59234. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59235. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59236. func_cb_client.method = wolfDTLS_client_method;
  59237. func_cb_server.method = wolfDTLSv1_2_server_method;
  59238. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  59239. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59240. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59241. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59242. return EXPECT_RESULT();
  59243. }
  59244. #else
  59245. static int test_dtls_1_0_hvr_downgrade(void)
  59246. {
  59247. EXPECT_DECLS;
  59248. return EXPECT_RESULT();
  59249. }
  59250. #endif
  59251. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  59252. defined(HAVE_SESSION_TICKET)
  59253. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  59254. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  59255. {
  59256. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  59257. AssertNotNull(test_session_ticket_no_id_session);
  59258. }
  59259. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  59260. {
  59261. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  59262. }
  59263. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  59264. {
  59265. test_session_ticket_no_id_session->sessionIDSz = 0;
  59266. AssertIntEQ(WOLFSSL_SUCCESS,
  59267. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  59268. }
  59269. static int test_session_ticket_no_id(void)
  59270. {
  59271. /* We are testing an expired (invalid crypto context in out case since the
  59272. * ctx changes) session ticket being sent with the session ID being 0
  59273. * length. */
  59274. EXPECT_DECLS;
  59275. callback_functions func_cb_client;
  59276. callback_functions func_cb_server;
  59277. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  59278. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  59279. func_cb_client.method = wolfTLSv1_2_client_method;
  59280. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59281. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  59282. func_cb_server.method = wolfTLSv1_2_server_method;
  59283. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59284. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59285. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59286. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59287. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  59288. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  59289. func_cb_client.method = wolfTLSv1_2_client_method;
  59290. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59291. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  59292. func_cb_server.method = wolfTLSv1_2_server_method;
  59293. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  59294. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59295. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59296. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59297. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  59298. return EXPECT_RESULT();
  59299. }
  59300. #else
  59301. static int test_session_ticket_no_id(void)
  59302. {
  59303. EXPECT_DECLS;
  59304. return EXPECT_RESULT();
  59305. }
  59306. #endif
  59307. static int test_session_ticket_hs_update(void)
  59308. {
  59309. EXPECT_DECLS;
  59310. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  59311. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59312. struct test_memio_ctx test_ctx;
  59313. struct test_memio_ctx test_ctx2;
  59314. struct test_memio_ctx test_ctx3;
  59315. WOLFSSL_CTX *ctx_c = NULL;
  59316. WOLFSSL_CTX *ctx_s = NULL;
  59317. WOLFSSL *ssl_c = NULL;
  59318. WOLFSSL *ssl_c2 = NULL;
  59319. WOLFSSL *ssl_c3 = NULL;
  59320. WOLFSSL *ssl_s = NULL;
  59321. WOLFSSL *ssl_s2 = NULL;
  59322. WOLFSSL *ssl_s3 = NULL;
  59323. WOLFSSL_SESSION *sess = NULL;
  59324. byte read_data[1];
  59325. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59326. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  59327. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  59328. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59329. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59330. /* Generate tickets */
  59331. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59332. wolfSSL_SetLoggingPrefix("client");
  59333. /* Read the ticket msg */
  59334. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  59335. WOLFSSL_FATAL_ERROR);
  59336. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59337. WOLFSSL_ERROR_WANT_READ);
  59338. wolfSSL_SetLoggingPrefix(NULL);
  59339. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  59340. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59341. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  59342. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59343. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59344. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  59345. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  59346. wolfSSL_SetLoggingPrefix("client");
  59347. /* Exchange initial flights for the second connection */
  59348. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  59349. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  59350. WOLFSSL_ERROR_WANT_READ);
  59351. wolfSSL_SetLoggingPrefix(NULL);
  59352. wolfSSL_SetLoggingPrefix("server");
  59353. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  59354. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  59355. WOLFSSL_ERROR_WANT_READ);
  59356. wolfSSL_SetLoggingPrefix(NULL);
  59357. /* Complete third connection so that new tickets are exchanged */
  59358. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  59359. /* Read the ticket msg */
  59360. wolfSSL_SetLoggingPrefix("client");
  59361. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  59362. WOLFSSL_FATAL_ERROR);
  59363. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  59364. WOLFSSL_ERROR_WANT_READ);
  59365. wolfSSL_SetLoggingPrefix(NULL);
  59366. /* Complete second connection */
  59367. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  59368. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  59369. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  59370. wolfSSL_free(ssl_c);
  59371. wolfSSL_free(ssl_c2);
  59372. wolfSSL_free(ssl_c3);
  59373. wolfSSL_free(ssl_s);
  59374. wolfSSL_free(ssl_s2);
  59375. wolfSSL_free(ssl_s3);
  59376. wolfSSL_CTX_free(ctx_c);
  59377. wolfSSL_CTX_free(ctx_s);
  59378. wolfSSL_SESSION_free(sess);
  59379. #endif
  59380. return EXPECT_RESULT();
  59381. }
  59382. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59383. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  59384. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  59385. {
  59386. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59387. WOLFSSL_SUCCESS);
  59388. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59389. }
  59390. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  59391. {
  59392. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59393. }
  59394. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  59395. {
  59396. char testMsg[] = "Message after SCR";
  59397. char msgBuf[sizeof(testMsg)];
  59398. if (wolfSSL_is_server(ssl)) {
  59399. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  59400. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  59401. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59402. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59403. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59404. sizeof(testMsg));
  59405. }
  59406. else {
  59407. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59408. sizeof(testMsg));
  59409. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59410. }
  59411. }
  59412. static int test_dtls_downgrade_scr_server(void)
  59413. {
  59414. EXPECT_DECLS;
  59415. callback_functions func_cb_client;
  59416. callback_functions func_cb_server;
  59417. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59418. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59419. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59420. func_cb_client.method = wolfDTLSv1_2_client_method;
  59421. func_cb_server.method = wolfDTLS_server_method;
  59422. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  59423. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  59424. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  59425. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  59426. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59427. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59428. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59429. return EXPECT_RESULT();
  59430. }
  59431. #else
  59432. static int test_dtls_downgrade_scr_server(void)
  59433. {
  59434. EXPECT_DECLS;
  59435. return EXPECT_RESULT();
  59436. }
  59437. #endif
  59438. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59439. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  59440. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  59441. {
  59442. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  59443. WOLFSSL_SUCCESS);
  59444. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  59445. }
  59446. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  59447. {
  59448. char testMsg[] = "Message after SCR";
  59449. char msgBuf[sizeof(testMsg)];
  59450. if (wolfSSL_is_server(ssl)) {
  59451. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  59452. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  59453. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59454. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  59455. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59456. sizeof(testMsg));
  59457. }
  59458. else {
  59459. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  59460. sizeof(testMsg));
  59461. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  59462. }
  59463. }
  59464. static int test_dtls_downgrade_scr(void)
  59465. {
  59466. EXPECT_DECLS;
  59467. callback_functions func_cb_client;
  59468. callback_functions func_cb_server;
  59469. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  59470. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  59471. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  59472. func_cb_client.method = wolfDTLS_client_method;
  59473. func_cb_server.method = wolfDTLSv1_2_server_method;
  59474. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  59475. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  59476. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  59477. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  59478. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  59479. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  59480. return EXPECT_RESULT();
  59481. }
  59482. #else
  59483. static int test_dtls_downgrade_scr(void)
  59484. {
  59485. EXPECT_DECLS;
  59486. return EXPECT_RESULT();
  59487. }
  59488. #endif
  59489. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59490. && !defined(WOLFSSL_NO_TLS12)
  59491. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  59492. char *data, int sz, void *ctx)
  59493. {
  59494. static int call_counter = 0;
  59495. call_counter++;
  59496. (void)ssl;
  59497. (void)data;
  59498. (void)sz;
  59499. (void)ctx;
  59500. switch (call_counter) {
  59501. case 1:
  59502. case 2:
  59503. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59504. case 3:
  59505. return WOLFSSL_CBIO_ERR_WANT_READ;
  59506. default:
  59507. AssertIntLE(call_counter, 3);
  59508. return -1;
  59509. }
  59510. }
  59511. #endif
  59512. /* Make sure we don't send acks before getting a server hello */
  59513. static int test_dtls_client_hello_timeout_downgrade(void)
  59514. {
  59515. EXPECT_DECLS;
  59516. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59517. && !defined(WOLFSSL_NO_TLS12)
  59518. WOLFSSL_CTX *ctx_c = NULL;
  59519. WOLFSSL_CTX *ctx_s = NULL;
  59520. WOLFSSL *ssl_c = NULL;
  59521. WOLFSSL *ssl_s = NULL;
  59522. struct test_memio_ctx test_ctx;
  59523. DtlsRecordLayerHeader* dtlsRH;
  59524. size_t len;
  59525. byte sequence_number[8];
  59526. int i;
  59527. for (i = 0; i < 2; i++) {
  59528. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59529. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59530. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  59531. if (i == 0) {
  59532. /* First time simulate timeout in IO layer */
  59533. /* CH1 */
  59534. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59535. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59536. /* HVR */
  59537. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59538. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59539. /* CH2 */
  59540. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59541. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59542. /* SH flight */
  59543. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59544. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59545. /* Drop the SH */
  59546. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59547. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59548. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59549. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59550. (sizeof(DtlsRecordLayerHeader) + len));
  59551. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59552. /* Read the remainder of the flight */
  59553. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59554. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59555. wolfSSL_SSLSetIORecv(ssl_c,
  59556. test_dtls_client_hello_timeout_downgrade_read_cb);
  59557. /* CH3 */
  59558. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59559. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59560. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  59561. }
  59562. else {
  59563. /* Second time call wolfSSL_dtls_got_timeout */
  59564. /* CH1 */
  59565. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59566. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59567. /* HVR */
  59568. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59569. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59570. /* CH2 */
  59571. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59572. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59573. /* SH flight */
  59574. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59575. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59576. /* Drop the SH */
  59577. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  59578. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59579. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59580. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59581. (sizeof(DtlsRecordLayerHeader) + len));
  59582. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59583. /* Read the remainder of the flight */
  59584. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59585. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59586. /* Quick timeout should be set as we received at least one msg */
  59587. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  59588. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59589. /* Quick timeout should be cleared after a quick timeout */
  59590. /* CH3 */
  59591. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  59592. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59593. }
  59594. /* Parse out to make sure we got exactly one ClientHello message */
  59595. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59596. /* Second ClientHello after HVR */
  59597. sequence_number[7] = 2;
  59598. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  59599. ExpectIntEQ(dtlsRH->type, handshake);
  59600. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59601. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59602. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59603. sizeof(sequence_number)), 0);
  59604. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59605. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59606. /* Connection should be able to continue */
  59607. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59608. wolfSSL_free(ssl_c);
  59609. wolfSSL_free(ssl_s);
  59610. wolfSSL_CTX_free(ctx_c);
  59611. wolfSSL_CTX_free(ctx_s);
  59612. ssl_c = NULL;
  59613. ssl_s = NULL;
  59614. ctx_c = NULL;
  59615. ctx_s = NULL;
  59616. if (!EXPECT_SUCCESS())
  59617. break;
  59618. }
  59619. #endif
  59620. return EXPECT_RESULT();
  59621. }
  59622. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59623. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  59624. int sz, void *ctx)
  59625. {
  59626. static int call_counter = 0;
  59627. call_counter++;
  59628. (void)ssl;
  59629. (void)data;
  59630. (void)sz;
  59631. (void)ctx;
  59632. switch (call_counter) {
  59633. case 1:
  59634. return WOLFSSL_CBIO_ERR_TIMEOUT;
  59635. case 2:
  59636. return WOLFSSL_CBIO_ERR_WANT_READ;
  59637. default:
  59638. AssertIntLE(call_counter, 2);
  59639. return -1;
  59640. }
  59641. }
  59642. #endif
  59643. /* Make sure we don't send acks before getting a server hello */
  59644. static int test_dtls_client_hello_timeout(void)
  59645. {
  59646. EXPECT_DECLS;
  59647. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59648. WOLFSSL *ssl_c = NULL;
  59649. WOLFSSL_CTX *ctx_c = NULL;
  59650. struct test_memio_ctx test_ctx;
  59651. DtlsRecordLayerHeader* dtlsRH;
  59652. size_t idx;
  59653. size_t len;
  59654. byte sequence_number[8];
  59655. int i;
  59656. for (i = 0; i < 2; i++) {
  59657. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59658. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59659. wolfDTLSv1_3_client_method, NULL), 0);
  59660. if (i == 0) {
  59661. /* First time simulate timeout in IO layer */
  59662. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  59663. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59664. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59665. }
  59666. else {
  59667. /* Second time call wolfSSL_dtls_got_timeout */
  59668. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59669. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59670. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59671. }
  59672. /* Parse out to make sure we got exactly two ClientHello messages */
  59673. idx = 0;
  59674. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  59675. /* First ClientHello */
  59676. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  59677. ExpectIntEQ(dtlsRH->type, handshake);
  59678. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59679. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59680. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59681. sizeof(sequence_number)), 0);
  59682. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59683. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59684. idx += sizeof(DtlsRecordLayerHeader) + len;
  59685. /* Second ClientHello */
  59686. sequence_number[7] = 1;
  59687. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  59688. ExpectIntEQ(dtlsRH->type, handshake);
  59689. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  59690. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  59691. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  59692. sizeof(sequence_number)), 0);
  59693. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59694. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  59695. wolfSSL_free(ssl_c);
  59696. wolfSSL_CTX_free(ctx_c);
  59697. ssl_c = NULL;
  59698. ctx_c = NULL;
  59699. if (!EXPECT_SUCCESS())
  59700. break;
  59701. }
  59702. #endif
  59703. return EXPECT_RESULT();
  59704. }
  59705. /* DTLS test when dropping the changed cipher spec message */
  59706. static int test_dtls_dropped_ccs(void)
  59707. {
  59708. EXPECT_DECLS;
  59709. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  59710. && !defined(WOLFSSL_NO_TLS12)
  59711. WOLFSSL_CTX *ctx_c = NULL;
  59712. WOLFSSL_CTX *ctx_s = NULL;
  59713. WOLFSSL *ssl_c = NULL;
  59714. WOLFSSL *ssl_s = NULL;
  59715. struct test_memio_ctx test_ctx;
  59716. DtlsRecordLayerHeader* dtlsRH;
  59717. size_t len;
  59718. byte data[1];
  59719. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59720. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59721. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59722. /* CH1 */
  59723. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59724. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59725. /* HVR */
  59726. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59727. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59728. /* CH2 */
  59729. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59730. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59731. /* Server first flight */
  59732. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  59733. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59734. /* Client flight */
  59735. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59736. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59737. /* Server ccs + finished */
  59738. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  59739. /* Drop the ccs */
  59740. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  59741. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  59742. ExpectIntEQ(len, 1);
  59743. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  59744. if (EXPECT_SUCCESS()) {
  59745. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  59746. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  59747. (sizeof(DtlsRecordLayerHeader) + len));
  59748. }
  59749. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  59750. /* Client rtx flight */
  59751. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  59752. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59753. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59754. /* Server ccs + finished rtx */
  59755. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  59756. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  59757. /* Client processes finished */
  59758. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  59759. wolfSSL_free(ssl_c);
  59760. wolfSSL_free(ssl_s);
  59761. wolfSSL_CTX_free(ctx_c);
  59762. wolfSSL_CTX_free(ctx_s);
  59763. #endif
  59764. return EXPECT_RESULT();
  59765. }
  59766. /**
  59767. * Make sure we don't send RSA Signature Hash Algorithms in the
  59768. * CertificateRequest when we don't have any such ciphers set.
  59769. * @return EXPECT_RESULT()
  59770. */
  59771. static int test_certreq_sighash_algos(void)
  59772. {
  59773. EXPECT_DECLS;
  59774. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  59775. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  59776. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59777. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  59778. WOLFSSL_CTX *ctx_c = NULL;
  59779. WOLFSSL_CTX *ctx_s = NULL;
  59780. WOLFSSL *ssl_c = NULL;
  59781. WOLFSSL *ssl_s = NULL;
  59782. struct test_memio_ctx test_ctx;
  59783. int idx = 0;
  59784. int maxIdx = 0;
  59785. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59786. test_ctx.c_ciphers = test_ctx.s_ciphers =
  59787. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  59788. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59789. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59790. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  59791. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  59792. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  59793. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  59794. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59795. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  59796. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59797. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  59798. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59799. WOLFSSL_ERROR_WANT_READ);
  59800. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  59801. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59802. WOLFSSL_ERROR_WANT_READ);
  59803. /* Find the CertificateRequest message */
  59804. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  59805. word16 len;
  59806. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  59807. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  59808. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  59809. ato16(test_ctx.c_buff + idx, &len);
  59810. idx += OPAQUE16_LEN;
  59811. if (test_ctx.c_buff[idx] == certificate_request) {
  59812. idx++;
  59813. /* length */
  59814. idx += OPAQUE24_LEN;
  59815. /* cert types */
  59816. idx += 1 + test_ctx.c_buff[idx];
  59817. /* Sig algos */
  59818. ato16(test_ctx.c_buff + idx, &len);
  59819. idx += OPAQUE16_LEN;
  59820. maxIdx = idx + (int)len;
  59821. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  59822. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  59823. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  59824. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  59825. else
  59826. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  59827. }
  59828. break;
  59829. }
  59830. else {
  59831. idx += (int)len;
  59832. }
  59833. }
  59834. ExpectIntLT(idx, test_ctx.c_len);
  59835. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59836. wolfSSL_free(ssl_c);
  59837. wolfSSL_free(ssl_s);
  59838. wolfSSL_CTX_free(ctx_c);
  59839. wolfSSL_CTX_free(ctx_s);
  59840. #endif
  59841. return EXPECT_RESULT();
  59842. }
  59843. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59844. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  59845. {
  59846. EXPECT_DECLS;
  59847. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  59848. myVerifyAction = VERIFY_USE_PREVERFIY;
  59849. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59850. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59851. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59852. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59853. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  59854. WOLFSSL_SUCCESS);
  59855. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59856. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  59857. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59858. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59859. "./certs/crl/ca-int.pem",
  59860. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59861. return EXPECT_RESULT();
  59862. }
  59863. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  59864. {
  59865. EXPECT_DECLS;
  59866. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  59867. myVerifyAction = VERIFY_USE_PREVERFIY;
  59868. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59869. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59870. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59871. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59872. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  59873. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  59874. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  59875. WOLFSSL_SUCCESS);
  59876. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59877. "./certs/crl/ca-int2.pem",
  59878. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59879. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59880. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  59881. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59882. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  59883. "./certs/crl/ca-int.pem",
  59884. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59885. return EXPECT_RESULT();
  59886. }
  59887. #endif
  59888. static int test_revoked_loaded_int_cert(void)
  59889. {
  59890. EXPECT_DECLS;
  59891. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59892. test_ssl_cbf client_cbf;
  59893. test_ssl_cbf server_cbf;
  59894. struct {
  59895. const char* certPemFile;
  59896. const char* keyPemFile;
  59897. ctx_cb client_ctx_ready;
  59898. } test_params[] = {
  59899. {"./certs/intermediate/ca-int2-cert.pem",
  59900. "./certs/intermediate/ca-int2-key.pem",
  59901. test_revoked_loaded_int_cert_ctx_ready1},
  59902. {"./certs/intermediate/server-chain.pem",
  59903. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  59904. {"./certs/intermediate/server-chain-short.pem",
  59905. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  59906. };
  59907. size_t i;
  59908. printf("\n");
  59909. for (i = 0; i < XELEM_CNT(test_params); i++) {
  59910. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  59911. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  59912. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  59913. server_cbf.certPemFile = test_params[i].certPemFile;
  59914. server_cbf.keyPemFile = test_params[i].keyPemFile;
  59915. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  59916. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  59917. &server_cbf, NULL), TEST_FAIL);
  59918. #ifndef WOLFSSL_HAPROXY
  59919. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  59920. #else
  59921. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  59922. #endif
  59923. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  59924. if (!EXPECT_SUCCESS())
  59925. break;
  59926. printf("\t%s passed\n", test_params[i].certPemFile);
  59927. }
  59928. #endif
  59929. return EXPECT_RESULT();
  59930. }
  59931. static int test_dtls13_frag_ch_pq(void)
  59932. {
  59933. EXPECT_DECLS;
  59934. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59935. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  59936. WOLFSSL_CTX *ctx_c = NULL;
  59937. WOLFSSL_CTX *ctx_s = NULL;
  59938. WOLFSSL *ssl_c = NULL;
  59939. WOLFSSL *ssl_s = NULL;
  59940. struct test_memio_ctx test_ctx;
  59941. const char *test_str = "test";
  59942. int test_str_size;
  59943. byte buf[255];
  59944. int group = WOLFSSL_KYBER_LEVEL5;
  59945. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59946. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59947. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59948. /* Add in a large post-quantum key share to make the CH long. */
  59949. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  59950. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  59951. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  59952. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59953. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  59954. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  59955. test_str_size = XSTRLEN("test") + 1;
  59956. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59957. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  59958. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  59959. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59960. wolfSSL_free(ssl_c);
  59961. wolfSSL_free(ssl_s);
  59962. wolfSSL_CTX_free(ctx_c);
  59963. wolfSSL_CTX_free(ctx_s);
  59964. #endif
  59965. return EXPECT_RESULT();
  59966. }
  59967. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  59968. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  59969. static int test_dtls_frag_ch_count_records(byte* b, int len)
  59970. {
  59971. DtlsRecordLayerHeader* dtlsRH;
  59972. int records = 0;
  59973. size_t recordLen;
  59974. while (len > 0) {
  59975. records++;
  59976. dtlsRH = (DtlsRecordLayerHeader*)b;
  59977. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  59978. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  59979. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  59980. }
  59981. return records;
  59982. }
  59983. #endif
  59984. static int test_dtls_frag_ch(void)
  59985. {
  59986. EXPECT_DECLS;
  59987. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  59988. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  59989. WOLFSSL_CTX *ctx_c = NULL;
  59990. WOLFSSL_CTX *ctx_s = NULL;
  59991. WOLFSSL *ssl_c = NULL;
  59992. WOLFSSL *ssl_s = NULL;
  59993. struct test_memio_ctx test_ctx;
  59994. static unsigned int DUMMY_MTU = 256;
  59995. unsigned char four_frag_CH[] = {
  59996. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59997. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59998. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  59999. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  60000. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  60001. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  60002. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  60003. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  60004. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  60005. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  60006. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  60007. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  60008. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  60009. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  60010. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  60011. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  60012. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  60013. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  60014. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  60015. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60016. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  60017. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  60018. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  60019. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  60020. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  60021. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  60022. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  60023. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  60024. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  60025. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  60026. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  60027. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  60028. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  60029. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  60030. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  60031. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  60032. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  60033. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  60034. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  60035. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  60036. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  60037. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  60038. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  60039. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  60040. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  60041. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  60042. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  60043. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  60044. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  60045. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  60046. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  60047. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  60048. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  60049. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  60050. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  60051. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  60052. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  60053. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  60054. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  60055. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  60056. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  60057. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  60058. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  60059. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  60060. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  60061. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  60062. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  60063. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  60064. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  60065. 0xa5, 0xcb, 0x68, 0xf3
  60066. };
  60067. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60068. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60069. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60070. /* Fragment msgs */
  60071. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  60072. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  60073. /* Add in some key shares to make the CH long */
  60074. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  60075. WOLFSSL_SUCCESS);
  60076. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  60077. WOLFSSL_SUCCESS);
  60078. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  60079. WOLFSSL_SUCCESS);
  60080. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  60081. WOLFSSL_SUCCESS);
  60082. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  60083. /* Reject fragmented first CH */
  60084. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  60085. sizeof(four_frag_CH)), 4);
  60086. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  60087. test_ctx.s_len = sizeof(four_frag_CH);
  60088. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  60089. int s_len = test_ctx.s_len;
  60090. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60091. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60092. /* Fail if we didn't advance the buffer to avoid infinite loops */
  60093. ExpectIntLT(test_ctx.s_len, s_len);
  60094. }
  60095. /* Expect all fragments read */
  60096. ExpectIntEQ(test_ctx.s_len, 0);
  60097. /* Expect quietly dropping fragmented first CH */
  60098. ExpectIntEQ(test_ctx.c_len, 0);
  60099. /* CH1 */
  60100. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60101. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60102. /* Count records. Expect 1 unfragmented CH */
  60103. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  60104. test_ctx.s_len), 1);
  60105. /* HRR */
  60106. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60107. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60108. /* CH2 */
  60109. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60110. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60111. /* Count records. Expect fragmented CH */
  60112. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  60113. test_ctx.s_len), 1);
  60114. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60115. wolfSSL_free(ssl_c);
  60116. wolfSSL_free(ssl_s);
  60117. wolfSSL_CTX_free(ctx_c);
  60118. wolfSSL_CTX_free(ctx_s);
  60119. ssl_c = ssl_s = NULL;
  60120. ctx_c = ctx_s = NULL;
  60121. #endif
  60122. return EXPECT_RESULT();
  60123. }
  60124. static int test_dtls_empty_keyshare_with_cookie(void)
  60125. {
  60126. EXPECT_DECLS;
  60127. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60128. WOLFSSL_CTX *ctx_s = NULL;
  60129. WOLFSSL *ssl_s = NULL;
  60130. struct test_memio_ctx test_ctx;
  60131. unsigned char ch_empty_keyshare_with_cookie[] = {
  60132. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  60133. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  60134. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  60135. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  60136. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  60137. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  60138. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  60139. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  60140. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  60141. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  60142. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  60143. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  60144. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  60145. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  60146. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  60147. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  60148. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  60149. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  60150. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  60151. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  60152. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  60153. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  60154. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  60155. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  60156. };
  60157. DtlsRecordLayerHeader* dtlsRH;
  60158. byte sequence_number[8];
  60159. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60160. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60161. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  60162. sizeof(ch_empty_keyshare_with_cookie));
  60163. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  60164. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60165. NULL, wolfDTLSv1_3_server_method), 0);
  60166. /* CH1 */
  60167. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60168. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60169. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  60170. ExpectIntEQ(test_ctx.c_len, 15);
  60171. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  60172. ExpectIntEQ(dtlsRH->type, alert);
  60173. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60174. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60175. sequence_number[7] = 1;
  60176. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60177. sizeof(sequence_number)), 0);
  60178. ExpectIntEQ(dtlsRH->length[0], 0);
  60179. ExpectIntEQ(dtlsRH->length[1], 2);
  60180. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  60181. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  60182. wolfSSL_free(ssl_s);
  60183. wolfSSL_CTX_free(ctx_s);
  60184. #endif
  60185. return EXPECT_RESULT();
  60186. }
  60187. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60188. defined(HAVE_LIBOQS)
  60189. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  60190. {
  60191. int group = WOLFSSL_KYBER_LEVEL5;
  60192. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  60193. }
  60194. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  60195. {
  60196. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  60197. }
  60198. #endif
  60199. static int test_tls13_pq_groups(void)
  60200. {
  60201. EXPECT_DECLS;
  60202. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60203. defined(HAVE_LIBOQS)
  60204. callback_functions func_cb_client;
  60205. callback_functions func_cb_server;
  60206. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60207. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60208. func_cb_client.method = wolfTLSv1_3_client_method;
  60209. func_cb_server.method = wolfTLSv1_3_server_method;
  60210. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  60211. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  60212. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  60213. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60214. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60215. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60216. #endif
  60217. return EXPECT_RESULT();
  60218. }
  60219. static int test_dtls13_early_data(void)
  60220. {
  60221. EXPECT_DECLS;
  60222. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) && \
  60223. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  60224. struct test_memio_ctx test_ctx;
  60225. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60226. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60227. WOLFSSL_SESSION *sess = NULL;
  60228. int written = 0;
  60229. int read = 0;
  60230. char msg[] = "This is early data";
  60231. char msg2[] = "This is client data";
  60232. char msg3[] = "This is server data";
  60233. char msg4[] = "This is server immediate data";
  60234. char msgBuf[50];
  60235. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60236. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60237. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60238. /* Get a ticket so that we can do 0-RTT on the next connection */
  60239. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60240. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  60241. wolfSSL_free(ssl_c);
  60242. ssl_c = NULL;
  60243. wolfSSL_free(ssl_s);
  60244. ssl_s = NULL;
  60245. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60246. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60247. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60248. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  60249. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  60250. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  60251. #else
  60252. /* Let's test this but we generally don't recommend turning off the
  60253. * cookie exchange */
  60254. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60255. #endif
  60256. /* Test 0-RTT data */
  60257. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  60258. &written), sizeof(msg));
  60259. ExpectIntEQ(written, sizeof(msg));
  60260. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  60261. &read), sizeof(msg));
  60262. ExpectIntEQ(read, sizeof(msg));
  60263. ExpectStrEQ(msg, msgBuf);
  60264. /* Test 0.5-RTT data */
  60265. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  60266. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60267. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  60268. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  60269. ExpectStrEQ(msg4, msgBuf);
  60270. /* Complete handshake */
  60271. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60272. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60273. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  60274. * a user would loop until it is true but here we control both sides so we
  60275. * just assert the expected value. wolfSSL_read_early_data does not provide
  60276. * handshake status to us with non-blocking IO and we can't use
  60277. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  60278. * parsing logic. */
  60279. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  60280. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  60281. &read), -1);
  60282. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60283. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60284. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  60285. /* Test bi-directional write */
  60286. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  60287. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  60288. ExpectStrEQ(msg2, msgBuf);
  60289. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  60290. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  60291. ExpectStrEQ(msg3, msgBuf);
  60292. ExpectTrue(wolfSSL_session_reused(ssl_c));
  60293. ExpectTrue(wolfSSL_session_reused(ssl_s));
  60294. wolfSSL_SESSION_free(sess);
  60295. wolfSSL_free(ssl_c);
  60296. wolfSSL_free(ssl_s);
  60297. wolfSSL_CTX_free(ctx_c);
  60298. wolfSSL_CTX_free(ctx_s);
  60299. #endif
  60300. return EXPECT_RESULT();
  60301. }
  60302. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  60303. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  60304. {
  60305. EXPECT_DECLS;
  60306. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60307. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  60308. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  60309. return EXPECT_RESULT();
  60310. }
  60311. #endif
  60312. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  60313. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  60314. {
  60315. EXPECT_DECLS;
  60316. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60317. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  60318. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  60319. return EXPECT_RESULT();
  60320. }
  60321. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  60322. {
  60323. EXPECT_DECLS;
  60324. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60325. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  60326. 0), 1);
  60327. return EXPECT_RESULT();
  60328. }
  60329. #endif
  60330. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  60331. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  60332. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  60333. {
  60334. EXPECT_DECLS;
  60335. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  60336. return EXPECT_RESULT();
  60337. }
  60338. #endif
  60339. static int test_self_signed_stapling(void)
  60340. {
  60341. EXPECT_DECLS;
  60342. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  60343. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  60344. test_ssl_cbf client_cbf;
  60345. test_ssl_cbf server_cbf;
  60346. size_t i;
  60347. struct {
  60348. method_provider client_meth;
  60349. method_provider server_meth;
  60350. ctx_cb client_ctx;
  60351. const char* tls_version;
  60352. } params[] = {
  60353. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  60354. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60355. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  60356. #endif
  60357. #ifndef WOLFSSL_NO_TLS12
  60358. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  60359. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60360. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  60361. #endif
  60362. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  60363. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60364. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  60365. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60366. test_self_signed_stapling_client_v2_multi_ctx_ready,
  60367. "TLSv1_2 v2 multi" },
  60368. #endif
  60369. #endif
  60370. };
  60371. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60372. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60373. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60374. printf("\nTesting self-signed cert with status request: %s\n",
  60375. params[i].tls_version);
  60376. client_cbf.method = params[i].client_meth;
  60377. client_cbf.ctx_ready = params[i].client_ctx;
  60378. server_cbf.method = params[i].server_meth;
  60379. server_cbf.certPemFile = "certs/ca-cert.pem";
  60380. server_cbf.keyPemFile = "certs/ca-key.pem";
  60381. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  60382. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60383. &server_cbf, NULL), TEST_SUCCESS);
  60384. }
  60385. #endif
  60386. return EXPECT_RESULT();
  60387. }
  60388. /*----------------------------------------------------------------------------*
  60389. | Main
  60390. *----------------------------------------------------------------------------*/
  60391. typedef int (*TEST_FUNC)(void);
  60392. typedef struct {
  60393. const char *name;
  60394. TEST_FUNC func;
  60395. byte run:1;
  60396. byte fail:1;
  60397. } TEST_CASE;
  60398. #define TEST_DECL(func) { #func, func, 0, 0 }
  60399. int testAll = 1;
  60400. TEST_CASE testCases[] = {
  60401. TEST_DECL(test_fileAccess),
  60402. /*********************************
  60403. * wolfcrypt
  60404. *********************************/
  60405. TEST_DECL(test_ForceZero),
  60406. TEST_DECL(test_wolfCrypt_Init),
  60407. /* Locking with Compat Mutex */
  60408. TEST_DECL(test_wc_SetMutexCb),
  60409. TEST_DECL(test_wc_LockMutex_ex),
  60410. /* Digests */
  60411. TEST_DECL(test_wc_InitMd5),
  60412. TEST_DECL(test_wc_Md5Update),
  60413. TEST_DECL(test_wc_Md5Final),
  60414. TEST_DECL(test_wc_InitSha),
  60415. TEST_DECL(test_wc_ShaUpdate),
  60416. TEST_DECL(test_wc_ShaFinal),
  60417. TEST_DECL(test_wc_InitSha256),
  60418. TEST_DECL(test_wc_Sha256Update),
  60419. TEST_DECL(test_wc_Sha256Final),
  60420. TEST_DECL(test_wc_Sha256FinalRaw),
  60421. TEST_DECL(test_wc_Sha256GetFlags),
  60422. TEST_DECL(test_wc_Sha256Free),
  60423. TEST_DECL(test_wc_Sha256GetHash),
  60424. TEST_DECL(test_wc_Sha256Copy),
  60425. TEST_DECL(test_wc_InitSha224),
  60426. TEST_DECL(test_wc_Sha224Update),
  60427. TEST_DECL(test_wc_Sha224Final),
  60428. TEST_DECL(test_wc_Sha224SetFlags),
  60429. TEST_DECL(test_wc_Sha224GetFlags),
  60430. TEST_DECL(test_wc_Sha224Free),
  60431. TEST_DECL(test_wc_Sha224GetHash),
  60432. TEST_DECL(test_wc_Sha224Copy),
  60433. TEST_DECL(test_wc_InitSha512),
  60434. TEST_DECL(test_wc_Sha512Update),
  60435. TEST_DECL(test_wc_Sha512Final),
  60436. TEST_DECL(test_wc_Sha512GetFlags),
  60437. TEST_DECL(test_wc_Sha512FinalRaw),
  60438. TEST_DECL(test_wc_Sha512Free),
  60439. TEST_DECL(test_wc_Sha512GetHash),
  60440. TEST_DECL(test_wc_Sha512Copy),
  60441. TEST_DECL(test_wc_InitSha512_224),
  60442. TEST_DECL(test_wc_Sha512_224Update),
  60443. TEST_DECL(test_wc_Sha512_224Final),
  60444. TEST_DECL(test_wc_Sha512_224GetFlags),
  60445. TEST_DECL(test_wc_Sha512_224FinalRaw),
  60446. TEST_DECL(test_wc_Sha512_224Free),
  60447. TEST_DECL(test_wc_Sha512_224GetHash),
  60448. TEST_DECL(test_wc_Sha512_224Copy),
  60449. TEST_DECL(test_wc_InitSha512_256),
  60450. TEST_DECL(test_wc_Sha512_256Update),
  60451. TEST_DECL(test_wc_Sha512_256Final),
  60452. TEST_DECL(test_wc_Sha512_256GetFlags),
  60453. TEST_DECL(test_wc_Sha512_256FinalRaw),
  60454. TEST_DECL(test_wc_Sha512_256Free),
  60455. TEST_DECL(test_wc_Sha512_256GetHash),
  60456. TEST_DECL(test_wc_Sha512_256Copy),
  60457. TEST_DECL(test_wc_InitSha384),
  60458. TEST_DECL(test_wc_Sha384Update),
  60459. TEST_DECL(test_wc_Sha384Final),
  60460. TEST_DECL(test_wc_Sha384GetFlags),
  60461. TEST_DECL(test_wc_Sha384FinalRaw),
  60462. TEST_DECL(test_wc_Sha384Free),
  60463. TEST_DECL(test_wc_Sha384GetHash),
  60464. TEST_DECL(test_wc_Sha384Copy),
  60465. TEST_DECL(test_wc_InitBlake2b),
  60466. TEST_DECL(test_wc_InitBlake2b_WithKey),
  60467. TEST_DECL(test_wc_InitBlake2s_WithKey),
  60468. TEST_DECL(test_wc_InitRipeMd),
  60469. TEST_DECL(test_wc_RipeMdUpdate),
  60470. TEST_DECL(test_wc_RipeMdFinal),
  60471. TEST_DECL(test_wc_InitSha3),
  60472. TEST_DECL(testing_wc_Sha3_Update),
  60473. TEST_DECL(test_wc_Sha3_224_Final),
  60474. TEST_DECL(test_wc_Sha3_256_Final),
  60475. TEST_DECL(test_wc_Sha3_384_Final),
  60476. TEST_DECL(test_wc_Sha3_512_Final),
  60477. TEST_DECL(test_wc_Sha3_224_Copy),
  60478. TEST_DECL(test_wc_Sha3_256_Copy),
  60479. TEST_DECL(test_wc_Sha3_384_Copy),
  60480. TEST_DECL(test_wc_Sha3_512_Copy),
  60481. TEST_DECL(test_wc_Sha3_GetFlags),
  60482. TEST_DECL(test_wc_InitShake256),
  60483. TEST_DECL(testing_wc_Shake256_Update),
  60484. TEST_DECL(test_wc_Shake256_Final),
  60485. TEST_DECL(test_wc_Shake256_Copy),
  60486. TEST_DECL(test_wc_Shake256Hash),
  60487. /* SM3 Digest */
  60488. TEST_DECL(test_wc_InitSm3Free),
  60489. TEST_DECL(test_wc_Sm3UpdateFinal),
  60490. TEST_DECL(test_wc_Sm3GetHash),
  60491. TEST_DECL(test_wc_Sm3Copy),
  60492. TEST_DECL(test_wc_Sm3FinalRaw),
  60493. TEST_DECL(test_wc_Sm3GetSetFlags),
  60494. TEST_DECL(test_wc_Sm3Hash),
  60495. TEST_DECL(test_wc_HashInit),
  60496. TEST_DECL(test_wc_HashSetFlags),
  60497. TEST_DECL(test_wc_HashGetFlags),
  60498. /* HMAC */
  60499. TEST_DECL(test_wc_Md5HmacSetKey),
  60500. TEST_DECL(test_wc_Md5HmacUpdate),
  60501. TEST_DECL(test_wc_Md5HmacFinal),
  60502. TEST_DECL(test_wc_ShaHmacSetKey),
  60503. TEST_DECL(test_wc_ShaHmacUpdate),
  60504. TEST_DECL(test_wc_ShaHmacFinal),
  60505. TEST_DECL(test_wc_Sha224HmacSetKey),
  60506. TEST_DECL(test_wc_Sha224HmacUpdate),
  60507. TEST_DECL(test_wc_Sha224HmacFinal),
  60508. TEST_DECL(test_wc_Sha256HmacSetKey),
  60509. TEST_DECL(test_wc_Sha256HmacUpdate),
  60510. TEST_DECL(test_wc_Sha256HmacFinal),
  60511. TEST_DECL(test_wc_Sha384HmacSetKey),
  60512. TEST_DECL(test_wc_Sha384HmacUpdate),
  60513. TEST_DECL(test_wc_Sha384HmacFinal),
  60514. /* CMAC */
  60515. TEST_DECL(test_wc_InitCmac),
  60516. TEST_DECL(test_wc_CmacUpdate),
  60517. TEST_DECL(test_wc_CmacFinal),
  60518. TEST_DECL(test_wc_AesCmacGenerate),
  60519. /* Cipher */
  60520. TEST_DECL(test_wc_AesGcmStream),
  60521. TEST_DECL(test_wc_Des3_SetIV),
  60522. TEST_DECL(test_wc_Des3_SetKey),
  60523. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  60524. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  60525. TEST_DECL(test_wc_Des3_EcbEncrypt),
  60526. TEST_DECL(test_wc_Chacha_SetKey),
  60527. TEST_DECL(test_wc_Chacha_Process),
  60528. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  60529. TEST_DECL(test_wc_Poly1305SetKey),
  60530. TEST_DECL(test_wc_CamelliaSetKey),
  60531. TEST_DECL(test_wc_CamelliaSetIV),
  60532. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  60533. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  60534. TEST_DECL(test_wc_Arc4SetKey),
  60535. TEST_DECL(test_wc_Arc4Process),
  60536. TEST_DECL(test_wc_Rc2SetKey),
  60537. TEST_DECL(test_wc_Rc2SetIV),
  60538. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  60539. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  60540. /* AES cipher and GMAC. */
  60541. TEST_DECL(test_wc_AesSetKey),
  60542. TEST_DECL(test_wc_AesSetIV),
  60543. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  60544. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  60545. TEST_DECL(test_wc_AesGcmSetKey),
  60546. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  60547. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  60548. TEST_DECL(test_wc_GmacSetKey),
  60549. TEST_DECL(test_wc_GmacUpdate),
  60550. TEST_DECL(test_wc_AesCcmSetKey),
  60551. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  60552. #if defined(WOLFSSL_AES_EAX) && \
  60553. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  60554. TEST_DECL(test_wc_AesEaxVectors),
  60555. TEST_DECL(test_wc_AesEaxEncryptAuth),
  60556. TEST_DECL(test_wc_AesEaxDecryptAuth),
  60557. #endif /* WOLFSSL_AES_EAX */
  60558. /* SM4 cipher */
  60559. TEST_DECL(test_wc_Sm4),
  60560. TEST_DECL(test_wc_Sm4Ecb),
  60561. TEST_DECL(test_wc_Sm4Cbc),
  60562. TEST_DECL(test_wc_Sm4Ctr),
  60563. TEST_DECL(test_wc_Sm4Gcm),
  60564. TEST_DECL(test_wc_Sm4Ccm),
  60565. /* RNG tests */
  60566. #ifdef HAVE_HASHDRBG
  60567. #ifdef TEST_RESEED_INTERVAL
  60568. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  60569. #endif
  60570. TEST_DECL(test_wc_RNG_GenerateBlock),
  60571. #endif
  60572. TEST_DECL(test_get_rand_digit),
  60573. TEST_DECL(test_wc_InitRngNonce),
  60574. TEST_DECL(test_wc_InitRngNonce_ex),
  60575. /* MP API tests */
  60576. TEST_DECL(test_get_digit_count),
  60577. TEST_DECL(test_mp_cond_copy),
  60578. TEST_DECL(test_mp_rand),
  60579. TEST_DECL(test_get_digit),
  60580. TEST_DECL(test_wc_export_int),
  60581. /* RSA */
  60582. TEST_DECL(test_wc_InitRsaKey),
  60583. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  60584. TEST_DECL(test_wc_RsaPublicKeyDecode),
  60585. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  60586. TEST_DECL(test_wc_MakeRsaKey),
  60587. TEST_DECL(test_wc_CheckProbablePrime),
  60588. TEST_DECL(test_wc_RsaPSS_Verify),
  60589. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  60590. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  60591. TEST_DECL(test_wc_RsaKeyToDer),
  60592. TEST_DECL(test_wc_RsaKeyToPublicDer),
  60593. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  60594. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  60595. TEST_DECL(test_wc_RsaEncryptSize),
  60596. TEST_DECL(test_wc_RsaSSL_SignVerify),
  60597. TEST_DECL(test_wc_RsaFlattenPublicKey),
  60598. TEST_DECL(test_RsaDecryptBoundsCheck),
  60599. /* DSA */
  60600. TEST_DECL(test_wc_InitDsaKey),
  60601. TEST_DECL(test_wc_DsaSignVerify),
  60602. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  60603. TEST_DECL(test_wc_MakeDsaKey),
  60604. TEST_DECL(test_wc_DsaKeyToDer),
  60605. TEST_DECL(test_wc_DsaKeyToPublicDer),
  60606. TEST_DECL(test_wc_DsaImportParamsRaw),
  60607. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  60608. TEST_DECL(test_wc_DsaExportParamsRaw),
  60609. TEST_DECL(test_wc_DsaExportKeyRaw),
  60610. /* DH */
  60611. TEST_DECL(test_wc_DhPublicKeyDecode),
  60612. /* wolfCrypt ECC tests */
  60613. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  60614. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  60615. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  60616. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  60617. !defined(HAVE_SELFTEST) && \
  60618. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  60619. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  60620. #endif
  60621. TEST_DECL(test_wc_ecc_make_key),
  60622. TEST_DECL(test_wc_ecc_init),
  60623. TEST_DECL(test_wc_ecc_check_key),
  60624. TEST_DECL(test_wc_ecc_get_generator),
  60625. TEST_DECL(test_wc_ecc_size),
  60626. TEST_DECL(test_wc_ecc_params),
  60627. TEST_DECL(test_wc_ecc_signVerify_hash),
  60628. TEST_DECL(test_wc_ecc_shared_secret),
  60629. TEST_DECL(test_wc_ecc_export_x963),
  60630. TEST_DECL(test_wc_ecc_export_x963_ex),
  60631. TEST_DECL(test_wc_ecc_import_x963),
  60632. TEST_DECL(test_wc_ecc_import_private_key),
  60633. TEST_DECL(test_wc_ecc_export_private_only),
  60634. TEST_DECL(test_wc_ecc_rs_to_sig),
  60635. TEST_DECL(test_wc_ecc_import_raw),
  60636. TEST_DECL(test_wc_ecc_import_unsigned),
  60637. TEST_DECL(test_wc_ecc_sig_size),
  60638. TEST_DECL(test_wc_ecc_ctx_new),
  60639. TEST_DECL(test_wc_ecc_ctx_reset),
  60640. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  60641. TEST_DECL(test_wc_ecc_ctx_set_info),
  60642. TEST_DECL(test_wc_ecc_encryptDecrypt),
  60643. TEST_DECL(test_wc_ecc_del_point),
  60644. TEST_DECL(test_wc_ecc_pointFns),
  60645. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  60646. TEST_DECL(test_wc_ecc_verify_hash_ex),
  60647. TEST_DECL(test_wc_ecc_mulmod),
  60648. TEST_DECL(test_wc_ecc_is_valid_idx),
  60649. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  60650. TEST_DECL(test_wc_ecc_sig_size_calc),
  60651. TEST_DECL(test_wc_EccPrivateKeyToDer),
  60652. /* SM2 elliptic curve */
  60653. TEST_DECL(test_wc_ecc_sm2_make_key),
  60654. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  60655. TEST_DECL(test_wc_ecc_sm2_create_digest),
  60656. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  60657. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  60658. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  60659. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  60660. /* Curve25519 */
  60661. TEST_DECL(test_wc_curve25519_init),
  60662. TEST_DECL(test_wc_curve25519_size),
  60663. TEST_DECL(test_wc_curve25519_export_key_raw),
  60664. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  60665. TEST_DECL(test_wc_curve25519_make_key),
  60666. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  60667. TEST_DECL(test_wc_curve25519_make_pub),
  60668. TEST_DECL(test_wc_curve25519_export_public_ex),
  60669. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  60670. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  60671. TEST_DECL(test_wc_curve25519_import_private),
  60672. /* ED25519 */
  60673. TEST_DECL(test_wc_ed25519_make_key),
  60674. TEST_DECL(test_wc_ed25519_init),
  60675. TEST_DECL(test_wc_ed25519_sign_msg),
  60676. TEST_DECL(test_wc_ed25519_import_public),
  60677. TEST_DECL(test_wc_ed25519_import_private_key),
  60678. TEST_DECL(test_wc_ed25519_export),
  60679. TEST_DECL(test_wc_ed25519_size),
  60680. TEST_DECL(test_wc_ed25519_exportKey),
  60681. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  60682. TEST_DECL(test_wc_Ed25519KeyToDer),
  60683. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  60684. /* Curve448 */
  60685. TEST_DECL(test_wc_curve448_make_key),
  60686. TEST_DECL(test_wc_curve448_shared_secret_ex),
  60687. TEST_DECL(test_wc_curve448_export_public_ex),
  60688. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  60689. TEST_DECL(test_wc_curve448_export_key_raw),
  60690. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  60691. TEST_DECL(test_wc_curve448_import_private),
  60692. TEST_DECL(test_wc_curve448_init),
  60693. TEST_DECL(test_wc_curve448_size),
  60694. /* Ed448 */
  60695. TEST_DECL(test_wc_ed448_make_key),
  60696. TEST_DECL(test_wc_ed448_init),
  60697. TEST_DECL(test_wc_ed448_sign_msg),
  60698. TEST_DECL(test_wc_ed448_import_public),
  60699. TEST_DECL(test_wc_ed448_import_private_key),
  60700. TEST_DECL(test_wc_ed448_export),
  60701. TEST_DECL(test_wc_ed448_size),
  60702. TEST_DECL(test_wc_ed448_exportKey),
  60703. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  60704. TEST_DECL(test_wc_Ed448KeyToDer),
  60705. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  60706. /* Signature API */
  60707. TEST_DECL(test_wc_SignatureGetSize_ecc),
  60708. TEST_DECL(test_wc_SignatureGetSize_rsa),
  60709. /* PEM and DER APIs. */
  60710. TEST_DECL(test_wc_PemToDer),
  60711. TEST_DECL(test_wc_AllocDer),
  60712. TEST_DECL(test_wc_CertPemToDer),
  60713. TEST_DECL(test_wc_KeyPemToDer),
  60714. TEST_DECL(test_wc_PubKeyPemToDer),
  60715. TEST_DECL(test_wc_PemPubKeyToDer),
  60716. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  60717. TEST_DECL(test_wc_CheckCertSigPubKey),
  60718. /* wolfCrypt ASN tests */
  60719. TEST_DECL(test_ToTraditional),
  60720. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  60721. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  60722. /* Certificate */
  60723. TEST_DECL(test_wc_SetSubjectRaw),
  60724. TEST_DECL(test_wc_GetSubjectRaw),
  60725. TEST_DECL(test_wc_SetIssuerRaw),
  60726. TEST_DECL(test_wc_SetIssueBuffer),
  60727. TEST_DECL(test_wc_SetSubjectKeyId),
  60728. TEST_DECL(test_wc_SetSubject),
  60729. TEST_DECL(test_CheckCertSignature),
  60730. TEST_DECL(test_wc_ParseCert),
  60731. TEST_DECL(test_wc_ParseCert_Error),
  60732. TEST_DECL(test_MakeCertWithPathLen),
  60733. TEST_DECL(test_MakeCertWithCaFalse),
  60734. TEST_DECL(test_wc_SetKeyUsage),
  60735. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  60736. TEST_DECL(test_wc_SetSubjectBuffer),
  60737. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  60738. /* wolfcrypt PKCS#7 */
  60739. TEST_DECL(test_wc_PKCS7_New),
  60740. TEST_DECL(test_wc_PKCS7_Init),
  60741. TEST_DECL(test_wc_PKCS7_InitWithCert),
  60742. TEST_DECL(test_wc_PKCS7_EncodeData),
  60743. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  60744. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  60745. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  60746. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  60747. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  60748. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  60749. TEST_DECL(test_wc_PKCS7_Degenerate),
  60750. TEST_DECL(test_wc_PKCS7_BER),
  60751. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  60752. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  60753. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  60754. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  60755. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  60756. /* wolfCrypt PKCS#12 */
  60757. TEST_DECL(test_wc_i2d_PKCS12),
  60758. /*
  60759. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  60760. * avoid memory leaks.
  60761. */
  60762. TEST_DECL(test_wolfCrypt_Cleanup),
  60763. TEST_DECL(test_wolfSSL_Init),
  60764. /*********************************
  60765. * OpenSSL compatibility API tests
  60766. *********************************/
  60767. /* If at some point a stub get implemented this test should fail indicating
  60768. * a need to implement a new test case
  60769. */
  60770. TEST_DECL(test_stubs_are_stubs),
  60771. /* ASN.1 compatibility API tests */
  60772. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  60773. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  60774. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  60775. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  60776. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  60777. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  60778. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  60779. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  60780. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  60781. TEST_DECL(test_wolfSSL_ASN1_get_object),
  60782. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  60783. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  60784. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  60785. TEST_DECL(test_wolfSSL_ASN1_STRING),
  60786. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  60787. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  60788. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  60789. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  60790. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  60791. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  60792. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  60793. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  60794. TEST_DECL(test_wolfSSL_ASN1_TIME),
  60795. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  60796. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  60797. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  60798. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  60799. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  60800. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  60801. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  60802. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  60803. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  60804. TEST_DECL(test_wolfSSL_lhash),
  60805. TEST_DECL(test_wolfSSL_certs),
  60806. TEST_DECL(test_wolfSSL_private_keys),
  60807. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  60808. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  60809. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  60810. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  60811. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  60812. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  60813. #ifndef NO_BIO
  60814. TEST_DECL(test_wolfSSL_BIO),
  60815. TEST_DECL(test_wolfSSL_PEM_read_bio),
  60816. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  60817. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  60818. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  60819. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  60820. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  60821. #endif
  60822. /* EVP API testing */
  60823. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  60824. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  60825. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  60826. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  60827. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  60828. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  60829. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  60830. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  60831. TEST_DECL(test_wolfSSL_EVP_shake128),
  60832. TEST_DECL(test_wolfSSL_EVP_shake256),
  60833. TEST_DECL(test_wolfSSL_EVP_sm3),
  60834. TEST_DECL(test_EVP_blake2),
  60835. #ifdef OPENSSL_ALL
  60836. TEST_DECL(test_wolfSSL_EVP_md4),
  60837. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  60838. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  60839. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  60840. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  60841. #endif
  60842. TEST_DECL(test_EVP_MD_do_all),
  60843. TEST_DECL(test_wolfSSL_EVP_MD_size),
  60844. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  60845. TEST_DECL(test_wolfSSL_EVP_Digest),
  60846. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  60847. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  60848. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  60849. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  60850. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  60851. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  60852. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  60853. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  60854. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  60855. TEST_DECL(test_wolfssl_EVP_chacha20),
  60856. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  60857. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  60858. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  60859. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  60860. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  60861. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  60862. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  60863. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  60864. #ifdef OPENSSL_ALL
  60865. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  60866. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  60867. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  60868. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  60869. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  60870. TEST_DECL(test_wolfSSL_EVP_rc4),
  60871. TEST_DECL(test_wolfSSL_EVP_enc_null),
  60872. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  60873. TEST_DECL(test_wolfSSL_EVP_mdc2),
  60874. TEST_DECL(test_evp_cipher_aes_gcm),
  60875. #endif
  60876. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  60877. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  60878. #ifdef OPENSSL_EXTRA
  60879. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  60880. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  60881. #endif
  60882. #ifdef OPENSSL_ALL
  60883. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  60884. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  60885. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  60886. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  60887. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  60888. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  60889. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  60890. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  60891. #endif
  60892. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  60893. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  60894. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  60895. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  60896. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  60897. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  60898. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  60899. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  60900. #ifndef NO_BIO
  60901. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  60902. #endif
  60903. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  60904. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  60905. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  60906. #ifndef NO_BIO
  60907. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  60908. #endif /* !NO_BIO */
  60909. #endif
  60910. #ifdef OPENSSL_ALL
  60911. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  60912. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  60913. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  60914. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  60915. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  60916. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  60917. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  60918. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  60919. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  60920. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  60921. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  60922. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  60923. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  60924. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  60925. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  60926. #endif
  60927. TEST_DECL(test_EVP_PKEY_rsa),
  60928. TEST_DECL(test_EVP_PKEY_ec),
  60929. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  60930. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  60931. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  60932. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  60933. TEST_DECL(test_EVP_PKEY_cmp),
  60934. #ifdef OPENSSL_ALL
  60935. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  60936. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  60937. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  60938. #endif
  60939. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  60940. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  60941. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  60942. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  60943. #endif
  60944. #ifndef NO_BIO
  60945. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  60946. TEST_DECL(test_wolfSSL_GetLoggingCb),
  60947. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  60948. TEST_DECL(test_wc_ERR_remove_state),
  60949. TEST_DECL(test_wc_ERR_print_errors_fp),
  60950. #endif
  60951. TEST_DECL(test_wolfSSL_configure_args),
  60952. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  60953. TEST_DECL(test_wolfSSL_set1_curves_list),
  60954. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  60955. TEST_DECL(test_wolfSSL_OtherName),
  60956. TEST_DECL(test_wolfSSL_FPKI),
  60957. TEST_DECL(test_wolfSSL_URI),
  60958. TEST_DECL(test_wolfSSL_TBS),
  60959. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  60960. TEST_DECL(test_X509_STORE_untrusted),
  60961. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  60962. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  60963. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  60964. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  60965. TEST_DECL(test_wolfSSL_X509_Name_canon),
  60966. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  60967. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  60968. TEST_DECL(test_wolfSSL_X509_NID),
  60969. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  60970. TEST_DECL(test_wolfSSL_get0_param),
  60971. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  60972. TEST_DECL(test_wolfSSL_set1_host),
  60973. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  60974. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  60975. TEST_DECL(test_wolfSSL_X509_STORE),
  60976. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  60977. TEST_DECL(test_X509_STORE_get0_objects),
  60978. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  60979. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  60980. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  60981. TEST_DECL(test_wolfSSL_X509_cmp_time),
  60982. TEST_DECL(test_wolfSSL_X509_time_adj),
  60983. /* X509 tests */
  60984. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  60985. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  60986. TEST_DECL(test_wolfSSL_X509_check_host),
  60987. TEST_DECL(test_wolfSSL_X509_check_email),
  60988. TEST_DECL(test_wolfSSL_X509_check_private_key),
  60989. TEST_DECL(test_wolfSSL_X509),
  60990. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  60991. TEST_DECL(test_wolfSSL_X509_sign),
  60992. TEST_DECL(test_wolfSSL_X509_sign2),
  60993. TEST_DECL(test_wolfSSL_X509_verify),
  60994. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  60995. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  60996. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  60997. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  60998. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  60999. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  61000. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  61001. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  61002. TEST_DECL(test_wolfSSL_X509_NAME),
  61003. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  61004. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  61005. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  61006. TEST_DECL(test_wolfSSL_X509_set_name),
  61007. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  61008. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  61009. TEST_DECL(test_wolfSSL_X509_set_version),
  61010. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  61011. TEST_DECL(test_wolfSSL_X509_CRL),
  61012. TEST_DECL(test_wolfSSL_i2d_X509),
  61013. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  61014. TEST_DECL(test_wolfSSL_PEM_read_X509),
  61015. TEST_DECL(test_wolfSSL_X509_check_ca),
  61016. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  61017. TEST_DECL(test_wolfSSL_make_cert),
  61018. #ifndef NO_BIO
  61019. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  61020. TEST_DECL(test_wolfSSL_X509_INFO),
  61021. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  61022. #endif
  61023. #ifdef OPENSSL_ALL
  61024. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  61025. #endif
  61026. TEST_DECL(test_wolfSSL_X509_CA_num),
  61027. TEST_DECL(test_wolfSSL_X509_get_version),
  61028. #ifndef NO_BIO
  61029. TEST_DECL(test_wolfSSL_X509_print),
  61030. TEST_DECL(test_wolfSSL_X509_CRL_print),
  61031. #endif
  61032. TEST_DECL(test_X509_get_signature_nid),
  61033. /* X509 extension testing. */
  61034. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  61035. TEST_DECL(test_wolfSSL_X509_get_ext),
  61036. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  61037. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  61038. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  61039. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  61040. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  61041. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  61042. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  61043. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  61044. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  61045. TEST_DECL(test_wolfSSL_X509V3_EXT),
  61046. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  61047. TEST_DECL(test_wolfSSL_X509_cmp),
  61048. TEST_DECL(test_GENERAL_NAME_set0_othername),
  61049. TEST_DECL(test_othername_and_SID_ext),
  61050. TEST_DECL(test_wolfSSL_dup_CA_list),
  61051. /* OpenSSL sk_X509 API test */
  61052. TEST_DECL(test_sk_X509),
  61053. /* OpenSSL sk_X509_CRL API test */
  61054. TEST_DECL(test_sk_X509_CRL),
  61055. /* OpenSSL X509 REQ API test */
  61056. TEST_DECL(test_X509_REQ),
  61057. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  61058. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  61059. TEST_DECL(test_X509_LOOKUP_add_dir),
  61060. /* RAND compatibility API */
  61061. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  61062. TEST_DECL(test_wolfSSL_RAND_bytes),
  61063. TEST_DECL(test_wolfSSL_RAND),
  61064. /* BN compatibility API */
  61065. TEST_DECL(test_wolfSSL_BN_CTX),
  61066. TEST_DECL(test_wolfSSL_BN),
  61067. TEST_DECL(test_wolfSSL_BN_init),
  61068. TEST_DECL(test_wolfSSL_BN_enc_dec),
  61069. TEST_DECL(test_wolfSSL_BN_word),
  61070. TEST_DECL(test_wolfSSL_BN_bits),
  61071. TEST_DECL(test_wolfSSL_BN_shift),
  61072. TEST_DECL(test_wolfSSL_BN_math),
  61073. TEST_DECL(test_wolfSSL_BN_math_mod),
  61074. TEST_DECL(test_wolfSSL_BN_math_other),
  61075. TEST_DECL(test_wolfSSL_BN_rand),
  61076. TEST_DECL(test_wolfSSL_BN_prime),
  61077. /* OpenSSL PKCS5 API test */
  61078. TEST_DECL(test_wolfSSL_PKCS5),
  61079. /* OpenSSL PKCS8 API test */
  61080. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  61081. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  61082. /* OpenSSL PKCS7 API test */
  61083. TEST_DECL(test_wolfssl_PKCS7),
  61084. TEST_DECL(test_wolfSSL_PKCS7_certs),
  61085. TEST_DECL(test_wolfSSL_PKCS7_sign),
  61086. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  61087. #ifndef NO_BIO
  61088. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  61089. #ifdef HAVE_SMIME
  61090. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  61091. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  61092. #endif /* HAVE_SMIME */
  61093. #endif /* !NO_BIO */
  61094. /* OpenSSL PKCS12 API test */
  61095. TEST_DECL(test_wolfSSL_PKCS12),
  61096. /* Can't memory test as callbacks use Assert. */
  61097. TEST_DECL(test_error_queue_per_thread),
  61098. TEST_DECL(test_wolfSSL_ERR_put_error),
  61099. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  61100. #ifndef NO_BIO
  61101. TEST_DECL(test_wolfSSL_ERR_print_errors),
  61102. #endif
  61103. TEST_DECL(test_OBJ_NAME_do_all),
  61104. TEST_DECL(test_wolfSSL_OBJ),
  61105. TEST_DECL(test_wolfSSL_OBJ_cmp),
  61106. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  61107. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  61108. #ifdef OPENSSL_ALL
  61109. TEST_DECL(test_wolfSSL_OBJ_ln),
  61110. TEST_DECL(test_wolfSSL_OBJ_sn),
  61111. #endif
  61112. #ifndef NO_BIO
  61113. TEST_DECL(test_wolfSSL_BIO_gets),
  61114. TEST_DECL(test_wolfSSL_BIO_puts),
  61115. TEST_DECL(test_wolfSSL_BIO_dump),
  61116. /* Can't memory test as server hangs. */
  61117. TEST_DECL(test_wolfSSL_BIO_should_retry),
  61118. TEST_DECL(test_wolfSSL_BIO_write),
  61119. TEST_DECL(test_wolfSSL_BIO_printf),
  61120. TEST_DECL(test_wolfSSL_BIO_f_md),
  61121. TEST_DECL(test_wolfSSL_BIO_up_ref),
  61122. TEST_DECL(test_wolfSSL_BIO_reset),
  61123. TEST_DECL(test_wolfSSL_BIO_get_len),
  61124. #endif
  61125. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61126. TEST_DECL(test_wolfSSL_check_domain),
  61127. #endif
  61128. TEST_DECL(test_wolfSSL_cert_cb),
  61129. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  61130. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  61131. TEST_DECL(test_wolfSSL_sigalg_info),
  61132. /* Can't memory test as tcp_connect aborts. */
  61133. TEST_DECL(test_wolfSSL_SESSION),
  61134. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  61135. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  61136. TEST_DECL(test_wolfSSL_ticket_keys),
  61137. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  61138. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  61139. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  61140. TEST_DECL(test_wolfSSL_verify_mode),
  61141. TEST_DECL(test_wolfSSL_verify_depth),
  61142. TEST_DECL(test_wolfSSL_verify_result),
  61143. TEST_DECL(test_wolfSSL_msg_callback),
  61144. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  61145. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  61146. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  61147. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  61148. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  61149. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  61150. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  61151. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  61152. TEST_DECL(test_wolfSSL_PEM_read),
  61153. TEST_DECL(test_wolfSSL_OpenSSL_version),
  61154. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  61155. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  61156. TEST_DECL(test_CONF_modules_xxx),
  61157. #ifdef OPENSSL_ALL
  61158. TEST_DECL(test_wolfSSL_TXT_DB),
  61159. TEST_DECL(test_wolfSSL_NCONF),
  61160. #endif
  61161. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  61162. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  61163. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  61164. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  61165. TEST_DECL(test_CRYPTO_THREADID_xxx),
  61166. TEST_DECL(test_ENGINE_cleanup),
  61167. /* test the no op functions for compatibility */
  61168. TEST_DECL(test_no_op_functions),
  61169. /* OpenSSL error API tests */
  61170. TEST_DECL(test_ERR_load_crypto_strings),
  61171. #ifdef OPENSSL_ALL
  61172. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  61173. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  61174. TEST_DECL(test_wolfSSL_CTX_ctrl),
  61175. #endif /* OPENSSL_ALL */
  61176. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  61177. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  61178. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  61179. /*********************************
  61180. * Crypto API tests
  61181. *********************************/
  61182. TEST_DECL(test_wolfSSL_MD4),
  61183. TEST_DECL(test_wolfSSL_MD5),
  61184. TEST_DECL(test_wolfSSL_MD5_Transform),
  61185. TEST_DECL(test_wolfSSL_SHA),
  61186. TEST_DECL(test_wolfSSL_SHA_Transform),
  61187. TEST_DECL(test_wolfSSL_SHA224),
  61188. TEST_DECL(test_wolfSSL_SHA256),
  61189. TEST_DECL(test_wolfSSL_SHA256_Transform),
  61190. TEST_DECL(test_wolfSSL_SHA512_Transform),
  61191. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  61192. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  61193. TEST_DECL(test_wolfSSL_HMAC_CTX),
  61194. TEST_DECL(test_wolfSSL_HMAC),
  61195. TEST_DECL(test_wolfSSL_CMAC),
  61196. TEST_DECL(test_wolfSSL_DES),
  61197. TEST_DECL(test_wolfSSL_DES_ncbc),
  61198. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  61199. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  61200. TEST_DECL(test_wolfSSL_AES_encrypt),
  61201. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  61202. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  61203. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  61204. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  61205. TEST_DECL(test_wolfSSL_RC4),
  61206. TEST_DECL(test_wolfSSL_RSA),
  61207. TEST_DECL(test_wolfSSL_RSA_DER),
  61208. TEST_DECL(test_wolfSSL_RSA_print),
  61209. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  61210. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  61211. TEST_DECL(test_wolfSSL_RSA_get0_key),
  61212. TEST_DECL(test_wolfSSL_RSA_meth),
  61213. TEST_DECL(test_wolfSSL_RSA_verify),
  61214. TEST_DECL(test_wolfSSL_RSA_sign),
  61215. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  61216. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  61217. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  61218. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  61219. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  61220. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  61221. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  61222. TEST_DECL(test_wolfSSL_RSA_ex_data),
  61223. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  61224. TEST_DECL(test_wolfSSL_RSA_To_Der),
  61225. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  61226. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  61227. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  61228. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  61229. TEST_DECL(test_wolfSSL_DH),
  61230. TEST_DECL(test_wolfSSL_DH_dup),
  61231. TEST_DECL(test_wolfSSL_DH_check),
  61232. TEST_DECL(test_wolfSSL_DH_prime),
  61233. TEST_DECL(test_wolfSSL_DH_1536_prime),
  61234. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  61235. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  61236. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  61237. TEST_DECL(test_wolfSSL_d2i_DHparams),
  61238. TEST_DECL(test_wolfSSL_DH_LoadDer),
  61239. TEST_DECL(test_wolfSSL_i2d_DHparams),
  61240. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  61241. TEST_DECL(test_wolfSSL_EC_GROUP),
  61242. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  61243. TEST_DECL(test_wolfSSL_EC_POINT),
  61244. TEST_DECL(test_wolfSSL_SPAKE),
  61245. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  61246. TEST_DECL(test_EC_i2d),
  61247. TEST_DECL(test_wolfSSL_EC_curve),
  61248. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  61249. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  61250. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  61251. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  61252. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  61253. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  61254. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  61255. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  61256. TEST_DECL(test_ECDSA_size_sign),
  61257. TEST_DECL(test_ECDH_compute_key),
  61258. #endif
  61259. #ifdef OPENSSL_EXTRA
  61260. TEST_DECL(test_ED25519),
  61261. TEST_DECL(test_ED448),
  61262. #endif
  61263. TEST_DECL(test_DSA_do_sign_verify),
  61264. #ifdef OPENSSL_ALL
  61265. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  61266. TEST_DECL(test_wolfSSL_DSA_SIG),
  61267. #endif
  61268. TEST_DECL(test_openssl_generate_key_and_cert),
  61269. TEST_DECL(test_wolfSSL_FIPS_mode),
  61270. TEST_DECL(test_openssl_FIPS_drbg),
  61271. /*********************************
  61272. * CertManager API tests
  61273. *********************************/
  61274. TEST_DECL(test_wolfSSL_CertManagerAPI),
  61275. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  61276. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  61277. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  61278. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  61279. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  61280. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  61281. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  61282. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  61283. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  61284. TEST_DECL(test_wolfSSL_CertManagerCRL),
  61285. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  61286. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  61287. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  61288. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  61289. !defined(WOLFSSL_NO_CLIENT_AUTH))
  61290. TEST_DECL(test_various_pathlen_chains),
  61291. #endif
  61292. /*********************************
  61293. * SSL/TLS API tests
  61294. *********************************/
  61295. TEST_DECL(test_wolfSSL_Method_Allocators),
  61296. #ifndef NO_WOLFSSL_SERVER
  61297. TEST_DECL(test_wolfSSL_CTX_new),
  61298. #endif
  61299. TEST_DECL(test_server_wolfSSL_new),
  61300. TEST_DECL(test_client_wolfSSL_new),
  61301. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  61302. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  61303. TEST_DECL(test_for_double_Free),
  61304. #endif
  61305. TEST_DECL(test_wolfSSL_set_options),
  61306. #ifdef WOLFSSL_TLS13
  61307. /* TLS v1.3 API tests */
  61308. TEST_DECL(test_tls13_apis),
  61309. TEST_DECL(test_tls13_cipher_suites),
  61310. #endif
  61311. TEST_DECL(test_wolfSSL_tmp_dh),
  61312. TEST_DECL(test_wolfSSL_ctrl),
  61313. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  61314. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  61315. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  61316. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  61317. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  61318. #endif
  61319. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  61320. TEST_DECL(test_wolfSSL_security_level),
  61321. TEST_DECL(test_wolfSSL_SSL_in_init),
  61322. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  61323. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  61324. TEST_DECL(test_CONF_CTX_FILE),
  61325. TEST_DECL(test_CONF_CTX_CMDLINE),
  61326. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  61327. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  61328. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  61329. /* Bad certificate signature tests */
  61330. TEST_DECL(test_EccSigFailure_cm),
  61331. TEST_DECL(test_RsaSigFailure_cm),
  61332. #endif /* NO_CERTS */
  61333. /* PKCS8 testing */
  61334. TEST_DECL(test_wolfSSL_no_password_cb),
  61335. TEST_DECL(test_wolfSSL_PKCS8),
  61336. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  61337. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  61338. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  61339. TEST_DECL(test_wolfSSL_get_finished),
  61340. /* Uses Assert in handshake callback. */
  61341. TEST_DECL(test_wolfSSL_CTX_add_session),
  61342. /* Large number of memory allocations. */
  61343. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  61344. /* Large number of memory allocations. */
  61345. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  61346. /* Large number of memory allocations. */
  61347. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  61348. /* Large number of memory allocations. */
  61349. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  61350. /* Large number of memory allocations. */
  61351. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  61352. /* Large number of memory allocations. */
  61353. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  61354. #endif
  61355. TEST_DECL(test_SSL_CIPHER_get_xxx),
  61356. TEST_DECL(test_wolfSSL_ERR_strings),
  61357. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  61358. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  61359. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  61360. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  61361. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  61362. /* Large number of memory allocations. */
  61363. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  61364. TEST_DECL(test_wolfSSL_CertRsaPss),
  61365. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  61366. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  61367. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  61368. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  61369. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  61370. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  61371. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  61372. TEST_DECL(test_multiple_crls_same_issuer),
  61373. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  61374. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  61375. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  61376. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  61377. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  61378. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  61379. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  61380. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  61381. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  61382. TEST_DECL(test_SetTmpEC_DHE_Sz),
  61383. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  61384. #ifdef WOLFSSL_DTLS
  61385. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  61386. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  61387. #endif
  61388. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  61389. /* Uses Assert in handshake callback. */
  61390. TEST_DECL(test_wolfSSL_dtls_plaintext),
  61391. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61392. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61393. TEST_DECL(test_wolfSSL_read_write),
  61394. /* Can't memory test as server hangs if client fails before second connect.
  61395. */
  61396. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  61397. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  61398. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  61399. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  61400. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  61401. /* Can't memory test as server hangs. */
  61402. TEST_DECL(test_wolfSSL_dtls_export),
  61403. /* Uses Assert in handshake callback. */
  61404. TEST_DECL(test_wolfSSL_tls_export),
  61405. #endif
  61406. TEST_DECL(test_wolfSSL_dtls_export_peers),
  61407. TEST_DECL(test_wolfSSL_SetMinVersion),
  61408. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  61409. /* wolfSSL handshake APIs. */
  61410. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  61411. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  61412. TEST_DECL(test_wolfSSL_BUF),
  61413. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  61414. /* Can't memory test as server hangs. */
  61415. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  61416. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  61417. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  61418. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  61419. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  61420. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  61421. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  61422. /* Can't memory test as server hangs. */
  61423. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  61424. TEST_DECL(test_wolfSSL_Tls13_postauth),
  61425. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  61426. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  61427. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  61428. TEST_DECL(test_wolfSSL_THREADID_hash),
  61429. /* TLS extensions tests */
  61430. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  61431. #ifdef HAVE_SNI
  61432. TEST_DECL(test_wolfSSL_UseSNI_params),
  61433. /* Uses Assert in handshake callback. */
  61434. TEST_DECL(test_wolfSSL_UseSNI_connection),
  61435. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  61436. #endif /* HAVE_SNI */
  61437. #endif
  61438. TEST_DECL(test_wolfSSL_UseTrustedCA),
  61439. TEST_DECL(test_wolfSSL_UseMaxFragment),
  61440. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  61441. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  61442. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  61443. /* Uses Assert in handshake callback. */
  61444. TEST_DECL(test_wolfSSL_UseALPN_connection),
  61445. TEST_DECL(test_wolfSSL_UseALPN_params),
  61446. #endif
  61447. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  61448. /* Uses Assert in handshake callback. */
  61449. TEST_DECL(test_wolfSSL_set_alpn_protos),
  61450. #endif
  61451. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  61452. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  61453. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  61454. TEST_DECL(test_tls_ext_duplicate),
  61455. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  61456. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61457. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  61458. /* Uses Assert in handshake callback. */
  61459. TEST_DECL(test_wolfSSL_Tls13_ECH),
  61460. #endif
  61461. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  61462. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  61463. /* OCSP Stapling */
  61464. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  61465. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  61466. TEST_DECL(test_self_signed_stapling),
  61467. /* Multicast */
  61468. TEST_DECL(test_wolfSSL_mcast),
  61469. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  61470. TEST_DECL(test_wolfSSL_msgCb),
  61471. TEST_DECL(test_wolfSSL_either_side),
  61472. TEST_DECL(test_wolfSSL_DTLS_either_side),
  61473. /* Uses Assert in handshake callback. */
  61474. TEST_DECL(test_wolfSSL_dtls_fragments),
  61475. /* Uses Assert in handshake callback. */
  61476. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  61477. /* Uses Assert in handshake callback. */
  61478. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  61479. /* Uses Assert in handshake callback. */
  61480. TEST_DECL(test_wolfSSL_dtls_bad_record),
  61481. /* Uses Assert in handshake callback. */
  61482. TEST_DECL(test_wolfSSL_dtls_stateless),
  61483. TEST_DECL(test_generate_cookie),
  61484. #ifndef NO_BIO
  61485. /* Can't memory test as server hangs. */
  61486. TEST_DECL(test_wolfSSL_BIO_connect),
  61487. /* Can't memory test as server Asserts in thread. */
  61488. TEST_DECL(test_wolfSSL_BIO_accept),
  61489. TEST_DECL(test_wolfSSL_BIO_tls),
  61490. #endif
  61491. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  61492. TEST_DECL(test_DhCallbacks),
  61493. #endif
  61494. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61495. TEST_DECL(test_export_keying_material),
  61496. #endif
  61497. /* Can't memory test as client/server Asserts in thread. */
  61498. TEST_DECL(test_ticket_and_psk_mixing),
  61499. /* Can't memory test as client/server Asserts in thread. */
  61500. TEST_DECL(test_prioritize_psk),
  61501. /* Can't memory test as client/server hangs. */
  61502. TEST_DECL(test_wc_CryptoCb),
  61503. /* Can't memory test as client/server hangs. */
  61504. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  61505. #if !defined(NO_FILESYSTEM) && \
  61506. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61507. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  61508. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  61509. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  61510. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  61511. #ifdef HAVE_MAX_FRAGMENT
  61512. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  61513. #endif /* HAVE_MAX_FRAGMENT */
  61514. #ifndef NO_RSA
  61515. TEST_DECL(test_wolfSSL_dtls_stateless2),
  61516. #if !defined(NO_OLD_TLS)
  61517. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  61518. #endif /* !defined(NO_OLD_TLS) */
  61519. #endif /* ! NO_RSA */
  61520. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61521. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  61522. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  61523. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  61524. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  61525. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  61526. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  61527. && defined(WOLFSSL_TLS13) && \
  61528. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  61529. TEST_DECL(test_ticket_nonce_malloc),
  61530. #endif
  61531. TEST_DECL(test_ticket_ret_create),
  61532. TEST_DECL(test_extra_alerts_wrong_cs),
  61533. TEST_DECL(test_extra_alerts_skip_hs),
  61534. TEST_DECL(test_extra_alerts_bad_psk),
  61535. /* Can't memory test as client/server Asserts. */
  61536. TEST_DECL(test_harden_no_secure_renegotiation),
  61537. TEST_DECL(test_override_alt_cert_chain),
  61538. TEST_DECL(test_rpk_set_xxx_cert_type),
  61539. TEST_DECL(test_tls13_rpk_handshake),
  61540. TEST_DECL(test_dtls13_bad_epoch_ch),
  61541. TEST_DECL(test_short_session_id),
  61542. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  61543. /* Can't memory test as client/server hangs. */
  61544. TEST_DECL(test_dtls_msg_from_other_peer),
  61545. TEST_DECL(test_dtls_ipv6_check),
  61546. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  61547. TEST_DECL(test_dtls_no_extensions),
  61548. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  61549. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  61550. TEST_DECL(test_session_ticket_no_id),
  61551. TEST_DECL(test_session_ticket_hs_update),
  61552. TEST_DECL(test_dtls_downgrade_scr_server),
  61553. TEST_DECL(test_dtls_downgrade_scr),
  61554. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  61555. TEST_DECL(test_dtls_client_hello_timeout),
  61556. TEST_DECL(test_dtls_dropped_ccs),
  61557. TEST_DECL(test_certreq_sighash_algos),
  61558. TEST_DECL(test_revoked_loaded_int_cert),
  61559. TEST_DECL(test_dtls_frag_ch),
  61560. TEST_DECL(test_dtls13_frag_ch_pq),
  61561. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  61562. TEST_DECL(test_tls13_pq_groups),
  61563. TEST_DECL(test_dtls13_early_data),
  61564. /* This test needs to stay at the end to clean up any caches allocated. */
  61565. TEST_DECL(test_wolfSSL_Cleanup)
  61566. };
  61567. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  61568. static void TestSetup(void)
  61569. {
  61570. /* Stub, for now. Add common test setup code here. */
  61571. }
  61572. static void TestCleanup(void)
  61573. {
  61574. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  61575. /* Clear any errors added to the error queue during the test run. */
  61576. wolfSSL_ERR_clear_error();
  61577. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  61578. }
  61579. /* Print out all API test cases with numeric identifier.
  61580. */
  61581. void ApiTest_PrintTestCases(void)
  61582. {
  61583. int i;
  61584. printf("All Test Cases:\n");
  61585. for (i = 0; i < TEST_CASE_CNT; i++) {
  61586. printf("%3d: %s\n", i + 1, testCases[i].name);
  61587. }
  61588. }
  61589. /* Add test case with index to the list to run.
  61590. *
  61591. * @param [in] idx Index of test case to run starting at 1.
  61592. * @return 0 on success.
  61593. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  61594. */
  61595. int ApiTest_RunIdx(int idx)
  61596. {
  61597. if (idx < 1 || idx > TEST_CASE_CNT) {
  61598. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  61599. return BAD_FUNC_ARG;
  61600. }
  61601. testAll = 0;
  61602. testCases[idx-1].run = 1;
  61603. return 0;
  61604. }
  61605. /* Add test case with name to the list to run.
  61606. *
  61607. * @param [in] name Name of test case to run.
  61608. * @return 0 on success.
  61609. * @return BAD_FUNC_ARG when name is not a known test case name.
  61610. */
  61611. int ApiTest_RunName(char* name)
  61612. {
  61613. int i;
  61614. for (i = 0; i < TEST_CASE_CNT; i++) {
  61615. if (XSTRCMP(testCases[i].name, name) == 0) {
  61616. testAll = 0;
  61617. testCases[i].run = 1;
  61618. return 0;
  61619. }
  61620. }
  61621. printf("Test case name not found: %s\n", name);
  61622. printf("Use --list to see all test case names.\n");
  61623. return BAD_FUNC_ARG;
  61624. }
  61625. /* Converts the result code to a string.
  61626. *
  61627. * @param [in] res Test result code.
  61628. * @return String describing test result.
  61629. */
  61630. static const char* apitest_res_string(int res)
  61631. {
  61632. const char* str = "invalid result";
  61633. switch (res) {
  61634. case TEST_SUCCESS:
  61635. str = "passed";
  61636. break;
  61637. case TEST_FAIL:
  61638. str = "failed";
  61639. break;
  61640. case TEST_SKIPPED:
  61641. str = "skipped";
  61642. break;
  61643. }
  61644. return str;
  61645. }
  61646. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61647. static double gettime_secs(void)
  61648. #if defined(_MSC_VER) && defined(_WIN32)
  61649. {
  61650. /* there's no gettimeofday for Windows, so we'll use system time */
  61651. #define EPOCH_DIFF 11644473600LL
  61652. FILETIME currentFileTime;
  61653. GetSystemTimePreciseAsFileTime(&currentFileTime);
  61654. ULARGE_INTEGER uli = { 0, 0 };
  61655. uli.LowPart = currentFileTime.dwLowDateTime;
  61656. uli.HighPart = currentFileTime.dwHighDateTime;
  61657. /* Convert to seconds since Unix epoch */
  61658. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  61659. }
  61660. #else
  61661. {
  61662. struct timeval tv;
  61663. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  61664. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  61665. }
  61666. #endif
  61667. #endif
  61668. int ApiTest(void)
  61669. {
  61670. int i;
  61671. int ret;
  61672. int res = 0;
  61673. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61674. double timeDiff;
  61675. #endif
  61676. printf(" Begin API Tests\n");
  61677. fflush(stdout);
  61678. /* we must perform init and cleanup if not all tests are running */
  61679. if (!testAll) {
  61680. #ifdef WOLFCRYPT_ONLY
  61681. if (wolfCrypt_Init() != 0) {
  61682. printf("wolfCrypt Initialization failed\n");
  61683. res = 1;
  61684. }
  61685. #else
  61686. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  61687. printf("wolfSSL Initialization failed\n");
  61688. res = 1;
  61689. }
  61690. #endif
  61691. }
  61692. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61693. if (res == 0) {
  61694. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  61695. printf("failed to create tmp dir\n");
  61696. res = 1;
  61697. }
  61698. else {
  61699. tmpDirNameSet = 1;
  61700. }
  61701. }
  61702. #endif
  61703. if (res == 0) {
  61704. for (i = 0; i < TEST_CASE_CNT; ++i) {
  61705. EXPECT_DECLS;
  61706. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61707. currentTestName = testCases[i].name;
  61708. #endif
  61709. /* When not testing all cases then skip if not marked for running.
  61710. */
  61711. if (!testAll && !testCases[i].run) {
  61712. continue;
  61713. }
  61714. TestSetup();
  61715. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  61716. fflush(stdout);
  61717. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61718. timeDiff = gettime_secs();
  61719. #endif
  61720. ret = testCases[i].func();
  61721. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61722. timeDiff = gettime_secs() - timeDiff;
  61723. #endif
  61724. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  61725. if (ret != TEST_SKIPPED) {
  61726. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  61727. }
  61728. else
  61729. #endif
  61730. {
  61731. printf(" %s\n", apitest_res_string(ret));
  61732. }
  61733. fflush(stdout);
  61734. /* if return code is < 0 and not skipped then assert error */
  61735. Expect((ret > 0 || ret == TEST_SKIPPED),
  61736. ("Test failed\n"),
  61737. ("ret %d", ret));
  61738. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  61739. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  61740. TestCleanup();
  61741. }
  61742. }
  61743. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  61744. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  61745. wc_ecc_fp_free(); /* free per thread cache */
  61746. #endif
  61747. if (!testAll) {
  61748. #ifdef WOLFCRYPT_ONLY
  61749. wolfCrypt_Cleanup();
  61750. #else
  61751. wolfSSL_Cleanup();
  61752. #endif
  61753. }
  61754. (void)testDevId;
  61755. if (res != 0) {
  61756. printf("\nFAILURES:\n");
  61757. for (i = 0; i < TEST_CASE_CNT; ++i) {
  61758. if (testCases[i].fail) {
  61759. printf(" %3d: %s\n", i + 1, testCases[i].name);
  61760. }
  61761. }
  61762. printf("\n");
  61763. fflush(stdout);
  61764. }
  61765. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  61766. if (tmpDirNameSet) {
  61767. printf("\nBinary dumps of the memio streams can be found in the\n"
  61768. "%s directory. This can be imported into\n"
  61769. "Wireshark by transforming the file with\n"
  61770. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  61771. "And then loading test_output.dump.hex into Wireshark using\n"
  61772. "the \"Import from Hex Dump...\" option and selecting the\n"
  61773. "TCP encapsulation option.\n", tmpDirName);
  61774. }
  61775. #endif
  61776. printf(" End API Tests\n");
  61777. fflush(stdout);
  61778. return res;
  61779. }