api.c 2.4 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764667656676666767667686676966770667716677266773667746677566776667776677866779667806678166782667836678466785667866678766788667896679066791667926679366794667956679666797667986679966800668016680266803668046680566806668076680866809668106681166812668136681466815668166681766818668196682066821668226682366824668256682666827668286682966830668316683266833668346683566836668376683866839668406684166842668436684466845668466684766848668496685066851668526685366854668556685666857668586685966860668616686266863668646686566866668676686866869668706687166872668736687466875668766687766878668796688066881668826688366884668856688666887668886688966890668916689266893668946689566896668976689866899669006690166902669036690466905669066690766908669096691066911669126691366914669156691666917669186691966920669216692266923669246692566926669276692866929669306693166932669336693466935669366693766938669396694066941669426694366944669456694666947669486694966950669516695266953669546695566956669576695866959669606696166962669636696466965669666696766968669696697066971669726697366974669756697666977669786697966980669816698266983669846698566986669876698866989669906699166992669936699466995669966699766998669996700067001670026700367004670056700667007670086700967010670116701267013670146701567016670176701867019670206702167022670236702467025670266702767028670296703067031670326703367034670356703667037670386703967040670416704267043670446704567046670476704867049670506705167052670536705467055670566705767058670596706067061670626706367064670656706667067670686706967070670716707267073670746707567076670776707867079670806708167082670836708467085670866708767088670896709067091670926709367094670956709667097670986709967100671016710267103671046710567106671076710867109671106711167112671136711467115671166711767118671196712067121671226712367124671256712667127671286712967130671316713267133671346713567136671376713867139671406714167142671436714467145671466714767148671496715067151671526715367154671556715667157671586715967160671616716267163671646716567166671676716867169671706717167172671736717467175671766717767178671796718067181671826718367184671856718667187671886718967190671916719267193671946719567196671976719867199672006720167202672036720467205672066720767208672096721067211672126721367214672156721667217672186721967220672216722267223672246722567226672276722867229672306723167232672336723467235672366723767238672396724067241672426724367244672456724667247672486724967250672516725267253672546725567256672576725867259672606726167262672636726467265672666726767268672696727067271672726727367274672756727667277672786727967280672816728267283672846728567286672876728867289672906729167292672936729467295672966729767298672996730067301673026730367304673056730667307673086730967310673116731267313673146731567316673176731867319673206732167322673236732467325673266732767328673296733067331673326733367334673356733667337673386733967340673416734267343673446734567346673476734867349673506735167352673536735467355673566735767358673596736067361673626736367364673656736667367673686736967370673716737267373673746737567376673776737867379673806738167382673836738467385673866738767388673896739067391673926739367394673956739667397673986739967400674016740267403674046740567406674076740867409674106741167412674136741467415674166741767418674196742067421674226742367424674256742667427674286742967430674316743267433674346743567436674376743867439674406744167442674436744467445674466744767448674496745067451674526745367454674556745667457674586745967460674616746267463674646746567466674676746867469674706747167472674736747467475674766747767478674796748067481674826748367484674856748667487674886748967490674916749267493674946749567496674976749867499675006750167502675036750467505675066750767508675096751067511675126751367514675156751667517675186751967520675216752267523675246752567526675276752867529675306753167532675336753467535675366753767538675396754067541675426754367544675456754667547675486754967550675516755267553675546755567556675576755867559675606756167562675636756467565675666756767568675696757067571675726757367574675756757667577675786757967580675816758267583675846758567586675876758867589675906759167592675936759467595675966759767598675996760067601676026760367604676056760667607676086760967610676116761267613676146761567616676176761867619676206762167622676236762467625676266762767628676296763067631676326763367634676356763667637676386763967640676416764267643676446764567646676476764867649676506765167652676536765467655676566765767658676596766067661676626766367664676656766667667676686766967670676716767267673676746767567676676776767867679676806768167682676836768467685676866768767688676896769067691676926769367694676956769667697676986769967700677016770267703677046770567706677076770867709677106771167712677136771467715677166771767718677196772067721677226772367724677256772667727677286772967730677316773267733677346773567736677376773867739677406774167742677436774467745677466774767748677496775067751677526775367754677556775667757677586775967760677616776267763677646776567766677676776867769677706777167772677736777467775677766777767778677796778067781677826778367784677856778667787677886778967790677916779267793677946779567796677976779867799678006780167802678036780467805678066780767808678096781067811678126781367814678156781667817678186781967820678216782267823678246782567826678276782867829678306783167832678336783467835678366783767838678396784067841678426784367844678456784667847678486784967850678516785267853678546785567856678576785867859678606786167862678636786467865678666786767868678696787067871678726787367874678756787667877678786787967880678816788267883678846788567886678876788867889678906789167892678936789467895678966789767898678996790067901679026790367904679056790667907679086790967910679116791267913679146791567916679176791867919679206792167922679236792467925679266792767928679296793067931679326793367934679356793667937679386793967940679416794267943679446794567946679476794867949679506795167952679536795467955679566795767958679596796067961679626796367964679656796667967679686796967970679716797267973679746797567976679776797867979679806798167982679836798467985679866798767988679896799067991679926799367994679956799667997679986799968000680016800268003680046800568006680076800868009680106801168012680136801468015680166801768018680196802068021680226802368024680256802668027680286802968030680316803268033680346803568036680376803868039680406804168042680436804468045680466804768048680496805068051680526805368054680556805668057680586805968060680616806268063680646806568066680676806868069680706807168072680736807468075680766807768078680796808068081680826808368084680856808668087680886808968090680916809268093680946809568096680976809868099681006810168102681036810468105681066810768108681096811068111681126811368114681156811668117681186811968120681216812268123681246812568126681276812868129681306813168132681336813468135681366813768138681396814068141681426814368144681456814668147681486814968150681516815268153681546815568156681576815868159681606816168162681636816468165681666816768168681696817068171681726817368174681756817668177681786817968180681816818268183681846818568186681876818868189681906819168192681936819468195681966819768198681996820068201682026820368204682056820668207682086820968210682116821268213682146821568216682176821868219682206822168222682236822468225682266822768228682296823068231682326823368234682356823668237682386823968240682416824268243682446824568246682476824868249682506825168252682536825468255682566825768258682596826068261682626826368264682656826668267682686826968270682716827268273682746827568276682776827868279682806828168282682836828468285682866828768288682896829068291682926829368294682956829668297682986829968300683016830268303683046830568306683076830868309683106831168312683136831468315683166831768318683196832068321683226832368324683256832668327683286832968330683316833268333683346833568336683376833868339683406834168342683436834468345683466834768348683496835068351683526835368354683556835668357683586835968360683616836268363683646836568366683676836868369683706837168372683736837468375683766837768378683796838068381683826838368384683856838668387683886838968390683916839268393683946839568396683976839868399684006840168402684036840468405684066840768408684096841068411684126841368414684156841668417684186841968420684216842268423684246842568426684276842868429684306843168432684336843468435684366843768438684396844068441684426844368444684456844668447684486844968450684516845268453684546845568456684576845868459684606846168462684636846468465684666846768468684696847068471684726847368474684756847668477684786847968480684816848268483684846848568486684876848868489684906849168492684936849468495684966849768498684996850068501685026850368504685056850668507685086850968510685116851268513685146851568516685176851868519685206852168522685236852468525685266852768528685296853068531685326853368534685356853668537685386853968540685416854268543685446854568546685476854868549685506855168552685536855468555685566855768558685596856068561685626856368564685656856668567685686856968570685716857268573685746857568576685776857868579685806858168582685836858468585685866858768588685896859068591685926859368594685956859668597685986859968600686016860268603686046860568606686076860868609686106861168612686136861468615686166861768618686196862068621686226862368624686256862668627686286862968630686316863268633686346863568636686376863868639686406864168642686436864468645686466864768648686496865068651686526865368654686556865668657686586865968660686616866268663686646866568666686676866868669686706867168672686736867468675686766867768678686796868068681686826868368684686856868668687686886868968690686916869268693686946869568696686976869868699687006870168702687036870468705687066870768708687096871068711687126871368714687156871668717687186871968720687216872268723687246872568726687276872868729687306873168732687336873468735687366873768738687396874068741687426874368744687456874668747687486874968750687516875268753687546875568756687576875868759687606876168762687636876468765687666876768768687696877068771687726877368774687756877668777687786877968780687816878268783687846878568786687876878868789687906879168792687936879468795687966879768798687996880068801688026880368804688056880668807688086880968810688116881268813688146881568816688176881868819688206882168822688236882468825688266882768828688296883068831688326883368834688356883668837688386883968840688416884268843688446884568846688476884868849688506885168852688536885468855688566885768858688596886068861688626886368864688656886668867688686886968870688716887268873688746887568876688776887868879688806888168882688836888468885688866888768888688896889068891688926889368894688956889668897688986889968900689016890268903689046890568906689076890868909689106891168912689136891468915689166891768918689196892068921689226892368924689256892668927689286892968930689316893268933689346893568936689376893868939689406894168942689436894468945689466894768948689496895068951689526895368954689556895668957689586895968960689616896268963689646896568966689676896868969689706897168972689736897468975689766897768978689796898068981689826898368984689856898668987689886898968990689916899268993689946899568996689976899868999690006900169002690036900469005690066900769008690096901069011690126901369014690156901669017690186901969020690216902269023690246902569026690276902869029690306903169032690336903469035690366903769038690396904069041690426904369044690456904669047690486904969050690516905269053690546905569056690576905869059690606906169062690636906469065690666906769068690696907069071690726907369074690756907669077690786907969080690816908269083690846908569086690876908869089690906909169092690936909469095690966909769098690996910069101691026910369104691056910669107691086910969110691116911269113691146911569116691176911869119691206912169122691236912469125691266912769128691296913069131691326913369134691356913669137691386913969140691416914269143691446914569146691476914869149691506915169152691536915469155691566915769158691596916069161691626916369164691656916669167691686916969170691716917269173691746917569176691776917869179691806918169182691836918469185691866918769188691896919069191691926919369194691956919669197691986919969200692016920269203692046920569206692076920869209692106921169212692136921469215692166921769218692196922069221692226922369224692256922669227692286922969230692316923269233692346923569236692376923869239692406924169242692436924469245692466924769248692496925069251692526925369254692556925669257692586925969260692616926269263692646926569266692676926869269692706927169272692736927469275692766927769278692796928069281692826928369284692856928669287692886928969290692916929269293692946929569296692976929869299693006930169302693036930469305693066930769308693096931069311693126931369314693156931669317693186931969320693216932269323693246932569326693276932869329693306933169332693336933469335693366933769338693396934069341693426934369344693456934669347693486934969350693516935269353693546935569356693576935869359693606936169362693636936469365693666936769368693696937069371693726937369374693756937669377693786937969380693816938269383693846938569386693876938869389693906939169392693936939469395693966939769398693996940069401694026940369404694056940669407694086940969410694116941269413694146941569416694176941869419694206942169422694236942469425694266942769428694296943069431694326943369434694356943669437694386943969440694416944269443694446944569446694476944869449694506945169452694536945469455694566945769458694596946069461694626946369464694656946669467694686946969470694716947269473694746947569476694776947869479694806948169482694836948469485694866948769488694896949069491694926949369494694956949669497694986949969500695016950269503695046950569506695076950869509695106951169512695136951469515695166951769518695196952069521695226952369524695256952669527695286952969530695316953269533695346953569536695376953869539695406954169542695436954469545695466954769548695496955069551695526955369554695556955669557695586955969560695616956269563695646956569566695676956869569695706957169572695736957469575695766957769578695796958069581695826958369584695856958669587695886958969590695916959269593695946959569596695976959869599696006960169602696036960469605696066960769608696096961069611696126961369614696156961669617696186961969620696216962269623696246962569626696276962869629696306963169632696336963469635696366963769638696396964069641696426964369644696456964669647696486964969650696516965269653696546965569656696576965869659696606966169662696636966469665696666966769668696696967069671696726967369674696756967669677696786967969680696816968269683696846968569686696876968869689696906969169692696936969469695696966969769698696996970069701697026970369704697056970669707697086970969710697116971269713697146971569716697176971869719697206972169722697236972469725697266972769728697296973069731697326973369734697356973669737697386973969740697416974269743697446974569746697476974869749697506975169752697536975469755697566975769758697596976069761697626976369764697656976669767697686976969770697716977269773697746977569776697776977869779697806978169782697836978469785697866978769788697896979069791697926979369794697956979669797697986979969800698016980269803698046980569806698076980869809698106981169812698136981469815698166981769818698196982069821698226982369824698256982669827698286982969830698316983269833698346983569836698376983869839698406984169842698436984469845698466984769848698496985069851698526985369854698556985669857698586985969860698616986269863698646986569866698676986869869698706987169872698736987469875698766987769878698796988069881698826988369884698856988669887698886988969890698916989269893698946989569896698976989869899699006990169902699036990469905699066990769908699096991069911699126991369914699156991669917699186991969920699216992269923699246992569926699276992869929699306993169932699336993469935699366993769938699396994069941699426994369944699456994669947699486994969950699516995269953699546995569956699576995869959699606996169962699636996469965699666996769968699696997069971699726997369974699756997669977699786997969980699816998269983699846998569986699876998869989699906999169992699936999469995699966999769998699997000070001700027000370004700057000670007700087000970010700117001270013700147001570016700177001870019700207002170022700237002470025700267002770028700297003070031700327003370034700357003670037700387003970040700417004270043700447004570046700477004870049700507005170052700537005470055700567005770058700597006070061700627006370064700657006670067700687006970070700717007270073700747007570076700777007870079700807008170082700837008470085700867008770088700897009070091700927009370094700957009670097700987009970100701017010270103701047010570106701077010870109701107011170112701137011470115701167011770118701197012070121701227012370124701257012670127701287012970130701317013270133701347013570136701377013870139701407014170142701437014470145701467014770148701497015070151701527015370154701557015670157701587015970160701617016270163701647016570166701677016870169701707017170172701737017470175701767017770178701797018070181701827018370184701857018670187701887018970190701917019270193701947019570196701977019870199702007020170202702037020470205702067020770208702097021070211702127021370214702157021670217702187021970220702217022270223702247022570226702277022870229702307023170232702337023470235702367023770238702397024070241702427024370244702457024670247702487024970250702517025270253702547025570256702577025870259702607026170262702637026470265702667026770268702697027070271702727027370274702757027670277702787027970280702817028270283702847028570286702877028870289702907029170292702937029470295702967029770298702997030070301703027030370304703057030670307703087030970310703117031270313703147031570316703177031870319703207032170322703237032470325703267032770328703297033070331703327033370334703357033670337703387033970340703417034270343703447034570346703477034870349703507035170352703537035470355703567035770358703597036070361703627036370364703657036670367703687036970370703717037270373703747037570376703777037870379703807038170382703837038470385703867038770388703897039070391703927039370394703957039670397703987039970400704017040270403704047040570406704077040870409704107041170412704137041470415704167041770418704197042070421704227042370424704257042670427704287042970430704317043270433704347043570436704377043870439704407044170442704437044470445704467044770448704497045070451704527045370454704557045670457704587045970460704617046270463704647046570466704677046870469704707047170472704737047470475704767047770478704797048070481704827048370484704857048670487704887048970490704917049270493704947049570496704977049870499705007050170502705037050470505705067050770508705097051070511705127051370514705157051670517705187051970520705217052270523705247052570526705277052870529705307053170532705337053470535705367053770538705397054070541705427054370544705457054670547705487054970550705517055270553705547055570556705577055870559705607056170562705637056470565705667056770568705697057070571705727057370574705757057670577705787057970580705817058270583705847058570586705877058870589705907059170592705937059470595705967059770598705997060070601706027060370604706057060670607706087060970610706117061270613706147061570616706177061870619706207062170622706237062470625706267062770628706297063070631706327063370634706357063670637706387063970640706417064270643706447064570646706477064870649706507065170652706537065470655706567065770658706597066070661706627066370664706657066670667706687066970670706717067270673706747067570676706777067870679706807068170682706837068470685706867068770688706897069070691706927069370694706957069670697706987069970700707017070270703707047070570706707077070870709707107071170712707137071470715707167071770718707197072070721707227072370724707257072670727707287072970730707317073270733707347073570736707377073870739707407074170742707437074470745707467074770748707497075070751707527075370754707557075670757707587075970760707617076270763707647076570766707677076870769707707077170772707737077470775707767077770778707797078070781707827078370784707857078670787707887078970790707917079270793707947079570796707977079870799708007080170802708037080470805708067080770808708097081070811708127081370814708157081670817708187081970820708217082270823708247082570826708277082870829708307083170832708337083470835708367083770838708397084070841708427084370844708457084670847708487084970850708517085270853708547085570856708577085870859708607086170862708637086470865708667086770868708697087070871708727087370874708757087670877708787087970880708817088270883708847088570886708877088870889708907089170892708937089470895708967089770898708997090070901709027090370904709057090670907709087090970910709117091270913709147091570916709177091870919709207092170922709237092470925709267092770928709297093070931709327093370934709357093670937709387093970940709417094270943709447094570946709477094870949709507095170952709537095470955709567095770958709597096070961709627096370964709657096670967709687096970970709717097270973709747097570976709777097870979709807098170982709837098470985709867098770988709897099070991709927099370994709957099670997709987099971000710017100271003710047100571006710077100871009710107101171012710137101471015710167101771018710197102071021710227102371024710257102671027710287102971030710317103271033710347103571036710377103871039710407104171042710437104471045710467104771048710497105071051710527105371054710557105671057710587105971060710617106271063710647106571066710677106871069710707107171072710737107471075710767107771078710797108071081710827108371084710857108671087710887108971090710917109271093710947109571096710977109871099711007110171102711037110471105711067110771108711097111071111711127111371114711157111671117711187111971120711217112271123711247112571126711277112871129711307113171132711337113471135711367113771138711397114071141711427114371144711457114671147711487114971150711517115271153711547115571156711577115871159711607116171162711637116471165711667116771168711697117071171711727117371174711757117671177711787117971180711817118271183711847118571186711877118871189711907119171192711937119471195711967119771198711997120071201712027120371204712057120671207712087120971210712117121271213712147121571216712177121871219712207122171222712237122471225712267122771228712297123071231712327123371234712357123671237712387123971240712417124271243712447124571246712477124871249712507125171252712537125471255712567125771258712597126071261712627126371264712657126671267712687126971270712717127271273712747127571276712777127871279712807128171282712837128471285712867128771288712897129071291712927129371294712957129671297712987129971300713017130271303713047130571306713077130871309713107131171312713137131471315713167131771318713197132071321713227132371324713257132671327713287132971330713317133271333713347133571336713377133871339713407134171342713437134471345713467134771348713497135071351713527135371354713557135671357713587135971360713617136271363713647136571366713677136871369713707137171372713737137471375713767137771378713797138071381713827138371384713857138671387713887138971390713917139271393713947139571396713977139871399714007140171402714037140471405714067140771408714097141071411714127141371414714157141671417714187141971420714217142271423714247142571426714277142871429714307143171432714337143471435714367143771438714397144071441714427144371444714457144671447714487144971450714517145271453714547145571456714577145871459714607146171462714637146471465714667146771468714697147071471714727147371474714757147671477714787147971480714817148271483714847148571486714877148871489714907149171492714937149471495714967149771498714997150071501715027150371504715057150671507715087150971510715117151271513715147151571516715177151871519715207152171522715237152471525715267152771528715297153071531715327153371534715357153671537715387153971540715417154271543715447154571546715477154871549715507155171552715537155471555715567155771558715597156071561715627156371564715657156671567715687156971570715717157271573715747157571576715777157871579715807158171582715837158471585715867158771588715897159071591715927159371594715957159671597715987159971600716017160271603716047160571606716077160871609716107161171612716137161471615716167161771618716197162071621716227162371624716257162671627716287162971630716317163271633716347163571636716377163871639716407164171642716437164471645716467164771648716497165071651716527165371654716557165671657716587165971660716617166271663716647166571666716677166871669716707167171672716737167471675716767167771678716797168071681716827168371684716857168671687716887168971690716917169271693716947169571696716977169871699717007170171702717037170471705717067170771708717097171071711717127171371714717157171671717717187171971720717217172271723717247172571726717277172871729717307173171732717337173471735717367173771738717397174071741717427174371744717457174671747717487174971750717517175271753717547175571756717577175871759717607176171762717637176471765717667176771768717697177071771717727177371774717757177671777717787177971780717817178271783717847178571786717877178871789717907179171792717937179471795717967179771798717997180071801718027180371804718057180671807718087180971810718117181271813718147181571816718177181871819718207182171822718237182471825718267182771828718297183071831718327183371834718357183671837718387183971840718417184271843718447184571846718477184871849718507185171852718537185471855718567185771858718597186071861718627186371864718657186671867718687186971870718717187271873718747187571876718777187871879718807188171882718837188471885718867188771888718897189071891718927189371894718957189671897718987189971900719017190271903719047190571906719077190871909719107191171912719137191471915719167191771918719197192071921719227192371924719257192671927719287192971930719317193271933719347193571936719377193871939719407194171942719437194471945719467194771948719497195071951719527195371954719557195671957719587195971960719617196271963719647196571966719677196871969719707197171972719737197471975719767197771978719797198071981719827198371984719857198671987719887198971990719917199271993719947199571996719977199871999720007200172002720037200472005720067200772008720097201072011720127201372014720157201672017720187201972020720217202272023720247202572026720277202872029720307203172032720337203472035720367203772038720397204072041720427204372044720457204672047720487204972050720517205272053720547205572056720577205872059720607206172062720637206472065720667206772068720697207072071720727207372074720757207672077720787207972080720817208272083720847208572086720877208872089720907209172092720937209472095720967209772098720997210072101721027210372104721057210672107721087210972110721117211272113721147211572116721177211872119721207212172122721237212472125721267212772128721297213072131721327213372134721357213672137721387213972140721417214272143721447214572146721477214872149721507215172152721537215472155721567215772158721597216072161721627216372164721657216672167721687216972170721717217272173721747217572176721777217872179721807218172182721837218472185721867218772188721897219072191721927219372194721957219672197721987219972200722017220272203722047220572206722077220872209722107221172212722137221472215722167221772218722197222072221722227222372224722257222672227722287222972230722317223272233722347223572236722377223872239722407224172242722437224472245722467224772248722497225072251722527225372254722557225672257722587225972260722617226272263722647226572266722677226872269722707227172272722737227472275722767227772278722797228072281722827228372284722857228672287722887228972290722917229272293722947229572296722977229872299723007230172302723037230472305723067230772308723097231072311723127231372314723157231672317723187231972320723217232272323723247232572326723277232872329723307233172332723337233472335723367233772338723397234072341723427234372344723457234672347723487234972350723517235272353723547235572356723577235872359723607236172362723637236472365723667236772368723697237072371723727237372374723757237672377723787237972380723817238272383723847238572386723877238872389723907239172392723937239472395723967239772398723997240072401724027240372404724057240672407724087240972410724117241272413724147241572416724177241872419724207242172422724237242472425724267242772428724297243072431724327243372434724357243672437724387243972440724417244272443724447244572446724477244872449724507245172452724537245472455724567245772458724597246072461724627246372464724657246672467724687246972470724717247272473724747247572476724777247872479724807248172482724837248472485724867248772488724897249072491724927249372494724957249672497724987249972500725017250272503725047250572506725077250872509725107251172512725137251472515725167251772518725197252072521725227252372524725257252672527725287252972530725317253272533725347253572536725377253872539725407254172542725437254472545725467254772548725497255072551725527255372554725557255672557725587255972560725617256272563725647256572566725677256872569725707257172572725737257472575725767257772578725797258072581725827258372584725857258672587725887258972590725917259272593725947259572596725977259872599726007260172602726037260472605726067260772608726097261072611726127261372614726157261672617726187261972620726217262272623726247262572626726277262872629726307263172632726337263472635726367263772638726397264072641726427264372644726457264672647726487264972650726517265272653726547265572656726577265872659726607266172662726637266472665726667266772668726697267072671726727267372674726757267672677726787267972680726817268272683726847268572686726877268872689726907269172692726937269472695726967269772698726997270072701727027270372704727057270672707727087270972710727117271272713727147271572716727177271872719727207272172722727237272472725727267272772728727297273072731727327273372734727357273672737727387273972740727417274272743727447274572746727477274872749727507275172752727537275472755727567275772758727597276072761727627276372764727657276672767727687276972770727717277272773727747277572776727777277872779727807278172782727837278472785727867278772788727897279072791727927279372794727957279672797727987279972800728017280272803728047280572806728077280872809728107281172812728137281472815728167281772818728197282072821728227282372824728257282672827728287282972830728317283272833728347283572836728377283872839728407284172842728437284472845728467284772848728497285072851728527285372854728557285672857728587285972860728617286272863728647286572866728677286872869728707287172872728737287472875728767287772878728797288072881728827288372884728857288672887728887288972890728917289272893728947289572896728977289872899729007290172902729037290472905729067290772908729097291072911729127291372914729157291672917729187291972920729217292272923729247292572926729277292872929729307293172932729337293472935729367293772938729397294072941729427294372944729457294672947729487294972950729517295272953729547295572956729577295872959729607296172962729637296472965729667296772968729697297072971729727297372974729757297672977729787297972980729817298272983729847298572986729877298872989729907299172992729937299472995729967299772998729997300073001730027300373004730057300673007730087300973010730117301273013730147301573016730177301873019730207302173022730237302473025730267302773028730297303073031730327303373034730357303673037730387303973040730417304273043730447304573046730477304873049730507305173052730537305473055730567305773058730597306073061730627306373064730657306673067730687306973070730717307273073730747307573076730777307873079730807308173082730837308473085730867308773088730897309073091730927309373094730957309673097730987309973100731017310273103731047310573106731077310873109
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #endif
  215. #ifndef NO_SIG_WRAPPER
  216. #include <wolfssl/wolfcrypt/signature.h>
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. #include <wolfssl/wolfcrypt/aes.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #ifdef HAVE_LIBZ
  225. #include <wolfssl/wolfcrypt/compress.h>
  226. #endif
  227. #endif
  228. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  229. #include <wolfssl/wolfcrypt/asn.h>
  230. #endif
  231. #ifndef NO_DSA
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #ifndef ONEK_BUF
  234. #define ONEK_BUF 1024
  235. #endif
  236. #ifndef TWOK_BUF
  237. #define TWOK_BUF 2048
  238. #endif
  239. #ifndef FOURK_BUF
  240. #define FOURK_BUF 4096
  241. #endif
  242. #ifndef DSA_SIG_SIZE
  243. #define DSA_SIG_SIZE 40
  244. #endif
  245. #ifndef MAX_DSA_PARAM_SIZE
  246. #define MAX_DSA_PARAM_SIZE 256
  247. #endif
  248. #endif
  249. #ifdef WOLFSSL_CMAC
  250. #include <wolfssl/wolfcrypt/cmac.h>
  251. #endif
  252. #ifdef HAVE_ED25519
  253. #include <wolfssl/wolfcrypt/ed25519.h>
  254. #endif
  255. #ifdef HAVE_CURVE25519
  256. #include <wolfssl/wolfcrypt/curve25519.h>
  257. #endif
  258. #ifdef HAVE_ED448
  259. #include <wolfssl/wolfcrypt/ed448.h>
  260. #endif
  261. #ifdef HAVE_CURVE448
  262. #include <wolfssl/wolfcrypt/curve448.h>
  263. #endif
  264. #ifdef HAVE_PKCS12
  265. #include <wolfssl/wolfcrypt/pkcs12.h>
  266. #endif
  267. #include <wolfssl/wolfcrypt/logging.h>
  268. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  269. #include <wolfssl/openssl/ssl.h>
  270. #ifndef NO_ASN
  271. /* for ASN_COMMON_NAME DN_tags enum */
  272. #include <wolfssl/wolfcrypt/asn.h>
  273. #endif
  274. #ifdef HAVE_OCSP
  275. #include <wolfssl/openssl/ocsp.h>
  276. #endif
  277. #endif
  278. #ifdef OPENSSL_EXTRA
  279. #include <wolfssl/openssl/cmac.h>
  280. #include <wolfssl/openssl/x509v3.h>
  281. #include <wolfssl/openssl/asn1.h>
  282. #include <wolfssl/openssl/crypto.h>
  283. #include <wolfssl/openssl/pkcs12.h>
  284. #include <wolfssl/openssl/evp.h>
  285. #include <wolfssl/openssl/dh.h>
  286. #include <wolfssl/openssl/bn.h>
  287. #include <wolfssl/openssl/buffer.h>
  288. #include <wolfssl/openssl/pem.h>
  289. #include <wolfssl/openssl/ec.h>
  290. #include <wolfssl/openssl/ecdh.h>
  291. #include <wolfssl/openssl/engine.h>
  292. #include <wolfssl/openssl/hmac.h>
  293. #include <wolfssl/openssl/objects.h>
  294. #include <wolfssl/openssl/rand.h>
  295. #include <wolfssl/openssl/modes.h>
  296. #include <wolfssl/openssl/fips_rand.h>
  297. #include <wolfssl/openssl/kdf.h>
  298. #ifdef OPENSSL_ALL
  299. #include <wolfssl/openssl/txt_db.h>
  300. #include <wolfssl/openssl/lhash.h>
  301. #endif
  302. #ifndef NO_AES
  303. #include <wolfssl/openssl/aes.h>
  304. #endif
  305. #ifndef NO_DES3
  306. #include <wolfssl/openssl/des.h>
  307. #endif
  308. #ifndef NO_RC4
  309. #include <wolfssl/openssl/rc4.h>
  310. #endif
  311. #ifdef HAVE_ECC
  312. #include <wolfssl/openssl/ecdsa.h>
  313. #endif
  314. #ifdef HAVE_PKCS7
  315. #include <wolfssl/openssl/pkcs7.h>
  316. #endif
  317. #ifdef HAVE_CURVE25519
  318. #include <wolfssl/openssl/ec25519.h>
  319. #endif
  320. #ifdef HAVE_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_CURVE448
  324. #include <wolfssl/openssl/ec448.h>
  325. #endif
  326. #ifdef HAVE_ED448
  327. #include <wolfssl/openssl/ed448.h>
  328. #endif
  329. #endif /* OPENSSL_EXTRA */
  330. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  331. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  332. #include <wolfssl/wolfcrypt/srp.h>
  333. #endif
  334. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  335. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  336. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  337. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  338. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  339. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  340. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  341. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  342. * number tracking */
  343. #include "wolfssl/internal.h"
  344. #endif
  345. /* force enable test buffers */
  346. #ifndef USE_CERT_BUFFERS_2048
  347. #define USE_CERT_BUFFERS_2048
  348. #endif
  349. #ifndef USE_CERT_BUFFERS_256
  350. #define USE_CERT_BUFFERS_256
  351. #endif
  352. #include <wolfssl/certs_test.h>
  353. #include "tests/utils.h"
  354. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  355. * have default (hidden) visibility, and in the absence of visibility, it's
  356. * benign to mask out the library implementation.
  357. */
  358. #define WOLFSSL_MISC_INCLUDED
  359. #include <wolfcrypt/src/misc.c>
  360. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. /* FIPS build has replaced ecc.h. */
  362. #define wc_ecc_key_get_priv(key) (&((key)->k))
  363. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  364. #endif
  365. typedef struct testVector {
  366. const char* input;
  367. const char* output;
  368. size_t inLen;
  369. size_t outLen;
  370. } testVector;
  371. #if defined(HAVE_PKCS7)
  372. typedef struct {
  373. const byte* content;
  374. word32 contentSz;
  375. int contentOID;
  376. int encryptOID;
  377. int keyWrapOID;
  378. int keyAgreeOID;
  379. byte* cert;
  380. size_t certSz;
  381. byte* privateKey;
  382. word32 privateKeySz;
  383. } pkcs7EnvelopedVector;
  384. #ifndef NO_PKCS7_ENCRYPTED_DATA
  385. typedef struct {
  386. const byte* content;
  387. word32 contentSz;
  388. int contentOID;
  389. int encryptOID;
  390. byte* encryptionKey;
  391. word32 encryptionKeySz;
  392. } pkcs7EncryptedVector;
  393. #endif
  394. #endif /* HAVE_PKCS7 */
  395. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  396. typedef int (*ssl_cb)(WOLFSSL* ssl);
  397. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  398. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  399. typedef struct test_ssl_cbf {
  400. method_provider method;
  401. ctx_cb ctx_ready;
  402. ssl_cb ssl_ready;
  403. ssl_cb on_result;
  404. ctx_cb on_ctx_cleanup;
  405. ssl_cb on_cleanup;
  406. hs_cb on_handshake;
  407. WOLFSSL_CTX* ctx;
  408. const char* caPemFile;
  409. const char* certPemFile;
  410. const char* keyPemFile;
  411. const char* crlPemFile;
  412. #ifdef WOLFSSL_STATIC_MEMORY
  413. byte* mem;
  414. word32 memSz;
  415. wolfSSL_method_func method_ex;
  416. #endif
  417. int devId;
  418. int return_code;
  419. int last_err;
  420. unsigned char isSharedCtx:1;
  421. unsigned char loadToSSL:1;
  422. unsigned char ticNoInit:1;
  423. unsigned char doUdp:1;
  424. } test_ssl_cbf;
  425. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  426. typedef struct test_ssl_memio_ctx {
  427. WOLFSSL_CTX* s_ctx;
  428. WOLFSSL_CTX* c_ctx;
  429. WOLFSSL* s_ssl;
  430. WOLFSSL* c_ssl;
  431. const char* c_ciphers;
  432. const char* s_ciphers;
  433. char* c_msg;
  434. int c_msglen;
  435. char* s_msg;
  436. int s_msglen;
  437. test_ssl_cbf s_cb;
  438. test_ssl_cbf c_cb;
  439. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  440. int c_len;
  441. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  442. int s_len;
  443. } test_ssl_memio_ctx;
  444. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  445. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  446. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  447. const char* currentTestName;
  448. char tmpDirName[16];
  449. int tmpDirNameSet = 0;
  450. #endif
  451. /*----------------------------------------------------------------------------*
  452. | Constants
  453. *----------------------------------------------------------------------------*/
  454. /* Test result constants and macros. */
  455. /* Test succeeded. */
  456. #define TEST_SUCCESS (1)
  457. /* Test failed. */
  458. #define TEST_FAIL (0)
  459. /* Test skipped - not run. */
  460. #define TEST_SKIPPED (-7777)
  461. /* Returns the result based on whether check is true.
  462. *
  463. * @param [in] check Condition for success.
  464. * @return When condition is true: TEST_SUCCESS.
  465. * @return When condition is false: TEST_FAIL.
  466. */
  467. #ifdef DEBUG_WOLFSSL_VERBOSE
  468. #define XSTRINGIFY(s) STRINGIFY(s)
  469. #define STRINGIFY(s) #s
  470. #define TEST_RES_CHECK(check) ({ \
  471. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  472. if (_ret == TEST_FAIL) { \
  473. fprintf(stderr, " check \"%s\" at %d ", \
  474. XSTRINGIFY(check), __LINE__); \
  475. } \
  476. _ret; })
  477. #else
  478. #define TEST_RES_CHECK(check) \
  479. ((check) ? TEST_SUCCESS : TEST_FAIL)
  480. #endif /* DEBUG_WOLFSSL_VERBOSE */
  481. #define TEST_STRING "Everyone gets Friday off."
  482. #define TEST_STRING_SZ 25
  483. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  484. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  485. #define TEST_RSA_BITS 1024
  486. #else
  487. #define TEST_RSA_BITS 2048
  488. #endif
  489. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  490. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  491. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  492. static const char* bogusFile =
  493. #ifdef _WIN32
  494. "NUL"
  495. #else
  496. "/dev/null"
  497. #endif
  498. ;
  499. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  500. enum {
  501. TESTING_RSA = 1,
  502. TESTING_ECC = 2
  503. };
  504. #ifdef WOLFSSL_QNX_CAAM
  505. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  506. static int testDevId = WOLFSSL_CAAM_DEVID;
  507. #else
  508. static int testDevId = INVALID_DEVID;
  509. #endif
  510. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  511. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  512. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  513. #define HAVE_IO_TESTS_DEPENDENCIES
  514. #endif
  515. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  516. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  517. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  518. #endif
  519. /*----------------------------------------------------------------------------*
  520. | BIO with fixed read/write size
  521. *----------------------------------------------------------------------------*/
  522. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  523. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  524. int len)
  525. {
  526. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  527. len = 0;
  528. }
  529. else {
  530. if (bio->wrSz - bio->wrIdx < len) {
  531. len = bio->wrSz - bio->wrIdx;
  532. }
  533. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  534. bio->wrIdx += len;
  535. }
  536. return len;
  537. }
  538. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  539. {
  540. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  541. len = 0;
  542. }
  543. else {
  544. if (bio->wrSz - bio->rdIdx < len) {
  545. len = bio->wrSz - bio->rdIdx;
  546. }
  547. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  548. bio->rdIdx += len;
  549. }
  550. return len;
  551. }
  552. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  553. {
  554. static WOLFSSL_BIO_METHOD meth;
  555. meth.type = WOLFSSL_BIO_BIO;
  556. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  557. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  558. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  559. return &meth;
  560. }
  561. #endif
  562. /*----------------------------------------------------------------------------*
  563. | Setup
  564. *----------------------------------------------------------------------------*/
  565. static int test_wolfSSL_Init(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. static int test_wolfSSL_Cleanup(void)
  572. {
  573. EXPECT_DECLS;
  574. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  575. return EXPECT_RESULT();
  576. }
  577. /* Initialize the wolfCrypt state.
  578. * POST: 0 success.
  579. */
  580. static int test_wolfCrypt_Init(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Init(), 0);
  584. return EXPECT_RESULT();
  585. } /* END test_wolfCrypt_Init */
  586. static int test_wolfCrypt_Cleanup(void)
  587. {
  588. EXPECT_DECLS;
  589. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  590. return EXPECT_RESULT();
  591. }
  592. /*----------------------------------------------------------------------------*
  593. | Platform dependent function test
  594. *----------------------------------------------------------------------------*/
  595. static int test_fileAccess(void)
  596. {
  597. EXPECT_DECLS;
  598. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  599. const char *fname[] = {
  600. svrCertFile, svrKeyFile, caCertFile,
  601. eccCertFile, eccKeyFile, eccRsaCertFile,
  602. cliCertFile, cliCertDerFile, cliKeyFile,
  603. dhParamFile,
  604. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  605. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  606. NULL
  607. };
  608. const char derfile[] = "./certs/server-cert.der";
  609. XFILE f = XBADFILE;
  610. size_t sz;
  611. byte *buff = NULL;
  612. int i;
  613. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  614. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  615. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  616. XFCLOSE(f);
  617. }
  618. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  619. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  620. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  621. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  622. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  623. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  624. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  625. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  626. XFCLOSE(f);
  627. #endif
  628. return EXPECT_RESULT();
  629. }
  630. /*----------------------------------------------------------------------------*
  631. | Method Allocators
  632. *----------------------------------------------------------------------------*/
  633. static int test_wolfSSL_Method_Allocators(void)
  634. {
  635. EXPECT_DECLS;
  636. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  637. do { \
  638. WOLFSSL_METHOD *method = NULL; \
  639. condition(method = allocator()); \
  640. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  641. } while (0)
  642. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  643. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  644. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  645. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  646. #ifndef NO_OLD_TLS
  647. #ifdef WOLFSSL_ALLOW_SSLV3
  648. #ifndef NO_WOLFSSL_SERVER
  649. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  650. #endif
  651. #ifndef NO_WOLFSSL_CLIENT
  652. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  653. #endif
  654. #endif
  655. #ifdef WOLFSSL_ALLOW_TLSV10
  656. #ifndef NO_WOLFSSL_SERVER
  657. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  658. #endif
  659. #ifndef NO_WOLFSSL_CLIENT
  660. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  661. #endif
  662. #endif
  663. #ifndef NO_WOLFSSL_SERVER
  664. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  665. #endif
  666. #ifndef NO_WOLFSSL_CLIENT
  667. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  668. #endif
  669. #endif /* !NO_OLD_TLS */
  670. #ifndef WOLFSSL_NO_TLS12
  671. #ifndef NO_WOLFSSL_SERVER
  672. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  673. #endif
  674. #ifndef NO_WOLFSSL_CLIENT
  675. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  676. #endif
  677. #endif /* !WOLFSSL_NO_TLS12 */
  678. #ifdef WOLFSSL_TLS13
  679. #ifndef NO_WOLFSSL_SERVER
  680. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  681. #endif
  682. #ifndef NO_WOLFSSL_CLIENT
  683. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  684. #endif
  685. #endif /* WOLFSSL_TLS13 */
  686. #ifndef NO_WOLFSSL_SERVER
  687. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  688. #endif
  689. #ifndef NO_WOLFSSL_CLIENT
  690. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  691. #endif
  692. #ifdef WOLFSSL_DTLS
  693. #ifndef NO_OLD_TLS
  694. #ifndef NO_WOLFSSL_SERVER
  695. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  696. #endif
  697. #ifndef NO_WOLFSSL_CLIENT
  698. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  699. #endif
  700. #endif
  701. #ifndef WOLFSSL_NO_TLS12
  702. #ifndef NO_WOLFSSL_SERVER
  703. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  704. #endif
  705. #ifndef NO_WOLFSSL_CLIENT
  706. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  707. #endif
  708. #endif
  709. #endif /* WOLFSSL_DTLS */
  710. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  711. /* Stubs */
  712. #ifndef NO_WOLFSSL_SERVER
  713. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  714. #endif
  715. #ifndef NO_WOLFSSL_CLIENT
  716. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  717. #endif
  718. #endif
  719. /* Test Either Method (client or server) */
  720. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  721. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  722. #ifndef NO_OLD_TLS
  723. #ifdef WOLFSSL_ALLOW_TLSV10
  724. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  725. #endif
  726. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  727. #endif /* !NO_OLD_TLS */
  728. #ifndef WOLFSSL_NO_TLS12
  729. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  730. #endif /* !WOLFSSL_NO_TLS12 */
  731. #ifdef WOLFSSL_TLS13
  732. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  733. #endif /* WOLFSSL_TLS13 */
  734. #ifdef WOLFSSL_DTLS
  735. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  736. #ifndef NO_OLD_TLS
  737. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  738. #endif /* !NO_OLD_TLS */
  739. #ifndef WOLFSSL_NO_TLS12
  740. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  741. #endif /* !WOLFSSL_NO_TLS12 */
  742. #endif /* WOLFSSL_DTLS */
  743. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  744. return EXPECT_RESULT();
  745. }
  746. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  747. /*----------------------------------------------------------------------------*
  748. | Dual algorithm Certificate Tests
  749. *----------------------------------------------------------------------------*/
  750. #define LARGE_TEMP_SZ 4096
  751. /* To better understand this, please see the X9.146 example in wolfssl-examples
  752. * repo. */
  753. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  754. char *sapkiFile, char *altPrivFile)
  755. {
  756. EXPECT_DECLS;
  757. FILE* file = NULL;
  758. Cert newCert;
  759. DecodedCert preTBS;
  760. byte caKeyBuf[LARGE_TEMP_SZ];
  761. word32 caKeySz = LARGE_TEMP_SZ;
  762. byte sapkiBuf[LARGE_TEMP_SZ];
  763. word32 sapkiSz = LARGE_TEMP_SZ;
  764. byte altPrivBuf[LARGE_TEMP_SZ];
  765. word32 altPrivSz = LARGE_TEMP_SZ;
  766. byte altSigAlgBuf[LARGE_TEMP_SZ];
  767. word32 altSigAlgSz = LARGE_TEMP_SZ;
  768. byte scratchBuf[LARGE_TEMP_SZ];
  769. word32 scratchSz = LARGE_TEMP_SZ;
  770. byte preTbsBuf[LARGE_TEMP_SZ];
  771. word32 preTbsSz = LARGE_TEMP_SZ;
  772. byte altSigValBuf[LARGE_TEMP_SZ];
  773. word32 altSigValSz = LARGE_TEMP_SZ;
  774. byte *outBuf = NULL;
  775. word32 outSz = LARGE_TEMP_SZ;
  776. WC_RNG rng;
  777. RsaKey caKey;
  778. ecc_key altCaKey;
  779. word32 idx = 0;
  780. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  781. DYNAMIC_TYPE_TMP_BUFFER));
  782. ExpectIntEQ(wc_InitRng(&rng), 0);
  783. XMEMSET(caKeyBuf, 0, caKeySz);
  784. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  785. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  786. if (file) {
  787. fclose(file);
  788. file = NULL;
  789. }
  790. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  791. idx = 0;
  792. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  793. 0);
  794. XMEMSET(sapkiBuf, 0, sapkiSz);
  795. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  796. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  797. if (file) {
  798. fclose(file);
  799. file = NULL;
  800. }
  801. XMEMSET(altPrivBuf, 0, altPrivSz);
  802. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  803. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  804. if (file) {
  805. fclose(file);
  806. file = NULL;
  807. }
  808. wc_ecc_init(&altCaKey);
  809. idx = 0;
  810. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  811. (word32)altPrivSz), 0);
  812. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  813. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  814. oidSigType, 0), 0);
  815. wc_InitCert(&newCert);
  816. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  817. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  818. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  819. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  820. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  821. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  822. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  823. newCert.sigType = CTC_SHA256wRSA;
  824. newCert.isCA = 1;
  825. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  826. (const byte *)"This is NOT a critical extension", 32), 0);
  827. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  828. sapkiSz), 0);
  829. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  830. altSigAlgSz), 0);
  831. XMEMSET(scratchBuf, 0, scratchSz);
  832. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  833. &caKey, &rng), 0);
  834. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  835. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  836. XMEMSET(preTbsBuf, 0, preTbsSz);
  837. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  838. XMEMSET(altSigValBuf, 0, altSigValSz);
  839. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  840. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  841. &rng), 0);
  842. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  843. altSigValSz), 0);
  844. /* Finally, generate the new certificate. */
  845. XMEMSET(outBuf, 0, outSz);
  846. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  847. 0);
  848. *out = outBuf;
  849. wc_FreeRsaKey(&caKey);
  850. wc_FreeRng(&rng);
  851. wc_FreeDecodedCert(&preTBS);
  852. return outSz;
  853. }
  854. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  855. char *sapkiFile, char *altPrivFile,
  856. char *serverKeyFile,
  857. byte *caCertBuf, int caCertSz)
  858. {
  859. EXPECT_DECLS;
  860. FILE* file = NULL;
  861. Cert newCert;
  862. DecodedCert preTBS;
  863. byte serverKeyBuf[LARGE_TEMP_SZ];
  864. word32 serverKeySz = LARGE_TEMP_SZ;
  865. byte caKeyBuf[LARGE_TEMP_SZ];
  866. word32 caKeySz = LARGE_TEMP_SZ;
  867. byte sapkiBuf[LARGE_TEMP_SZ];
  868. word32 sapkiSz = LARGE_TEMP_SZ;
  869. byte altPrivBuf[LARGE_TEMP_SZ];
  870. word32 altPrivSz = LARGE_TEMP_SZ;
  871. byte altSigAlgBuf[LARGE_TEMP_SZ];
  872. word32 altSigAlgSz = LARGE_TEMP_SZ;
  873. byte scratchBuf[LARGE_TEMP_SZ];
  874. word32 scratchSz = LARGE_TEMP_SZ;
  875. byte preTbsBuf[LARGE_TEMP_SZ];
  876. word32 preTbsSz = LARGE_TEMP_SZ;
  877. byte altSigValBuf[LARGE_TEMP_SZ];
  878. word32 altSigValSz = LARGE_TEMP_SZ;
  879. byte *outBuf = NULL;
  880. word32 outSz = LARGE_TEMP_SZ;
  881. WC_RNG rng;
  882. RsaKey caKey;
  883. RsaKey serverKey;
  884. ecc_key altCaKey;
  885. word32 idx = 0;
  886. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  887. DYNAMIC_TYPE_TMP_BUFFER));
  888. ExpectIntEQ(wc_InitRng(&rng), 0);
  889. XMEMSET(serverKeyBuf, 0, serverKeySz);
  890. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  891. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  892. 0);
  893. if (file) {
  894. fclose(file);
  895. file = NULL;
  896. }
  897. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  898. idx = 0;
  899. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  900. (word32)serverKeySz), 0);
  901. XMEMSET(caKeyBuf, 0, caKeySz);
  902. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  903. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  904. if (file) {
  905. fclose(file);
  906. file = NULL;
  907. }
  908. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  909. idx = 0;
  910. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  911. (word32)caKeySz), 0);
  912. XMEMSET(sapkiBuf, 0, sapkiSz);
  913. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  914. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  915. if (file) {
  916. fclose(file);
  917. file = NULL;
  918. }
  919. XMEMSET(altPrivBuf, 0, altPrivSz);
  920. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  921. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  922. if (file) {
  923. fclose(file);
  924. file = NULL;
  925. }
  926. wc_ecc_init(&altCaKey);
  927. idx = 0;
  928. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  929. (word32)altPrivSz), 0);
  930. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  931. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  932. oidSigType, 0), 0);
  933. wc_InitCert(&newCert);
  934. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  935. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  936. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  937. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  938. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  939. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  940. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  941. newCert.sigType = CTC_SHA256wRSA;
  942. newCert.isCA = 0;
  943. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  944. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  945. (const byte *)"This is NOT a critical extension", 32), 0);
  946. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  947. sapkiSz), 0);
  948. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  949. altSigAlgSz), 0);
  950. XMEMSET(scratchBuf, 0, scratchSz);
  951. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  952. &rng), 0);
  953. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  954. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  955. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  956. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  957. XMEMSET(preTbsBuf, 0, preTbsSz);
  958. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  959. XMEMSET(altSigValBuf, 0, altSigValSz);
  960. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  961. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  962. &rng), 0);
  963. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  964. altSigValBuf, altSigValSz), 0);
  965. /* Finally, generate the new certificate. */
  966. XMEMSET(outBuf, 0, outSz);
  967. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  968. 0);
  969. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  970. outSz, &caKey, NULL, &rng), 0);
  971. *out = outBuf;
  972. wc_FreeRsaKey(&caKey);
  973. wc_FreeRsaKey(&serverKey);
  974. wc_FreeRng(&rng);
  975. wc_FreeDecodedCert(&preTBS);
  976. return outSz;
  977. }
  978. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  979. byte *serverCert, word32 serverCertSz,
  980. byte *serverKey, word32 serverKeySz,
  981. int negative_test)
  982. {
  983. EXPECT_DECLS;
  984. WOLFSSL_CTX *ctx_c = NULL;
  985. WOLFSSL_CTX *ctx_s = NULL;
  986. WOLFSSL *ssl_c = NULL;
  987. WOLFSSL *ssl_s = NULL;
  988. struct test_memio_ctx test_ctx;
  989. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  990. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  991. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  992. caCert, caCertSz, serverCert, serverCertSz,
  993. serverKey, serverKeySz), 0);
  994. if (negative_test) {
  995. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  996. }
  997. else {
  998. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  999. }
  1000. wolfSSL_free(ssl_c);
  1001. wolfSSL_free(ssl_s);
  1002. wolfSSL_CTX_free(ctx_c);
  1003. wolfSSL_CTX_free(ctx_s);
  1004. return EXPECT_RESULT();
  1005. }
  1006. static int extCount = 0;
  1007. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1008. const unsigned char* der, word32 derSz)
  1009. {
  1010. (void) oid;
  1011. (void) oidSz;
  1012. (void) crit;
  1013. (void) der;
  1014. (void) derSz;
  1015. extCount ++;
  1016. /* Accept all extensions. This is only a test. Normally we would be much more
  1017. * careful about critical extensions. */
  1018. return 1;
  1019. }
  1020. static int test_dual_alg_support(void)
  1021. {
  1022. EXPECT_DECLS;
  1023. /* Root CA and server keys will be the same. This is only appropriate for
  1024. * testing. */
  1025. char keyFile[] = "./certs/ca-key.der";
  1026. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1027. char altPrivFile[] = "./certs/ecc-key.der";
  1028. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1029. byte *serverKey = NULL;
  1030. size_t serverKeySz = 0;
  1031. byte *root = NULL;
  1032. int rootSz = 0;
  1033. byte *server = NULL;
  1034. int serverSz = 0;
  1035. WOLFSSL_CERT_MANAGER* cm = NULL;
  1036. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1037. /* Base normal case. */
  1038. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1039. ExpectNotNull(root);
  1040. ExpectIntGT(rootSz, 0);
  1041. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1042. altPrivFile, keyFile, root, rootSz);
  1043. ExpectNotNull(server);
  1044. ExpectIntGT(serverSz, 0);
  1045. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1046. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1047. TEST_SUCCESS);
  1048. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1049. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1050. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1051. * the alternative signature and then set negative_test to true for the
  1052. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1053. * because the signature won't verify. The exception is if
  1054. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1055. * and this is no longer a negative test. */
  1056. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1057. ExpectNotNull(root);
  1058. ExpectIntGT(rootSz, 0);
  1059. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1060. wrongPrivFile, keyFile, root, rootSz);
  1061. ExpectNotNull(server);
  1062. ExpectIntGT(serverSz, 0);
  1063. #ifdef WOLFSSL_TRUST_PEER_CERT
  1064. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1065. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1066. TEST_SUCCESS);
  1067. #else
  1068. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1069. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1070. TEST_SUCCESS);
  1071. #endif
  1072. /* Lets see if CertManager can find the new extensions */
  1073. extCount = 0;
  1074. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1075. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1076. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1077. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1078. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1079. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1080. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1081. * because they are for the dual alg extensions. */
  1082. ExpectIntEQ(extCount, 1);
  1083. wolfSSL_CertManagerFree(cm);
  1084. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1085. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1086. free(serverKey);
  1087. return EXPECT_RESULT();
  1088. }
  1089. #else
  1090. static int test_dual_alg_support(void)
  1091. {
  1092. return TEST_SKIPPED;
  1093. }
  1094. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1095. /*----------------------------------------------------------------------------*
  1096. | Context
  1097. *----------------------------------------------------------------------------*/
  1098. #ifndef NO_WOLFSSL_SERVER
  1099. static int test_wolfSSL_CTX_new(void)
  1100. {
  1101. EXPECT_DECLS;
  1102. WOLFSSL_CTX *ctx;
  1103. WOLFSSL_METHOD* method;
  1104. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1105. ExpectNotNull(method = wolfSSLv23_server_method());
  1106. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1107. wolfSSL_CTX_free(ctx);
  1108. return EXPECT_RESULT();
  1109. }
  1110. #endif
  1111. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1112. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1113. static int test_for_double_Free(void)
  1114. {
  1115. EXPECT_DECLS;
  1116. WOLFSSL_CTX* ctx = NULL;
  1117. WOLFSSL* ssl = NULL;
  1118. int skipTest = 0;
  1119. const char* testCertFile;
  1120. const char* testKeyFile;
  1121. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1122. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1123. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1124. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1125. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1126. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1127. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1128. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1129. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1130. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1131. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1132. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1133. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1134. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1135. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1136. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1137. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1138. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1139. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1140. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1141. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1142. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1143. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1144. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1145. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1146. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1147. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1148. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1149. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1150. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1151. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1152. #ifdef OPENSSL_EXTRA
  1153. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1154. "!SRP:!kRSA:!aNULL:!eNULL";
  1155. #endif
  1156. #ifndef NO_RSA
  1157. testCertFile = svrCertFile;
  1158. testKeyFile = svrKeyFile;
  1159. #elif defined(HAVE_ECC)
  1160. testCertFile = eccCertFile;
  1161. testKeyFile = eccKeyFile;
  1162. #else
  1163. skipTest = 1;
  1164. #endif
  1165. if (skipTest != 1) {
  1166. #ifndef NO_WOLFSSL_SERVER
  1167. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1168. #else
  1169. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1170. #endif
  1171. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1172. WOLFSSL_FILETYPE_PEM));
  1173. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1174. WOLFSSL_FILETYPE_PEM));
  1175. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1176. /* First test freeing SSL, then CTX */
  1177. wolfSSL_free(ssl);
  1178. ssl = NULL;
  1179. wolfSSL_CTX_free(ctx);
  1180. ctx = NULL;
  1181. #ifndef NO_WOLFSSL_CLIENT
  1182. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1183. #else
  1184. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1185. #endif
  1186. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1187. WOLFSSL_FILETYPE_PEM));
  1188. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1189. WOLFSSL_FILETYPE_PEM));
  1190. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1191. /* Next test freeing CTX then SSL */
  1192. wolfSSL_CTX_free(ctx);
  1193. ctx = NULL;
  1194. wolfSSL_free(ssl);
  1195. ssl = NULL;
  1196. #ifndef NO_WOLFSSL_SERVER
  1197. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1198. #else
  1199. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1200. #endif
  1201. /* Test setting ciphers at ctx level */
  1202. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1203. WOLFSSL_FILETYPE_PEM));
  1204. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1205. WOLFSSL_FILETYPE_PEM));
  1206. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1207. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1208. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1209. /* only update TLSv13 suites */
  1210. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1211. #endif
  1212. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1213. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1214. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1215. /* only update pre-TLSv13 suites */
  1216. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1217. "ECDHE-RSA-AES128-GCM-SHA256"));
  1218. #endif
  1219. #ifdef OPENSSL_EXTRA
  1220. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1221. #endif
  1222. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1223. wolfSSL_CTX_free(ctx);
  1224. ctx = NULL;
  1225. wolfSSL_free(ssl);
  1226. ssl = NULL;
  1227. #ifndef NO_WOLFSSL_CLIENT
  1228. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1229. #else
  1230. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1231. #endif
  1232. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1233. WOLFSSL_FILETYPE_PEM));
  1234. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1235. WOLFSSL_FILETYPE_PEM));
  1236. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1237. /* test setting ciphers at SSL level */
  1238. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1239. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1240. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1241. /* only update TLSv13 suites */
  1242. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1243. #endif
  1244. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1245. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1246. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1247. /* only update pre-TLSv13 suites */
  1248. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1249. #endif
  1250. wolfSSL_CTX_free(ctx);
  1251. ctx = NULL;
  1252. wolfSSL_free(ssl);
  1253. ssl = NULL;
  1254. }
  1255. return EXPECT_RESULT();
  1256. }
  1257. #endif
  1258. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1259. {
  1260. EXPECT_DECLS;
  1261. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1262. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1263. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1264. const char* testCertFile;
  1265. const char* testKeyFile;
  1266. WOLFSSL_CTX* ctx = NULL;
  1267. WOLFSSL* ssl = NULL;
  1268. const byte cipherList[] =
  1269. {
  1270. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1271. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1272. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1273. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1274. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1275. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1276. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1277. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1278. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1279. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1280. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1281. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1282. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1283. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1284. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1285. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1286. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1287. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1288. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1289. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1290. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1291. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1292. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1293. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1294. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1295. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1296. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1297. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1298. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1299. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1300. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1301. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1302. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1303. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1304. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1305. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1306. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1307. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1308. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1309. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1310. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1311. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1312. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1313. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1314. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1315. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1316. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1317. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1318. /* SHA256 */
  1319. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1320. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1321. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1322. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1323. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1324. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1325. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1326. /* SHA384 */
  1327. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1328. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1329. /* AES-GCM */
  1330. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1331. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1332. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1333. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1334. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1335. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1336. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1337. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1338. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1339. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1340. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1341. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1342. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1343. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1344. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1345. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1346. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1347. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1348. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1349. * also, in some of the other AES-CCM suites
  1350. * there will be second byte number conflicts
  1351. * with non-ECC AES-GCM */
  1352. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1353. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1354. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1355. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1356. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1357. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1358. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1359. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1360. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1361. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1362. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1363. /* Camellia */
  1364. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1365. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1366. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1367. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1368. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1369. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1370. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1371. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1372. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1373. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1374. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1375. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1376. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1377. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1378. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1379. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1380. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1381. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1382. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1383. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1384. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1385. /* TLS v1.3 cipher suites */
  1386. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1387. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1388. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1389. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1390. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1391. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1392. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1393. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1394. };
  1395. #ifndef NO_RSA
  1396. testCertFile = svrCertFile;
  1397. testKeyFile = svrKeyFile;
  1398. #elif defined(HAVE_ECC)
  1399. testCertFile = eccCertFile;
  1400. testKeyFile = eccKeyFile;
  1401. #endif
  1402. #ifndef NO_WOLFSSL_SERVER
  1403. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1404. #else
  1405. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1406. #endif
  1407. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1408. sizeof(cipherList)));
  1409. wolfSSL_CTX_free(ctx);
  1410. ctx = NULL;
  1411. #ifndef NO_WOLFSSL_SERVER
  1412. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1413. #else
  1414. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1415. #endif
  1416. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1417. WOLFSSL_FILETYPE_PEM));
  1418. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1419. WOLFSSL_FILETYPE_PEM));
  1420. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1421. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1422. sizeof(cipherList)));
  1423. wolfSSL_free(ssl);
  1424. wolfSSL_CTX_free(ctx);
  1425. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1426. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1427. return EXPECT_RESULT();
  1428. }
  1429. static int test_wolfSSL_CTX_use_certificate_file(void)
  1430. {
  1431. EXPECT_DECLS;
  1432. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1433. WOLFSSL_CTX *ctx = NULL;
  1434. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1435. /* invalid context */
  1436. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1437. WOLFSSL_FILETYPE_PEM));
  1438. /* invalid cert file */
  1439. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1440. WOLFSSL_FILETYPE_PEM));
  1441. /* invalid cert type */
  1442. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1443. #ifdef NO_RSA
  1444. /* rsa needed */
  1445. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1446. WOLFSSL_FILETYPE_PEM));
  1447. #else
  1448. /* success */
  1449. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1450. WOLFSSL_FILETYPE_PEM));
  1451. #endif
  1452. wolfSSL_CTX_free(ctx);
  1453. #endif
  1454. return EXPECT_RESULT();
  1455. }
  1456. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1457. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1458. {
  1459. EXPECT_DECLS;
  1460. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1461. WOLFSSL_CTX* ctx = NULL;
  1462. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1463. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1464. server_cert_der_2048), WOLFSSL_SUCCESS);
  1465. wolfSSL_CTX_free(ctx);
  1466. #endif
  1467. return EXPECT_RESULT();
  1468. }
  1469. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1470. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1471. * context using buffer.
  1472. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1473. * --enable-testcert flag.
  1474. */
  1475. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1476. {
  1477. EXPECT_DECLS;
  1478. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1479. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1480. WOLFSSL_CTX* ctx = NULL;
  1481. int ret;
  1482. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1483. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1484. server_cert_der_2048, sizeof_server_cert_der_2048,
  1485. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1486. wolfSSL_CTX_free(ctx);
  1487. #endif
  1488. return EXPECT_RESULT();
  1489. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1490. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1491. {
  1492. EXPECT_DECLS;
  1493. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1494. WOLFSSL_CTX *ctx = NULL;
  1495. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1496. /* invalid context */
  1497. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1498. WOLFSSL_FILETYPE_PEM));
  1499. /* invalid key file */
  1500. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1501. WOLFSSL_FILETYPE_PEM));
  1502. /* invalid key type */
  1503. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1504. /* success */
  1505. #ifdef NO_RSA
  1506. /* rsa needed */
  1507. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1508. WOLFSSL_FILETYPE_PEM));
  1509. #else
  1510. /* success */
  1511. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1512. WOLFSSL_FILETYPE_PEM));
  1513. #endif
  1514. wolfSSL_CTX_free(ctx);
  1515. #endif
  1516. return EXPECT_RESULT();
  1517. }
  1518. /* test both file and buffer versions along with unloading trusted peer certs */
  1519. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1520. {
  1521. EXPECT_DECLS;
  1522. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1523. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1524. WOLFSSL_CTX *ctx = NULL;
  1525. WOLFSSL* ssl = NULL;
  1526. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1527. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1528. #if !defined(NO_FILESYSTEM)
  1529. /* invalid file */
  1530. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1531. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1532. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1533. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1534. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1535. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1536. /* success */
  1537. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1538. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1539. /* unload cert */
  1540. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1541. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1542. /* invalid file */
  1543. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1544. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1545. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1546. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1547. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1548. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1549. /* success */
  1550. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1551. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1552. #ifdef WOLFSSL_LOCAL_X509_STORE
  1553. /* unload cert */
  1554. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1555. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1556. #endif
  1557. #endif
  1558. /* Test of loading certs from buffers */
  1559. /* invalid buffer */
  1560. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1561. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1562. /* success */
  1563. #ifdef USE_CERT_BUFFERS_1024
  1564. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1565. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1566. #endif
  1567. #ifdef USE_CERT_BUFFERS_2048
  1568. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1569. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1570. #endif
  1571. /* unload cert */
  1572. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1573. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1574. wolfSSL_free(ssl);
  1575. wolfSSL_CTX_free(ctx);
  1576. #endif
  1577. return EXPECT_RESULT();
  1578. }
  1579. static int test_wolfSSL_CTX_load_verify_locations(void)
  1580. {
  1581. EXPECT_DECLS;
  1582. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1583. WOLFSSL_CTX *ctx = NULL;
  1584. #ifndef NO_RSA
  1585. WOLFSSL_CERT_MANAGER* cm = NULL;
  1586. #ifdef PERSIST_CERT_CACHE
  1587. int cacheSz = 0;
  1588. unsigned char* cache = NULL;
  1589. int used = 0;
  1590. #ifndef NO_FILESYSTEM
  1591. const char* cacheFile = "./tests/cert_cache.tmp";
  1592. #endif
  1593. int i;
  1594. int t;
  1595. int* p;
  1596. #endif
  1597. #endif
  1598. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1599. const char* load_certs_path = "./certs/external";
  1600. const char* load_no_certs_path = "./examples";
  1601. const char* load_expired_path = "./certs/test/expired";
  1602. #endif
  1603. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1604. /* invalid arguments */
  1605. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1606. WOLFSSL_FAILURE);
  1607. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1608. WOLFSSL_FAILURE);
  1609. /* invalid ca file */
  1610. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1611. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1612. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1613. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1614. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1615. /* invalid path */
  1616. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1617. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1618. #endif
  1619. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1620. /* test ignoring the invalid path */
  1621. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1622. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1623. #endif
  1624. /* load ca cert */
  1625. #ifdef NO_RSA
  1626. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1627. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1628. #else /* Skip the following test without RSA certs. */
  1629. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1630. WOLFSSL_SUCCESS);
  1631. #ifdef PERSIST_CERT_CACHE
  1632. /* Get cert cache size */
  1633. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1634. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1635. DYNAMIC_TYPE_TMP_BUFFER));
  1636. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1637. BAD_FUNC_ARG);
  1638. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1639. BAD_FUNC_ARG);
  1640. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1641. BAD_FUNC_ARG);
  1642. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1643. BAD_FUNC_ARG);
  1644. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1645. BAD_FUNC_ARG);
  1646. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1647. BAD_FUNC_ARG);
  1648. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1649. BAD_FUNC_ARG);
  1650. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1651. BAD_FUNC_ARG);
  1652. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1653. BAD_FUNC_ARG);
  1654. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1655. BUFFER_E);
  1656. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1657. ExpectIntEQ(cacheSz, used);
  1658. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1659. BAD_FUNC_ARG);
  1660. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1661. BAD_FUNC_ARG);
  1662. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1663. BAD_FUNC_ARG);
  1664. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1665. BAD_FUNC_ARG);
  1666. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1667. BAD_FUNC_ARG);
  1668. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1669. BAD_FUNC_ARG);
  1670. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1671. BAD_FUNC_ARG);
  1672. /* Smaller than header. */
  1673. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1674. for (i = 1; i < cacheSz; i++) {
  1675. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1676. BUFFER_E);
  1677. }
  1678. if (EXPECT_SUCCESS()) {
  1679. /* Modify header for bad results! */
  1680. p = (int*)cache;
  1681. /* version */
  1682. t = p[0]; p[0] = 0xff;
  1683. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1684. CACHE_MATCH_ERROR);
  1685. p[0] = t; p++;
  1686. /* rows */
  1687. t = p[0]; p[0] = 0xff;
  1688. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1689. CACHE_MATCH_ERROR);
  1690. p[0] = t; p++;
  1691. /* columns[0] */
  1692. t = p[0]; p[0] = -1;
  1693. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1694. PARSE_ERROR);
  1695. p[0] = t; p += CA_TABLE_SIZE;
  1696. /* signerSz*/
  1697. t = p[0]; p[0] = 0xff;
  1698. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1699. CACHE_MATCH_ERROR);
  1700. p[0] = t;
  1701. }
  1702. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1703. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1704. #ifndef NO_FILESYSTEM
  1705. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1706. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1707. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1708. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1709. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1710. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1711. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1712. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1713. WOLFSSL_BAD_FILE);
  1714. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1715. /* File contents is not a cache. */
  1716. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1717. CACHE_MATCH_ERROR);
  1718. #endif
  1719. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1720. #endif
  1721. /* Test unloading CA's */
  1722. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1723. #ifdef PERSIST_CERT_CACHE
  1724. /* Verify no certs (result is less than cacheSz) */
  1725. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1726. #endif
  1727. /* load ca cert again */
  1728. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1729. WOLFSSL_SUCCESS);
  1730. /* Test getting CERT_MANAGER */
  1731. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1732. /* Test unloading CA's using CM */
  1733. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1734. #ifdef PERSIST_CERT_CACHE
  1735. /* Verify no certs (result is less than cacheSz) */
  1736. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1737. #endif
  1738. #endif
  1739. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1740. /* Test loading CA certificates using a path */
  1741. #ifdef NO_RSA
  1742. /* failure here okay since certs in external directory are RSA */
  1743. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1744. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1745. #else
  1746. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1747. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1748. #endif
  1749. /* Test loading path with no files */
  1750. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1751. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1752. /* Test loading expired CA certificates */
  1753. #ifdef NO_RSA
  1754. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1755. load_expired_path,
  1756. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1757. WOLFSSL_SUCCESS);
  1758. #else
  1759. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1760. load_expired_path,
  1761. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1762. WOLFSSL_SUCCESS);
  1763. #endif
  1764. /* Test loading CA certificates and ignoring all errors */
  1765. #ifdef NO_RSA
  1766. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1767. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1768. #else
  1769. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1770. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1771. #endif
  1772. #endif
  1773. wolfSSL_CTX_free(ctx);
  1774. #endif
  1775. return EXPECT_RESULT();
  1776. }
  1777. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1778. {
  1779. int res = TEST_SKIPPED;
  1780. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1781. (!defined(NO_RSA) || defined(HAVE_ECC))
  1782. WOLFSSL_CTX* ctx;
  1783. byte dirValid = 0;
  1784. int ret = 0;
  1785. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1786. if (ctx == NULL) {
  1787. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1788. ret = -1;
  1789. }
  1790. if (ret == 0) {
  1791. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1792. dirValid = 1;
  1793. #else
  1794. word32 numDirs;
  1795. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1796. if (caDirs == NULL || numDirs == 0) {
  1797. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1798. ret = -1;
  1799. }
  1800. else {
  1801. ReadDirCtx dirCtx;
  1802. word32 i;
  1803. for (i = 0; i < numDirs; ++i) {
  1804. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1805. /* Directory isn't empty. */
  1806. dirValid = 1;
  1807. wc_ReadDirClose(&dirCtx);
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. #endif
  1813. }
  1814. /*
  1815. * If the directory isn't empty, we should be able to load CA
  1816. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1817. * usable.
  1818. */
  1819. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1820. WOLFSSL_SUCCESS) {
  1821. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1822. ret = -1;
  1823. }
  1824. #ifdef OPENSSL_EXTRA
  1825. if (ret == 0 &&
  1826. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1827. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1828. ret = -1;
  1829. }
  1830. #endif /* OPENSSL_EXTRA */
  1831. wolfSSL_CTX_free(ctx);
  1832. res = TEST_RES_CHECK(ret == 0);
  1833. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1834. return res;
  1835. }
  1836. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1837. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1838. int file_type)
  1839. {
  1840. int ret;
  1841. WOLFSSL_CERT_MANAGER* cm;
  1842. cm = wolfSSL_CertManagerNew();
  1843. if (cm == NULL) {
  1844. fprintf(stderr, "test_cm_load_ca failed\n");
  1845. return -1;
  1846. }
  1847. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1848. wolfSSL_CertManagerFree(cm);
  1849. return ret;
  1850. }
  1851. static int test_cm_load_ca_file(const char* ca_cert_file)
  1852. {
  1853. int ret = 0;
  1854. byte* cert_buf = NULL;
  1855. size_t cert_sz = 0;
  1856. #if defined(WOLFSSL_PEM_TO_DER)
  1857. DerBuffer* pDer = NULL;
  1858. #endif
  1859. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1860. if (ret == 0) {
  1861. /* normal test */
  1862. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1863. if (ret == WOLFSSL_SUCCESS) {
  1864. /* test including null terminator in length */
  1865. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1866. if (tmp == NULL) {
  1867. ret = MEMORY_E;
  1868. }
  1869. else {
  1870. cert_buf = tmp;
  1871. cert_buf[cert_sz] = '\0';
  1872. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1873. WOLFSSL_FILETYPE_PEM);
  1874. }
  1875. }
  1876. #if defined(WOLFSSL_PEM_TO_DER)
  1877. if (ret == WOLFSSL_SUCCESS) {
  1878. /* test loading DER */
  1879. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1880. if (ret == 0 && pDer != NULL) {
  1881. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1882. WOLFSSL_FILETYPE_ASN1);
  1883. wc_FreeDer(&pDer);
  1884. }
  1885. }
  1886. #endif
  1887. }
  1888. free(cert_buf);
  1889. return ret;
  1890. }
  1891. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1892. int file_type, word32 flags)
  1893. {
  1894. int ret;
  1895. WOLFSSL_CERT_MANAGER* cm;
  1896. cm = wolfSSL_CertManagerNew();
  1897. if (cm == NULL) {
  1898. fprintf(stderr, "test_cm_load_ca failed\n");
  1899. return -1;
  1900. }
  1901. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1902. 0, flags);
  1903. wolfSSL_CertManagerFree(cm);
  1904. return ret;
  1905. }
  1906. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1907. {
  1908. int ret = 0;
  1909. byte* cert_buf = NULL;
  1910. size_t cert_sz = 0;
  1911. #if defined(WOLFSSL_PEM_TO_DER)
  1912. DerBuffer* pDer = NULL;
  1913. #endif
  1914. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1915. if (ret == 0) {
  1916. /* normal test */
  1917. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1918. WOLFSSL_FILETYPE_PEM, flags);
  1919. if (ret == WOLFSSL_SUCCESS) {
  1920. /* test including null terminator in length */
  1921. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1922. if (tmp == NULL) {
  1923. ret = MEMORY_E;
  1924. }
  1925. else {
  1926. cert_buf = tmp;
  1927. cert_buf[cert_sz] = '\0';
  1928. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1929. WOLFSSL_FILETYPE_PEM, flags);
  1930. }
  1931. }
  1932. #if defined(WOLFSSL_PEM_TO_DER)
  1933. if (ret == WOLFSSL_SUCCESS) {
  1934. /* test loading DER */
  1935. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1936. if (ret == 0 && pDer != NULL) {
  1937. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1938. WOLFSSL_FILETYPE_ASN1, flags);
  1939. wc_FreeDer(&pDer);
  1940. }
  1941. }
  1942. #endif
  1943. }
  1944. free(cert_buf);
  1945. return ret;
  1946. }
  1947. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1948. static int test_wolfSSL_CertManagerAPI(void)
  1949. {
  1950. EXPECT_DECLS;
  1951. #ifndef NO_CERTS
  1952. WOLFSSL_CERT_MANAGER* cm = NULL;
  1953. unsigned char c;
  1954. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1955. wolfSSL_CertManagerFree(NULL);
  1956. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1957. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1958. #ifdef WOLFSSL_TRUST_PEER_CERT
  1959. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1960. #endif
  1961. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1962. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1963. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1964. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1965. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1966. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1967. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1968. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1969. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1970. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1971. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1972. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1973. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1974. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1975. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1976. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1977. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1978. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1979. WOLFSSL_BAD_FILETYPE);
  1980. #endif
  1981. #if !defined(NO_FILESYSTEM)
  1982. {
  1983. const char* ca_cert = "./certs/ca-cert.pem";
  1984. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1985. const char* ca_cert_der = "./certs/ca-cert.der";
  1986. #endif
  1987. const char* ca_path = "./certs";
  1988. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1989. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1990. BAD_FUNC_ARG);
  1991. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1992. BAD_FUNC_ARG);
  1993. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1994. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1995. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1996. WOLFSSL_BAD_FILETYPE);
  1997. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1998. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1999. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2000. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2001. #endif
  2002. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2003. WOLFSSL_FATAL_ERROR);
  2004. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2005. WOLFSSL_FATAL_ERROR);
  2006. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2007. WOLFSSL_FATAL_ERROR);
  2008. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2009. WOLFSSL_FATAL_ERROR);
  2010. }
  2011. #endif
  2012. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2013. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2014. #elif !defined(HAVE_CRL)
  2015. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2016. #endif
  2017. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2018. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2019. #ifdef HAVE_CRL
  2020. /* Test APIs when CRL is disabled. */
  2021. #ifdef HAVE_CRL_IO
  2022. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2023. #endif
  2024. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2025. sizeof_server_cert_der_2048), 1);
  2026. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2027. #endif
  2028. /* OCSP */
  2029. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2030. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2031. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2032. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2033. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2034. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2035. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2036. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2037. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2038. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2039. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2040. #endif
  2041. #ifdef HAVE_OCSP
  2042. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2043. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2044. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2045. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2046. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2047. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2048. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2049. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2050. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2051. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2052. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2053. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2054. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2055. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2056. BAD_FUNC_ARG);
  2057. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2058. BAD_FUNC_ARG);
  2059. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2060. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2061. BAD_FUNC_ARG);
  2062. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2063. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2064. /* Test APIs when OCSP is disabled. */
  2065. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2066. NULL, NULL, NULL, NULL), 1);
  2067. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2068. #endif
  2069. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2070. if (EXPECT_SUCCESS()) {
  2071. wolfSSL_CertManagerFree(cm);
  2072. }
  2073. wolfSSL_CertManagerFree(cm);
  2074. cm = NULL;
  2075. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2076. #ifdef HAVE_OCSP
  2077. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2078. WOLFSSL_OCSP_CHECKALL), 1);
  2079. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2080. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2081. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2082. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2083. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2084. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2085. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2086. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2087. #endif
  2088. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2089. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2090. #endif
  2091. #ifdef WOLFSSL_TRUST_PEER_CERT
  2092. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2093. #endif
  2094. wolfSSL_CertManagerFree(cm);
  2095. #endif
  2096. return EXPECT_RESULT();
  2097. }
  2098. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2099. {
  2100. EXPECT_DECLS;
  2101. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2102. const char* ca_cert = "./certs/ca-cert.pem";
  2103. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2104. int ret;
  2105. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2106. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2107. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2108. #elif defined(NO_RSA)
  2109. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2110. #else
  2111. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2112. #endif
  2113. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2114. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2115. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2116. #elif defined(NO_RSA)
  2117. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2118. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2119. !defined(NO_ASN_TIME)
  2120. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2121. #else
  2122. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2123. #endif
  2124. #endif
  2125. return EXPECT_RESULT();
  2126. }
  2127. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2128. {
  2129. EXPECT_DECLS;
  2130. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2131. const char* ca_cert = "./certs/ca-cert.pem";
  2132. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2133. int ret;
  2134. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2135. 1);
  2136. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2137. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2138. #elif defined(NO_RSA)
  2139. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2140. #else
  2141. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2142. #endif
  2143. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2144. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2145. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2146. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2147. #elif defined(NO_RSA)
  2148. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2149. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2150. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2151. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2152. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2153. #else
  2154. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2155. #endif
  2156. #endif
  2157. return EXPECT_RESULT();
  2158. }
  2159. static int test_wolfSSL_CertManagerGetCerts(void)
  2160. {
  2161. EXPECT_DECLS;
  2162. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2163. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2164. defined(WOLFSSL_SIGNER_DER_CERT)
  2165. WOLFSSL_CERT_MANAGER* cm = NULL;
  2166. WOLFSSL_STACK* sk = NULL;
  2167. X509* x509 = NULL;
  2168. X509* cert1 = NULL;
  2169. FILE* file1 = NULL;
  2170. #ifdef DEBUG_WOLFSSL_VERBOSE
  2171. WOLFSSL_BIO* bio = NULL;
  2172. #endif
  2173. int i = 0;
  2174. int ret = 0;
  2175. const byte* der = NULL;
  2176. int derSz = 0;
  2177. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2178. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2179. if (file1 != NULL) {
  2180. fclose(file1);
  2181. }
  2182. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2183. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2184. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2185. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2186. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2187. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2188. * and full OpenSSL compatibility */
  2189. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2190. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2191. #else
  2192. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2193. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2194. #endif
  2195. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2196. "./certs/ca-cert.pem", NULL));
  2197. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2198. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2199. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2200. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2201. #ifdef DEBUG_WOLFSSL_VERBOSE
  2202. bio = BIO_new(wolfSSL_BIO_s_file());
  2203. if (bio != NULL) {
  2204. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2205. X509_print(bio, x509);
  2206. BIO_free(bio);
  2207. }
  2208. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2209. }
  2210. wolfSSL_X509_free(cert1);
  2211. sk_X509_pop_free(sk, NULL);
  2212. wolfSSL_CertManagerFree(cm);
  2213. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2214. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2215. defined(WOLFSSL_SIGNER_DER_CERT) */
  2216. return EXPECT_RESULT();
  2217. }
  2218. static int test_wolfSSL_CertManagerSetVerify(void)
  2219. {
  2220. EXPECT_DECLS;
  2221. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2222. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2223. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2224. WOLFSSL_CERT_MANAGER* cm = NULL;
  2225. int tmp = myVerifyAction;
  2226. const char* ca_cert = "./certs/ca-cert.pem";
  2227. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2228. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2229. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2230. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2231. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2232. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2233. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2234. #else
  2235. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2236. WOLFSSL_SUCCESS);
  2237. #endif
  2238. /* Use the test CB that always accepts certs */
  2239. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2240. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2241. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2242. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2243. {
  2244. const char* verifyCert = "./certs/server-cert.der";
  2245. /* Use the test CB that always fails certs */
  2246. myVerifyAction = VERIFY_FORCE_FAIL;
  2247. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2248. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2249. }
  2250. #endif
  2251. wolfSSL_CertManagerFree(cm);
  2252. myVerifyAction = tmp;
  2253. #endif
  2254. return EXPECT_RESULT();
  2255. }
  2256. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2257. defined(DEBUG_UNIT_TEST_CERTS)
  2258. /* Used when debugging name constraint tests. Not static to allow use in
  2259. * multiple locations with complex define guards. */
  2260. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2261. {
  2262. BIO* out = BIO_new_file(fileName, "wb");
  2263. if (out != NULL) {
  2264. PEM_write_bio_X509(out, x509);
  2265. BIO_free(out);
  2266. }
  2267. }
  2268. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2269. {
  2270. BIO* out = BIO_new_file(fileName, "wb");
  2271. if (out != NULL) {
  2272. BIO_write(out, der, derSz);
  2273. BIO_free(out);
  2274. }
  2275. }
  2276. #else
  2277. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2278. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2279. #endif
  2280. static int test_wolfSSL_CertManagerNameConstraint(void)
  2281. {
  2282. EXPECT_DECLS;
  2283. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2284. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2285. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2286. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2287. !defined(NO_SHA256)
  2288. WOLFSSL_CERT_MANAGER* cm = NULL;
  2289. WOLFSSL_EVP_PKEY *priv = NULL;
  2290. WOLFSSL_X509_NAME* name = NULL;
  2291. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2292. const char* server_cert = "./certs/test/server-goodcn.pem";
  2293. int i = 0;
  2294. static const byte extNameConsOid[] = {85, 29, 30};
  2295. RsaKey key;
  2296. WC_RNG rng;
  2297. byte *der = NULL;
  2298. int derSz = 0;
  2299. word32 idx = 0;
  2300. byte *pt;
  2301. WOLFSSL_X509 *x509 = NULL;
  2302. WOLFSSL_X509 *ca = NULL;
  2303. wc_InitRng(&rng);
  2304. /* load in CA private key for signing */
  2305. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2306. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2307. sizeof_server_key_der_2048), 0);
  2308. /* get ca certificate then alter it */
  2309. ExpectNotNull(der =
  2310. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2311. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2312. WOLFSSL_FILETYPE_ASN1));
  2313. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2314. if (EXPECT_SUCCESS() && (der != NULL)) {
  2315. XMEMCPY(der, pt, derSz);
  2316. /* find the name constraint extension and alter it */
  2317. pt = der;
  2318. for (i = 0; i < derSz - 3; i++) {
  2319. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2320. pt += 3;
  2321. break;
  2322. }
  2323. pt++;
  2324. }
  2325. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2326. /* go to the length value and set it to 0 */
  2327. while (i < derSz && *pt != 0x81) {
  2328. pt++;
  2329. i++;
  2330. }
  2331. ExpectIntNE(i, derSz); /* did not place to alter */
  2332. pt++;
  2333. *pt = 0x00;
  2334. }
  2335. /* resign the altered certificate */
  2336. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2337. FOURK_BUF, &key, NULL, &rng)), 0);
  2338. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2339. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2340. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2341. wolfSSL_CertManagerFree(cm);
  2342. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2343. wolfSSL_X509_free(x509);
  2344. wc_FreeRsaKey(&key);
  2345. wc_FreeRng(&rng);
  2346. /* add email alt name to satisfy constraint */
  2347. pt = (byte*)server_key_der_2048;
  2348. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2349. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2350. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2351. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2352. WOLFSSL_FILETYPE_ASN1));
  2353. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2354. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2355. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2356. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2357. /* Good cert test with proper alt email name */
  2358. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2359. WOLFSSL_FILETYPE_PEM));
  2360. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2361. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2362. name = NULL;
  2363. ExpectNotNull(name = X509_NAME_new());
  2364. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2365. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2366. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2367. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2368. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2369. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2370. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2371. X509_NAME_free(name);
  2372. name = NULL;
  2373. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2374. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2375. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2376. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2377. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2378. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2379. wolfSSL_X509_free(x509);
  2380. x509 = NULL;
  2381. /* Cert with bad alt name list */
  2382. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2383. WOLFSSL_FILETYPE_PEM));
  2384. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2385. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2386. name = NULL;
  2387. ExpectNotNull(name = X509_NAME_new());
  2388. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2389. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2390. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2391. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2392. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2393. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2394. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2395. X509_NAME_free(name);
  2396. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2397. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2398. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2399. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2400. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2401. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2402. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2403. wolfSSL_CertManagerFree(cm);
  2404. wolfSSL_X509_free(x509);
  2405. wolfSSL_X509_free(ca);
  2406. wolfSSL_EVP_PKEY_free(priv);
  2407. #endif
  2408. return EXPECT_RESULT();
  2409. }
  2410. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2411. {
  2412. EXPECT_DECLS;
  2413. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2414. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2415. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2416. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2417. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2418. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2419. const char* server_cert = "./certs/server-cert.pem";
  2420. WOLFSSL_CERT_MANAGER* cm = NULL;
  2421. WOLFSSL_X509 *x509 = NULL;
  2422. WOLFSSL_X509 *ca = NULL;
  2423. const unsigned char *der = NULL;
  2424. const unsigned char *pt;
  2425. WOLFSSL_EVP_PKEY *priv = NULL;
  2426. WOLFSSL_X509_NAME* name = NULL;
  2427. int derSz = 0;
  2428. /* C=US*/
  2429. char altName[] = {
  2430. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2431. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2432. };
  2433. /* C=ID */
  2434. char altNameFail[] = {
  2435. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2436. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2437. };
  2438. /* C=US ST=California*/
  2439. char altNameExc[] = {
  2440. 0x30, 0x22,
  2441. 0x31, 0x0B,
  2442. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2443. 0x31, 0x13,
  2444. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2445. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2446. };
  2447. /* load in CA private key for signing */
  2448. pt = ca_key_der_2048;
  2449. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2450. sizeof_ca_key_der_2048));
  2451. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2452. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2453. WOLFSSL_FILETYPE_ASN1));
  2454. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2455. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2456. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2457. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2458. WOLFSSL_FILETYPE_PEM));
  2459. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2460. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2461. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2462. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2463. #else
  2464. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2465. #endif
  2466. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2467. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2468. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2469. /* add in matching DIR alt name and resign */
  2470. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2471. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2472. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2473. #else
  2474. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2475. #endif
  2476. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2477. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2478. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2479. wolfSSL_X509_free(x509);
  2480. x509 = NULL;
  2481. /* check verify fail */
  2482. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2483. WOLFSSL_FILETYPE_PEM));
  2484. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2485. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2486. /* add in miss matching DIR alt name and resign */
  2487. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2488. ASN_DIR_TYPE);
  2489. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2490. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2491. #else
  2492. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2493. #endif
  2494. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2495. #ifndef WOLFSSL_NO_ASN_STRICT
  2496. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2497. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2498. #else
  2499. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2500. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2501. #endif
  2502. /* check that it still fails if one bad altname and one good altname is in
  2503. * the certificate */
  2504. wolfSSL_X509_free(x509);
  2505. x509 = NULL;
  2506. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2507. WOLFSSL_FILETYPE_PEM));
  2508. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2509. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2510. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2511. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2512. ASN_DIR_TYPE);
  2513. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2514. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2515. #else
  2516. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2517. #endif
  2518. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2519. #ifndef WOLFSSL_NO_ASN_STRICT
  2520. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2521. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2522. #else
  2523. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2524. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2525. #endif
  2526. /* check it fails with switching position of bad altname */
  2527. wolfSSL_X509_free(x509);
  2528. x509 = NULL;
  2529. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2530. WOLFSSL_FILETYPE_PEM));
  2531. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2532. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2533. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2534. ASN_DIR_TYPE);
  2535. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2536. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2537. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2538. #else
  2539. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2540. #endif
  2541. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2542. #ifndef WOLFSSL_NO_ASN_STRICT
  2543. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2544. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2545. #else
  2546. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2547. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2548. #endif
  2549. wolfSSL_CertManagerFree(cm);
  2550. wolfSSL_X509_free(x509);
  2551. x509 = NULL;
  2552. wolfSSL_X509_free(ca);
  2553. ca = NULL;
  2554. /* now test with excluded name constraint */
  2555. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2556. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2557. WOLFSSL_FILETYPE_ASN1));
  2558. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2559. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2560. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2561. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2562. WOLFSSL_FILETYPE_PEM));
  2563. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2564. ASN_DIR_TYPE);
  2565. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2566. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2567. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2568. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2569. #else
  2570. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2571. #endif
  2572. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2573. #ifndef WOLFSSL_NO_ASN_STRICT
  2574. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2575. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2576. #else
  2577. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2578. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2579. #endif
  2580. wolfSSL_CertManagerFree(cm);
  2581. wolfSSL_X509_free(x509);
  2582. wolfSSL_X509_free(ca);
  2583. wolfSSL_EVP_PKEY_free(priv);
  2584. #endif
  2585. return EXPECT_RESULT();
  2586. }
  2587. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2588. {
  2589. EXPECT_DECLS;
  2590. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2591. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2592. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2593. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2594. !defined(NO_SHA256)
  2595. WOLFSSL_CERT_MANAGER* cm = NULL;
  2596. WOLFSSL_EVP_PKEY *priv = NULL;
  2597. WOLFSSL_X509_NAME* name = NULL;
  2598. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2599. const char* server_cert = "./certs/test/server-goodcn.pem";
  2600. byte *der = NULL;
  2601. int derSz = 0;
  2602. byte *pt;
  2603. WOLFSSL_X509 *x509 = NULL;
  2604. WOLFSSL_X509 *ca = NULL;
  2605. pt = (byte*)server_key_der_2048;
  2606. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2607. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2608. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2609. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2610. WOLFSSL_FILETYPE_ASN1));
  2611. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2612. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2613. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2615. /* check satisfying .wolfssl.com constraint passes */
  2616. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2617. WOLFSSL_FILETYPE_PEM));
  2618. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2619. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2620. name = NULL;
  2621. ExpectNotNull(name = X509_NAME_new());
  2622. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2623. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2624. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2625. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2626. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2627. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2628. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2629. X509_NAME_free(name);
  2630. name = NULL;
  2631. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2632. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2633. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2634. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2635. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2636. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2637. wolfSSL_X509_free(x509);
  2638. x509 = NULL;
  2639. /* check satisfying .random.com constraint passes */
  2640. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2641. WOLFSSL_FILETYPE_PEM));
  2642. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2643. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2644. name = NULL;
  2645. ExpectNotNull(name = X509_NAME_new());
  2646. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2647. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2648. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2649. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2650. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2651. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2652. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2653. X509_NAME_free(name);
  2654. name = NULL;
  2655. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2656. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2657. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2658. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2659. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2660. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2661. wolfSSL_X509_free(x509);
  2662. x509 = NULL;
  2663. /* check fail case when neither constraint is matched */
  2664. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2665. WOLFSSL_FILETYPE_PEM));
  2666. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2667. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2668. name = NULL;
  2669. ExpectNotNull(name = X509_NAME_new());
  2670. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2671. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2672. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2673. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2674. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2675. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2676. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2677. X509_NAME_free(name);
  2678. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2679. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2680. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2681. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2682. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2683. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2684. wolfSSL_CertManagerFree(cm);
  2685. wolfSSL_X509_free(x509);
  2686. wolfSSL_X509_free(ca);
  2687. wolfSSL_EVP_PKEY_free(priv);
  2688. #endif
  2689. return EXPECT_RESULT();
  2690. }
  2691. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2692. {
  2693. EXPECT_DECLS;
  2694. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2695. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2696. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2697. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2698. !defined(NO_SHA256)
  2699. WOLFSSL_CERT_MANAGER* cm = NULL;
  2700. WOLFSSL_EVP_PKEY *priv = NULL;
  2701. WOLFSSL_X509_NAME* name = NULL;
  2702. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2703. const char* server_cert = "./certs/test/server-goodcn.pem";
  2704. byte *der = NULL;
  2705. int derSz;
  2706. byte *pt;
  2707. WOLFSSL_X509 *x509 = NULL;
  2708. WOLFSSL_X509 *ca = NULL;
  2709. pt = (byte*)server_key_der_2048;
  2710. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2711. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2712. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2713. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2714. WOLFSSL_FILETYPE_ASN1));
  2715. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2716. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2717. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2718. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2719. /* check satisfying wolfssl.com constraint passes */
  2720. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2721. WOLFSSL_FILETYPE_PEM));
  2722. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2723. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2724. name = NULL;
  2725. ExpectNotNull(name = X509_NAME_new());
  2726. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2727. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2728. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2729. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2730. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2731. X509_NAME_free(name);
  2732. name = NULL;
  2733. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2734. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2735. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2736. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2737. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2738. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2739. wolfSSL_X509_free(x509);
  2740. x509 = NULL;
  2741. /* check satisfying example.com constraint passes */
  2742. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2743. WOLFSSL_FILETYPE_PEM));
  2744. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2745. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2746. name = NULL;
  2747. ExpectNotNull(name = X509_NAME_new());
  2748. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2749. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2750. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2751. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2752. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2753. X509_NAME_free(name);
  2754. name = NULL;
  2755. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2756. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2757. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2758. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2759. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2760. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2761. wolfSSL_X509_free(x509);
  2762. x509 = NULL;
  2763. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2764. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2765. WOLFSSL_FILETYPE_PEM));
  2766. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2767. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2768. name = NULL;
  2769. ExpectNotNull(name = X509_NAME_new());
  2770. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2771. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2772. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2773. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2774. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2775. X509_NAME_free(name);
  2776. name = NULL;
  2777. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2778. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2779. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2780. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2781. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2782. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2783. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2784. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2785. wolfSSL_X509_free(x509);
  2786. x509 = NULL;
  2787. /* check fail when one DNS in the list is bad */
  2788. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2789. WOLFSSL_FILETYPE_PEM));
  2790. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2791. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2792. name = NULL;
  2793. ExpectNotNull(name = X509_NAME_new());
  2794. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2795. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2796. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2797. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2798. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2799. X509_NAME_free(name);
  2800. name = NULL;
  2801. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2802. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2803. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2804. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2805. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2806. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2807. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2808. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2809. wolfSSL_X509_free(x509);
  2810. x509 = NULL;
  2811. /* check fail case when neither constraint is matched */
  2812. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2813. WOLFSSL_FILETYPE_PEM));
  2814. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2815. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2816. name = NULL;
  2817. ExpectNotNull(name = X509_NAME_new());
  2818. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2819. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2820. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2821. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2822. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2823. X509_NAME_free(name);
  2824. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2825. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2826. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2827. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2828. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2829. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2830. wolfSSL_CertManagerFree(cm);
  2831. wolfSSL_X509_free(x509);
  2832. wolfSSL_X509_free(ca);
  2833. wolfSSL_EVP_PKEY_free(priv);
  2834. #endif
  2835. return EXPECT_RESULT();
  2836. }
  2837. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2838. {
  2839. EXPECT_DECLS;
  2840. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2841. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2842. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2843. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2844. !defined(NO_SHA256)
  2845. WOLFSSL_CERT_MANAGER* cm = NULL;
  2846. WOLFSSL_EVP_PKEY *priv = NULL;
  2847. WOLFSSL_X509_NAME* name = NULL;
  2848. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2849. const char* server_cert = "./certs/test/server-goodcn.pem";
  2850. byte *der = NULL;
  2851. int derSz;
  2852. byte *pt;
  2853. WOLFSSL_X509 *x509 = NULL;
  2854. WOLFSSL_X509 *ca = NULL;
  2855. pt = (byte*)server_key_der_2048;
  2856. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2857. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2858. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2859. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2860. WOLFSSL_FILETYPE_ASN1));
  2861. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2862. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2863. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2864. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2865. /* check satisfying wolfssl.com constraint passes */
  2866. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2867. WOLFSSL_FILETYPE_PEM));
  2868. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2869. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2870. name = NULL;
  2871. ExpectNotNull(name = X509_NAME_new());
  2872. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2873. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2874. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2875. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2876. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2877. X509_NAME_free(name);
  2878. name = NULL;
  2879. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2880. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2881. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2882. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2883. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2884. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2885. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2886. wolfSSL_X509_free(x509);
  2887. x509 = NULL;
  2888. /* fail with DNS check because of common name */
  2889. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2890. WOLFSSL_FILETYPE_PEM));
  2891. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2892. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2893. name = NULL;
  2894. ExpectNotNull(name = X509_NAME_new());
  2895. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2896. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2897. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2898. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2899. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2900. X509_NAME_free(name);
  2901. name = NULL;
  2902. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2903. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2904. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2905. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2906. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2907. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2908. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2909. wolfSSL_X509_free(x509);
  2910. x509 = NULL;
  2911. /* fail on permitted DNS name constraint */
  2912. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2913. WOLFSSL_FILETYPE_PEM));
  2914. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2915. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2916. name = NULL;
  2917. ExpectNotNull(name = X509_NAME_new());
  2918. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2919. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2920. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2921. X509_NAME_free(name);
  2922. name = NULL;
  2923. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2924. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2925. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2926. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2927. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2928. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2929. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2930. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2931. wolfSSL_X509_free(x509);
  2932. x509 = NULL;
  2933. /* fail on permitted email name constraint */
  2934. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2935. WOLFSSL_FILETYPE_PEM));
  2936. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2937. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2938. name = NULL;
  2939. ExpectNotNull(name = X509_NAME_new());
  2940. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2941. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2942. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2943. X509_NAME_free(name);
  2944. name = NULL;
  2945. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2946. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2947. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2948. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2949. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2950. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2951. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2952. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2953. wolfSSL_X509_free(x509);
  2954. x509 = NULL;
  2955. /* success with empty email name */
  2956. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2957. WOLFSSL_FILETYPE_PEM));
  2958. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2959. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2960. name = NULL;
  2961. ExpectNotNull(name = X509_NAME_new());
  2962. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2963. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2964. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2965. X509_NAME_free(name);
  2966. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2967. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2968. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2969. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2970. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2971. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2972. wolfSSL_X509_free(x509);
  2973. wolfSSL_CertManagerFree(cm);
  2974. wolfSSL_X509_free(ca);
  2975. wolfSSL_EVP_PKEY_free(priv);
  2976. #endif
  2977. return EXPECT_RESULT();
  2978. }
  2979. static int test_wolfSSL_CertManagerCRL(void)
  2980. {
  2981. EXPECT_DECLS;
  2982. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2983. !defined(NO_RSA)
  2984. const char* ca_cert = "./certs/ca-cert.pem";
  2985. const char* crl1 = "./certs/crl/crl.pem";
  2986. const char* crl2 = "./certs/crl/crl2.pem";
  2987. #ifdef WC_RSA_PSS
  2988. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  2989. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  2990. #endif
  2991. const unsigned char crl_buff[] = {
  2992. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2993. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2994. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2995. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2996. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2997. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2998. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2999. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3000. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3001. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3002. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3003. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3004. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3005. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3006. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3007. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3008. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3009. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3010. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3011. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3012. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3013. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3014. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3015. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3016. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3017. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3018. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3019. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3020. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3021. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3022. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3023. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3024. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3025. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3026. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3027. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3028. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3029. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3030. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3031. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3032. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3033. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3034. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3035. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3036. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3037. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3038. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3039. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3040. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3041. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3042. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3043. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3044. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3045. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3046. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3047. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3048. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3049. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3050. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3051. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3052. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3053. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3054. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3055. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3056. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3057. };
  3058. WOLFSSL_CERT_MANAGER* cm = NULL;
  3059. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3060. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3061. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3062. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3063. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3064. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3065. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3066. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3067. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3068. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3069. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3070. BAD_FUNC_ARG);
  3071. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3072. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3073. BAD_FUNC_ARG);
  3074. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3075. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3076. BAD_FUNC_ARG);
  3077. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3078. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3079. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3080. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3081. #ifdef HAVE_CRL_IO
  3082. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3083. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3084. #endif
  3085. #ifndef NO_FILESYSTEM
  3086. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3087. 0), BAD_FUNC_ARG);
  3088. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3089. 0), BAD_FUNC_ARG);
  3090. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3091. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3092. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3093. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3094. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3095. BAD_FUNC_ARG);
  3096. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3097. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3098. ASN_PARSE_E);
  3099. #endif
  3100. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3101. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3102. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3103. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3104. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3105. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3106. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3107. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3108. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3109. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3110. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3111. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3112. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3113. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3114. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3115. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3116. ExpectIntEQ(WOLFSSL_SUCCESS,
  3117. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3118. ExpectIntEQ(WOLFSSL_SUCCESS,
  3119. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3120. ExpectIntEQ(WOLFSSL_SUCCESS,
  3121. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3122. wolfSSL_CertManagerFreeCRL(cm);
  3123. ExpectIntEQ(WOLFSSL_SUCCESS,
  3124. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3125. ExpectIntEQ(WOLFSSL_SUCCESS,
  3126. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3127. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3128. sizeof_server_cert_der_2048), CRL_MISSING);
  3129. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3130. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3131. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3132. WOLFSSL_FILETYPE_ASN1), 1);
  3133. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3134. /* loading should fail without the CA set */
  3135. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3136. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3137. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3138. ExpectIntEQ(WOLFSSL_SUCCESS,
  3139. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3140. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3141. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3142. #endif
  3143. wolfSSL_CertManagerFree(cm);
  3144. #endif
  3145. return EXPECT_RESULT();
  3146. }
  3147. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3148. {
  3149. EXPECT_DECLS;
  3150. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3151. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3152. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3153. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3154. defined(HAVE_LIGHTY)
  3155. WOLFSSL_CERT_MANAGER* cm = NULL;
  3156. /* Raw OCSP response bytes captured using the following setup:
  3157. * - Run responder with
  3158. * openssl ocsp -port 9999 -ndays 9999
  3159. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3160. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3161. * -rkey certs/ocsp/ocsp-responder-key.pem
  3162. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3163. * - Run client with
  3164. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3165. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3166. * -cert certs/ocsp/server1-cert.pem
  3167. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3168. * - Select the response packet in Wireshark, and export it using
  3169. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3170. * packets only". After importing into the editor, remove the initial
  3171. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3172. */
  3173. static const byte response[] = {
  3174. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3175. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3176. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3177. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3178. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3179. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3180. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3181. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3182. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3183. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3184. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3185. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3186. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3187. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3188. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3189. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3190. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3191. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3192. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3193. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3194. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3195. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3196. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3197. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3198. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3199. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3200. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3201. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3202. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3203. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3204. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3205. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3206. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3207. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3208. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3209. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3210. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3211. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3212. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3213. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3214. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3215. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3216. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3217. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3218. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3219. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3220. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3221. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3222. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3223. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3224. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3225. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3226. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3227. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3228. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3229. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3230. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3231. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3232. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3233. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3234. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3235. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3236. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3237. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3238. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3239. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3240. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3241. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3242. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3243. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3244. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3245. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3246. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3247. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3248. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3249. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3250. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3251. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3252. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3253. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3254. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3255. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3256. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3257. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3258. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3259. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3260. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3261. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3262. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3263. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3264. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3265. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3266. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3267. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3268. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3269. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3270. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3271. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3272. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3273. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3274. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3275. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3276. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3277. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3278. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3279. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3280. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3281. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3282. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3283. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3284. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3285. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3286. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3287. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3288. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3289. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3290. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3291. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3292. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3293. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3294. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3295. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3296. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3297. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3298. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3299. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3300. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3301. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3302. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3303. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3304. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3305. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3306. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3307. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3308. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3309. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3310. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3311. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3312. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3313. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3314. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3315. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3316. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3317. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3318. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3319. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3320. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3321. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3322. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3323. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3324. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3325. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3326. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3327. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3328. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3329. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3330. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3331. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3332. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3333. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3334. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3335. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3336. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3337. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3338. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3339. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3340. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3341. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3342. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3343. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3344. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3345. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3346. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3347. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3348. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3349. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3350. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3351. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3352. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3353. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3354. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3355. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3356. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3357. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3358. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3359. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3360. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3361. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3362. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3363. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3364. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3365. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3366. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3367. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3368. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3369. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3370. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3371. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3372. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3373. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3374. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3375. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3376. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3377. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3378. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3379. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3380. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3381. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3382. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3383. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3384. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3385. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3386. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3387. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3388. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3389. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3390. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3391. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3392. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3393. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3394. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3395. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3396. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3397. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3398. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3399. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3400. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3401. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3402. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3403. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3404. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3405. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3406. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3407. };
  3408. OcspEntry entry[1];
  3409. CertStatus status[1];
  3410. OcspRequest* request = NULL;
  3411. #ifndef NO_FILESYSTEM
  3412. const char* ca_cert = "./certs/ca-cert.pem";
  3413. #endif
  3414. byte serial[] = {0x05};
  3415. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3416. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3417. XMEMSET(entry, 0, sizeof(OcspEntry));
  3418. XMEMSET(status, 0, sizeof(CertStatus));
  3419. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3420. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3421. DYNAMIC_TYPE_OCSP_REQUEST));
  3422. if ((request != NULL) && (request->serial != NULL)) {
  3423. request->serialSz = sizeof(serial);
  3424. XMEMCPY(request->serial, serial, sizeof(serial));
  3425. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3426. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3427. }
  3428. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3429. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3430. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3431. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3432. /* Response should be valid. */
  3433. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3434. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3435. /* Flip a byte in the request serial number, response should be invalid
  3436. * now. */
  3437. if ((request != NULL) && (request->serial != NULL))
  3438. request->serial[0] ^= request->serial[0];
  3439. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3440. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3441. #ifndef NO_FILESYSTEM
  3442. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3443. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3444. ExpectIntEQ(WOLFSSL_SUCCESS,
  3445. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3446. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3447. sizeof(server_cert_der_2048)), 1);
  3448. #endif
  3449. wolfSSL_OCSP_REQUEST_free(request);
  3450. wolfSSL_CertManagerFree(cm);
  3451. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3452. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3453. #endif /* HAVE_OCSP */
  3454. return EXPECT_RESULT();
  3455. }
  3456. static int test_wolfSSL_CheckOCSPResponse(void)
  3457. {
  3458. EXPECT_DECLS;
  3459. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3460. defined(OPENSSL_ALL)
  3461. const char* responseFile = "./certs/ocsp/test-response.der";
  3462. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3463. const char* responseNoInternFile =
  3464. "./certs/ocsp/test-response-nointern.der";
  3465. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3466. OcspResponse* res = NULL;
  3467. byte data[4096];
  3468. const unsigned char* pt;
  3469. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3470. * gcc sanitizer with --enable-heapmath.
  3471. */
  3472. XFILE f = XBADFILE;
  3473. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3474. WOLFSSL_X509_STORE* st = NULL;
  3475. WOLFSSL_X509* issuer = NULL;
  3476. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3477. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3478. if (f != XBADFILE) {
  3479. XFCLOSE(f);
  3480. f = XBADFILE;
  3481. }
  3482. pt = data;
  3483. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3484. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3485. SSL_FILETYPE_PEM));
  3486. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3487. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3488. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3489. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3490. wolfSSL_OCSP_BASICRESP_free(bs);
  3491. bs = NULL;
  3492. wolfSSL_OCSP_RESPONSE_free(res);
  3493. res = NULL;
  3494. wolfSSL_X509_STORE_free(st);
  3495. st = NULL;
  3496. wolfSSL_X509_free(issuer);
  3497. issuer = NULL;
  3498. /* check loading a response with optional certs */
  3499. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3500. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3501. if (f != XBADFILE)
  3502. XFCLOSE(f);
  3503. f = XBADFILE;
  3504. pt = data;
  3505. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3506. wolfSSL_OCSP_RESPONSE_free(res);
  3507. res = NULL;
  3508. /* check loading a response with multiple certs */
  3509. {
  3510. WOLFSSL_CERT_MANAGER* cm = NULL;
  3511. OcspEntry *entry = NULL;
  3512. CertStatus* status = NULL;
  3513. OcspRequest* request = NULL;
  3514. byte serial1[] = {0x01};
  3515. byte serial[] = {0x02};
  3516. byte issuerHash[] = {
  3517. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3518. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3519. 0x37, 0x15, 0x8A, 0x88
  3520. };
  3521. byte issuerKeyHash[] = {
  3522. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3523. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3524. 0x7E, 0x72, 0x15, 0x21
  3525. };
  3526. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3527. DYNAMIC_TYPE_OPENSSL));
  3528. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3529. DYNAMIC_TYPE_OPENSSL));
  3530. if (entry != NULL)
  3531. XMEMSET(entry, 0, sizeof(OcspEntry));
  3532. if (status != NULL)
  3533. XMEMSET(status, 0, sizeof(CertStatus));
  3534. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3535. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3536. DYNAMIC_TYPE_OCSP_REQUEST));
  3537. if (request != NULL && request->serial != NULL) {
  3538. request->serialSz = sizeof(serial);
  3539. XMEMCPY(request->serial, serial, sizeof(serial));
  3540. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3541. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3542. sizeof(issuerKeyHash));
  3543. }
  3544. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3545. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3546. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3547. WOLFSSL_SUCCESS);
  3548. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3549. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3550. if (f != XBADFILE)
  3551. XFCLOSE(f);
  3552. f = XBADFILE;
  3553. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3554. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3555. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3556. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3557. ExpectNotNull(entry->status);
  3558. if (request != NULL && request->serial != NULL)
  3559. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3560. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3561. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3562. /* store both status's in the entry to check that "next" is not
  3563. * overwritten */
  3564. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3565. status->next = entry->status;
  3566. entry->status = status;
  3567. }
  3568. if (request != NULL && request->serial != NULL)
  3569. XMEMCPY(request->serial, serial, sizeof(serial));
  3570. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3571. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3572. ExpectNotNull(entry->status->next);
  3573. /* compare the status found */
  3574. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3575. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3576. status->serialSz), 0);
  3577. if (status != NULL && entry != NULL && entry->status != status) {
  3578. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3579. }
  3580. wolfSSL_OCSP_CERTID_free(entry);
  3581. wolfSSL_OCSP_REQUEST_free(request);
  3582. wolfSSL_CertManagerFree(cm);
  3583. }
  3584. #if defined(WC_RSA_PSS)
  3585. {
  3586. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3587. /* check loading a response with RSA-PSS signature */
  3588. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3589. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3590. if (f != XBADFILE)
  3591. XFCLOSE(f);
  3592. pt = data;
  3593. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3594. /* try to verify the response */
  3595. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3596. SSL_FILETYPE_PEM));
  3597. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3598. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3599. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3600. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3601. WOLFSSL_SUCCESS);
  3602. wolfSSL_OCSP_BASICRESP_free(bs);
  3603. wolfSSL_OCSP_RESPONSE_free(res);
  3604. wolfSSL_X509_STORE_free(st);
  3605. wolfSSL_X509_free(issuer);
  3606. }
  3607. #endif
  3608. #endif /* HAVE_OCSP */
  3609. return EXPECT_RESULT();
  3610. }
  3611. static int test_wolfSSL_FPKI(void)
  3612. {
  3613. EXPECT_DECLS;
  3614. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3615. XFILE f = XBADFILE;
  3616. const char* fpkiCert = "./certs/fpki-cert.der";
  3617. DecodedCert cert;
  3618. byte buf[4096];
  3619. byte* uuid = NULL;
  3620. byte* fascn = NULL;
  3621. word32 fascnSz;
  3622. word32 uuidSz;
  3623. int bytes = 0;
  3624. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3625. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3626. if (f != XBADFILE)
  3627. XFCLOSE(f);
  3628. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3629. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3630. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3631. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3632. DYNAMIC_TYPE_TMP_BUFFER));
  3633. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3634. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3635. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3636. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3637. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3638. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3639. wc_FreeDecodedCert(&cert);
  3640. #endif
  3641. return EXPECT_RESULT();
  3642. }
  3643. /* use RID in confuncture with other names to test parsing of unknown other
  3644. * names */
  3645. static int test_wolfSSL_OtherName(void)
  3646. {
  3647. EXPECT_DECLS;
  3648. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3649. XFILE f = XBADFILE;
  3650. const char* ridCert = "./certs/rid-cert.der";
  3651. DecodedCert cert;
  3652. byte buf[4096];
  3653. int bytes = 0;
  3654. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3655. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3656. if (f != XBADFILE)
  3657. XFCLOSE(f);
  3658. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3659. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3660. wc_FreeDecodedCert(&cert);
  3661. #endif
  3662. return EXPECT_RESULT();
  3663. }
  3664. static int test_wolfSSL_CertRsaPss(void)
  3665. {
  3666. EXPECT_DECLS;
  3667. /* FIPS v2 and below don't support long salts. */
  3668. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3669. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3670. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3671. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3672. XFILE f = XBADFILE;
  3673. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3674. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3675. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3676. RSA_MAX_SIZE >= 3072
  3677. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3678. #endif
  3679. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3680. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3681. #endif
  3682. DecodedCert cert;
  3683. byte buf[4096];
  3684. int bytes = 0;
  3685. WOLFSSL_CERT_MANAGER* cm = NULL;
  3686. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3687. ExpectIntEQ(WOLFSSL_SUCCESS,
  3688. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3689. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3690. ExpectIntEQ(WOLFSSL_SUCCESS,
  3691. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3692. #endif
  3693. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3694. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3695. if (f != XBADFILE) {
  3696. XFCLOSE(f);
  3697. f = XBADFILE;
  3698. }
  3699. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3700. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3701. wc_FreeDecodedCert(&cert);
  3702. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3703. RSA_MAX_SIZE >= 3072
  3704. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3705. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3706. if (f != XBADFILE)
  3707. XFCLOSE(f);
  3708. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3709. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3710. wc_FreeDecodedCert(&cert);
  3711. #endif
  3712. wolfSSL_CertManagerFree(cm);
  3713. #endif
  3714. return EXPECT_RESULT();
  3715. }
  3716. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3717. {
  3718. EXPECT_DECLS;
  3719. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3720. !defined(NO_WOLFSSL_CLIENT)
  3721. WOLFSSL_CTX* ctx = NULL;
  3722. const char* ca_cert = "./certs/ca-cert.pem";
  3723. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3724. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3725. /* test good CA */
  3726. ExpectTrue(WOLFSSL_SUCCESS ==
  3727. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3728. WOLFSSL_LOAD_FLAG_NONE));
  3729. /* test expired CA */
  3730. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3731. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3732. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3733. #else
  3734. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3735. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3736. #endif
  3737. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3738. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3739. wolfSSL_CTX_free(ctx);
  3740. #endif
  3741. return EXPECT_RESULT();
  3742. }
  3743. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3744. {
  3745. EXPECT_DECLS;
  3746. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3747. defined(USE_CERT_BUFFERS_2048)
  3748. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3749. WOLFSSL_CTX* ctx;
  3750. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3751. byte ca_expired_cert[TWOK_BUF];
  3752. word32 sizeof_ca_expired_cert = 0;
  3753. XFILE fp = XBADFILE;
  3754. #ifndef NO_WOLFSSL_CLIENT
  3755. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3756. #else
  3757. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3758. #endif
  3759. ExpectNotNull(ctx);
  3760. /* test good CA */
  3761. ExpectTrue(WOLFSSL_SUCCESS ==
  3762. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3763. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3764. WOLFSSL_LOAD_FLAG_NONE));
  3765. /* load expired CA */
  3766. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3767. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3768. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3769. sizeof(ca_expired_cert), fp), 0);
  3770. if (fp != XBADFILE)
  3771. XFCLOSE(fp);
  3772. /* test expired CA failure */
  3773. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3774. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3775. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3776. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3777. #else
  3778. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3779. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3780. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3781. #endif
  3782. /* test expired CA success */
  3783. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3784. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3785. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3786. wolfSSL_CTX_free(ctx);
  3787. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3788. #endif
  3789. return EXPECT_RESULT();
  3790. }
  3791. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3792. {
  3793. EXPECT_DECLS;
  3794. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3795. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3796. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3797. WOLFSSL_CTX* ctx = NULL;
  3798. #ifndef NO_WOLFSSL_CLIENT
  3799. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3800. #else
  3801. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3802. #endif
  3803. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3804. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3805. WOLFSSL_FILETYPE_ASN1));
  3806. wolfSSL_CTX_free(ctx);
  3807. #endif
  3808. return EXPECT_RESULT();
  3809. }
  3810. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3811. {
  3812. EXPECT_DECLS;
  3813. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3814. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3815. WOLFSSL_CTX* ctx;
  3816. WOLFSSL* ssl = NULL;
  3817. const char *certChain[] = {
  3818. "./certs/intermediate/client-int-cert.pem",
  3819. "./certs/intermediate/ca-int2-cert.pem",
  3820. "./certs/intermediate/ca-int-cert.pem",
  3821. "./certs/ca-cert.pem",
  3822. NULL
  3823. };
  3824. const char** cert;
  3825. WOLFSSL_X509* x509 = NULL;
  3826. WOLF_STACK_OF(X509)* chain = NULL;
  3827. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3828. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3829. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3830. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3831. WOLFSSL_FILETYPE_PEM));
  3832. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3833. X509_free(x509);
  3834. x509 = NULL;
  3835. }
  3836. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3837. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3838. WOLFSSL_FILETYPE_PEM));
  3839. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3840. X509_free(x509);
  3841. x509 = NULL;
  3842. }
  3843. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3844. ExpectIntEQ(sk_X509_num(chain), 3);
  3845. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3846. ExpectIntEQ(sk_X509_num(chain), 3);
  3847. SSL_free(ssl);
  3848. SSL_CTX_free(ctx);
  3849. #endif
  3850. return EXPECT_RESULT();
  3851. }
  3852. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3853. {
  3854. EXPECT_DECLS;
  3855. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3856. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3857. const char* server_chain_der = "./certs/server-cert-chain.der";
  3858. const char* client_single_pem = "./certs/client-cert.pem";
  3859. WOLFSSL_CTX* ctx;
  3860. (void)server_chain_der;
  3861. (void)client_single_pem;
  3862. (void)ctx;
  3863. #ifndef NO_WOLFSSL_CLIENT
  3864. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3865. #else
  3866. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3867. #endif
  3868. ExpectNotNull(ctx);
  3869. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3870. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3871. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3872. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3873. wolfSSL_CTX_free(ctx);
  3874. #endif
  3875. return EXPECT_RESULT();
  3876. }
  3877. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3878. {
  3879. EXPECT_DECLS;
  3880. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3881. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3882. WOLFSSL_CTX *ctx = NULL;
  3883. (void)ctx;
  3884. #ifndef NO_WOLFSSL_CLIENT
  3885. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3886. #else
  3887. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3888. #endif
  3889. /* invalid context */
  3890. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3891. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3892. /* invalid dhParamFile file */
  3893. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3894. NULL, WOLFSSL_FILETYPE_PEM));
  3895. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3896. bogusFile, WOLFSSL_FILETYPE_PEM));
  3897. /* success */
  3898. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3899. WOLFSSL_FILETYPE_PEM));
  3900. wolfSSL_CTX_free(ctx);
  3901. #endif
  3902. return EXPECT_RESULT();
  3903. }
  3904. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3905. {
  3906. EXPECT_DECLS;
  3907. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3908. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3909. WOLFSSL_CTX *ctx = NULL;
  3910. #ifndef NO_WOLFSSL_CLIENT
  3911. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3912. #else
  3913. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3914. #endif
  3915. /* invalid context */
  3916. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3917. dh_key_der_2048, sizeof_dh_key_der_2048,
  3918. WOLFSSL_FILETYPE_ASN1));
  3919. /* invalid dhParamFile file */
  3920. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3921. 0, WOLFSSL_FILETYPE_ASN1));
  3922. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3923. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3924. WOLFSSL_FILETYPE_ASN1));
  3925. /* success */
  3926. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3927. dh_key_der_2048, sizeof_dh_key_der_2048,
  3928. WOLFSSL_FILETYPE_ASN1));
  3929. wolfSSL_CTX_free(ctx);
  3930. #endif
  3931. return EXPECT_RESULT();
  3932. }
  3933. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3934. {
  3935. EXPECT_DECLS;
  3936. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3937. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3938. WOLFSSL_CTX *ctx;
  3939. (void)ctx;
  3940. #ifndef NO_WOLFSSL_CLIENT
  3941. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3942. #else
  3943. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3944. #endif
  3945. ExpectNotNull(ctx);
  3946. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3947. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3948. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3949. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3950. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3951. dh_key_der_2048, sizeof_dh_key_der_2048,
  3952. WOLFSSL_FILETYPE_ASN1));
  3953. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3954. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3955. dh_key_der_2048, sizeof_dh_key_der_2048,
  3956. WOLFSSL_FILETYPE_ASN1));
  3957. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3958. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3959. dh_key_der_2048, sizeof_dh_key_der_2048,
  3960. WOLFSSL_FILETYPE_ASN1));
  3961. wolfSSL_CTX_free(ctx);
  3962. #endif
  3963. return EXPECT_RESULT();
  3964. }
  3965. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3966. {
  3967. EXPECT_DECLS;
  3968. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3969. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3970. WOLFSSL_CTX* ctx = NULL;
  3971. const char* derCert = "./certs/server-cert.der";
  3972. const char* nullPath = NULL;
  3973. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3974. const char* emptyPath = "";
  3975. /* der load Case 1 ctx NULL */
  3976. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3977. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3978. #ifndef NO_WOLFSSL_CLIENT
  3979. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3980. #else
  3981. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3982. #endif
  3983. /* Case 2 filePath NULL */
  3984. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3985. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3986. /* Case 3 invalid format */
  3987. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3988. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3989. /* Case 4 filePath not valid */
  3990. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3991. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3992. /* Case 5 filePath empty */
  3993. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3994. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3995. #ifndef NO_RSA
  3996. /* Case 6 success case */
  3997. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3998. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3999. #endif
  4000. wolfSSL_CTX_free(ctx);
  4001. #endif
  4002. return EXPECT_RESULT();
  4003. }
  4004. static int test_wolfSSL_CTX_enable_disable(void)
  4005. {
  4006. EXPECT_DECLS;
  4007. #ifndef NO_CERTS
  4008. WOLFSSL_CTX* ctx = NULL;
  4009. #ifdef HAVE_CRL
  4010. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4011. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4012. #endif
  4013. #ifdef HAVE_OCSP
  4014. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4015. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4016. #endif
  4017. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4018. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4019. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4020. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4021. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4022. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4023. #endif
  4024. #ifndef NO_WOLFSSL_CLIENT
  4025. #ifdef HAVE_EXTENDED_MASTER
  4026. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4027. #endif
  4028. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4029. #ifdef HAVE_EXTENDED_MASTER
  4030. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4031. #endif
  4032. #elif !defined(NO_WOLFSSL_SERVER)
  4033. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4034. #endif
  4035. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4036. #ifdef HAVE_CRL
  4037. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4038. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4039. #endif
  4040. #ifdef HAVE_OCSP
  4041. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4042. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4043. WOLFSSL_SUCCESS);
  4044. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4045. WOLFSSL_SUCCESS);
  4046. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4047. WOLFSSL_SUCCESS);
  4048. #endif
  4049. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4050. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4051. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4052. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4053. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4054. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4055. #endif
  4056. wolfSSL_CTX_free(ctx);
  4057. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4058. #endif /* NO_CERTS */
  4059. return EXPECT_RESULT();
  4060. }
  4061. static int test_wolfSSL_CTX_ticket_API(void)
  4062. {
  4063. EXPECT_DECLS;
  4064. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4065. WOLFSSL_CTX* ctx = NULL;
  4066. void *userCtx = (void*)"this is my ctx";
  4067. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4068. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4069. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4070. wolfSSL_CTX_free(ctx);
  4071. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4072. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4073. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4074. return EXPECT_RESULT();
  4075. }
  4076. static int test_wolfSSL_set_minmax_proto_version(void)
  4077. {
  4078. EXPECT_DECLS;
  4079. #ifdef OPENSSL_EXTRA
  4080. WOLFSSL_CTX *ctx = NULL;
  4081. WOLFSSL *ssl = NULL;
  4082. (void)ssl;
  4083. #ifndef NO_WOLFSSL_CLIENT
  4084. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4085. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4086. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4087. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4088. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4089. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4090. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4091. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4092. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4093. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4094. wolfSSL_free(ssl);
  4095. wolfSSL_CTX_free(ctx);
  4096. ctx = NULL;
  4097. #endif
  4098. #ifndef NO_WOLFSSL_SERVER
  4099. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4100. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4101. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4102. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4103. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4104. wolfSSL_CTX_free(ctx);
  4105. #endif
  4106. #endif
  4107. return EXPECT_RESULT();
  4108. }
  4109. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4110. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4111. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4112. {
  4113. EXPECT_DECLS;
  4114. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4115. return EXPECT_RESULT();
  4116. }
  4117. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4118. {
  4119. EXPECT_DECLS;
  4120. /* Set TLS 1.2 */
  4121. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4122. WOLFSSL_SUCCESS);
  4123. return EXPECT_RESULT();
  4124. }
  4125. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4126. * what was set at ctx creation. */
  4127. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4128. {
  4129. EXPECT_DECLS;
  4130. test_ssl_cbf client_cbs;
  4131. test_ssl_cbf server_cbs;
  4132. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4133. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4134. client_cbs.method = wolfTLS_client_method;
  4135. server_cbs.method = wolfTLS_server_method;
  4136. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4137. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4138. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4139. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4140. &server_cbs, NULL), TEST_SUCCESS);
  4141. return EXPECT_RESULT();
  4142. }
  4143. #else
  4144. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4145. {
  4146. return TEST_SKIPPED;
  4147. }
  4148. #endif
  4149. /*----------------------------------------------------------------------------*
  4150. | SSL
  4151. *----------------------------------------------------------------------------*/
  4152. static int test_server_wolfSSL_new(void)
  4153. {
  4154. EXPECT_DECLS;
  4155. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4156. !defined(NO_WOLFSSL_SERVER)
  4157. WOLFSSL_CTX *ctx = NULL;
  4158. WOLFSSL_CTX *ctx_nocert = NULL;
  4159. WOLFSSL *ssl = NULL;
  4160. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4162. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4163. WOLFSSL_FILETYPE_PEM));
  4164. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4165. WOLFSSL_FILETYPE_PEM));
  4166. /* invalid context */
  4167. ExpectNull(ssl = wolfSSL_new(NULL));
  4168. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4169. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4170. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4171. #endif
  4172. /* success */
  4173. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4174. wolfSSL_free(ssl);
  4175. wolfSSL_CTX_free(ctx);
  4176. wolfSSL_CTX_free(ctx_nocert);
  4177. #endif
  4178. return EXPECT_RESULT();
  4179. }
  4180. static int test_client_wolfSSL_new(void)
  4181. {
  4182. EXPECT_DECLS;
  4183. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4184. !defined(NO_WOLFSSL_CLIENT)
  4185. WOLFSSL_CTX *ctx = NULL;
  4186. WOLFSSL_CTX *ctx_nocert = NULL;
  4187. WOLFSSL *ssl = NULL;
  4188. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4189. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4190. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4191. /* invalid context */
  4192. ExpectNull(ssl = wolfSSL_new(NULL));
  4193. /* success */
  4194. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4195. wolfSSL_free(ssl);
  4196. ssl = NULL;
  4197. /* success */
  4198. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4199. wolfSSL_free(ssl);
  4200. wolfSSL_CTX_free(ctx);
  4201. wolfSSL_CTX_free(ctx_nocert);
  4202. #endif
  4203. return EXPECT_RESULT();
  4204. }
  4205. static int test_wolfSSL_SetTmpDH_file(void)
  4206. {
  4207. EXPECT_DECLS;
  4208. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4209. !defined(NO_WOLFSSL_SERVER)
  4210. WOLFSSL_CTX *ctx = NULL;
  4211. WOLFSSL *ssl = NULL;
  4212. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4213. #ifndef NO_RSA
  4214. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4215. WOLFSSL_FILETYPE_PEM));
  4216. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4217. WOLFSSL_FILETYPE_PEM));
  4218. #elif defined(HAVE_ECC)
  4219. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4220. WOLFSSL_FILETYPE_PEM));
  4221. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4222. WOLFSSL_FILETYPE_PEM));
  4223. #elif defined(HAVE_ED25519)
  4224. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4225. WOLFSSL_FILETYPE_PEM));
  4226. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4227. WOLFSSL_FILETYPE_PEM));
  4228. #elif defined(HAVE_ED448)
  4229. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4230. WOLFSSL_FILETYPE_PEM));
  4231. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4232. WOLFSSL_FILETYPE_PEM));
  4233. #endif
  4234. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4235. /* invalid ssl */
  4236. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4237. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4238. /* invalid dhParamFile file */
  4239. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4240. NULL, WOLFSSL_FILETYPE_PEM));
  4241. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4242. bogusFile, WOLFSSL_FILETYPE_PEM));
  4243. /* success */
  4244. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4245. WOLFSSL_FILETYPE_PEM));
  4246. wolfSSL_free(ssl);
  4247. wolfSSL_CTX_free(ctx);
  4248. #endif
  4249. return EXPECT_RESULT();
  4250. }
  4251. static int test_wolfSSL_SetTmpDH_buffer(void)
  4252. {
  4253. EXPECT_DECLS;
  4254. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4255. WOLFSSL_CTX *ctx = NULL;
  4256. WOLFSSL *ssl = NULL;
  4257. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4258. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4259. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4260. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4261. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4262. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4263. /* invalid ssl */
  4264. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4265. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4266. /* invalid dhParamFile file */
  4267. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4268. 0, WOLFSSL_FILETYPE_ASN1));
  4269. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4270. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4271. /* success */
  4272. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4273. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4274. wolfSSL_free(ssl);
  4275. wolfSSL_CTX_free(ctx);
  4276. #endif
  4277. return EXPECT_RESULT();
  4278. }
  4279. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4280. {
  4281. EXPECT_DECLS;
  4282. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4283. WOLFSSL_CTX *ctx = NULL;
  4284. WOLFSSL_CTX *ctx2 = NULL;
  4285. WOLFSSL *ssl = NULL;
  4286. WOLFSSL *ssl2 = NULL;
  4287. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4288. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4289. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4290. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4291. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4292. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4293. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4294. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4295. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4296. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4297. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4298. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4299. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4300. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4301. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4302. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4303. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4304. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4305. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4306. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4307. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4308. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4309. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4310. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4311. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4312. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4313. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4314. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4315. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4316. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4317. wolfSSL_free(ssl2);
  4318. wolfSSL_CTX_free(ctx2);
  4319. wolfSSL_free(ssl);
  4320. wolfSSL_CTX_free(ctx);
  4321. #endif
  4322. return EXPECT_RESULT();
  4323. }
  4324. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4325. * allowed.
  4326. * POST: return 1 on success.
  4327. */
  4328. static int test_wolfSSL_SetMinVersion(void)
  4329. {
  4330. int res = TEST_SKIPPED;
  4331. #ifndef NO_WOLFSSL_CLIENT
  4332. int failFlag = WOLFSSL_SUCCESS;
  4333. WOLFSSL_CTX* ctx = NULL;
  4334. WOLFSSL* ssl = NULL;
  4335. int itr;
  4336. #ifndef NO_OLD_TLS
  4337. const int versions[] = {
  4338. #ifdef WOLFSSL_ALLOW_TLSV10
  4339. WOLFSSL_TLSV1,
  4340. #endif
  4341. WOLFSSL_TLSV1_1,
  4342. WOLFSSL_TLSV1_2};
  4343. #elif !defined(WOLFSSL_NO_TLS12)
  4344. const int versions[] = { WOLFSSL_TLSV1_2 };
  4345. #else
  4346. const int versions[] = { WOLFSSL_TLSV1_3 };
  4347. #endif
  4348. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4349. ssl = wolfSSL_new(ctx);
  4350. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4351. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4352. failFlag = WOLFSSL_FAILURE;
  4353. }
  4354. }
  4355. wolfSSL_free(ssl);
  4356. wolfSSL_CTX_free(ctx);
  4357. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4358. #endif
  4359. return res;
  4360. } /* END test_wolfSSL_SetMinVersion */
  4361. #ifdef OPENSSL_EXTRA
  4362. static int test_EC25519(void)
  4363. {
  4364. EXPECT_DECLS;
  4365. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4366. byte priv[CURVE25519_KEYSIZE];
  4367. unsigned int privSz = CURVE25519_KEYSIZE;
  4368. byte pub[CURVE25519_KEYSIZE];
  4369. unsigned int pubSz = CURVE25519_KEYSIZE;
  4370. byte priv2[CURVE25519_KEYSIZE];
  4371. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4372. byte pub2[CURVE25519_KEYSIZE];
  4373. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4374. byte shared[CURVE25519_KEYSIZE];
  4375. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4376. byte shared2[CURVE25519_KEYSIZE];
  4377. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4378. /* Bad parameter testing of key generation. */
  4379. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4380. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4381. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4382. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4383. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4384. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4385. /* Bad length */
  4386. privSz = 1;
  4387. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4388. privSz = CURVE25519_KEYSIZE;
  4389. pubSz = 1;
  4390. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4391. pubSz = CURVE25519_KEYSIZE;
  4392. /* Good case of generating key. */
  4393. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4394. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4395. 1);
  4396. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4397. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4398. /* Bad parameter testing of shared key. */
  4399. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4400. NULL, pubSz), 0);
  4401. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4402. NULL, pubSz), 0);
  4403. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4404. pub, pubSz), 0);
  4405. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4406. pub, pubSz), 0);
  4407. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4408. NULL, pubSz), 0);
  4409. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4410. pub, pubSz), 0);
  4411. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4412. pub, pubSz), 0);
  4413. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4414. pub, pubSz), 0);
  4415. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4416. NULL, pubSz), 0);
  4417. /* Bad length. */
  4418. sharedSz = 1;
  4419. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4420. pub, pubSz), 0);
  4421. sharedSz = CURVE25519_KEYSIZE;
  4422. privSz = 1;
  4423. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4424. pub, pubSz), 0);
  4425. privSz = CURVE25519_KEYSIZE;
  4426. pubSz = 1;
  4427. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4428. pub, pubSz), 0);
  4429. pubSz = CURVE25519_KEYSIZE;
  4430. /* Good case of shared key. */
  4431. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4432. pub2, pub2Sz), 1);
  4433. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4434. pub, pubSz), 1);
  4435. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4436. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4437. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4438. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4439. return EXPECT_RESULT();
  4440. }
  4441. static int test_ED25519(void)
  4442. {
  4443. EXPECT_DECLS;
  4444. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4445. defined(WOLFSSL_KEY_GEN)
  4446. byte priv[ED25519_PRV_KEY_SIZE];
  4447. unsigned int privSz = (unsigned int)sizeof(priv);
  4448. byte pub[ED25519_PUB_KEY_SIZE];
  4449. unsigned int pubSz = (unsigned int)sizeof(pub);
  4450. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4451. const char* msg = TEST_STRING;
  4452. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4453. byte sig[ED25519_SIG_SIZE];
  4454. unsigned int sigSz = (unsigned int)sizeof(sig);
  4455. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4456. /* Bad parameter testing of key generation. */
  4457. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4458. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4459. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4460. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4461. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4462. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4463. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4464. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4465. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4466. /* Bad length. */
  4467. privSz = 1;
  4468. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4469. privSz = ED25519_PRV_KEY_SIZE;
  4470. pubSz = 1;
  4471. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4472. pubSz = ED25519_PUB_KEY_SIZE;
  4473. /* Good case of generating key. */
  4474. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4475. 1);
  4476. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4477. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4478. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4479. /* Bad parameter testing of signing. */
  4480. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4481. NULL), 0);
  4482. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4483. NULL), 0);
  4484. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4485. NULL), 0);
  4486. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4487. NULL), 0);
  4488. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4489. &sigSz), 0);
  4490. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4491. &sigSz), 0);
  4492. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4493. &sigSz), 0);
  4494. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4495. &sigSz), 0);
  4496. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4497. NULL), 0);
  4498. /* Bad length. */
  4499. privSz = 1;
  4500. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4501. &sigSz), 0);
  4502. privSz = ED25519_PRV_KEY_SIZE;
  4503. sigSz = 1;
  4504. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4505. &sigSz), 0);
  4506. sigSz = ED25519_SIG_SIZE;
  4507. /* Good case of signing. */
  4508. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4509. &sigSz), 1);
  4510. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4511. #ifdef HAVE_ED25519_VERIFY
  4512. /* Bad parameter testing of verification. */
  4513. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4514. sigSz), 0);
  4515. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4516. sigSz), 0);
  4517. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4518. sigSz), 0);
  4519. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4520. sigSz), 0);
  4521. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4522. sigSz), 0);
  4523. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4524. sigSz), 0);
  4525. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4526. sigSz), 0);
  4527. /* Bad length. */
  4528. pubSz = 1;
  4529. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4530. sigSz), 0);
  4531. pubSz = ED25519_PUB_KEY_SIZE;
  4532. sigSz = 1;
  4533. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4534. sigSz), 0);
  4535. sigSz = ED25519_SIG_SIZE;
  4536. /* Good case of verification. */
  4537. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4538. sigSz), 1);
  4539. /* Bad signature. */
  4540. if (EXPECT_SUCCESS()) {
  4541. sig[1] ^= 0x80;
  4542. }
  4543. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4544. sigSz), 0);
  4545. #endif /* HAVE_ED25519_VERIFY */
  4546. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4547. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4548. return EXPECT_RESULT();
  4549. }
  4550. static int test_EC448(void)
  4551. {
  4552. EXPECT_DECLS;
  4553. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4554. byte priv[CURVE448_KEY_SIZE];
  4555. unsigned int privSz = CURVE448_KEY_SIZE;
  4556. byte pub[CURVE448_KEY_SIZE];
  4557. unsigned int pubSz = CURVE448_KEY_SIZE;
  4558. byte priv2[CURVE448_KEY_SIZE];
  4559. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4560. byte pub2[CURVE448_KEY_SIZE];
  4561. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4562. byte shared[CURVE448_KEY_SIZE];
  4563. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4564. byte shared2[CURVE448_KEY_SIZE];
  4565. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4566. /* Bad parameter testing of key generation. */
  4567. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4568. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4569. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4570. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4571. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4572. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4573. /* Bad length. */
  4574. privSz = 1;
  4575. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4576. privSz = CURVE448_KEY_SIZE;
  4577. pubSz = 1;
  4578. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4579. pubSz = CURVE448_KEY_SIZE;
  4580. /* Good case of generating key. */
  4581. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4582. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4583. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4584. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4585. /* Bad parameter testing of shared key. */
  4586. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4587. NULL, pubSz), 0);
  4588. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4589. NULL, pubSz), 0);
  4590. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4591. pub, pubSz), 0);
  4592. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4593. pub, pubSz), 0);
  4594. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4595. NULL, pubSz), 0);
  4596. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4597. pub, pubSz), 0);
  4598. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4599. pub, pubSz), 0);
  4600. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4601. pub, pubSz), 0);
  4602. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4603. NULL, pubSz), 0);
  4604. /* Bad length. */
  4605. sharedSz = 1;
  4606. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4607. pub, pubSz), 0);
  4608. sharedSz = CURVE448_KEY_SIZE;
  4609. privSz = 1;
  4610. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4611. pub, pubSz), 0);
  4612. privSz = CURVE448_KEY_SIZE;
  4613. pubSz = 1;
  4614. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4615. pub, pubSz), 0);
  4616. pubSz = CURVE448_KEY_SIZE;
  4617. /* Good case of shared key. */
  4618. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4619. pub2, pub2Sz), 1);
  4620. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4621. pub, pubSz), 1);
  4622. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4623. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4624. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4625. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4626. return EXPECT_RESULT();
  4627. }
  4628. static int test_ED448(void)
  4629. {
  4630. EXPECT_DECLS;
  4631. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4632. defined(WOLFSSL_KEY_GEN)
  4633. byte priv[ED448_PRV_KEY_SIZE];
  4634. unsigned int privSz = (unsigned int)sizeof(priv);
  4635. byte pub[ED448_PUB_KEY_SIZE];
  4636. unsigned int pubSz = (unsigned int)sizeof(pub);
  4637. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4638. const char* msg = TEST_STRING;
  4639. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4640. byte sig[ED448_SIG_SIZE];
  4641. unsigned int sigSz = (unsigned int)sizeof(sig);
  4642. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4643. /* Bad parameter testing of key generation. */
  4644. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4645. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4646. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4647. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4648. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4649. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4650. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4651. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4652. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4653. /* Bad length. */
  4654. privSz = 1;
  4655. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4656. privSz = ED448_PRV_KEY_SIZE;
  4657. pubSz = 1;
  4658. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4659. pubSz = ED448_PUB_KEY_SIZE;
  4660. /* Good case of generating key. */
  4661. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4662. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4663. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4664. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4665. /* Bad parameter testing of signing. */
  4666. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4667. NULL), 0);
  4668. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4669. NULL), 0);
  4670. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4671. NULL), 0);
  4672. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4673. NULL), 0);
  4674. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4675. &sigSz), 0);
  4676. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4677. &sigSz), 0);
  4678. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4679. &sigSz), 0);
  4680. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4681. &sigSz), 0);
  4682. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4683. NULL), 0);
  4684. /* Bad length. */
  4685. privSz = 1;
  4686. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4687. &sigSz), 0);
  4688. privSz = ED448_PRV_KEY_SIZE;
  4689. sigSz = 1;
  4690. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4691. &sigSz), 0);
  4692. sigSz = ED448_SIG_SIZE;
  4693. /* Good case of signing. */
  4694. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4695. &sigSz), 1);
  4696. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4697. #ifdef HAVE_ED448_VERIFY
  4698. /* Bad parameter testing of verification. */
  4699. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4700. sigSz), 0);
  4701. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4702. sigSz), 0);
  4703. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4704. sigSz), 0);
  4705. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4706. sigSz), 0);
  4707. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4708. sigSz), 0);
  4709. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4710. sigSz), 0);
  4711. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4712. sigSz), 0);
  4713. /* Bad length. */
  4714. pubSz = 1;
  4715. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4716. sigSz), 0);
  4717. pubSz = ED448_PUB_KEY_SIZE;
  4718. sigSz = 1;
  4719. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4720. sigSz), 0);
  4721. sigSz = ED448_SIG_SIZE;
  4722. /* Good case of verification. */
  4723. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4724. sigSz), 1);
  4725. /* Bad signature. */
  4726. if (EXPECT_SUCCESS()) {
  4727. sig[1] ^= 0x80;
  4728. }
  4729. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4730. sigSz), 0);
  4731. #endif /* HAVE_ED448_VERIFY */
  4732. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4733. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4734. return EXPECT_RESULT();
  4735. }
  4736. #endif /* OPENSSL_EXTRA */
  4737. #include <wolfssl/openssl/pem.h>
  4738. /*----------------------------------------------------------------------------*
  4739. | EVP
  4740. *----------------------------------------------------------------------------*/
  4741. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4742. {
  4743. EXPECT_DECLS;
  4744. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4745. WOLFSSL_BIO* rbio = NULL;
  4746. WOLFSSL_BIO* wbio = NULL;
  4747. WOLFSSL_EVP_PKEY* pkey = NULL;
  4748. char line[256] = { 0 };
  4749. char line1[256] = { 0 };
  4750. int i = 0;
  4751. /* test error cases */
  4752. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4753. /*
  4754. * test RSA public key print
  4755. * in this test, pass '3' for indent
  4756. */
  4757. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4758. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4759. sizeof_client_keypub_der_1024));
  4760. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4761. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4762. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4763. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4764. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4765. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4766. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4767. strcpy(line1, " Modulus:\n");
  4768. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4769. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4770. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4771. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4772. /* skip to the end of modulus element*/
  4773. for (i = 0; i < 8 ;i++) {
  4774. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4775. }
  4776. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4777. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4778. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4779. /* should reach EOF */
  4780. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4781. EVP_PKEY_free(pkey);
  4782. pkey = NULL;
  4783. BIO_free(rbio);
  4784. BIO_free(wbio);
  4785. rbio = NULL;
  4786. wbio = NULL;
  4787. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4788. /*
  4789. * test DSA public key print
  4790. */
  4791. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4792. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4793. sizeof_dsa_pub_key_der_2048));
  4794. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4795. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4796. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4797. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4798. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4799. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4800. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4801. strcpy(line1, "pub:\n");
  4802. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4803. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4804. strcpy(line1,
  4805. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4806. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4807. /* skip to the end of pub element*/
  4808. for (i = 0; i < 17 ;i++) {
  4809. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4810. }
  4811. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4812. strcpy(line1, "P:\n");
  4813. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4814. /* skip to the end of P element*/
  4815. for (i = 0; i < 18 ;i++) {
  4816. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4817. }
  4818. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4819. strcpy(line1, "Q:\n");
  4820. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4821. /* skip to the end of Q element*/
  4822. for (i = 0; i < 3 ;i++) {
  4823. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4824. }
  4825. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4826. strcpy(line1, "G:\n");
  4827. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4828. /* skip to the end of G element*/
  4829. for (i = 0; i < 18 ;i++) {
  4830. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4831. }
  4832. /* should reach EOF */
  4833. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4834. EVP_PKEY_free(pkey);
  4835. pkey = NULL;
  4836. BIO_free(rbio);
  4837. BIO_free(wbio);
  4838. rbio = NULL;
  4839. wbio = NULL;
  4840. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4841. /*
  4842. * test ECC public key print
  4843. */
  4844. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4845. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4846. sizeof_ecc_clikeypub_der_256));
  4847. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4848. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4849. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4850. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4851. strcpy(line1, "Public-Key: (256 bit)\n");
  4852. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4853. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4854. strcpy(line1, "pub:\n");
  4855. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4856. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4857. strcpy(line1,
  4858. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4859. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4860. /* skip to the end of pub element*/
  4861. for (i = 0; i < 4 ;i++) {
  4862. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4863. }
  4864. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4865. strcpy(line1, "ASN1 OID: prime256v1\n");
  4866. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4867. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4868. strcpy(line1, "NIST CURVE: P-256\n");
  4869. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4870. /* should reach EOF */
  4871. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4872. EVP_PKEY_free(pkey);
  4873. pkey = NULL;
  4874. BIO_free(rbio);
  4875. BIO_free(wbio);
  4876. rbio = NULL;
  4877. wbio = NULL;
  4878. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4879. /*
  4880. * test DH public key print
  4881. */
  4882. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4883. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4884. sizeof_dh_pub_key_der_2048));
  4885. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4886. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4887. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4888. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4889. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4890. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4891. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4892. strcpy(line1, "public-key:\n");
  4893. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4894. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4895. strcpy(line1,
  4896. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4897. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4898. /* skip to the end of public-key element*/
  4899. for (i = 0; i < 17 ;i++) {
  4900. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4901. }
  4902. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4903. strcpy(line1, "prime:\n");
  4904. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4905. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4906. strcpy(line1,
  4907. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4908. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4909. /* skip to the end of prime element*/
  4910. for (i = 0; i < 17 ;i++) {
  4911. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4912. }
  4913. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4914. strcpy(line1, "generator: 2 (0x02)\n");
  4915. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4916. /* should reach EOF */
  4917. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4918. EVP_PKEY_free(pkey);
  4919. pkey = NULL;
  4920. BIO_free(rbio);
  4921. BIO_free(wbio);
  4922. rbio = NULL;
  4923. wbio = NULL;
  4924. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4925. /* to prevent "unused variable" warning */
  4926. (void)pkey;
  4927. (void)wbio;
  4928. (void)rbio;
  4929. (void)line;
  4930. (void)line1;
  4931. (void)i;
  4932. #endif /* OPENSSL_EXTRA */
  4933. return EXPECT_RESULT();
  4934. }
  4935. /* Test functions for base64 encode/decode */
  4936. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4937. {
  4938. EXPECT_DECLS;
  4939. #if defined(OPENSSL_EXTRA) && \
  4940. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4941. EVP_ENCODE_CTX* ctx = NULL;
  4942. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4943. ExpectIntEQ(ctx->remaining,0);
  4944. ExpectIntEQ(ctx->data[0],0);
  4945. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4946. EVP_ENCODE_CTX_free(ctx);
  4947. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4948. return EXPECT_RESULT();
  4949. }
  4950. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4951. {
  4952. EXPECT_DECLS;
  4953. #if defined(OPENSSL_EXTRA) && \
  4954. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4955. EVP_ENCODE_CTX* ctx = NULL;
  4956. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4957. EVP_ENCODE_CTX_free(ctx);
  4958. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4959. return EXPECT_RESULT();
  4960. }
  4961. static int test_wolfSSL_EVP_EncodeInit(void)
  4962. {
  4963. EXPECT_DECLS;
  4964. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4965. EVP_ENCODE_CTX* ctx = NULL;
  4966. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4967. ExpectIntEQ(ctx->remaining, 0);
  4968. ExpectIntEQ(ctx->data[0], 0);
  4969. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4970. if (ctx != NULL) {
  4971. /* make ctx dirty */
  4972. ctx->remaining = 10;
  4973. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4974. }
  4975. EVP_EncodeInit(ctx);
  4976. ExpectIntEQ(ctx->remaining, 0);
  4977. ExpectIntEQ(ctx->data[0], 0);
  4978. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4979. EVP_ENCODE_CTX_free(ctx);
  4980. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4981. return EXPECT_RESULT();
  4982. }
  4983. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4984. {
  4985. EXPECT_DECLS;
  4986. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4987. int outl;
  4988. int total;
  4989. const unsigned char plain0[] = {"Th"};
  4990. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4991. const unsigned char plain2[] = {"This is additional data."};
  4992. const unsigned char encBlock0[] = {"VGg="};
  4993. const unsigned char enc0[] = {"VGg=\n"};
  4994. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4995. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4996. const unsigned char enc2[] =
  4997. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4998. unsigned char encOutBuff[300];
  4999. EVP_ENCODE_CTX* ctx = NULL;
  5000. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5001. EVP_EncodeInit(ctx);
  5002. /* illegal parameter test */
  5003. ExpectIntEQ(
  5004. EVP_EncodeUpdate(
  5005. NULL, /* pass NULL as ctx */
  5006. encOutBuff,
  5007. &outl,
  5008. plain1,
  5009. sizeof(plain1)-1),
  5010. 0 /* expected result code 0: fail */
  5011. );
  5012. ExpectIntEQ(
  5013. EVP_EncodeUpdate(
  5014. ctx,
  5015. NULL, /* pass NULL as out buff */
  5016. &outl,
  5017. plain1,
  5018. sizeof(plain1)-1),
  5019. 0 /* expected result code 0: fail */
  5020. );
  5021. ExpectIntEQ(
  5022. EVP_EncodeUpdate(
  5023. ctx,
  5024. encOutBuff,
  5025. NULL, /* pass NULL as outl */
  5026. plain1,
  5027. sizeof(plain1)-1),
  5028. 0 /* expected result code 0: fail */
  5029. );
  5030. ExpectIntEQ(
  5031. EVP_EncodeUpdate(
  5032. ctx,
  5033. encOutBuff,
  5034. &outl,
  5035. NULL, /* pass NULL as in */
  5036. sizeof(plain1)-1),
  5037. 0 /* expected result code 0: fail */
  5038. );
  5039. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5040. /* meaningless parameter test */
  5041. ExpectIntEQ(
  5042. EVP_EncodeUpdate(
  5043. ctx,
  5044. encOutBuff,
  5045. &outl,
  5046. plain1,
  5047. 0), /* pass zero input */
  5048. 1 /* expected result code 1: success */
  5049. );
  5050. /* very small data encoding test */
  5051. EVP_EncodeInit(ctx);
  5052. ExpectIntEQ(
  5053. EVP_EncodeUpdate(
  5054. ctx,
  5055. encOutBuff,
  5056. &outl,
  5057. plain0,
  5058. sizeof(plain0)-1),
  5059. 1 /* expected result code 1: success */
  5060. );
  5061. ExpectIntEQ(outl,0);
  5062. if (EXPECT_SUCCESS()) {
  5063. EVP_EncodeFinal(
  5064. ctx,
  5065. encOutBuff + outl,
  5066. &outl);
  5067. }
  5068. ExpectIntEQ( outl, sizeof(enc0)-1);
  5069. ExpectIntEQ(
  5070. XSTRNCMP(
  5071. (const char*)encOutBuff,
  5072. (const char*)enc0,sizeof(enc0) ),
  5073. 0);
  5074. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5075. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5076. sizeof(encBlock0)-1);
  5077. ExpectStrEQ(encOutBuff, encBlock0);
  5078. /* pass small size( < 48bytes ) input, then make sure they are not
  5079. * encoded and just stored in ctx
  5080. */
  5081. EVP_EncodeInit(ctx);
  5082. total = 0;
  5083. outl = 0;
  5084. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5085. ExpectIntEQ(
  5086. EVP_EncodeUpdate(
  5087. ctx,
  5088. encOutBuff, /* buffer for output */
  5089. &outl, /* size of output */
  5090. plain1, /* input */
  5091. sizeof(plain1)-1), /* size of input */
  5092. 1); /* expected result code 1:success */
  5093. total += outl;
  5094. ExpectIntEQ(outl, 0); /* no output expected */
  5095. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5096. ExpectTrue(
  5097. XSTRNCMP((const char*)(ctx->data),
  5098. (const char*)plain1,
  5099. ctx->remaining) ==0 );
  5100. ExpectTrue(encOutBuff[0] == 0);
  5101. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5102. * the stored data and the new input together
  5103. */
  5104. ExpectIntEQ(
  5105. EVP_EncodeUpdate(
  5106. ctx,
  5107. encOutBuff + outl, /* buffer for output */
  5108. &outl, /* size of output */
  5109. plain2, /* additional input */
  5110. sizeof(plain2) -1), /* size of additional input */
  5111. 1); /* expected result code 1:success */
  5112. total += outl;
  5113. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5114. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5115. ExpectIntEQ(
  5116. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5117. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5118. EVP_EncodeFinal(
  5119. ctx,
  5120. encOutBuff + outl,
  5121. &outl);
  5122. total += outl;
  5123. ExpectIntNE(total,0);
  5124. ExpectIntNE(outl,0);
  5125. ExpectIntEQ(XSTRNCMP(
  5126. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5127. /* test with illeagal parameters */
  5128. outl = 1;
  5129. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5130. ExpectIntEQ(outl, 0);
  5131. outl = 1;
  5132. EVP_EncodeFinal(ctx, NULL, &outl);
  5133. ExpectIntEQ(outl, 0);
  5134. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5135. EVP_EncodeFinal(NULL, NULL, NULL);
  5136. EVP_ENCODE_CTX_free(ctx);
  5137. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5138. return EXPECT_RESULT();
  5139. }
  5140. static int test_wolfSSL_EVP_EncodeFinal(void)
  5141. {
  5142. int res = TEST_SKIPPED;
  5143. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5144. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5145. * test_wolfSSL_EVP_EncodeUpdate
  5146. */
  5147. res = TEST_SUCCESS;
  5148. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5149. return res;
  5150. }
  5151. static int test_wolfSSL_EVP_DecodeInit(void)
  5152. {
  5153. EXPECT_DECLS;
  5154. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5155. EVP_ENCODE_CTX* ctx = NULL;
  5156. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5157. ExpectIntEQ( ctx->remaining,0);
  5158. ExpectIntEQ( ctx->data[0],0);
  5159. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5160. if (ctx != NULL) {
  5161. /* make ctx dirty */
  5162. ctx->remaining = 10;
  5163. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5164. }
  5165. EVP_DecodeInit(ctx);
  5166. ExpectIntEQ( ctx->remaining,0);
  5167. ExpectIntEQ( ctx->data[0],0);
  5168. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5169. EVP_ENCODE_CTX_free(ctx);
  5170. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5171. return EXPECT_RESULT();
  5172. }
  5173. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5174. {
  5175. EXPECT_DECLS;
  5176. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5177. int outl;
  5178. unsigned char decOutBuff[300];
  5179. EVP_ENCODE_CTX* ctx = NULL;
  5180. static const unsigned char enc1[] =
  5181. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5182. /* const unsigned char plain1[] =
  5183. {"This is a base64 decoding test."} */
  5184. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5185. EVP_DecodeInit(ctx);
  5186. /* illegal parameter tests */
  5187. /* pass NULL as ctx */
  5188. ExpectIntEQ(
  5189. EVP_DecodeUpdate(
  5190. NULL, /* pass NULL as ctx */
  5191. decOutBuff,
  5192. &outl,
  5193. enc1,
  5194. sizeof(enc1)-1),
  5195. -1 /* expected result code -1: fail */
  5196. );
  5197. ExpectIntEQ( outl, 0);
  5198. /* pass NULL as output */
  5199. ExpectIntEQ(
  5200. EVP_DecodeUpdate(
  5201. ctx,
  5202. NULL, /* pass NULL as out buff */
  5203. &outl,
  5204. enc1,
  5205. sizeof(enc1)-1),
  5206. -1 /* expected result code -1: fail */
  5207. );
  5208. ExpectIntEQ( outl, 0);
  5209. /* pass NULL as outl */
  5210. ExpectIntEQ(
  5211. EVP_DecodeUpdate(
  5212. ctx,
  5213. decOutBuff,
  5214. NULL, /* pass NULL as outl */
  5215. enc1,
  5216. sizeof(enc1)-1),
  5217. -1 /* expected result code -1: fail */
  5218. );
  5219. /* pass NULL as input */
  5220. ExpectIntEQ(
  5221. EVP_DecodeUpdate(
  5222. ctx,
  5223. decOutBuff,
  5224. &outl,
  5225. NULL, /* pass NULL as in */
  5226. sizeof(enc1)-1),
  5227. -1 /* expected result code -1: fail */
  5228. );
  5229. ExpectIntEQ( outl, 0);
  5230. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5231. /* pass zero length input */
  5232. ExpectIntEQ(
  5233. EVP_DecodeUpdate(
  5234. ctx,
  5235. decOutBuff,
  5236. &outl,
  5237. enc1,
  5238. 0), /* pass zero as input len */
  5239. 1 /* expected result code 1: success */
  5240. );
  5241. /* decode correct base64 string */
  5242. {
  5243. static const unsigned char enc2[] =
  5244. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5245. static const unsigned char plain2[] =
  5246. {"This is a base64 decoding test."};
  5247. EVP_EncodeInit(ctx);
  5248. ExpectIntEQ(
  5249. EVP_DecodeUpdate(
  5250. ctx,
  5251. decOutBuff,
  5252. &outl,
  5253. enc2,
  5254. sizeof(enc2)-1),
  5255. 0 /* expected result code 0: success */
  5256. );
  5257. ExpectIntEQ(outl,sizeof(plain2) -1);
  5258. ExpectIntEQ(
  5259. EVP_DecodeFinal(
  5260. ctx,
  5261. decOutBuff + outl,
  5262. &outl),
  5263. 1 /* expected result code 1: success */
  5264. );
  5265. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5266. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5267. sizeof(plain2) -1 ),0);
  5268. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5269. sizeof(plain2)-1);
  5270. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5271. sizeof(plain2) -1 ),0);
  5272. }
  5273. /* decode correct base64 string which does not have '\n' in its last*/
  5274. {
  5275. static const unsigned char enc3[] =
  5276. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5277. static const unsigned char plain3[] =
  5278. {"This is a base64 decoding test."}; /* 31 chars */
  5279. EVP_EncodeInit(ctx);
  5280. ExpectIntEQ(
  5281. EVP_DecodeUpdate(
  5282. ctx,
  5283. decOutBuff,
  5284. &outl,
  5285. enc3,
  5286. sizeof(enc3)-1),
  5287. 0 /* expected result code 0: success */
  5288. );
  5289. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5290. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5291. sizeof(plain3) -1 ),0);
  5292. ExpectIntEQ(
  5293. EVP_DecodeFinal(
  5294. ctx,
  5295. decOutBuff + outl,
  5296. &outl),
  5297. 1 /* expected result code 1: success */
  5298. );
  5299. ExpectIntEQ(outl,0 );
  5300. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5301. sizeof(plain3)-1);
  5302. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5303. sizeof(plain3) -1 ),0);
  5304. }
  5305. /* decode string which has a padding char ('=') in the illegal position*/
  5306. {
  5307. static const unsigned char enc4[] =
  5308. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5309. EVP_EncodeInit(ctx);
  5310. ExpectIntEQ(
  5311. EVP_DecodeUpdate(
  5312. ctx,
  5313. decOutBuff,
  5314. &outl,
  5315. enc4,
  5316. sizeof(enc4)-1),
  5317. -1 /* expected result code -1: error */
  5318. );
  5319. ExpectIntEQ(outl,0);
  5320. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5321. }
  5322. /* small data decode test */
  5323. {
  5324. static const unsigned char enc00[] = {"VG"};
  5325. static const unsigned char enc01[] = {"g=\n"};
  5326. static const unsigned char plain4[] = {"Th"};
  5327. EVP_EncodeInit(ctx);
  5328. ExpectIntEQ(
  5329. EVP_DecodeUpdate(
  5330. ctx,
  5331. decOutBuff,
  5332. &outl,
  5333. enc00,
  5334. sizeof(enc00)-1),
  5335. 1 /* expected result code 1: success */
  5336. );
  5337. ExpectIntEQ(outl,0);
  5338. ExpectIntEQ(
  5339. EVP_DecodeUpdate(
  5340. ctx,
  5341. decOutBuff + outl,
  5342. &outl,
  5343. enc01,
  5344. sizeof(enc01)-1),
  5345. 0 /* expected result code 0: success */
  5346. );
  5347. ExpectIntEQ(outl,sizeof(plain4)-1);
  5348. /* test with illegal parameters */
  5349. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5350. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5351. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5352. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5353. if (EXPECT_SUCCESS()) {
  5354. EVP_DecodeFinal(
  5355. ctx,
  5356. decOutBuff + outl,
  5357. &outl);
  5358. }
  5359. ExpectIntEQ( outl, 0);
  5360. ExpectIntEQ(
  5361. XSTRNCMP(
  5362. (const char*)decOutBuff,
  5363. (const char*)plain4,sizeof(plain4)-1 ),
  5364. 0);
  5365. }
  5366. EVP_ENCODE_CTX_free(ctx);
  5367. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5368. return EXPECT_RESULT();
  5369. }
  5370. static int test_wolfSSL_EVP_DecodeFinal(void)
  5371. {
  5372. int res = TEST_SKIPPED;
  5373. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5374. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5375. * test_wolfSSL_EVP_DecodeUpdate
  5376. */
  5377. res = TEST_SUCCESS;
  5378. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5379. return res;
  5380. }
  5381. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5382. */
  5383. #ifdef OPENSSL_EXTRA
  5384. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5385. {
  5386. EXPECT_DECLS;
  5387. #ifndef NO_AES
  5388. const WOLFSSL_EVP_CIPHER* c;
  5389. c = wolfSSL_EVP_get_cipherbynid(419);
  5390. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5391. defined(WOLFSSL_AES_128)
  5392. ExpectNotNull(c);
  5393. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5394. #else
  5395. ExpectNull(c);
  5396. #endif
  5397. c = wolfSSL_EVP_get_cipherbynid(423);
  5398. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5399. defined(WOLFSSL_AES_192)
  5400. ExpectNotNull(c);
  5401. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5402. #else
  5403. ExpectNull(c);
  5404. #endif
  5405. c = wolfSSL_EVP_get_cipherbynid(427);
  5406. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5407. defined(WOLFSSL_AES_256)
  5408. ExpectNotNull(c);
  5409. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5410. #else
  5411. ExpectNull(c);
  5412. #endif
  5413. c = wolfSSL_EVP_get_cipherbynid(904);
  5414. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5415. ExpectNotNull(c);
  5416. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5417. #else
  5418. ExpectNull(c);
  5419. #endif
  5420. c = wolfSSL_EVP_get_cipherbynid(905);
  5421. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5422. ExpectNotNull(c);
  5423. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5424. #else
  5425. ExpectNull(c);
  5426. #endif
  5427. c = wolfSSL_EVP_get_cipherbynid(906);
  5428. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5429. ExpectNotNull(c);
  5430. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5431. #else
  5432. ExpectNull(c);
  5433. #endif
  5434. c = wolfSSL_EVP_get_cipherbynid(418);
  5435. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5436. ExpectNotNull(c);
  5437. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5438. #else
  5439. ExpectNull(c);
  5440. #endif
  5441. c = wolfSSL_EVP_get_cipherbynid(422);
  5442. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5443. ExpectNotNull(c);
  5444. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5445. #else
  5446. ExpectNull(c);
  5447. #endif
  5448. c = wolfSSL_EVP_get_cipherbynid(426);
  5449. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5450. ExpectNotNull(c);
  5451. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5452. #else
  5453. ExpectNull(c);
  5454. #endif
  5455. #endif /* !NO_AES */
  5456. #ifndef NO_DES3
  5457. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5458. #ifdef WOLFSSL_DES_ECB
  5459. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5460. #endif
  5461. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5462. #ifdef WOLFSSL_DES_ECB
  5463. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5464. #endif
  5465. #endif /* !NO_DES3 */
  5466. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5467. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5468. #endif
  5469. /* test for nid is out of range */
  5470. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5471. return EXPECT_RESULT();
  5472. }
  5473. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5474. {
  5475. EXPECT_DECLS;
  5476. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5477. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5478. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5479. const EVP_CIPHER *test;
  5480. byte key[AES_BLOCK_SIZE] = {0};
  5481. byte iv[AES_BLOCK_SIZE] = {0};
  5482. ExpectNotNull(ctx);
  5483. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5484. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5485. test = EVP_CIPHER_CTX_cipher(ctx);
  5486. ExpectTrue(init == test);
  5487. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5488. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5489. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5490. EVP_CIPHER_CTX_free(ctx);
  5491. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5492. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5493. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5494. return EXPECT_RESULT();
  5495. }
  5496. #endif /* OPENSSL_EXTRA */
  5497. /*----------------------------------------------------------------------------*
  5498. | IO
  5499. *----------------------------------------------------------------------------*/
  5500. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5501. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5502. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5503. #ifdef WC_SHA512_DIGEST_SIZE
  5504. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5505. #else
  5506. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5507. #endif
  5508. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5509. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5510. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5511. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5512. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5513. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5514. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5515. defined(HAVE_AES_CBC)
  5516. typedef struct openssl_key_ctx {
  5517. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5518. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5519. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5520. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5521. } openssl_key_ctx;
  5522. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5523. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5524. static WC_INLINE int OpenSSLTicketInit(void)
  5525. {
  5526. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5527. if (ret != 0) return ret;
  5528. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5529. sizeof(myOpenSSLKey_ctx.name));
  5530. if (ret != 0) return ret;
  5531. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5532. sizeof(myOpenSSLKey_ctx.key));
  5533. if (ret != 0) return ret;
  5534. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5535. sizeof(myOpenSSLKey_ctx.hmacKey));
  5536. if (ret != 0) return ret;
  5537. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5538. sizeof(myOpenSSLKey_ctx.iv));
  5539. if (ret != 0) return ret;
  5540. return 0;
  5541. }
  5542. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5543. byte name[WOLFSSL_TICKET_NAME_SZ],
  5544. byte iv[WOLFSSL_TICKET_IV_SZ],
  5545. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5546. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5547. (void)ssl;
  5548. if (enc) {
  5549. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5550. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5551. }
  5552. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5553. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5554. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5555. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5556. return 0;
  5557. }
  5558. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5559. if (enc)
  5560. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5561. else
  5562. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5563. return 1;
  5564. }
  5565. static WC_INLINE void OpenSSLTicketCleanup(void)
  5566. {
  5567. wc_FreeRng(&myOpenSSLKey_rng);
  5568. }
  5569. #endif
  5570. #endif
  5571. /* helper functions */
  5572. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5573. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5574. void *ctx)
  5575. {
  5576. struct test_ssl_memio_ctx *test_ctx;
  5577. byte *buf;
  5578. int *len;
  5579. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5580. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5581. buf = test_ctx->c_buff;
  5582. len = &test_ctx->c_len;
  5583. }
  5584. else {
  5585. buf = test_ctx->s_buff;
  5586. len = &test_ctx->s_len;
  5587. }
  5588. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5589. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5590. XMEMCPY(buf + *len, data, sz);
  5591. *len += sz;
  5592. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5593. {
  5594. /* This can be imported into Wireshark by transforming the file with
  5595. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5596. * And then loading test_output.dump.hex into Wireshark using the
  5597. * "Import from Hex Dump..." option ion and selecting the TCP
  5598. * encapsulation option. */
  5599. char dump_file_name[64];
  5600. WOLFSSL_BIO *dump_file;
  5601. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5602. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5603. if (dump_file != NULL) {
  5604. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5605. wolfSSL_BIO_free(dump_file);
  5606. }
  5607. }
  5608. #endif
  5609. return sz;
  5610. }
  5611. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5612. void *ctx)
  5613. {
  5614. struct test_ssl_memio_ctx *test_ctx;
  5615. int read_sz;
  5616. byte *buf;
  5617. int *len;
  5618. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5619. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5620. buf = test_ctx->s_buff;
  5621. len = &test_ctx->s_len;
  5622. }
  5623. else {
  5624. buf = test_ctx->c_buff;
  5625. len = &test_ctx->c_len;
  5626. }
  5627. if (*len == 0)
  5628. return WOLFSSL_CBIO_ERR_WANT_READ;
  5629. read_sz = sz < *len ? sz : *len;
  5630. XMEMCPY(data, buf, read_sz);
  5631. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5632. *len -= read_sz;
  5633. return read_sz;
  5634. }
  5635. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5636. {
  5637. EXPECT_DECLS;
  5638. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5639. int c_sharedCtx = 0;
  5640. int s_sharedCtx = 0;
  5641. #endif
  5642. const char* clientCertFile = cliCertFile;
  5643. const char* clientKeyFile = cliKeyFile;
  5644. const char* serverCertFile = svrCertFile;
  5645. const char* serverKeyFile = svrKeyFile;
  5646. /********************************
  5647. * Create WOLFSSL_CTX for client.
  5648. ********************************/
  5649. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5650. if (ctx->c_ctx != NULL) {
  5651. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5652. }
  5653. else
  5654. #endif
  5655. {
  5656. WOLFSSL_METHOD* method = NULL;
  5657. if (ctx->c_cb.method != NULL) {
  5658. method = ctx->c_cb.method();
  5659. }
  5660. else {
  5661. method = wolfSSLv23_client_method();
  5662. }
  5663. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5664. }
  5665. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5666. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5667. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5668. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5669. #endif
  5670. if (ctx->c_cb.caPemFile != NULL)
  5671. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5672. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5673. else
  5674. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5675. caCertFile, 0), WOLFSSL_SUCCESS);
  5676. if (ctx->c_cb.certPemFile != NULL) {
  5677. clientCertFile = ctx->c_cb.certPemFile;
  5678. }
  5679. if (ctx->c_cb.keyPemFile != NULL) {
  5680. clientKeyFile = ctx->c_cb.keyPemFile;
  5681. }
  5682. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5683. if (!c_sharedCtx)
  5684. #endif
  5685. {
  5686. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5687. clientCertFile), WOLFSSL_SUCCESS);
  5688. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5689. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5690. }
  5691. #ifdef HAVE_CRL
  5692. if (ctx->c_cb.crlPemFile != NULL) {
  5693. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5694. WOLFSSL_SUCCESS);
  5695. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5696. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5697. }
  5698. #endif
  5699. if (ctx->c_ciphers != NULL) {
  5700. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5701. WOLFSSL_SUCCESS);
  5702. }
  5703. if (ctx->c_cb.ctx_ready != NULL) {
  5704. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5705. }
  5706. /********************************
  5707. * Create WOLFSSL_CTX for server.
  5708. ********************************/
  5709. if (ctx->s_ctx != NULL) {
  5710. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5711. s_sharedCtx = 1;
  5712. #endif
  5713. ctx->s_cb.isSharedCtx = 1;
  5714. }
  5715. else
  5716. {
  5717. WOLFSSL_METHOD* method = NULL;
  5718. if (ctx->s_cb.method != NULL) {
  5719. method = ctx->s_cb.method();
  5720. }
  5721. else {
  5722. method = wolfSSLv23_server_method();
  5723. }
  5724. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5725. ctx->s_cb.isSharedCtx = 0;
  5726. }
  5727. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5728. #if defined(HAVE_SESSION_TICKET) && \
  5729. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5730. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5731. OpenSSLTicketInit();
  5732. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5733. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5734. TicketInit();
  5735. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5736. #endif
  5737. #endif
  5738. }
  5739. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5740. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5741. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5742. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5743. if (ctx->s_cb.caPemFile != NULL)
  5744. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5745. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5746. else
  5747. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5748. cliCertFile, 0), WOLFSSL_SUCCESS);
  5749. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5750. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5751. #endif
  5752. if (ctx->s_cb.certPemFile != NULL) {
  5753. serverCertFile = ctx->s_cb.certPemFile;
  5754. }
  5755. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5756. if (!s_sharedCtx)
  5757. #endif
  5758. {
  5759. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5760. serverCertFile), WOLFSSL_SUCCESS);
  5761. }
  5762. if (ctx->s_cb.keyPemFile != NULL) {
  5763. serverKeyFile = ctx->s_cb.keyPemFile;
  5764. }
  5765. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5766. if (!s_sharedCtx)
  5767. #endif
  5768. {
  5769. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5770. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5771. }
  5772. if (ctx->s_ciphers != NULL) {
  5773. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5774. WOLFSSL_SUCCESS);
  5775. }
  5776. if (ctx->s_cb.ctx_ready != NULL) {
  5777. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5778. }
  5779. /****************************
  5780. * Create WOLFSSL for client.
  5781. ****************************/
  5782. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5783. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5784. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5785. if (0
  5786. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5787. || c_sharedCtx
  5788. #endif
  5789. )
  5790. {
  5791. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5792. clientCertFile), WOLFSSL_SUCCESS);
  5793. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5794. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5795. }
  5796. if (ctx->c_cb.ssl_ready != NULL) {
  5797. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5798. }
  5799. /****************************
  5800. * Create WOLFSSL for server.
  5801. ****************************/
  5802. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5803. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5804. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5805. if (0
  5806. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5807. || s_sharedCtx
  5808. #endif
  5809. )
  5810. {
  5811. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5812. serverCertFile), WOLFSSL_SUCCESS);
  5813. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5814. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5815. }
  5816. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5817. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5818. #elif !defined(NO_DH)
  5819. /* will repick suites with DHE, higher priority than PSK */
  5820. SetDH(ctx->s_ssl);
  5821. #endif
  5822. if (ctx->s_cb.ssl_ready != NULL) {
  5823. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5824. }
  5825. return EXPECT_RESULT();
  5826. }
  5827. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5828. int* rounds)
  5829. {
  5830. int handshake_complete = 0;
  5831. int hs_c = 0;
  5832. int hs_s = 0;
  5833. int failing_s = 0;
  5834. int failing_c = 0;
  5835. int ret;
  5836. int err;
  5837. if (rounds != NULL) {
  5838. *rounds = 0;
  5839. }
  5840. while ((!handshake_complete) && (max_rounds > 0)) {
  5841. if (!hs_c) {
  5842. wolfSSL_SetLoggingPrefix("client");
  5843. ret = wolfSSL_connect(ctx->c_ssl);
  5844. wolfSSL_SetLoggingPrefix(NULL);
  5845. if (ret == WOLFSSL_SUCCESS) {
  5846. hs_c = 1;
  5847. }
  5848. else {
  5849. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5850. if (err != WOLFSSL_ERROR_WANT_READ &&
  5851. err != WOLFSSL_ERROR_WANT_WRITE) {
  5852. char buff[WOLFSSL_MAX_ERROR_SZ];
  5853. fprintf(stderr, "error = %d, %s\n", err,
  5854. wolfSSL_ERR_error_string(err, buff));
  5855. failing_c = 1;
  5856. hs_c = 1;
  5857. if (failing_c && failing_s) {
  5858. break;
  5859. }
  5860. }
  5861. }
  5862. }
  5863. if (!hs_s) {
  5864. wolfSSL_SetLoggingPrefix("server");
  5865. ret = wolfSSL_accept(ctx->s_ssl);
  5866. wolfSSL_SetLoggingPrefix(NULL);
  5867. if (ret == WOLFSSL_SUCCESS) {
  5868. hs_s = 1;
  5869. }
  5870. else {
  5871. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5872. if (err != WOLFSSL_ERROR_WANT_READ &&
  5873. err != WOLFSSL_ERROR_WANT_WRITE) {
  5874. char buff[WOLFSSL_MAX_ERROR_SZ];
  5875. fprintf(stderr, "error = %d, %s\n", err,
  5876. wolfSSL_ERR_error_string(err, buff));
  5877. failing_s = 1;
  5878. hs_s = 1;
  5879. if (failing_c && failing_s) {
  5880. break;
  5881. }
  5882. }
  5883. }
  5884. }
  5885. handshake_complete = hs_c && hs_s;
  5886. max_rounds--;
  5887. if (rounds != NULL) {
  5888. *rounds += 1;
  5889. }
  5890. }
  5891. if (!handshake_complete || failing_c || failing_s) {
  5892. return TEST_FAIL;
  5893. }
  5894. return TEST_SUCCESS;
  5895. }
  5896. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5897. {
  5898. EXPECT_DECLS;
  5899. char input[1024];
  5900. int idx = 0;
  5901. const char* msg_c = "hello wolfssl!";
  5902. int msglen_c = (int)XSTRLEN(msg_c);
  5903. const char* msg_s = "I hear you fa shizzle!";
  5904. int msglen_s = (int)XSTRLEN(msg_s);
  5905. if (ctx->c_msg != NULL) {
  5906. msg_c = ctx->c_msg;
  5907. msglen_c = ctx->c_msglen;
  5908. }
  5909. if (ctx->s_msg != NULL) {
  5910. msg_s = ctx->s_msg;
  5911. msglen_s = ctx->s_msglen;
  5912. }
  5913. wolfSSL_SetLoggingPrefix("client");
  5914. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5915. wolfSSL_SetLoggingPrefix("server");
  5916. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5917. if (idx >= 0) {
  5918. input[idx] = '\0';
  5919. }
  5920. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5921. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5922. ctx->s_cb.return_code = EXPECT_RESULT();
  5923. wolfSSL_SetLoggingPrefix("client");
  5924. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5925. wolfSSL_SetLoggingPrefix(NULL);
  5926. if (idx >= 0) {
  5927. input[idx] = '\0';
  5928. }
  5929. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5930. ctx->c_cb.return_code = EXPECT_RESULT();
  5931. if (ctx->c_cb.on_result != NULL) {
  5932. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5933. }
  5934. if (ctx->s_cb.on_result != NULL) {
  5935. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5936. }
  5937. return EXPECT_RESULT();
  5938. }
  5939. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5940. {
  5941. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5942. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5943. if (ctx->c_cb.on_cleanup != NULL) {
  5944. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5945. }
  5946. if (ctx->s_cb.on_cleanup != NULL) {
  5947. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5948. }
  5949. wolfSSL_shutdown(ctx->s_ssl);
  5950. wolfSSL_shutdown(ctx->c_ssl);
  5951. wolfSSL_free(ctx->s_ssl);
  5952. wolfSSL_free(ctx->c_ssl);
  5953. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  5954. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  5955. }
  5956. if (!ctx->c_cb.isSharedCtx) {
  5957. wolfSSL_CTX_free(ctx->c_ctx);
  5958. ctx->c_ctx = NULL;
  5959. }
  5960. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  5961. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  5962. }
  5963. if (!ctx->s_cb.isSharedCtx) {
  5964. wolfSSL_CTX_free(ctx->s_ctx);
  5965. ctx->s_ctx = NULL;
  5966. }
  5967. if (!ctx->s_cb.ticNoInit) {
  5968. #if defined(HAVE_SESSION_TICKET) && \
  5969. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5970. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5971. OpenSSLTicketCleanup();
  5972. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5973. TicketCleanup();
  5974. #endif
  5975. #endif
  5976. }
  5977. }
  5978. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5979. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5980. {
  5981. EXPECT_DECLS;
  5982. struct test_ssl_memio_ctx test_ctx;
  5983. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5984. size_t msg_len;
  5985. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5986. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5987. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5988. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5989. test_ctx.c_ctx = client_cb->ctx;
  5990. test_ctx.s_ctx = server_cb->ctx;
  5991. test_ctx.c_cb.return_code = TEST_FAIL;
  5992. test_ctx.s_cb.return_code = TEST_FAIL;
  5993. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5994. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5995. if (client_on_handshake != NULL) {
  5996. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5997. TEST_SUCCESS);
  5998. }
  5999. if (client_cb->on_handshake != NULL) {
  6000. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6001. TEST_SUCCESS);
  6002. }
  6003. if (server_cb->on_handshake != NULL) {
  6004. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6005. TEST_SUCCESS);
  6006. }
  6007. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6008. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6009. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6010. MD_MAX_SIZE);
  6011. ExpectIntGE(msg_len, 0);
  6012. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6013. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6014. MD_MAX_SIZE);
  6015. ExpectIntGE(msg_len, 0);
  6016. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6017. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6018. test_ssl_memio_cleanup(&test_ctx);
  6019. client_cb->return_code = test_ctx.c_cb.return_code;
  6020. client_cb->last_err = test_ctx.c_cb.last_err;
  6021. server_cb->return_code = test_ctx.s_cb.return_code;
  6022. server_cb->last_err = test_ctx.s_cb.last_err;
  6023. return EXPECT_RESULT();
  6024. }
  6025. #endif
  6026. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6027. #ifdef WOLFSSL_SESSION_EXPORT
  6028. #ifdef WOLFSSL_DTLS
  6029. /* set up function for sending session information */
  6030. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6031. {
  6032. WOLFSSL_CTX* ctx = NULL;
  6033. WOLFSSL* ssl = NULL;
  6034. AssertNotNull(inSsl);
  6035. AssertNotNull(buf);
  6036. AssertIntNE(0, sz);
  6037. /* Set ctx to DTLS 1.2 */
  6038. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6039. AssertNotNull(ctx);
  6040. ssl = wolfSSL_new(ctx);
  6041. AssertNotNull(ssl);
  6042. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6043. wolfSSL_free(ssl);
  6044. wolfSSL_CTX_free(ctx);
  6045. (void)userCtx;
  6046. return 0;
  6047. }
  6048. #endif
  6049. /* returns negative value on fail and positive (including 0) on success */
  6050. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6051. {
  6052. int ret, err, loop_count, count, timeout = 10;
  6053. char msg[] = "I hear you fa shizzle!";
  6054. char input[1024];
  6055. loop_count = ((func_args*)args)->argc;
  6056. #ifdef WOLFSSL_ASYNC_CRYPT
  6057. err = 0; /* Reset error */
  6058. #endif
  6059. do {
  6060. #ifdef WOLFSSL_ASYNC_CRYPT
  6061. if (err == WC_PENDING_E) {
  6062. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6063. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6064. }
  6065. #endif
  6066. ret = wolfSSL_accept(ssl);
  6067. err = wolfSSL_get_error(ssl, 0);
  6068. if (err == WOLFSSL_ERROR_WANT_READ ||
  6069. err == WOLFSSL_ERROR_WANT_WRITE) {
  6070. int select_ret;
  6071. err = WC_PENDING_E;
  6072. select_ret = tcp_select(*sockfd, timeout);
  6073. if (select_ret == TEST_TIMEOUT) {
  6074. return WOLFSSL_FATAL_ERROR;
  6075. }
  6076. }
  6077. } while (err == WC_PENDING_E);
  6078. if (ret != WOLFSSL_SUCCESS) {
  6079. char buff[WOLFSSL_MAX_ERROR_SZ];
  6080. fprintf(stderr, "error = %d, %s\n", err,
  6081. wolfSSL_ERR_error_string(err, buff));
  6082. return ret;
  6083. }
  6084. for (count = 0; count < loop_count; count++) {
  6085. int select_ret;
  6086. select_ret = tcp_select(*sockfd, timeout);
  6087. if (select_ret == TEST_TIMEOUT) {
  6088. ret = WOLFSSL_FATAL_ERROR;
  6089. break;
  6090. }
  6091. do {
  6092. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6093. if (ret > 0) {
  6094. input[ret] = '\0';
  6095. fprintf(stderr, "Client message: %s\n", input);
  6096. }
  6097. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6098. do {
  6099. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6100. return WOLFSSL_FATAL_ERROR;
  6101. }
  6102. err = wolfSSL_get_error(ssl, ret);
  6103. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6104. }
  6105. return ret;
  6106. }
  6107. #endif /* WOLFSSL_SESSION_EXPORT */
  6108. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6109. {
  6110. SOCKET_T sockfd = 0;
  6111. SOCKET_T clientfd = 0;
  6112. word16 port;
  6113. callback_functions* cbf;
  6114. WOLFSSL_CTX* ctx = NULL;
  6115. WOLFSSL* ssl = NULL;
  6116. func_args* opts = (func_args*)args;
  6117. char msg[] = "I hear you fa shizzle!";
  6118. char input[1024];
  6119. int idx;
  6120. int ret, err = 0;
  6121. int sharedCtx = 0;
  6122. int doUdp = 0;
  6123. SOCKADDR_IN_T cliAddr;
  6124. socklen_t cliLen;
  6125. const char* certFile = svrCertFile;
  6126. const char* keyFile = svrKeyFile;
  6127. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6128. size_t msg_len = 0;
  6129. #endif
  6130. wolfSSL_SetLoggingPrefix("server");
  6131. #ifdef WOLFSSL_TIRTOS
  6132. fdOpenSession(Task_self());
  6133. #endif
  6134. opts->return_code = TEST_FAIL;
  6135. cbf = opts->callbacks;
  6136. if (cbf != NULL && cbf->ctx) {
  6137. ctx = cbf->ctx;
  6138. sharedCtx = 1;
  6139. }
  6140. else
  6141. {
  6142. WOLFSSL_METHOD* method = NULL;
  6143. if (cbf != NULL && cbf->method != NULL) {
  6144. method = cbf->method();
  6145. }
  6146. else {
  6147. method = wolfSSLv23_server_method();
  6148. }
  6149. ctx = wolfSSL_CTX_new(method);
  6150. }
  6151. if (ctx == NULL) {
  6152. /* Release the wait for TCP ready. */
  6153. signal_ready(opts->signal);
  6154. goto done;
  6155. }
  6156. if (cbf == NULL || !cbf->ticNoInit) {
  6157. #if defined(HAVE_SESSION_TICKET) && \
  6158. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6159. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6160. OpenSSLTicketInit();
  6161. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6162. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6163. TicketInit();
  6164. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6165. #endif
  6166. #endif
  6167. }
  6168. #if defined(USE_WINDOWS_API)
  6169. port = opts->signal->port;
  6170. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6171. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6172. /* Let tcp_listen assign port */
  6173. port = 0;
  6174. #else
  6175. /* Use default port */
  6176. port = wolfSSLPort;
  6177. #endif
  6178. if (cbf != NULL)
  6179. doUdp = cbf->doUdp;
  6180. /* do it here to detect failure */
  6181. tcp_accept(
  6182. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6183. if (doUdp) {
  6184. cliLen = sizeof(cliAddr);
  6185. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6186. (struct sockaddr*)&cliAddr, &cliLen);
  6187. AssertIntGT(idx, 0);
  6188. }
  6189. else {
  6190. CloseSocket(sockfd);
  6191. }
  6192. wolfSSL_CTX_set_verify(ctx,
  6193. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6194. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6195. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6196. #endif
  6197. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6198. != WOLFSSL_SUCCESS) {
  6199. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6200. goto done;
  6201. }
  6202. if (cbf != NULL && cbf->certPemFile != NULL)
  6203. certFile = cbf->certPemFile;
  6204. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6205. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6206. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6207. #else
  6208. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6209. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6210. #endif
  6211. /*err_sys("can't load server cert chain file, "
  6212. "Please run from wolfSSL home dir");*/
  6213. goto done;
  6214. }
  6215. if (cbf != NULL && cbf->keyPemFile != NULL)
  6216. keyFile = cbf->keyPemFile;
  6217. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6218. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6219. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6220. #else
  6221. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6222. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6223. #endif
  6224. /*err_sys("can't load server key file, "
  6225. "Please run from wolfSSL home dir");*/
  6226. goto done;
  6227. }
  6228. #ifdef HAVE_CRL
  6229. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6230. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6231. goto done;
  6232. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6233. != WOLFSSL_SUCCESS)
  6234. goto done;
  6235. }
  6236. #endif
  6237. /* call ctx setup callback */
  6238. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6239. cbf->ctx_ready(ctx);
  6240. }
  6241. ssl = wolfSSL_new(ctx);
  6242. if (ssl == NULL) {
  6243. goto done;
  6244. }
  6245. if (doUdp) {
  6246. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6247. if (err != WOLFSSL_SUCCESS)
  6248. goto done;
  6249. }
  6250. #ifdef WOLFSSL_SESSION_EXPORT
  6251. /* only add in more complex nonblocking case with session export tests */
  6252. if (args && opts->argc > 0) {
  6253. /* set as nonblock and time out for waiting on read/write */
  6254. tcp_set_nonblocking(&clientfd);
  6255. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6256. }
  6257. #endif
  6258. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6259. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6260. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6261. #else
  6262. if (wolfSSL_use_certificate_file(ssl, certFile,
  6263. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6264. #endif
  6265. /*err_sys("can't load server cert chain file, "
  6266. "Please run from wolfSSL home dir");*/
  6267. goto done;
  6268. }
  6269. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6270. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6271. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6272. #else
  6273. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6274. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6275. #endif
  6276. /*err_sys("can't load server key file, "
  6277. "Please run from wolfSSL home dir");*/
  6278. goto done;
  6279. }
  6280. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6281. /*err_sys("SSL_set_fd failed");*/
  6282. goto done;
  6283. }
  6284. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6285. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6286. #elif !defined(NO_DH)
  6287. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6288. #endif
  6289. /* call ssl setup callback */
  6290. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6291. cbf->ssl_ready(ssl);
  6292. }
  6293. #ifdef WOLFSSL_SESSION_EXPORT
  6294. /* only add in more complex nonblocking case with session export tests */
  6295. if (opts->argc > 0) {
  6296. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6297. if (ret >= 0) {
  6298. opts->return_code = TEST_SUCCESS;
  6299. }
  6300. #ifdef WOLFSSL_TIRTOS
  6301. Task_yield();
  6302. #endif
  6303. goto done;
  6304. }
  6305. #endif
  6306. #ifdef WOLFSSL_ASYNC_CRYPT
  6307. err = 0; /* Reset error */
  6308. #endif
  6309. do {
  6310. #ifdef WOLFSSL_ASYNC_CRYPT
  6311. if (err == WC_PENDING_E) {
  6312. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6313. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6314. }
  6315. #endif
  6316. ret = wolfSSL_negotiate(ssl);
  6317. err = wolfSSL_get_error(ssl, 0);
  6318. } while (err == WC_PENDING_E);
  6319. if (ret != WOLFSSL_SUCCESS) {
  6320. char buff[WOLFSSL_MAX_ERROR_SZ];
  6321. fprintf(stderr, "error = %d, %s\n", err,
  6322. wolfSSL_ERR_error_string(err, buff));
  6323. /*err_sys("SSL_accept failed");*/
  6324. goto done;
  6325. }
  6326. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6327. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6328. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6329. AssertIntGE(msg_len, 0);
  6330. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6331. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6332. AssertIntGE(msg_len, 0);
  6333. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6334. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6335. if (idx > 0) {
  6336. input[idx] = '\0';
  6337. fprintf(stderr, "Client message: %s\n", input);
  6338. }
  6339. else if (idx < 0) {
  6340. goto done;
  6341. }
  6342. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6343. /*err_sys("SSL_write failed");*/
  6344. goto done;
  6345. }
  6346. if (cbf != NULL && cbf->on_result != NULL)
  6347. cbf->on_result(ssl);
  6348. #ifdef WOLFSSL_TIRTOS
  6349. Task_yield();
  6350. #endif
  6351. opts->return_code = TEST_SUCCESS;
  6352. done:
  6353. if (cbf != NULL)
  6354. cbf->last_err = err;
  6355. if (cbf != NULL && cbf->on_cleanup != NULL)
  6356. cbf->on_cleanup(ssl);
  6357. wolfSSL_shutdown(ssl);
  6358. wolfSSL_free(ssl);
  6359. if (!sharedCtx)
  6360. wolfSSL_CTX_free(ctx);
  6361. CloseSocket(clientfd);
  6362. #ifdef WOLFSSL_TIRTOS
  6363. fdCloseSession(Task_self());
  6364. #endif
  6365. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6366. && defined(HAVE_THREAD_LS)
  6367. wc_ecc_fp_free(); /* free per thread cache */
  6368. #endif
  6369. if (cbf == NULL || !cbf->ticNoInit) {
  6370. #if defined(HAVE_SESSION_TICKET) && \
  6371. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6372. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6373. OpenSSLTicketCleanup();
  6374. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6375. TicketCleanup();
  6376. #endif
  6377. #endif
  6378. }
  6379. wolfSSL_SetLoggingPrefix(NULL);
  6380. WOLFSSL_RETURN_FROM_THREAD(0);
  6381. }
  6382. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6383. !defined(WOLFSSL_NO_TLS12)
  6384. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6385. {
  6386. SOCKET_T sockfd = 0;
  6387. SOCKET_T clientfd = 0;
  6388. word16 port;
  6389. callback_functions* cbf;
  6390. WOLFSSL_CTX* ctx = 0;
  6391. WOLFSSL* ssl = 0;
  6392. char msg[] = "I hear you fa shizzle!";
  6393. char input[1024];
  6394. int idx;
  6395. int ret, err = 0;
  6396. int sharedCtx = 0;
  6397. func_args* opts = (func_args*)args;
  6398. int loop_count = opts->argc;
  6399. int count = 0;
  6400. #ifdef WOLFSSL_TIRTOS
  6401. fdOpenSession(Task_self());
  6402. #endif
  6403. opts->return_code = TEST_FAIL;
  6404. cbf = opts->callbacks;
  6405. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6406. if (cbf != NULL && cbf->ctx) {
  6407. ctx = cbf->ctx;
  6408. sharedCtx = 1;
  6409. }
  6410. else
  6411. #endif
  6412. {
  6413. WOLFSSL_METHOD* method = NULL;
  6414. if (cbf != NULL && cbf->method != NULL) {
  6415. method = cbf->method();
  6416. }
  6417. else {
  6418. method = wolfSSLv23_server_method();
  6419. }
  6420. ctx = wolfSSL_CTX_new(method);
  6421. }
  6422. #if defined(USE_WINDOWS_API)
  6423. port = opts->signal->port;
  6424. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6425. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6426. /* Let tcp_listen assign port */
  6427. port = 0;
  6428. #else
  6429. /* Use default port */
  6430. port = wolfSSLPort;
  6431. #endif
  6432. wolfSSL_CTX_set_verify(ctx,
  6433. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6434. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6435. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6436. #endif
  6437. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6438. != WOLFSSL_SUCCESS) {
  6439. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6440. /* Release the wait for TCP ready. */
  6441. signal_ready(opts->signal);
  6442. goto done;
  6443. }
  6444. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6445. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6446. /*err_sys("can't load server cert chain file, "
  6447. "Please run from wolfSSL home dir");*/
  6448. /* Release the wait for TCP ready. */
  6449. signal_ready(opts->signal);
  6450. goto done;
  6451. }
  6452. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6453. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6454. /*err_sys("can't load server key file, "
  6455. "Please run from wolfSSL home dir");*/
  6456. /* Release the wait for TCP ready. */
  6457. signal_ready(opts->signal);
  6458. goto done;
  6459. }
  6460. /* call ctx setup callback */
  6461. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6462. cbf->ctx_ready(ctx);
  6463. }
  6464. while (count != loop_count) {
  6465. ssl = wolfSSL_new(ctx);
  6466. if (ssl == NULL) {
  6467. signal_ready(opts->signal);
  6468. goto done;
  6469. }
  6470. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6471. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6472. /*err_sys("can't load server cert chain file, "
  6473. "Please run from wolfSSL home dir");*/
  6474. /* Release the wait for TCP ready. */
  6475. signal_ready(opts->signal);
  6476. goto done;
  6477. }
  6478. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6479. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6480. /*err_sys("can't load server key file, "
  6481. "Please run from wolfSSL home dir");*/
  6482. /* Release the wait for TCP ready. */
  6483. signal_ready(opts->signal);
  6484. goto done;
  6485. }
  6486. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6487. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6488. #elif !defined(NO_DH)
  6489. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6490. #endif
  6491. /* call ssl setup callback */
  6492. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6493. cbf->ssl_ready(ssl);
  6494. }
  6495. /* do it here to detect failure */
  6496. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6497. 0);
  6498. CloseSocket(sockfd);
  6499. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6500. /*err_sys("SSL_set_fd failed");*/
  6501. goto done;
  6502. }
  6503. #ifdef WOLFSSL_ASYNC_CRYPT
  6504. err = 0; /* Reset error */
  6505. #endif
  6506. do {
  6507. #ifdef WOLFSSL_ASYNC_CRYPT
  6508. if (err == WC_PENDING_E) {
  6509. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6510. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6511. }
  6512. #endif
  6513. ret = wolfSSL_accept(ssl);
  6514. err = wolfSSL_get_error(ssl, 0);
  6515. } while (err == WC_PENDING_E);
  6516. if (ret != WOLFSSL_SUCCESS) {
  6517. char buff[WOLFSSL_MAX_ERROR_SZ];
  6518. fprintf(stderr, "error = %d, %s\n", err,
  6519. wolfSSL_ERR_error_string(err, buff));
  6520. /*err_sys("SSL_accept failed");*/
  6521. goto done;
  6522. }
  6523. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6524. if (idx > 0) {
  6525. input[idx] = '\0';
  6526. fprintf(stderr, "Client message: %s\n", input);
  6527. }
  6528. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6529. /*err_sys("SSL_write failed");*/
  6530. goto done;
  6531. }
  6532. /* free ssl for this connection */
  6533. wolfSSL_shutdown(ssl);
  6534. wolfSSL_free(ssl); ssl = NULL;
  6535. CloseSocket(clientfd);
  6536. count++;
  6537. }
  6538. #ifdef WOLFSSL_TIRTOS
  6539. Task_yield();
  6540. #endif
  6541. opts->return_code = TEST_SUCCESS;
  6542. done:
  6543. if (ssl != NULL) {
  6544. wolfSSL_shutdown(ssl);
  6545. wolfSSL_free(ssl);
  6546. }
  6547. if (!sharedCtx)
  6548. wolfSSL_CTX_free(ctx);
  6549. CloseSocket(clientfd);
  6550. #ifdef WOLFSSL_TIRTOS
  6551. fdCloseSession(Task_self());
  6552. #endif
  6553. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6554. && defined(HAVE_THREAD_LS)
  6555. wc_ecc_fp_free(); /* free per thread cache */
  6556. #endif
  6557. WOLFSSL_RETURN_FROM_THREAD(0);
  6558. }
  6559. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6560. static int test_client_nofail(void* args, cbType cb)
  6561. {
  6562. #if !defined(NO_WOLFSSL_CLIENT)
  6563. SOCKET_T sockfd = 0;
  6564. callback_functions* cbf;
  6565. WOLFSSL_CTX* ctx = 0;
  6566. WOLFSSL* ssl = 0;
  6567. WOLFSSL_CIPHER* cipher;
  6568. char msg[64] = "hello wolfssl!";
  6569. char reply[1024];
  6570. int input;
  6571. int msgSz = (int)XSTRLEN(msg);
  6572. int ret, err = 0;
  6573. int cipherSuite;
  6574. int sharedCtx = 0;
  6575. int doUdp = 0;
  6576. const char* cipherName1, *cipherName2;
  6577. wolfSSL_SetLoggingPrefix("client");
  6578. #ifdef WOLFSSL_TIRTOS
  6579. fdOpenSession(Task_self());
  6580. #endif
  6581. ((func_args*)args)->return_code = TEST_FAIL;
  6582. cbf = ((func_args*)args)->callbacks;
  6583. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6584. if (cbf != NULL && cbf->ctx) {
  6585. ctx = cbf->ctx;
  6586. sharedCtx = cbf->isSharedCtx;
  6587. }
  6588. else
  6589. #endif
  6590. {
  6591. WOLFSSL_METHOD* method = NULL;
  6592. if (cbf != NULL && cbf->method != NULL) {
  6593. method = cbf->method();
  6594. }
  6595. else {
  6596. method = wolfSSLv23_client_method();
  6597. }
  6598. ctx = wolfSSL_CTX_new(method);
  6599. }
  6600. if (cbf != NULL)
  6601. doUdp = cbf->doUdp;
  6602. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6603. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6604. #endif
  6605. /* Do connect here so server detects failures */
  6606. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6607. doUdp, 0, NULL);
  6608. /* Connect the socket so that we don't have to set the peer later on */
  6609. if (doUdp)
  6610. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6611. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6612. {
  6613. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6614. goto done;
  6615. }
  6616. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6617. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6618. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6619. #else
  6620. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6621. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6622. #endif
  6623. /*err_sys("can't load client cert file, "
  6624. "Please run from wolfSSL home dir");*/
  6625. goto done;
  6626. }
  6627. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6628. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6629. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6630. #else
  6631. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6632. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6633. #endif
  6634. /*err_sys("can't load client key file, "
  6635. "Please run from wolfSSL home dir");*/
  6636. goto done;
  6637. }
  6638. #ifdef HAVE_CRL
  6639. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6640. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6641. goto done;
  6642. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6643. != WOLFSSL_SUCCESS)
  6644. goto done;
  6645. }
  6646. #endif
  6647. /* call ctx setup callback */
  6648. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6649. cbf->ctx_ready(ctx);
  6650. }
  6651. ssl = wolfSSL_new(ctx);
  6652. if (ssl == NULL) {
  6653. goto done;
  6654. }
  6655. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6656. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6657. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6658. #else
  6659. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6660. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6661. #endif
  6662. /*err_sys("can't load client cert file, "
  6663. "Please run from wolfSSL home dir");*/
  6664. goto done;
  6665. }
  6666. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6667. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6668. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6669. #else
  6670. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6671. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6672. #endif
  6673. /*err_sys("can't load client key file, "
  6674. "Please run from wolfSSL home dir");*/
  6675. goto done;
  6676. }
  6677. if (!doUdp) {
  6678. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6679. /*err_sys("SSL_set_fd failed");*/
  6680. goto done;
  6681. }
  6682. }
  6683. else {
  6684. #ifdef WOLFSSL_DTLS
  6685. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6686. /*err_sys("SSL_set_fd failed");*/
  6687. goto done;
  6688. }
  6689. #else
  6690. goto done;
  6691. #endif
  6692. }
  6693. /* call ssl setup callback */
  6694. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6695. cbf->ssl_ready(ssl);
  6696. }
  6697. #ifdef WOLFSSL_ASYNC_CRYPT
  6698. err = 0; /* Reset error */
  6699. #endif
  6700. do {
  6701. #ifdef WOLFSSL_ASYNC_CRYPT
  6702. if (err == WC_PENDING_E) {
  6703. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6704. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6705. }
  6706. #endif
  6707. ret = wolfSSL_negotiate(ssl);
  6708. err = wolfSSL_get_error(ssl, 0);
  6709. } while (err == WC_PENDING_E);
  6710. if (ret != WOLFSSL_SUCCESS) {
  6711. char buff[WOLFSSL_MAX_ERROR_SZ];
  6712. fprintf(stderr, "error = %d, %s\n", err,
  6713. wolfSSL_ERR_error_string(err, buff));
  6714. /*err_sys("SSL_connect failed");*/
  6715. goto done;
  6716. }
  6717. /* test the various get cipher methods */
  6718. /* Internal cipher suite names */
  6719. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6720. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6721. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6722. (cipherSuite >> 8), cipherSuite & 0xFF);
  6723. AssertStrEQ(cipherName1, cipherName2);
  6724. /* IANA Cipher Suites Names */
  6725. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6726. then it's the internal cipher suite name */
  6727. cipher = wolfSSL_get_current_cipher(ssl);
  6728. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6729. cipherName2 = wolfSSL_get_cipher(ssl);
  6730. AssertStrEQ(cipherName1, cipherName2);
  6731. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6732. !defined(WOLFSSL_QT)
  6733. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6734. (cipherSuite >> 8), cipherSuite & 0xFF);
  6735. AssertStrEQ(cipherName1, cipherName2);
  6736. #endif
  6737. if (cb != NULL)
  6738. (cb)(ctx, ssl);
  6739. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6740. /*err_sys("SSL_write failed");*/
  6741. goto done;
  6742. }
  6743. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6744. if (input > 0) {
  6745. reply[input] = '\0';
  6746. fprintf(stderr, "Server response: %s\n", reply);
  6747. }
  6748. if (cbf != NULL && cbf->on_result != NULL)
  6749. cbf->on_result(ssl);
  6750. ((func_args*)args)->return_code = TEST_SUCCESS;
  6751. done:
  6752. if (cbf != NULL)
  6753. cbf->last_err = err;
  6754. if (cbf != NULL && cbf->on_cleanup != NULL)
  6755. cbf->on_cleanup(ssl);
  6756. wolfSSL_free(ssl);
  6757. if (!sharedCtx)
  6758. wolfSSL_CTX_free(ctx);
  6759. CloseSocket(sockfd);
  6760. #ifdef WOLFSSL_TIRTOS
  6761. fdCloseSession(Task_self());
  6762. #endif
  6763. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6764. && defined(HAVE_THREAD_LS)
  6765. wc_ecc_fp_free(); /* free per thread cache */
  6766. #endif
  6767. #else
  6768. (void)args;
  6769. (void)cb;
  6770. #endif /* !NO_WOLFSSL_CLIENT */
  6771. wolfSSL_SetLoggingPrefix(NULL);
  6772. return 0;
  6773. }
  6774. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6775. callback_functions* server_cb, cbType client_on_handshake)
  6776. {
  6777. func_args client_args;
  6778. func_args server_args;
  6779. tcp_ready ready;
  6780. THREAD_TYPE serverThread;
  6781. XMEMSET(&client_args, 0, sizeof(func_args));
  6782. XMEMSET(&server_args, 0, sizeof(func_args));
  6783. #ifdef WOLFSSL_TIRTOS
  6784. fdOpenSession(Task_self());
  6785. #endif
  6786. StartTCP();
  6787. InitTcpReady(&ready);
  6788. #if defined(USE_WINDOWS_API)
  6789. /* use RNG to get random port if using windows */
  6790. ready.port = GetRandomPort();
  6791. #endif
  6792. server_args.signal = &ready;
  6793. server_args.callbacks = server_cb;
  6794. client_args.signal = &ready;
  6795. client_args.callbacks = client_cb;
  6796. start_thread(test_server_nofail, &server_args, &serverThread);
  6797. wait_tcp_ready(&server_args);
  6798. test_client_nofail(&client_args, client_on_handshake);
  6799. join_thread(serverThread);
  6800. client_cb->return_code = client_args.return_code;
  6801. server_cb->return_code = server_args.return_code;
  6802. FreeTcpReady(&ready);
  6803. #ifdef WOLFSSL_TIRTOS
  6804. fdOpenSession(Task_self());
  6805. #endif
  6806. }
  6807. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6808. callback_functions* server_cb)
  6809. {
  6810. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6811. }
  6812. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6813. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6814. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6815. void* server_args)
  6816. {
  6817. SOCKET_T sockfd = 0;
  6818. callback_functions* cbf;
  6819. WOLFSSL_CTX* ctx = 0;
  6820. WOLFSSL* ssl = 0;
  6821. WOLFSSL_SESSION* session = NULL;
  6822. char msg[64] = "hello wolfssl!";
  6823. char reply[1024];
  6824. int input;
  6825. int msgSz = (int)XSTRLEN(msg);
  6826. int ret, err = 0;
  6827. int sharedCtx = 0;
  6828. #ifdef WOLFSSL_TIRTOS
  6829. fdOpenSession(Task_self());
  6830. #endif
  6831. ((func_args*)args)->return_code = TEST_FAIL;
  6832. cbf = ((func_args*)args)->callbacks;
  6833. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6834. if (cbf != NULL && cbf->ctx) {
  6835. ctx = cbf->ctx;
  6836. sharedCtx = 1;
  6837. }
  6838. else
  6839. #endif
  6840. {
  6841. WOLFSSL_METHOD* method = NULL;
  6842. if (cbf != NULL && cbf->method != NULL) {
  6843. method = cbf->method();
  6844. }
  6845. else {
  6846. method = wolfSSLv23_client_method();
  6847. }
  6848. ctx = wolfSSL_CTX_new(method);
  6849. }
  6850. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6851. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6852. #endif
  6853. /* Do connect here so server detects failures */
  6854. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6855. 0, 0, NULL);
  6856. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6857. WOLFSSL_SUCCESS) {
  6858. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6859. goto done;
  6860. }
  6861. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6862. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6863. /*err_sys("can't load client cert file, "
  6864. "Please run from wolfSSL home dir");*/
  6865. goto done;
  6866. }
  6867. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6868. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6869. /*err_sys("can't load client key file, "
  6870. "Please run from wolfSSL home dir");*/
  6871. goto done;
  6872. }
  6873. /* call ctx setup callback */
  6874. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6875. cbf->ctx_ready(ctx);
  6876. }
  6877. ssl = wolfSSL_new(ctx);
  6878. if (ssl == NULL) {
  6879. goto done;
  6880. }
  6881. /* keep handshake resources for re-using WOLFSSL obj */
  6882. wolfSSL_KeepArrays(ssl);
  6883. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6884. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6885. goto done;
  6886. }
  6887. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6888. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6889. /*err_sys("can't load client cert file, "
  6890. "Please run from wolfSSL home dir");*/
  6891. goto done;
  6892. }
  6893. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6894. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6895. /*err_sys("can't load client key file, "
  6896. "Please run from wolfSSL home dir");*/
  6897. goto done;
  6898. }
  6899. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6900. /*err_sys("SSL_set_fd failed");*/
  6901. goto done;
  6902. }
  6903. /* call ssl setup callback */
  6904. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6905. cbf->ssl_ready(ssl);
  6906. }
  6907. #ifdef WOLFSSL_ASYNC_CRYPT
  6908. err = 0; /* Reset error */
  6909. #endif
  6910. do {
  6911. #ifdef WOLFSSL_ASYNC_CRYPT
  6912. if (err == WC_PENDING_E) {
  6913. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6914. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6915. }
  6916. #endif
  6917. ret = wolfSSL_connect(ssl);
  6918. err = wolfSSL_get_error(ssl, 0);
  6919. } while (err == WC_PENDING_E);
  6920. if (ret != WOLFSSL_SUCCESS) {
  6921. char buff[WOLFSSL_MAX_ERROR_SZ];
  6922. fprintf(stderr, "error = %d, %s\n", err,
  6923. wolfSSL_ERR_error_string(err, buff));
  6924. /*err_sys("SSL_connect failed");*/
  6925. goto done;
  6926. }
  6927. /* Build first session */
  6928. if (cb != NULL)
  6929. cb(ctx, ssl);
  6930. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6931. /*err_sys("SSL_write failed");*/
  6932. goto done;
  6933. }
  6934. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6935. if (input > 0) {
  6936. reply[input] = '\0';
  6937. fprintf(stderr, "Server response: %s\n", reply);
  6938. }
  6939. /* Session Resumption by re-using WOLFSSL object */
  6940. wolfSSL_set_quiet_shutdown(ssl, 1);
  6941. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6942. /* err_sys ("SSL shutdown failed"); */
  6943. goto done;
  6944. }
  6945. session = wolfSSL_get1_session(ssl);
  6946. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6947. wolfSSL_SESSION_free(session);
  6948. /* err_sys ("SSL_clear failed"); */
  6949. goto done;
  6950. }
  6951. wolfSSL_set_session(ssl, session);
  6952. wolfSSL_SESSION_free(session);
  6953. session = NULL;
  6954. /* close socket once */
  6955. CloseSocket(sockfd);
  6956. sockfd = 0;
  6957. /* wait until server ready */
  6958. wait_tcp_ready((func_args*)server_args);
  6959. fprintf(stderr, "session resumption\n");
  6960. /* Do re-connect */
  6961. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6962. 0, 0, NULL);
  6963. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6964. /*err_sys("SSL_set_fd failed");*/
  6965. goto done;
  6966. }
  6967. #ifdef WOLFSSL_ASYNC_CRYPT
  6968. err = 0; /* Reset error */
  6969. #endif
  6970. do {
  6971. #ifdef WOLFSSL_ASYNC_CRYPT
  6972. if (err == WC_PENDING_E) {
  6973. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6974. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6975. }
  6976. #endif
  6977. ret = wolfSSL_connect(ssl);
  6978. err = wolfSSL_get_error(ssl, 0);
  6979. } while (err == WC_PENDING_E);
  6980. if (ret != WOLFSSL_SUCCESS) {
  6981. char buff[WOLFSSL_MAX_ERROR_SZ];
  6982. fprintf(stderr, "error = %d, %s\n", err,
  6983. wolfSSL_ERR_error_string(err, buff));
  6984. /*err_sys("SSL_connect failed");*/
  6985. goto done;
  6986. }
  6987. /* Build first session */
  6988. if (cb != NULL)
  6989. cb(ctx, ssl);
  6990. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6991. /*err_sys("SSL_write failed");*/
  6992. goto done;
  6993. }
  6994. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6995. if (input > 0) {
  6996. reply[input] = '\0';
  6997. fprintf(stderr, "Server response: %s\n", reply);
  6998. }
  6999. ((func_args*)args)->return_code = TEST_SUCCESS;
  7000. done:
  7001. wolfSSL_free(ssl);
  7002. if (!sharedCtx)
  7003. wolfSSL_CTX_free(ctx);
  7004. CloseSocket(sockfd);
  7005. #ifdef WOLFSSL_TIRTOS
  7006. fdCloseSession(Task_self());
  7007. #endif
  7008. return;
  7009. }
  7010. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7011. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7012. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7013. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7014. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7015. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7016. #define HAVE_ALPN_PROTOS_SUPPORT
  7017. #endif
  7018. /* Generic TLS client / server with callbacks for API unit tests
  7019. * Used by SNI / ALPN / crypto callback helper functions */
  7020. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7021. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7022. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7023. #define ENABLE_TLS_CALLBACK_TEST
  7024. #endif
  7025. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7026. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7027. /* TLS server for API unit testing - generic */
  7028. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7029. {
  7030. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7031. WOLFSSL_CTX* ctx = NULL;
  7032. WOLFSSL* ssl = NULL;
  7033. SOCKET_T sfd = 0;
  7034. SOCKET_T cfd = 0;
  7035. word16 port;
  7036. char msg[] = "I hear you fa shizzle!";
  7037. int len = (int) XSTRLEN(msg);
  7038. char input[1024];
  7039. int idx;
  7040. int ret, err = 0;
  7041. ((func_args*)args)->return_code = TEST_FAIL;
  7042. #if defined(USE_WINDOWS_API)
  7043. port = ((func_args*)args)->signal->port;
  7044. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7045. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7046. /* Let tcp_listen assign port */
  7047. port = 0;
  7048. #else
  7049. /* Use default port */
  7050. port = wolfSSLPort;
  7051. #endif
  7052. #ifdef WOLFSSL_DTLS
  7053. if (callbacks->method == wolfDTLS_server_method
  7054. #ifdef WOLFSSL_STATIC_MEMORY
  7055. || callbacks->method_ex == wolfDTLS_server_method_ex
  7056. #endif
  7057. #ifndef NO_OLD_TLS
  7058. || callbacks->method == wolfDTLSv1_server_method
  7059. #ifdef WOLFSSL_STATIC_MEMORY
  7060. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7061. #endif
  7062. #endif
  7063. #ifndef WOLFSSL_NO_TLS12
  7064. || callbacks->method == wolfDTLSv1_2_server_method
  7065. #ifdef WOLFSSL_STATIC_MEMORY
  7066. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7067. #endif
  7068. #endif
  7069. #ifdef WOLFSSL_DTLS13
  7070. || callbacks->method == wolfDTLSv1_3_server_method
  7071. #ifdef WOLFSSL_STATIC_MEMORY
  7072. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7073. #endif
  7074. #endif
  7075. ) {
  7076. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7077. }
  7078. else
  7079. #endif
  7080. {
  7081. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7082. }
  7083. #ifdef WOLFSSL_STATIC_MEMORY
  7084. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7085. callbacks->memSz > 0) {
  7086. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7087. callbacks->mem, callbacks->memSz, 0, 1);
  7088. if (ret != WOLFSSL_SUCCESS) {
  7089. fprintf(stderr, "CTX static new failed %d\n", ret);
  7090. goto cleanup;
  7091. }
  7092. }
  7093. #else
  7094. ctx = wolfSSL_CTX_new(callbacks->method());
  7095. #endif
  7096. if (ctx == NULL) {
  7097. fprintf(stderr, "CTX new failed\n");
  7098. goto cleanup;
  7099. }
  7100. /* set defaults */
  7101. if (callbacks->caPemFile == NULL)
  7102. callbacks->caPemFile = cliCertFile;
  7103. if (callbacks->certPemFile == NULL)
  7104. callbacks->certPemFile = svrCertFile;
  7105. if (callbacks->keyPemFile == NULL)
  7106. callbacks->keyPemFile = svrKeyFile;
  7107. #ifdef WOLFSSL_TIRTOS
  7108. fdOpenSession(Task_self());
  7109. #endif
  7110. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7111. wolfSSL_CTX_set_verify(ctx,
  7112. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7113. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7114. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7115. #endif
  7116. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7117. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7118. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7119. goto cleanup;
  7120. }
  7121. #endif
  7122. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7123. WOLFSSL_SUCCESS) {
  7124. goto cleanup;
  7125. }
  7126. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7127. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7128. goto cleanup;
  7129. }
  7130. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7131. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7132. goto cleanup;
  7133. }
  7134. #ifdef HAVE_CRL
  7135. if (callbacks->crlPemFile != NULL) {
  7136. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7137. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7138. goto cleanup;
  7139. }
  7140. }
  7141. #endif
  7142. if (callbacks->ctx_ready)
  7143. callbacks->ctx_ready(ctx);
  7144. ssl = wolfSSL_new(ctx);
  7145. if (ssl == NULL) {
  7146. fprintf(stderr, "SSL new failed\n");
  7147. goto cleanup;
  7148. }
  7149. if (wolfSSL_dtls(ssl)) {
  7150. SOCKADDR_IN_T cliAddr;
  7151. socklen_t cliLen;
  7152. cliLen = sizeof(cliAddr);
  7153. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7154. (struct sockaddr*)&cliAddr, &cliLen);
  7155. if (idx <= 0) {
  7156. goto cleanup;
  7157. }
  7158. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7159. }
  7160. else {
  7161. CloseSocket(sfd);
  7162. }
  7163. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7164. goto cleanup;
  7165. }
  7166. if (callbacks->loadToSSL) {
  7167. wolfSSL_SetDevId(ssl, callbacks->devId);
  7168. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7169. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7170. goto cleanup;
  7171. }
  7172. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7173. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7174. goto cleanup;
  7175. }
  7176. }
  7177. #ifdef NO_PSK
  7178. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7179. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7180. #elif !defined(NO_DH)
  7181. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7182. #endif
  7183. #endif
  7184. if (callbacks->ssl_ready)
  7185. callbacks->ssl_ready(ssl);
  7186. #ifdef WOLFSSL_ASYNC_CRYPT
  7187. err = 0; /* Reset error */
  7188. #endif
  7189. do {
  7190. #ifdef WOLFSSL_ASYNC_CRYPT
  7191. if (err == WC_PENDING_E) {
  7192. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7193. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7194. }
  7195. #endif
  7196. ret = wolfSSL_accept(ssl);
  7197. err = wolfSSL_get_error(ssl, ret);
  7198. } while (err == WC_PENDING_E);
  7199. if (ret != WOLFSSL_SUCCESS) {
  7200. char buff[WOLFSSL_MAX_ERROR_SZ];
  7201. fprintf(stderr, "accept error = %d, %s\n", err,
  7202. wolfSSL_ERR_error_string(err, buff));
  7203. /*err_sys("SSL_accept failed");*/
  7204. }
  7205. else {
  7206. #ifdef WOLFSSL_ASYNC_CRYPT
  7207. err = 0; /* Reset error */
  7208. #endif
  7209. do {
  7210. #ifdef WOLFSSL_ASYNC_CRYPT
  7211. if (err == WC_PENDING_E) {
  7212. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7213. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7214. }
  7215. #endif
  7216. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7217. err = wolfSSL_get_error(ssl, idx);
  7218. } while (err == WC_PENDING_E);
  7219. if (idx > 0) {
  7220. input[idx] = 0;
  7221. fprintf(stderr, "Client message: %s\n", input);
  7222. }
  7223. #ifdef WOLFSSL_ASYNC_CRYPT
  7224. err = 0; /* Reset error */
  7225. #endif
  7226. do {
  7227. #ifdef WOLFSSL_ASYNC_CRYPT
  7228. if (err == WC_PENDING_E) {
  7229. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7230. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7231. }
  7232. #endif
  7233. ret = wolfSSL_write(ssl, msg, len);
  7234. err = wolfSSL_get_error(ssl, ret);
  7235. } while (err == WC_PENDING_E);
  7236. if (len != ret) {
  7237. goto cleanup;
  7238. }
  7239. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7240. defined(WOLFSSL_DTLS)
  7241. if (wolfSSL_dtls(ssl)) {
  7242. byte* import;
  7243. word32 sz;
  7244. wolfSSL_dtls_export(ssl, NULL, &sz);
  7245. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7246. if (import == NULL) {
  7247. goto cleanup;
  7248. }
  7249. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7250. if (idx < 0) {
  7251. goto cleanup;
  7252. }
  7253. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7254. goto cleanup;
  7255. }
  7256. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7257. }
  7258. #endif
  7259. #ifdef WOLFSSL_TIRTOS
  7260. Task_yield();
  7261. #endif
  7262. ((func_args*)args)->return_code = TEST_SUCCESS;
  7263. }
  7264. if (callbacks->on_result)
  7265. callbacks->on_result(ssl);
  7266. wolfSSL_shutdown(ssl);
  7267. cleanup:
  7268. wolfSSL_free(ssl);
  7269. wolfSSL_CTX_free(ctx);
  7270. CloseSocket(cfd);
  7271. #ifdef WOLFSSL_TIRTOS
  7272. fdCloseSession(Task_self());
  7273. #endif
  7274. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7275. && defined(HAVE_THREAD_LS)
  7276. wc_ecc_fp_free(); /* free per thread cache */
  7277. #endif
  7278. WOLFSSL_RETURN_FROM_THREAD(0);
  7279. }
  7280. /* TLS Client for API unit testing - generic */
  7281. static void run_wolfssl_client(void* args)
  7282. {
  7283. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7284. WOLFSSL_CTX* ctx = NULL;
  7285. WOLFSSL* ssl = NULL;
  7286. SOCKET_T sfd = 0;
  7287. char msg[] = "hello wolfssl server!";
  7288. int len = (int) XSTRLEN(msg);
  7289. char input[1024];
  7290. int ret, err = 0;
  7291. ((func_args*)args)->return_code = TEST_FAIL;
  7292. /* set defaults */
  7293. if (callbacks->caPemFile == NULL)
  7294. callbacks->caPemFile = caCertFile;
  7295. if (callbacks->certPemFile == NULL)
  7296. callbacks->certPemFile = cliCertFile;
  7297. if (callbacks->keyPemFile == NULL)
  7298. callbacks->keyPemFile = cliKeyFile;
  7299. #ifdef WOLFSSL_STATIC_MEMORY
  7300. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7301. callbacks->memSz > 0) {
  7302. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7303. callbacks->mem, callbacks->memSz, 0, 1);
  7304. if (ret != WOLFSSL_SUCCESS) {
  7305. fprintf(stderr, "CTX static new failed %d\n", ret);
  7306. goto cleanup;
  7307. }
  7308. }
  7309. #else
  7310. ctx = wolfSSL_CTX_new(callbacks->method());
  7311. #endif
  7312. if (ctx == NULL) {
  7313. fprintf(stderr, "CTX new failed\n");
  7314. goto cleanup;
  7315. }
  7316. #ifdef WOLFSSL_TIRTOS
  7317. fdOpenSession(Task_self());
  7318. #endif
  7319. if (!callbacks->loadToSSL) {
  7320. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7321. }
  7322. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7323. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7324. #endif
  7325. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7326. WOLFSSL_SUCCESS) {
  7327. goto cleanup;
  7328. }
  7329. if (!callbacks->loadToSSL) {
  7330. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7331. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7332. goto cleanup;
  7333. }
  7334. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7335. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7336. goto cleanup;
  7337. }
  7338. }
  7339. #ifdef HAVE_CRL
  7340. if (callbacks->crlPemFile != NULL) {
  7341. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7342. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7343. goto cleanup;
  7344. }
  7345. }
  7346. #endif
  7347. if (callbacks->ctx_ready)
  7348. callbacks->ctx_ready(ctx);
  7349. ssl = wolfSSL_new(ctx);
  7350. if (wolfSSL_dtls(ssl)) {
  7351. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7352. 1, 0, ssl);
  7353. }
  7354. else {
  7355. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7356. 0, 0, ssl);
  7357. }
  7358. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7359. goto cleanup;
  7360. }
  7361. if (callbacks->loadToSSL) {
  7362. wolfSSL_SetDevId(ssl, callbacks->devId);
  7363. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7364. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7365. goto cleanup;
  7366. }
  7367. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7368. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7369. goto cleanup;
  7370. }
  7371. }
  7372. if (callbacks->ssl_ready)
  7373. callbacks->ssl_ready(ssl);
  7374. #ifdef WOLFSSL_ASYNC_CRYPT
  7375. err = 0; /* Reset error */
  7376. #endif
  7377. do {
  7378. #ifdef WOLFSSL_ASYNC_CRYPT
  7379. if (err == WC_PENDING_E) {
  7380. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7381. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7382. }
  7383. #endif
  7384. ret = wolfSSL_connect(ssl);
  7385. err = wolfSSL_get_error(ssl, ret);
  7386. } while (err == WC_PENDING_E);
  7387. if (ret != WOLFSSL_SUCCESS) {
  7388. char buff[WOLFSSL_MAX_ERROR_SZ];
  7389. fprintf(stderr, "error = %d, %s\n", err,
  7390. wolfSSL_ERR_error_string(err, buff));
  7391. /*err_sys("SSL_connect failed");*/
  7392. }
  7393. else {
  7394. #ifdef WOLFSSL_ASYNC_CRYPT
  7395. err = 0; /* Reset error */
  7396. #endif
  7397. do {
  7398. #ifdef WOLFSSL_ASYNC_CRYPT
  7399. if (err == WC_PENDING_E) {
  7400. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7401. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7402. }
  7403. #endif
  7404. ret = wolfSSL_write(ssl, msg, len);
  7405. err = wolfSSL_get_error(ssl, ret);
  7406. } while (err == WC_PENDING_E);
  7407. if (len != ret)
  7408. goto cleanup;
  7409. #ifdef WOLFSSL_ASYNC_CRYPT
  7410. err = 0; /* Reset error */
  7411. #endif
  7412. do {
  7413. #ifdef WOLFSSL_ASYNC_CRYPT
  7414. if (err == WC_PENDING_E) {
  7415. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7416. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7417. }
  7418. #endif
  7419. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7420. err = wolfSSL_get_error(ssl, ret);
  7421. } while (err == WC_PENDING_E);
  7422. if (ret > 0) {
  7423. input[ret] = '\0'; /* null term */
  7424. fprintf(stderr, "Server response: %s\n", input);
  7425. }
  7426. ((func_args*)args)->return_code = TEST_SUCCESS;
  7427. }
  7428. if (callbacks->on_result)
  7429. callbacks->on_result(ssl);
  7430. cleanup:
  7431. wolfSSL_free(ssl);
  7432. wolfSSL_CTX_free(ctx);
  7433. CloseSocket(sfd);
  7434. #ifdef WOLFSSL_TIRTOS
  7435. fdCloseSession(Task_self());
  7436. #endif
  7437. }
  7438. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7439. static int test_wolfSSL_read_write(void)
  7440. {
  7441. /* The unit testing for read and write shall happen simultaneously, since
  7442. * one can't do anything with one without the other. (Except for a failure
  7443. * test case.) This function will call all the others that will set up,
  7444. * execute, and report their test findings.
  7445. *
  7446. * Set up the success case first. This function will become the template
  7447. * for the other tests. This should eventually be renamed
  7448. *
  7449. * The success case isn't interesting, how can this fail?
  7450. * - Do not give the client context a CA certificate. The connect should
  7451. * fail. Do not need server for this?
  7452. * - Using NULL for the ssl object on server. Do not need client for this.
  7453. * - Using NULL for the ssl object on client. Do not need server for this.
  7454. * - Good ssl objects for client and server. Client write() without server
  7455. * read().
  7456. * - Good ssl objects for client and server. Server write() without client
  7457. * read().
  7458. * - Forgetting the password callback?
  7459. */
  7460. tcp_ready ready;
  7461. func_args client_args;
  7462. func_args server_args;
  7463. THREAD_TYPE serverThread;
  7464. EXPECT_DECLS;
  7465. XMEMSET(&client_args, 0, sizeof(func_args));
  7466. XMEMSET(&server_args, 0, sizeof(func_args));
  7467. #ifdef WOLFSSL_TIRTOS
  7468. fdOpenSession(Task_self());
  7469. #endif
  7470. StartTCP();
  7471. InitTcpReady(&ready);
  7472. #if defined(USE_WINDOWS_API)
  7473. /* use RNG to get random port if using windows */
  7474. ready.port = GetRandomPort();
  7475. #endif
  7476. server_args.signal = &ready;
  7477. client_args.signal = &ready;
  7478. start_thread(test_server_nofail, &server_args, &serverThread);
  7479. wait_tcp_ready(&server_args);
  7480. test_client_nofail(&client_args, NULL);
  7481. join_thread(serverThread);
  7482. ExpectTrue(client_args.return_code);
  7483. ExpectTrue(server_args.return_code);
  7484. FreeTcpReady(&ready);
  7485. #ifdef WOLFSSL_TIRTOS
  7486. fdOpenSession(Task_self());
  7487. #endif
  7488. return EXPECT_RESULT();
  7489. }
  7490. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7491. {
  7492. EXPECT_DECLS;
  7493. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7494. !defined(WOLFSSL_NO_TLS12)
  7495. /* The unit test for session resumption by re-using WOLFSSL object.
  7496. * WOLFSSL object is not cleared after first session. It reuse the object
  7497. * for second connection.
  7498. */
  7499. tcp_ready ready;
  7500. func_args client_args;
  7501. func_args server_args;
  7502. THREAD_TYPE serverThread;
  7503. callback_functions client_cbf;
  7504. callback_functions server_cbf;
  7505. XMEMSET(&client_args, 0, sizeof(func_args));
  7506. XMEMSET(&server_args, 0, sizeof(func_args));
  7507. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7508. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7509. #ifdef WOLFSSL_TIRTOS
  7510. fdOpenSession(Task_self());
  7511. #endif
  7512. StartTCP();
  7513. InitTcpReady(&ready);
  7514. #if defined(USE_WINDOWS_API)
  7515. /* use RNG to get random port if using windows */
  7516. ready.port = GetRandomPort();
  7517. #endif
  7518. client_cbf.method = wolfTLSv1_2_client_method;
  7519. server_cbf.method = wolfTLSv1_2_server_method;
  7520. client_args.callbacks = &client_cbf;
  7521. server_args.callbacks = &server_cbf;
  7522. server_args.signal = &ready;
  7523. client_args.signal = &ready;
  7524. /* the var is used for loop number */
  7525. server_args.argc = 2;
  7526. start_thread(test_server_loop, &server_args, &serverThread);
  7527. wait_tcp_ready(&server_args);
  7528. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7529. join_thread(serverThread);
  7530. ExpectTrue(client_args.return_code);
  7531. ExpectTrue(server_args.return_code);
  7532. FreeTcpReady(&ready);
  7533. #ifdef WOLFSSL_TIRTOS
  7534. fdOpenSession(Task_self());
  7535. #endif
  7536. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7537. * !defined(WOLFSSL_TLS13) */
  7538. return EXPECT_RESULT();
  7539. }
  7540. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7541. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7542. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7543. WOLFSSL_CTX* ctx)
  7544. {
  7545. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7546. myVerifyAction = VERIFY_USE_PREVERFIY;
  7547. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7548. return TEST_SUCCESS;
  7549. }
  7550. #endif
  7551. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7552. {
  7553. EXPECT_DECLS;
  7554. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7555. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7556. test_ssl_cbf client_cbf;
  7557. test_ssl_cbf server_cbf;
  7558. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7559. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7560. #ifdef WOLFSSL_TLS13
  7561. client_cbf.method = wolfTLSv1_3_client_method;
  7562. #endif /* WOLFSSL_TLS13 */
  7563. client_cbf.ctx_ready =
  7564. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7565. /* test case 1 verify depth is equal to peer chain */
  7566. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7567. &server_cbf, NULL), TEST_SUCCESS);
  7568. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7569. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7570. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7571. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7572. return EXPECT_RESULT();
  7573. }
  7574. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7575. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7576. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7577. WOLFSSL_CTX* ctx)
  7578. {
  7579. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7580. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7581. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7582. return TEST_SUCCESS;
  7583. }
  7584. #endif
  7585. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7586. {
  7587. EXPECT_DECLS;
  7588. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7589. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7590. test_ssl_cbf client_cbf;
  7591. test_ssl_cbf server_cbf;
  7592. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7593. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7594. #ifdef WOLFSSL_TLS13
  7595. client_cbf.method = wolfTLSv1_3_client_method;
  7596. #endif /* WOLFSSL_TLS13 */
  7597. client_cbf.ctx_ready =
  7598. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7599. /* test case 2
  7600. * verify depth is zero, number of peer's chain is 2.
  7601. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7602. * callback.
  7603. */
  7604. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7605. &server_cbf, NULL), TEST_SUCCESS);
  7606. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7607. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7608. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7609. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7610. return EXPECT_RESULT();
  7611. }
  7612. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7613. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7614. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7615. WOLFSSL_CTX* ctx)
  7616. {
  7617. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7618. myVerifyAction = VERIFY_USE_PREVERFIY;
  7619. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7620. return TEST_SUCCESS;
  7621. }
  7622. #endif
  7623. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7624. {
  7625. EXPECT_DECLS;
  7626. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7627. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7628. test_ssl_cbf client_cbf;
  7629. test_ssl_cbf server_cbf;
  7630. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7631. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7632. #ifdef WOLFSSL_TLS13
  7633. client_cbf.method = wolfTLSv1_3_client_method;
  7634. #endif /* WOLFSSL_TLS13 */
  7635. client_cbf.ctx_ready =
  7636. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7637. /* test case 3
  7638. * verify depth is zero, number of peer's chain is 2
  7639. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7640. * therefore, handshake becomes failure.
  7641. */
  7642. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7643. &server_cbf, NULL), TEST_FAIL);
  7644. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7645. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7646. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7647. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7648. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7649. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7650. return EXPECT_RESULT();
  7651. }
  7652. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7653. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7654. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7655. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7656. {
  7657. EXPECT_DECLS;
  7658. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7659. return EXPECT_RESULT();
  7660. }
  7661. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7662. {
  7663. EXPECT_DECLS;
  7664. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7665. return EXPECT_RESULT();
  7666. }
  7667. #endif
  7668. static int test_wolfSSL_CTX_set_cipher_list(void)
  7669. {
  7670. EXPECT_DECLS;
  7671. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7672. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7673. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7674. WOLFSSL_CTX* ctxClient = NULL;
  7675. WOLFSSL* sslClient = NULL;
  7676. test_ssl_cbf client_cbf;
  7677. test_ssl_cbf server_cbf;
  7678. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7679. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7680. server_cbf.method = wolfTLSv1_2_server_method;
  7681. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7682. client_cbf.method = wolfTLSv1_2_client_method;
  7683. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7684. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7685. &server_cbf, NULL), TEST_SUCCESS);
  7686. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7687. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7688. /* check with cipher string that has '+' */
  7689. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7690. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7691. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7692. /* check for the existence of an ECDHE ECDSA cipher suite */
  7693. if (EXPECT_SUCCESS()) {
  7694. int i = 0;
  7695. int found = 0;
  7696. const char* suite;
  7697. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7698. WOLFSSL_CIPHER* current;
  7699. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7700. do {
  7701. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7702. if (current) {
  7703. suite = wolfSSL_CIPHER_get_name(current);
  7704. if (suite && XSTRSTR(suite, "ECDSA")) {
  7705. found = 1;
  7706. break;
  7707. }
  7708. }
  7709. } while (current);
  7710. ExpectIntEQ(found, 1);
  7711. }
  7712. wolfSSL_free(sslClient);
  7713. wolfSSL_CTX_free(ctxClient);
  7714. #endif
  7715. return EXPECT_RESULT();
  7716. }
  7717. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7718. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7719. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7720. WOLFSSL* ssl)
  7721. {
  7722. EXPECT_DECLS;
  7723. size_t msg_len;
  7724. (void)ctx;
  7725. /* get_finished test */
  7726. /* 1. get own sent message */
  7727. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7728. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7729. ExpectIntGE(msg_len, 0);
  7730. /* 2. get peer message */
  7731. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7732. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7733. ExpectIntGE(msg_len, 0);
  7734. return EXPECT_RESULT();
  7735. }
  7736. #endif
  7737. static int test_wolfSSL_get_finished(void)
  7738. {
  7739. EXPECT_DECLS;
  7740. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7741. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7742. test_ssl_cbf client_cbf;
  7743. test_ssl_cbf server_cbf;
  7744. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7745. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7746. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7747. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7748. TEST_SUCCESS);
  7749. /* test received msg vs sent msg */
  7750. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7751. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7752. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7753. return EXPECT_RESULT();
  7754. }
  7755. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7756. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7757. !defined(NO_SESSION_CACHE)
  7758. /* Sessions to restore/store */
  7759. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7760. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7761. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7762. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7763. {
  7764. /* Don't store sessions. Lookup is still enabled. */
  7765. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7766. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7767. #ifdef OPENSSL_EXTRA
  7768. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7769. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7770. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7771. #endif
  7772. /* Require both peers to provide certs */
  7773. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7774. }
  7775. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7776. {
  7777. WOLFSSL_SESSION** sess;
  7778. if (wolfSSL_is_server(ssl))
  7779. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7780. else
  7781. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7782. if (*sess == NULL) {
  7783. #ifdef NO_SESSION_CACHE_REF
  7784. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7785. #else
  7786. /* Test for backwards compatibility */
  7787. if (wolfSSL_is_server(ssl)) {
  7788. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7789. }
  7790. else {
  7791. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7792. }
  7793. #endif
  7794. /* Now save the session in the internal store to make it available
  7795. * for lookup. For TLS 1.3, we can't save the session without
  7796. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7797. * session from cache. */
  7798. if (wolfSSL_is_server(ssl)
  7799. #ifndef WOLFSSL_TICKET_HAVE_ID
  7800. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7801. #endif
  7802. )
  7803. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7804. *sess), WOLFSSL_SUCCESS);
  7805. }
  7806. else {
  7807. /* If we have a session retrieved then remaining connections should be
  7808. * resuming on that session */
  7809. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7810. }
  7811. /* Save CTX to be able to decrypt tickets */
  7812. if (wolfSSL_is_server(ssl) &&
  7813. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7814. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7815. = wolfSSL_get_SSL_CTX(ssl));
  7816. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7817. WOLFSSL_SUCCESS);
  7818. }
  7819. #ifdef SESSION_CERTS
  7820. #ifndef WOLFSSL_TICKET_HAVE_ID
  7821. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7822. wolfSSL_session_reused(ssl))
  7823. #endif
  7824. {
  7825. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7826. * for all connections. TLS 1.3 only has tickets so if we don't
  7827. * include the session id in the ticket then the certificates
  7828. * will not be available on resumption. */
  7829. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7830. AssertNotNull(peer);
  7831. wolfSSL_X509_free(peer);
  7832. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7833. #ifdef OPENSSL_EXTRA
  7834. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7835. #endif
  7836. }
  7837. #endif /* SESSION_CERTS */
  7838. }
  7839. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7840. {
  7841. /* Set the session to reuse for the client */
  7842. AssertIntEQ(wolfSSL_set_session(ssl,
  7843. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7844. }
  7845. #endif
  7846. static int test_wolfSSL_CTX_add_session(void)
  7847. {
  7848. EXPECT_DECLS;
  7849. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7850. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7851. !defined(NO_SESSION_CACHE)
  7852. tcp_ready ready;
  7853. func_args client_args;
  7854. func_args server_args;
  7855. THREAD_TYPE serverThread;
  7856. callback_functions client_cb;
  7857. callback_functions server_cb;
  7858. method_provider methods[][2] = {
  7859. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7860. !defined(NO_DES3))
  7861. /* Without AES there are almost no ciphersuites available. This leads
  7862. * to no ciphersuites being available and an error. */
  7863. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7864. #endif
  7865. #ifndef WOLFSSL_NO_TLS12
  7866. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7867. #endif
  7868. /* Needs the default ticket callback since it is tied to the
  7869. * connection context and this makes it easy to carry over the ticket
  7870. * crypto context between connections */
  7871. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7872. defined(HAVE_SESSION_TICKET)
  7873. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7874. #endif
  7875. };
  7876. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7877. size_t i, j;
  7878. for (i = 0; i < methodsLen; i++) {
  7879. /* First run creates a connection while the second+ run will attempt
  7880. * to resume the connection. The trick is that the internal cache
  7881. * is turned off. wolfSSL_CTX_add_session should put the session in
  7882. * the cache anyway. */
  7883. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7884. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7885. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7886. #ifdef NO_SESSION_CACHE_REF
  7887. for (j = 0; j < 4; j++) {
  7888. #else
  7889. /* The session may be overwritten in this case. Do only one resumption
  7890. * to stop this test from failing intermittently. */
  7891. for (j = 0; j < 2; j++) {
  7892. #endif
  7893. #ifdef WOLFSSL_TIRTOS
  7894. fdOpenSession(Task_self());
  7895. #endif
  7896. StartTCP();
  7897. InitTcpReady(&ready);
  7898. XMEMSET(&client_args, 0, sizeof(func_args));
  7899. XMEMSET(&server_args, 0, sizeof(func_args));
  7900. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7901. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7902. client_cb.method = methods[i][0];
  7903. server_cb.method = methods[i][1];
  7904. server_args.signal = &ready;
  7905. server_args.callbacks = &server_cb;
  7906. client_args.signal = &ready;
  7907. client_args.callbacks = &client_cb;
  7908. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7909. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7910. server_cb.isSharedCtx = 1;
  7911. }
  7912. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7913. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7914. if (j != 0)
  7915. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7916. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7917. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7918. server_cb.ticNoInit = 1; /* Use default builtin */
  7919. start_thread(test_server_nofail, &server_args, &serverThread);
  7920. wait_tcp_ready(&server_args);
  7921. test_client_nofail(&client_args, NULL);
  7922. join_thread(serverThread);
  7923. ExpectTrue(client_args.return_code);
  7924. ExpectTrue(server_args.return_code);
  7925. FreeTcpReady(&ready);
  7926. if (EXPECT_FAIL())
  7927. break;
  7928. }
  7929. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7930. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7931. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7932. if (EXPECT_FAIL())
  7933. break;
  7934. }
  7935. #endif
  7936. return EXPECT_RESULT();
  7937. }
  7938. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7939. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7940. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7941. defined(HAVE_SESSION_TICKET) && \
  7942. !defined(TITAN_SESSION_CACHE) && \
  7943. !defined(HUGE_SESSION_CACHE) && \
  7944. !defined(BIG_SESSION_CACHE) && \
  7945. !defined(MEDIUM_SESSION_CACHE)
  7946. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7947. /* Sessions to restore/store */
  7948. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7949. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7950. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7951. static int twcase_new_session_called = 0;
  7952. static int twcase_remove_session_called = 0;
  7953. static int twcase_get_session_called = 0;
  7954. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7955. #define SESSION_CACHE_SIZE 33
  7956. typedef struct {
  7957. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7958. WOLFSSL_SESSION* value;
  7959. } hashTable_entry;
  7960. typedef struct {
  7961. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7962. size_t capacity; /* size of entries */
  7963. size_t length; /* number of items in the hash table */
  7964. wolfSSL_Mutex htLock; /* lock */
  7965. }hashTable;
  7966. static hashTable server_sessionCache;
  7967. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7968. {
  7969. int i;
  7970. unsigned int len;
  7971. (void)ssl;
  7972. /*
  7973. * This example uses a hash table.
  7974. * Steps you should take for a non-demo code:
  7975. * - acquire a lock for the file named according to the session id
  7976. * - open the file
  7977. * - encrypt and write the SSL_SESSION object to the file
  7978. * - release the lock
  7979. *
  7980. * Return:
  7981. * 0: The callback does not wish to hold a reference of the sess
  7982. * 1: The callback wants to hold a reference of the sess. The callback is
  7983. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7984. */
  7985. if (sess == NULL)
  7986. return 0;
  7987. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7988. return 0;
  7989. }
  7990. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7991. if (server_sessionCache.entries[i].value == NULL) {
  7992. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7993. server_sessionCache.entries[i].value = sess;
  7994. server_sessionCache.length++;
  7995. break;
  7996. }
  7997. }
  7998. ++twcase_new_session_called;
  7999. wc_UnLockMutex(&server_sessionCache.htLock);
  8000. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8001. twcase_new_session_called);
  8002. return 1;
  8003. }
  8004. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8005. {
  8006. int i;
  8007. (void)ctx;
  8008. (void)sess;
  8009. if (sess == NULL)
  8010. return;
  8011. /*
  8012. * This example uses a hash table.
  8013. * Steps you should take for a non-demo code:
  8014. * - acquire a lock for the file named according to the session id
  8015. * - remove the file
  8016. * - release the lock
  8017. */
  8018. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8019. return;
  8020. }
  8021. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8022. if (server_sessionCache.entries[i].key != NULL &&
  8023. XMEMCMP(server_sessionCache.entries[i].key,
  8024. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8025. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8026. server_sessionCache.entries[i].value = NULL;
  8027. server_sessionCache.entries[i].key = NULL;
  8028. server_sessionCache.length--;
  8029. break;
  8030. }
  8031. }
  8032. ++twcase_remove_session_called;
  8033. wc_UnLockMutex(&server_sessionCache.htLock);
  8034. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8035. twcase_remove_session_called);
  8036. }
  8037. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8038. const unsigned char *id, int len, int *ref)
  8039. {
  8040. int i;
  8041. (void)ssl;
  8042. (void)id;
  8043. (void)len;
  8044. /*
  8045. * This example uses a hash table.
  8046. * Steps you should take for a non-demo code:
  8047. * - acquire a lock for the file named according to the session id in the
  8048. * 2nd arg
  8049. * - read and decrypt contents of file and create a new SSL_SESSION
  8050. * - object release the lock
  8051. * - return the new session object
  8052. */
  8053. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8054. ++twcase_get_session_called);
  8055. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8056. * be responsible for the pointer then set to 0. */
  8057. *ref = 1;
  8058. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8059. if (server_sessionCache.entries[i].key != NULL &&
  8060. XMEMCMP(server_sessionCache.entries[i].key, id,
  8061. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8062. return server_sessionCache.entries[i].value;
  8063. }
  8064. }
  8065. return NULL;
  8066. }
  8067. static int twcase_get_sessionCb_cleanup(void)
  8068. {
  8069. int i;
  8070. int cnt = 0;
  8071. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8072. * for freeing sessions */
  8073. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8074. if (server_sessionCache.entries[i].value != NULL) {
  8075. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8076. cnt++;
  8077. }
  8078. }
  8079. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8080. cnt);
  8081. return TEST_SUCCESS;
  8082. }
  8083. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8084. {
  8085. EXPECT_DECLS;
  8086. /* off - Disable internal cache */
  8087. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8088. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8089. #ifdef OPENSSL_EXTRA
  8090. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8091. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8092. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8093. #endif
  8094. /* off - Do not setup external cache */
  8095. /* Require both peers to provide certs */
  8096. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8097. return EXPECT_RESULT();
  8098. }
  8099. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8100. {
  8101. /* on - internal cache is on by default */
  8102. /* off - Do not setup external cache */
  8103. /* Require both peers to provide certs */
  8104. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8105. return TEST_SUCCESS;
  8106. }
  8107. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8108. {
  8109. EXPECT_DECLS;
  8110. /* off - Disable internal cache */
  8111. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8112. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8113. #ifdef OPENSSL_EXTRA
  8114. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8115. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8116. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8117. #endif
  8118. /* on - Enable external cache */
  8119. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8120. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8121. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8122. /* Require both peers to provide certs */
  8123. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8124. return EXPECT_RESULT();
  8125. }
  8126. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8127. {
  8128. /* on - internal cache is on by default */
  8129. /* on - Enable external cache */
  8130. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8131. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8132. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8133. /* Require both peers to provide certs */
  8134. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8135. return TEST_SUCCESS;
  8136. }
  8137. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8138. {
  8139. /* on - internal cache is on by default */
  8140. /* on - Enable external cache */
  8141. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8142. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8143. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8144. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8145. /* Require both peers to provide certs */
  8146. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8147. return TEST_SUCCESS;
  8148. }
  8149. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8150. {
  8151. EXPECT_DECLS;
  8152. WOLFSSL_SESSION** sess;
  8153. if (wolfSSL_is_server(ssl))
  8154. sess = &twcase_server_first_session_ptr;
  8155. else
  8156. return TEST_SUCCESS;
  8157. if (*sess == NULL) {
  8158. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8159. /* Now save the session in the internal store to make it available
  8160. * for lookup. For TLS 1.3, we can't save the session without
  8161. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8162. * session from cache. */
  8163. if (wolfSSL_is_server(ssl)
  8164. #ifndef WOLFSSL_TICKET_HAVE_ID
  8165. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8166. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8167. #endif
  8168. ) {
  8169. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8170. *sess), WOLFSSL_SUCCESS);
  8171. }
  8172. }
  8173. /* Save CTX to be able to decrypt tickets */
  8174. if (twcase_server_current_ctx_ptr == NULL) {
  8175. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8176. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8177. WOLFSSL_SUCCESS);
  8178. }
  8179. #ifdef SESSION_CERTS
  8180. #ifndef WOLFSSL_TICKET_HAVE_ID
  8181. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8182. wolfSSL_session_reused(ssl))
  8183. #endif
  8184. {
  8185. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8186. * for all connections. TLS 1.3 only has tickets so if we don't
  8187. * include the session id in the ticket then the certificates
  8188. * will not be available on resumption. */
  8189. WOLFSSL_X509* peer = NULL;
  8190. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8191. wolfSSL_X509_free(peer);
  8192. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8193. }
  8194. #endif
  8195. return EXPECT_RESULT();
  8196. }
  8197. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8198. {
  8199. EXPECT_DECLS;
  8200. WOLFSSL_SESSION** sess;
  8201. sess = &twcase_client_first_session_ptr;
  8202. if (*sess == NULL) {
  8203. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8204. }
  8205. else {
  8206. /* If we have a session retrieved then remaining connections should be
  8207. * resuming on that session */
  8208. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8209. }
  8210. #ifdef SESSION_CERTS
  8211. #ifndef WOLFSSL_TICKET_HAVE_ID
  8212. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8213. wolfSSL_session_reused(ssl))
  8214. #endif
  8215. {
  8216. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8217. ExpectNotNull(peer);
  8218. wolfSSL_X509_free(peer);
  8219. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8220. #ifdef OPENSSL_EXTRA
  8221. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8222. #endif
  8223. }
  8224. #endif
  8225. return EXPECT_RESULT();
  8226. }
  8227. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8228. {
  8229. EXPECT_DECLS;
  8230. /* Set the session to reuse for the client */
  8231. ExpectNotNull(ssl);
  8232. ExpectNotNull(twcase_client_first_session_ptr);
  8233. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8234. WOLFSSL_SUCCESS);
  8235. return EXPECT_RESULT();
  8236. }
  8237. struct test_add_session_ext_params {
  8238. method_provider client_meth;
  8239. method_provider server_meth;
  8240. const char* tls_version;
  8241. };
  8242. static int test_wolfSSL_CTX_add_session_ext(
  8243. struct test_add_session_ext_params* param)
  8244. {
  8245. EXPECT_DECLS;
  8246. /* Test the default 33 sessions */
  8247. int j;
  8248. /* Clear cache before starting */
  8249. wolfSSL_CTX_flush_sessions(NULL, -1);
  8250. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8251. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8252. return BAD_MUTEX_E;
  8253. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8254. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8255. for (j = 0; j < 5; j++) {
  8256. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8257. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8258. test_ssl_cbf client_cb;
  8259. test_ssl_cbf server_cb;
  8260. (void)dtls;
  8261. /* Test five cache configurations */
  8262. twcase_client_first_session_ptr = NULL;
  8263. twcase_server_first_session_ptr = NULL;
  8264. twcase_server_current_ctx_ptr = NULL;
  8265. twcase_new_session_called = 0;
  8266. twcase_remove_session_called = 0;
  8267. twcase_get_session_called = 0;
  8268. /* connection 1 - first connection */
  8269. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8270. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8271. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8272. client_cb.method = param->client_meth;
  8273. server_cb.method = param->server_meth;
  8274. if (dtls)
  8275. client_cb.doUdp = server_cb.doUdp = 1;
  8276. /* Setup internal and external cache */
  8277. switch (j) {
  8278. case 0:
  8279. /* SSL_OP_NO_TICKET stateful ticket case */
  8280. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8281. break;
  8282. case 1:
  8283. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8284. break;
  8285. case 2:
  8286. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8287. break;
  8288. case 3:
  8289. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8290. break;
  8291. case 4:
  8292. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8293. break;
  8294. }
  8295. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8296. /* Add session to internal cache and save SSL session for testing */
  8297. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8298. /* Save client SSL session for testing */
  8299. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8300. server_cb.ticNoInit = 1; /* Use default builtin */
  8301. /* Don't free/release ctx */
  8302. server_cb.ctx = twcase_server_current_ctx_ptr;
  8303. server_cb.isSharedCtx = 1;
  8304. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8305. &server_cb, NULL), TEST_SUCCESS);
  8306. ExpectIntEQ(twcase_get_session_called, 0);
  8307. if (EXPECT_FAIL()) {
  8308. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8309. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8310. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8311. break;
  8312. }
  8313. switch (j) {
  8314. case 0:
  8315. case 1:
  8316. case 2:
  8317. /* cache cannot be searched with out a connection */
  8318. /* Add a new session */
  8319. ExpectIntEQ(twcase_new_session_called, 1);
  8320. /* In twcase_server_sess_ctx_pre_shutdown
  8321. * wolfSSL_CTX_add_session which evicts the existing session
  8322. * in cache and adds it back in */
  8323. ExpectIntLE(twcase_remove_session_called, 1);
  8324. break;
  8325. case 3:
  8326. case 4:
  8327. /* no external cache */
  8328. ExpectIntEQ(twcase_new_session_called, 0);
  8329. ExpectIntEQ(twcase_remove_session_called, 0);
  8330. break;
  8331. }
  8332. /* connection 2 - session resume */
  8333. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8334. twcase_new_session_called = 0;
  8335. twcase_remove_session_called = 0;
  8336. twcase_get_session_called = 0;
  8337. server_cb.on_result = 0;
  8338. client_cb.on_result = 0;
  8339. server_cb.ticNoInit = 1; /* Use default builtin */
  8340. server_cb.ctx = twcase_server_current_ctx_ptr;
  8341. /* try session resumption */
  8342. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8343. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8344. &server_cb, NULL), TEST_SUCCESS);
  8345. /* Clear cache before checking */
  8346. wolfSSL_CTX_flush_sessions(NULL, -1);
  8347. switch (j) {
  8348. case 0:
  8349. if (tls13) {
  8350. /* (D)TLSv1.3 stateful case */
  8351. /* cache hit */
  8352. /* DTLS accesses cache once for stateless parsing and
  8353. * once for stateful parsing */
  8354. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8355. /* (D)TLSv1.3 creates a new ticket,
  8356. * updates both internal and external cache */
  8357. ExpectIntEQ(twcase_new_session_called, 1);
  8358. /* A new session ID is created for a new ticket */
  8359. ExpectIntEQ(twcase_remove_session_called, 2);
  8360. }
  8361. else {
  8362. /* non (D)TLSv1.3 case, no update */
  8363. /* DTLS accesses cache once for stateless parsing and
  8364. * once for stateful parsing */
  8365. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8366. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8367. #else
  8368. ExpectIntEQ(twcase_get_session_called, 1);
  8369. #endif
  8370. ExpectIntEQ(twcase_new_session_called, 0);
  8371. /* Called on session added in
  8372. * twcase_server_sess_ctx_pre_shutdown */
  8373. ExpectIntEQ(twcase_remove_session_called, 1);
  8374. }
  8375. break;
  8376. case 1:
  8377. if (tls13) {
  8378. /* (D)TLSv1.3 case */
  8379. /* cache hit */
  8380. ExpectIntEQ(twcase_get_session_called, 1);
  8381. /* (D)TLSv1.3 creates a new ticket,
  8382. * updates both internal and external cache */
  8383. ExpectIntEQ(twcase_new_session_called, 1);
  8384. /* Called on session added in
  8385. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8386. ExpectIntEQ(twcase_remove_session_called, 1);
  8387. }
  8388. else {
  8389. /* non (D)TLSv1.3 case */
  8390. /* cache hit */
  8391. /* DTLS accesses cache once for stateless parsing and
  8392. * once for stateful parsing */
  8393. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8394. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8395. #else
  8396. ExpectIntEQ(twcase_get_session_called, 1);
  8397. #endif
  8398. ExpectIntEQ(twcase_new_session_called, 0);
  8399. /* Called on session added in
  8400. * twcase_server_sess_ctx_pre_shutdown */
  8401. ExpectIntEQ(twcase_remove_session_called, 1);
  8402. }
  8403. break;
  8404. case 2:
  8405. if (tls13) {
  8406. /* (D)TLSv1.3 case */
  8407. /* cache hit */
  8408. ExpectIntEQ(twcase_get_session_called, 1);
  8409. /* (D)TLSv1.3 creates a new ticket,
  8410. * updates both internal and external cache */
  8411. ExpectIntEQ(twcase_new_session_called, 1);
  8412. /* Called on session added in
  8413. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8414. ExpectIntEQ(twcase_remove_session_called, 1);
  8415. }
  8416. else {
  8417. /* non (D)TLSv1.3 case */
  8418. /* cache hit */
  8419. /* DTLS accesses cache once for stateless parsing and
  8420. * once for stateful parsing */
  8421. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8422. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8423. #else
  8424. ExpectIntEQ(twcase_get_session_called, 1);
  8425. #endif
  8426. ExpectIntEQ(twcase_new_session_called, 0);
  8427. /* Called on session added in
  8428. * twcase_server_sess_ctx_pre_shutdown */
  8429. ExpectIntEQ(twcase_remove_session_called, 1);
  8430. }
  8431. break;
  8432. case 3:
  8433. case 4:
  8434. /* no external cache */
  8435. ExpectIntEQ(twcase_get_session_called, 0);
  8436. ExpectIntEQ(twcase_new_session_called, 0);
  8437. ExpectIntEQ(twcase_remove_session_called, 0);
  8438. break;
  8439. }
  8440. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8441. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8442. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8443. if (EXPECT_FAIL())
  8444. break;
  8445. }
  8446. twcase_get_sessionCb_cleanup();
  8447. XMEMSET(&server_sessionCache.entries, 0,
  8448. sizeof(server_sessionCache.entries));
  8449. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8450. wc_FreeMutex(&server_sessionCache.htLock);
  8451. return EXPECT_RESULT();
  8452. }
  8453. #endif
  8454. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8455. {
  8456. EXPECT_DECLS;
  8457. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8458. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8459. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8460. defined(HAVE_SESSION_TICKET) && \
  8461. !defined(TITAN_SESSION_CACHE) && \
  8462. !defined(HUGE_SESSION_CACHE) && \
  8463. !defined(BIG_SESSION_CACHE) && \
  8464. !defined(MEDIUM_SESSION_CACHE)
  8465. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8466. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8467. struct test_add_session_ext_params param[1] = {
  8468. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8469. };
  8470. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8471. #endif
  8472. #endif
  8473. return EXPECT_RESULT();
  8474. }
  8475. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8476. {
  8477. EXPECT_DECLS;
  8478. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8479. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8480. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8481. defined(HAVE_SESSION_TICKET) && \
  8482. !defined(TITAN_SESSION_CACHE) && \
  8483. !defined(HUGE_SESSION_CACHE) && \
  8484. !defined(BIG_SESSION_CACHE) && \
  8485. !defined(MEDIUM_SESSION_CACHE)
  8486. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8487. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8488. #ifdef WOLFSSL_DTLS13
  8489. struct test_add_session_ext_params param[1] = {
  8490. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8491. };
  8492. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8493. #endif
  8494. #endif
  8495. #endif
  8496. return EXPECT_RESULT();
  8497. }
  8498. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8499. {
  8500. EXPECT_DECLS;
  8501. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8502. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8503. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8504. defined(HAVE_SESSION_TICKET) && \
  8505. !defined(TITAN_SESSION_CACHE) && \
  8506. !defined(HUGE_SESSION_CACHE) && \
  8507. !defined(BIG_SESSION_CACHE) && \
  8508. !defined(MEDIUM_SESSION_CACHE)
  8509. #ifndef WOLFSSL_NO_TLS12
  8510. struct test_add_session_ext_params param[1] = {
  8511. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8512. };
  8513. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8514. #endif
  8515. #endif
  8516. return EXPECT_RESULT();
  8517. }
  8518. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8519. {
  8520. EXPECT_DECLS;
  8521. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8522. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8523. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8524. defined(HAVE_SESSION_TICKET) && \
  8525. !defined(TITAN_SESSION_CACHE) && \
  8526. !defined(HUGE_SESSION_CACHE) && \
  8527. !defined(BIG_SESSION_CACHE) && \
  8528. !defined(MEDIUM_SESSION_CACHE)
  8529. #ifndef WOLFSSL_NO_TLS12
  8530. #ifdef WOLFSSL_DTLS
  8531. struct test_add_session_ext_params param[1] = {
  8532. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8533. };
  8534. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8535. #endif
  8536. #endif
  8537. #endif
  8538. return EXPECT_RESULT();
  8539. }
  8540. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8541. {
  8542. EXPECT_DECLS;
  8543. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8544. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8545. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8546. defined(HAVE_SESSION_TICKET) && \
  8547. !defined(TITAN_SESSION_CACHE) && \
  8548. !defined(HUGE_SESSION_CACHE) && \
  8549. !defined(BIG_SESSION_CACHE) && \
  8550. !defined(MEDIUM_SESSION_CACHE)
  8551. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8552. !defined(NO_DES3))
  8553. struct test_add_session_ext_params param[1] = {
  8554. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8555. };
  8556. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8557. #endif
  8558. #endif
  8559. return EXPECT_RESULT();
  8560. }
  8561. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8562. {
  8563. EXPECT_DECLS;
  8564. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8565. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8566. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8567. defined(HAVE_SESSION_TICKET) && \
  8568. !defined(TITAN_SESSION_CACHE) && \
  8569. !defined(HUGE_SESSION_CACHE) && \
  8570. !defined(BIG_SESSION_CACHE) && \
  8571. !defined(MEDIUM_SESSION_CACHE)
  8572. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8573. !defined(NO_DES3))
  8574. #ifdef WOLFSSL_DTLS
  8575. struct test_add_session_ext_params param[1] = {
  8576. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8577. };
  8578. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8579. #endif
  8580. #endif
  8581. #endif
  8582. return EXPECT_RESULT();
  8583. }
  8584. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8585. /* canned export of a session using older version 3 */
  8586. static unsigned char version_3[] = {
  8587. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8588. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8589. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8590. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8591. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8592. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8593. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8594. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8595. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8597. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8598. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8599. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8600. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8601. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8602. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8603. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8604. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8605. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8606. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8607. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8608. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8609. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8610. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8611. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8612. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8613. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8614. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8615. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8616. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8617. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8618. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8619. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8620. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8621. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8622. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8623. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8624. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8625. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8626. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8627. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8628. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8629. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8630. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8631. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8632. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8633. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8634. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8635. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8636. 0x2E, 0x31, 0xED, 0x4F
  8637. };
  8638. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8639. static int test_wolfSSL_dtls_export(void)
  8640. {
  8641. EXPECT_DECLS;
  8642. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8643. tcp_ready ready;
  8644. func_args client_args;
  8645. func_args server_args;
  8646. THREAD_TYPE serverThread;
  8647. callback_functions server_cbf;
  8648. callback_functions client_cbf;
  8649. #ifdef WOLFSSL_TIRTOS
  8650. fdOpenSession(Task_self());
  8651. #endif
  8652. InitTcpReady(&ready);
  8653. #if defined(USE_WINDOWS_API)
  8654. /* use RNG to get random port if using windows */
  8655. ready.port = GetRandomPort();
  8656. #endif
  8657. /* set using dtls */
  8658. XMEMSET(&client_args, 0, sizeof(func_args));
  8659. XMEMSET(&server_args, 0, sizeof(func_args));
  8660. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8661. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8662. server_cbf.method = wolfDTLSv1_2_server_method;
  8663. client_cbf.method = wolfDTLSv1_2_client_method;
  8664. server_args.callbacks = &server_cbf;
  8665. client_args.callbacks = &client_cbf;
  8666. server_args.signal = &ready;
  8667. client_args.signal = &ready;
  8668. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8669. wait_tcp_ready(&server_args);
  8670. run_wolfssl_client(&client_args);
  8671. join_thread(serverThread);
  8672. ExpectTrue(client_args.return_code);
  8673. ExpectTrue(server_args.return_code);
  8674. FreeTcpReady(&ready);
  8675. #ifdef WOLFSSL_TIRTOS
  8676. fdOpenSession(Task_self());
  8677. #endif
  8678. if (EXPECT_SUCCESS()) {
  8679. SOCKET_T sockfd = 0;
  8680. WOLFSSL_CTX* ctx = NULL;
  8681. WOLFSSL* ssl = NULL;
  8682. char msg[64] = "hello wolfssl!";
  8683. char reply[1024];
  8684. int msgSz = (int)XSTRLEN(msg);
  8685. byte *session, *window;
  8686. unsigned int sessionSz = 0;
  8687. unsigned int windowSz = 0;
  8688. #ifndef TEST_IPV6
  8689. struct sockaddr_in peerAddr;
  8690. #else
  8691. struct sockaddr_in6 peerAddr;
  8692. #endif /* TEST_IPV6 */
  8693. int i;
  8694. /* Set ctx to DTLS 1.2 */
  8695. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8696. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8697. /* test importing version 3 */
  8698. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8699. /* test importing bad length and bad version */
  8700. version_3[2] += 1;
  8701. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8702. version_3[2] -= 1; version_3[1] = 0XA0;
  8703. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8704. wolfSSL_free(ssl);
  8705. wolfSSL_CTX_free(ctx);
  8706. /* check storing client state after connection and storing window only */
  8707. #ifdef WOLFSSL_TIRTOS
  8708. fdOpenSession(Task_self());
  8709. #endif
  8710. InitTcpReady(&ready);
  8711. #if defined(USE_WINDOWS_API)
  8712. /* use RNG to get random port if using windows */
  8713. ready.port = GetRandomPort();
  8714. #endif
  8715. /* set using dtls */
  8716. XMEMSET(&server_args, 0, sizeof(func_args));
  8717. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8718. server_cbf.method = wolfDTLSv1_2_server_method;
  8719. server_cbf.doUdp = 1;
  8720. server_args.callbacks = &server_cbf;
  8721. server_args.argc = 3; /* set loop_count to 3 */
  8722. server_args.signal = &ready;
  8723. start_thread(test_server_nofail, &server_args, &serverThread);
  8724. wait_tcp_ready(&server_args);
  8725. /* create and connect with client */
  8726. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8727. ExpectIntEQ(WOLFSSL_SUCCESS,
  8728. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8729. ExpectIntEQ(WOLFSSL_SUCCESS,
  8730. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8731. ExpectIntEQ(WOLFSSL_SUCCESS,
  8732. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8733. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8734. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8735. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8736. /* store server information connected too */
  8737. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8738. #ifndef TEST_IPV6
  8739. peerAddr.sin_family = AF_INET;
  8740. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8741. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8742. #else
  8743. peerAddr.sin6_family = AF_INET6;
  8744. ExpectIntEQ(
  8745. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8746. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8747. #endif
  8748. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8749. WOLFSSL_SUCCESS);
  8750. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8751. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8752. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8753. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8754. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8755. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8756. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8757. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8758. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8759. wolfSSL_free(ssl);
  8760. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8761. /* restore state */
  8762. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8763. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8764. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8765. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8766. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8767. WOLFSSL_SUCCESS);
  8768. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8769. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8770. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8771. wolfSSL_free(ssl);
  8772. }
  8773. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8774. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8775. wolfSSL_CTX_free(ctx);
  8776. fprintf(stderr, "done and waiting for server\n");
  8777. join_thread(serverThread);
  8778. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8779. FreeTcpReady(&ready);
  8780. #ifdef WOLFSSL_TIRTOS
  8781. fdOpenSession(Task_self());
  8782. #endif
  8783. }
  8784. #endif
  8785. return EXPECT_RESULT();
  8786. }
  8787. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8788. #ifdef WOLFSSL_TLS13
  8789. static const byte canned_client_tls13_session[] = {
  8790. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8791. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8792. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8793. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8794. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8795. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8796. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8797. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8798. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8799. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8800. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8801. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8802. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8803. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8804. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8805. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8806. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8807. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8808. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8809. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8810. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8811. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8812. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8813. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8814. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8815. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8816. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8817. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8818. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8819. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8820. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8821. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8822. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8823. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8824. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8825. 0x00, 0x03
  8826. };
  8827. static const byte canned_server_tls13_session[] = {
  8828. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8829. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8830. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8831. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8832. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8833. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8834. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8835. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8836. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8837. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8838. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8839. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8840. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8841. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8842. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8843. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8844. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8845. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8846. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8847. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8848. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8849. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8850. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8851. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8852. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8853. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8854. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8855. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8856. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8857. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8858. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8859. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8860. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8861. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8862. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8863. 0x00, 0x04
  8864. };
  8865. #endif /* WOLFSSL_TLS13 */
  8866. static const byte canned_client_session[] = {
  8867. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8868. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8869. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8870. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8871. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8872. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8873. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8874. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8875. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8876. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8877. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8878. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8879. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8880. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8881. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8882. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8883. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8884. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8885. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8886. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8887. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8888. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8889. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8890. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8891. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8892. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8893. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8894. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8895. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8896. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8897. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8900. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8901. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8902. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8903. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8904. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8905. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8906. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8907. 0x00, 0x03
  8908. };
  8909. static const byte canned_server_session[] = {
  8910. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8911. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8912. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8913. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8914. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8915. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8916. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8917. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8918. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8919. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8920. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8921. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8922. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8923. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8924. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8925. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8926. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8927. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8928. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8929. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8930. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8931. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8932. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8933. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8934. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8935. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8936. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8937. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8938. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8940. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8941. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8942. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8943. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8944. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8945. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8946. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8947. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8948. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8949. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8950. 0x00, 0x04
  8951. };
  8952. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8953. {
  8954. SOCKET_T sockfd = 0;
  8955. SOCKET_T clientfd = 0;
  8956. word16 port;
  8957. callback_functions* cbf;
  8958. WOLFSSL_CTX* ctx = 0;
  8959. WOLFSSL* ssl = 0;
  8960. char msg[] = "I hear you fa shizzle!";
  8961. char input[1024];
  8962. int idx;
  8963. #ifdef WOLFSSL_TIRTOS
  8964. fdOpenSession(Task_self());
  8965. #endif
  8966. ((func_args*)args)->return_code = TEST_FAIL;
  8967. cbf = ((func_args*)args)->callbacks;
  8968. #if defined(USE_WINDOWS_API)
  8969. port = ((func_args*)args)->signal->port;
  8970. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8971. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8972. /* Let tcp_listen assign port */
  8973. port = 0;
  8974. #else
  8975. /* Use default port */
  8976. port = wolfSSLPort;
  8977. #endif
  8978. /* do it here to detect failure */
  8979. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8980. CloseSocket(sockfd);
  8981. {
  8982. WOLFSSL_METHOD* method = NULL;
  8983. if (cbf != NULL && cbf->method != NULL) {
  8984. method = cbf->method();
  8985. }
  8986. else {
  8987. method = wolfTLSv1_2_server_method();
  8988. }
  8989. ctx = wolfSSL_CTX_new(method);
  8990. }
  8991. if (ctx == NULL) {
  8992. goto done;
  8993. }
  8994. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8995. /* call ctx setup callback */
  8996. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8997. cbf->ctx_ready(ctx);
  8998. }
  8999. ssl = wolfSSL_new(ctx);
  9000. if (ssl == NULL) {
  9001. goto done;
  9002. }
  9003. wolfSSL_set_fd(ssl, clientfd);
  9004. /* call ssl setup callback */
  9005. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9006. cbf->ssl_ready(ssl);
  9007. }
  9008. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9009. if (idx > 0) {
  9010. input[idx] = '\0';
  9011. fprintf(stderr, "Client message export/import: %s\n", input);
  9012. }
  9013. else {
  9014. fprintf(stderr, "ret = %d error = %d\n", idx,
  9015. wolfSSL_get_error(ssl, idx));
  9016. goto done;
  9017. }
  9018. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9019. /*err_sys("SSL_write failed");*/
  9020. WOLFSSL_RETURN_FROM_THREAD(0);
  9021. }
  9022. #ifdef WOLFSSL_TIRTOS
  9023. Task_yield();
  9024. #endif
  9025. ((func_args*)args)->return_code = TEST_SUCCESS;
  9026. done:
  9027. wolfSSL_shutdown(ssl);
  9028. wolfSSL_free(ssl);
  9029. wolfSSL_CTX_free(ctx);
  9030. CloseSocket(clientfd);
  9031. #ifdef WOLFSSL_TIRTOS
  9032. fdCloseSession(Task_self());
  9033. #endif
  9034. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9035. && defined(HAVE_THREAD_LS)
  9036. wc_ecc_fp_free(); /* free per thread cache */
  9037. #endif
  9038. #if defined(HAVE_SESSION_TICKET) && \
  9039. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9040. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9041. OpenSSLTicketCleanup();
  9042. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9043. TicketCleanup();
  9044. #endif
  9045. #endif
  9046. WOLFSSL_RETURN_FROM_THREAD(0);
  9047. }
  9048. static void load_tls12_canned_server(WOLFSSL* ssl)
  9049. {
  9050. int clientfd = wolfSSL_get_fd(ssl);
  9051. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9052. sizeof(canned_server_session)), sizeof(canned_server_session));
  9053. wolfSSL_set_fd(ssl, clientfd);
  9054. }
  9055. #ifdef WOLFSSL_TLS13
  9056. static void load_tls13_canned_server(WOLFSSL* ssl)
  9057. {
  9058. int clientfd = wolfSSL_get_fd(ssl);
  9059. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9060. sizeof(canned_server_tls13_session)),
  9061. sizeof(canned_server_tls13_session));
  9062. wolfSSL_set_fd(ssl, clientfd);
  9063. }
  9064. #endif
  9065. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9066. static int test_wolfSSL_tls_export_run(int v)
  9067. {
  9068. EXPECT_DECLS;
  9069. SOCKET_T sockfd = 0;
  9070. WOLFSSL_CTX* ctx = 0;
  9071. WOLFSSL* ssl = 0;
  9072. char msg[64] = "hello wolfssl!";
  9073. char reply[1024];
  9074. word32 replySz;
  9075. int msgSz = (int)XSTRLEN(msg);
  9076. const byte* clientSession = NULL;
  9077. int clientSessionSz = 0;
  9078. tcp_ready ready;
  9079. func_args server_args;
  9080. THREAD_TYPE serverThread;
  9081. callback_functions server_cbf;
  9082. #ifdef WOLFSSL_TIRTOS
  9083. fdOpenSession(Task_self());
  9084. #endif
  9085. InitTcpReady(&ready);
  9086. #if defined(USE_WINDOWS_API)
  9087. /* use RNG to get random port if using windows */
  9088. ready.port = GetRandomPort();
  9089. #endif
  9090. XMEMSET(&server_args, 0, sizeof(func_args));
  9091. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9092. switch (v) {
  9093. case WOLFSSL_TLSV1_2:
  9094. server_cbf.method = wolfTLSv1_2_server_method;
  9095. server_cbf.ssl_ready = load_tls12_canned_server;
  9096. /* setup the client side */
  9097. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9098. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9099. clientSession = canned_client_session;
  9100. clientSessionSz = sizeof(canned_client_session);
  9101. break;
  9102. #ifdef WOLFSSL_TLS13
  9103. case WOLFSSL_TLSV1_3:
  9104. server_cbf.method = wolfTLSv1_3_server_method;
  9105. server_cbf.ssl_ready = load_tls13_canned_server;
  9106. /* setup the client side */
  9107. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9108. clientSession = canned_client_tls13_session;
  9109. clientSessionSz = sizeof(canned_client_tls13_session);
  9110. break;
  9111. #endif
  9112. }
  9113. server_args.callbacks = &server_cbf;
  9114. server_args.signal = &ready;
  9115. start_thread(tls_export_server, &server_args, &serverThread);
  9116. wait_tcp_ready(&server_args);
  9117. #ifdef WOLFSSL_TIRTOS
  9118. fdOpenSession(Task_self());
  9119. #endif
  9120. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9121. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9122. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9123. clientSessionSz);
  9124. replySz = sizeof(reply);
  9125. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9126. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9127. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9128. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9129. #endif
  9130. wolfSSL_set_fd(ssl, sockfd);
  9131. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9132. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9133. wolfSSL_free(ssl);
  9134. wolfSSL_CTX_free(ctx);
  9135. CloseSocket(sockfd);
  9136. #ifdef WOLFSSL_TIRTOS
  9137. fdCloseSession(Task_self());
  9138. #endif
  9139. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9140. && defined(HAVE_THREAD_LS)
  9141. wc_ecc_fp_free(); /* free per thread cache */
  9142. #endif
  9143. join_thread(serverThread);
  9144. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9145. FreeTcpReady(&ready);
  9146. #ifdef WOLFSSL_TIRTOS
  9147. fdOpenSession(Task_self());
  9148. #endif
  9149. return EXPECT_RESULT();
  9150. }
  9151. #endif
  9152. static int test_wolfSSL_tls_export(void)
  9153. {
  9154. int res = TEST_SKIPPED;
  9155. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9156. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9157. #ifdef WOLFSSL_TLS13
  9158. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9159. #endif
  9160. res = TEST_RES_CHECK(1);
  9161. #endif
  9162. return res;
  9163. }
  9164. /*----------------------------------------------------------------------------*
  9165. | TLS extensions tests
  9166. *----------------------------------------------------------------------------*/
  9167. #ifdef ENABLE_TLS_CALLBACK_TEST
  9168. /* Connection test runner - generic */
  9169. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9170. callback_functions* server_callbacks)
  9171. {
  9172. tcp_ready ready;
  9173. func_args client_args;
  9174. func_args server_args;
  9175. THREAD_TYPE serverThread;
  9176. XMEMSET(&client_args, 0, sizeof(func_args));
  9177. XMEMSET(&server_args, 0, sizeof(func_args));
  9178. StartTCP();
  9179. client_args.callbacks = client_callbacks;
  9180. server_args.callbacks = server_callbacks;
  9181. #ifdef WOLFSSL_TIRTOS
  9182. fdOpenSession(Task_self());
  9183. #endif
  9184. /* RUN Server side */
  9185. InitTcpReady(&ready);
  9186. #if defined(USE_WINDOWS_API)
  9187. /* use RNG to get random port if using windows */
  9188. ready.port = GetRandomPort();
  9189. #endif
  9190. server_args.signal = &ready;
  9191. client_args.signal = &ready;
  9192. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9193. wait_tcp_ready(&server_args);
  9194. /* RUN Client side */
  9195. run_wolfssl_client(&client_args);
  9196. join_thread(serverThread);
  9197. FreeTcpReady(&ready);
  9198. #ifdef WOLFSSL_TIRTOS
  9199. fdCloseSession(Task_self());
  9200. #endif
  9201. client_callbacks->return_code = client_args.return_code;
  9202. server_callbacks->return_code = server_args.return_code;
  9203. }
  9204. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9205. #ifdef HAVE_SNI
  9206. static int test_wolfSSL_UseSNI_params(void)
  9207. {
  9208. EXPECT_DECLS;
  9209. #if !defined(NO_WOLFSSL_CLIENT)
  9210. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9211. WOLFSSL *ssl = wolfSSL_new(ctx);
  9212. ExpectNotNull(ctx);
  9213. ExpectNotNull(ssl);
  9214. /* invalid [ctx|ssl] */
  9215. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9216. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9217. /* invalid type */
  9218. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9219. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9220. /* invalid data */
  9221. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9222. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9223. /* success case */
  9224. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9225. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9226. wolfSSL_free(ssl);
  9227. wolfSSL_CTX_free(ctx);
  9228. #endif /* !NO_WOLFSSL_CLIENT */
  9229. return EXPECT_RESULT();
  9230. }
  9231. /* BEGIN of connection tests callbacks */
  9232. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9233. {
  9234. AssertIntEQ(WOLFSSL_SUCCESS,
  9235. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9236. }
  9237. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9238. {
  9239. AssertIntEQ(WOLFSSL_SUCCESS,
  9240. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9241. }
  9242. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9243. {
  9244. AssertIntEQ(WOLFSSL_SUCCESS,
  9245. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9246. }
  9247. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9248. {
  9249. use_SNI_at_ssl(ssl);
  9250. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9251. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9252. }
  9253. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9254. {
  9255. use_SNI_at_ssl(ssl);
  9256. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9257. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9258. }
  9259. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9260. {
  9261. use_SNI_at_ctx(ctx);
  9262. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9263. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9264. }
  9265. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9266. {
  9267. use_SNI_at_ssl(ssl);
  9268. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9269. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9270. }
  9271. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9272. {
  9273. use_SNI_at_ctx(ctx);
  9274. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9275. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9276. }
  9277. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9278. {
  9279. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9280. }
  9281. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9282. {
  9283. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9284. }
  9285. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9286. {
  9287. byte type = WOLFSSL_SNI_HOST_NAME;
  9288. void* request = (void*) &type; /* to be overwritten */
  9289. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9290. AssertNotNull(request);
  9291. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9292. AssertNull(request);
  9293. }
  9294. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9295. {
  9296. byte type = WOLFSSL_SNI_HOST_NAME;
  9297. void* request = NULL;
  9298. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9299. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9300. AssertNotNull(request);
  9301. AssertStrEQ("www.wolfssl.com", (char*)request);
  9302. }
  9303. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9304. {
  9305. byte type = WOLFSSL_SNI_HOST_NAME;
  9306. void* request = NULL;
  9307. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9308. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9309. AssertNotNull(request);
  9310. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9311. }
  9312. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9313. {
  9314. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9315. }
  9316. /* END of connection tests callbacks */
  9317. static int test_wolfSSL_UseSNI_connection(void)
  9318. {
  9319. int res = TEST_SKIPPED;
  9320. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9321. callback_functions client_cb;
  9322. callback_functions server_cb;
  9323. size_t i;
  9324. #ifdef WOLFSSL_STATIC_MEMORY
  9325. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9326. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9327. #endif
  9328. struct {
  9329. method_provider client_meth;
  9330. method_provider server_meth;
  9331. #ifdef WOLFSSL_STATIC_MEMORY
  9332. wolfSSL_method_func client_meth_ex;
  9333. wolfSSL_method_func server_meth_ex;
  9334. #endif
  9335. } methods[] = {
  9336. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9337. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9338. #ifdef WOLFSSL_STATIC_MEMORY
  9339. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9340. #endif
  9341. },
  9342. #endif
  9343. #ifndef WOLFSSL_NO_TLS12
  9344. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9345. #ifdef WOLFSSL_STATIC_MEMORY
  9346. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9347. #endif
  9348. },
  9349. #endif
  9350. #ifdef WOLFSSL_TLS13
  9351. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9352. #ifdef WOLFSSL_STATIC_MEMORY
  9353. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9354. #endif
  9355. },
  9356. #endif
  9357. };
  9358. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9359. for (i = 0; i < methodsSz; i++) {
  9360. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9361. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9362. client_cb.method = methods[i].client_meth;
  9363. server_cb.method = methods[i].server_meth;
  9364. client_cb.devId = testDevId;
  9365. server_cb.devId = testDevId;
  9366. #ifdef WOLFSSL_STATIC_MEMORY
  9367. client_cb.method_ex = methods[i].client_meth_ex;
  9368. server_cb.method_ex = methods[i].server_meth_ex;
  9369. client_cb.mem = cliMem;
  9370. client_cb.memSz = (word32)sizeof(cliMem);
  9371. server_cb.mem = svrMem;
  9372. server_cb.memSz = (word32)sizeof(svrMem);;
  9373. #endif
  9374. /* success case at ctx */
  9375. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9376. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9377. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9378. test_wolfSSL_client_server(&client_cb, &server_cb);
  9379. /* success case at ssl */
  9380. fprintf(stderr, "\tsuccess case at ssl\n");
  9381. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9382. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9383. test_wolfSSL_client_server(&client_cb, &server_cb);
  9384. /* default mismatch behavior */
  9385. fprintf(stderr, "\tdefault mismatch behavior\n");
  9386. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9387. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9388. test_wolfSSL_client_server(&client_cb, &server_cb);
  9389. /* continue on mismatch */
  9390. fprintf(stderr, "\tcontinue on mismatch\n");
  9391. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9392. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9393. test_wolfSSL_client_server(&client_cb, &server_cb);
  9394. /* fake answer on mismatch */
  9395. fprintf(stderr, "\tfake answer on mismatch\n");
  9396. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9397. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9398. test_wolfSSL_client_server(&client_cb, &server_cb);
  9399. /* sni abort - success */
  9400. fprintf(stderr, "\tsni abort - success\n");
  9401. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9402. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9403. test_wolfSSL_client_server(&client_cb, &server_cb);
  9404. /* sni abort - abort when absent (ctx) */
  9405. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9406. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9407. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9408. test_wolfSSL_client_server(&client_cb, &server_cb);
  9409. /* sni abort - abort when absent (ssl) */
  9410. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9411. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9412. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9413. test_wolfSSL_client_server(&client_cb, &server_cb);
  9414. /* sni abort - success when overwritten */
  9415. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9416. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9417. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9418. test_wolfSSL_client_server(&client_cb, &server_cb);
  9419. /* sni abort - success when allowing mismatches */
  9420. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9421. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9422. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9423. test_wolfSSL_client_server(&client_cb, &server_cb);
  9424. }
  9425. res = TEST_RES_CHECK(1);
  9426. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9427. return res;
  9428. }
  9429. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9430. {
  9431. EXPECT_DECLS;
  9432. byte buff[] = { /* www.paypal.com */
  9433. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9434. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9435. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9436. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9437. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9438. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9439. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9440. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9441. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9442. };
  9443. byte buff2[] = { /* api.textmate.org */
  9444. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9445. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9446. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9447. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9448. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9449. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9450. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9451. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9452. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9453. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9454. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9455. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9456. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9457. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9458. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9459. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9460. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9461. };
  9462. byte buff3[] = { /* no sni extension */
  9463. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9464. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9465. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9466. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9467. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9468. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9469. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9470. };
  9471. byte buff4[] = { /* last extension has zero size */
  9472. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9473. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9474. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9475. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9476. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9477. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9478. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9479. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9480. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9481. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9482. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9483. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9484. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9485. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9486. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9487. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9488. 0x12, 0x00, 0x00
  9489. };
  9490. byte buff5[] = { /* SSL v2.0 client hello */
  9491. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9492. /* dummy bytes below, just to pass size check */
  9493. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9494. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9495. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9496. };
  9497. byte result[32] = {0};
  9498. word32 length = 32;
  9499. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9500. 0, result, &length));
  9501. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9502. 0, result, &length));
  9503. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9504. 1, result, &length));
  9505. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9506. 0, result, &length));
  9507. buff[0] = 0x16;
  9508. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9509. 0, result, &length));
  9510. buff[1] = 0x03;
  9511. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9512. sizeof(buff), 0, result, &length));
  9513. buff[2] = 0x03;
  9514. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9515. sizeof(buff), 0, result, &length));
  9516. buff[4] = 0x64;
  9517. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9518. 0, result, &length));
  9519. if (EXPECT_SUCCESS())
  9520. result[length] = 0;
  9521. ExpectStrEQ("www.paypal.com", (const char*) result);
  9522. length = 32;
  9523. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9524. 0, result, &length));
  9525. if (EXPECT_SUCCESS())
  9526. result[length] = 0;
  9527. ExpectStrEQ("api.textmate.org", (const char*) result);
  9528. /* SSL v2.0 tests */
  9529. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9530. sizeof(buff5), 0, result, &length));
  9531. buff5[2] = 0x02;
  9532. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9533. sizeof(buff5), 0, result, &length));
  9534. buff5[2] = 0x01; buff5[6] = 0x08;
  9535. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9536. sizeof(buff5), 0, result, &length));
  9537. buff5[6] = 0x09; buff5[8] = 0x01;
  9538. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9539. sizeof(buff5), 0, result, &length));
  9540. return EXPECT_RESULT();
  9541. }
  9542. #endif /* HAVE_SNI */
  9543. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9544. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9545. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9546. /* Dummy peer functions to satisfy the exporter/importer */
  9547. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9548. int* ipSz, unsigned short* port, int* fam)
  9549. {
  9550. (void)ssl;
  9551. ip[0] = -1;
  9552. *ipSz = 1;
  9553. *port = 1;
  9554. *fam = 2;
  9555. return 1;
  9556. }
  9557. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9558. int ipSz, unsigned short port, int fam)
  9559. {
  9560. (void)ssl;
  9561. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9562. return 0;
  9563. return 1;
  9564. }
  9565. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9566. WOLFSSL **ssl)
  9567. {
  9568. EXPECT_DECLS;
  9569. unsigned char* sessionBuf = NULL;
  9570. unsigned int sessionSz = 0;
  9571. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9572. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9573. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9574. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9575. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9576. ExpectNotNull(sessionBuf =
  9577. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9578. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9579. wolfSSL_free(*ssl);
  9580. *ssl = NULL;
  9581. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9582. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9583. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9584. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9585. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9586. return EXPECT_RESULT();
  9587. }
  9588. #endif
  9589. static int test_wolfSSL_dtls_export_peers(void)
  9590. {
  9591. EXPECT_DECLS;
  9592. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9593. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9594. test_ssl_cbf client_cbf;
  9595. test_ssl_cbf server_cbf;
  9596. size_t i, j;
  9597. struct test_params {
  9598. method_provider client_meth;
  9599. method_provider server_meth;
  9600. const char* dtls_version;
  9601. } params[] = {
  9602. #ifndef NO_OLD_TLS
  9603. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9604. #endif
  9605. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9606. /* TODO DTLS 1.3 exporting not supported
  9607. #ifdef WOLFSSL_DTLS13
  9608. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9609. #endif
  9610. */
  9611. };
  9612. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9613. for (j = 0; j <= 0b11; j++) {
  9614. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9615. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9616. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9617. client_cbf.method = params[i].client_meth;
  9618. server_cbf.method = params[i].server_meth;
  9619. if (j & 0b01) {
  9620. client_cbf.on_handshake =
  9621. test_wolfSSL_dtls_export_peers_on_handshake;
  9622. printf(" With client export;");
  9623. }
  9624. if (j & 0b10) {
  9625. server_cbf.on_handshake =
  9626. test_wolfSSL_dtls_export_peers_on_handshake;
  9627. printf(" With server export;");
  9628. }
  9629. printf("\n");
  9630. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9631. &server_cbf, NULL), TEST_SUCCESS);
  9632. if (!EXPECT_SUCCESS())
  9633. break;
  9634. }
  9635. }
  9636. #endif
  9637. return EXPECT_RESULT();
  9638. }
  9639. static int test_wolfSSL_UseTrustedCA(void)
  9640. {
  9641. EXPECT_DECLS;
  9642. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9643. && !defined(NO_RSA)
  9644. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9645. WOLFSSL_CTX *ctx = NULL;
  9646. WOLFSSL *ssl = NULL;
  9647. byte id[20];
  9648. #ifndef NO_WOLFSSL_SERVER
  9649. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9650. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9651. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9652. #else
  9653. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9654. #endif
  9655. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9656. XMEMSET(id, 0, sizeof(id));
  9657. /* error cases */
  9658. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9659. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9660. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9661. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9662. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9663. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9664. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9665. #ifdef NO_SHA
  9666. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9667. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9668. #endif
  9669. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9670. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9671. /* success cases */
  9672. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9673. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9674. #ifndef NO_SHA
  9675. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9676. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9677. #endif
  9678. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9679. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9680. wolfSSL_free(ssl);
  9681. wolfSSL_CTX_free(ctx);
  9682. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9683. #endif /* HAVE_TRUSTED_CA */
  9684. return EXPECT_RESULT();
  9685. }
  9686. static int test_wolfSSL_UseMaxFragment(void)
  9687. {
  9688. EXPECT_DECLS;
  9689. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9690. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9691. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9692. #ifndef NO_WOLFSSL_SERVER
  9693. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9694. #else
  9695. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9696. #endif
  9697. WOLFSSL *ssl = NULL;
  9698. #ifdef OPENSSL_EXTRA
  9699. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  9700. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9701. #else
  9702. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9703. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9704. #endif
  9705. #ifndef NO_WOLFSSL_SERVER
  9706. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9707. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9708. #endif
  9709. ExpectNotNull(ctx);
  9710. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9711. #ifdef OPENSSL_EXTRA
  9712. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9713. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9714. #else
  9715. UseMaxFragment = wolfSSL_UseMaxFragment;
  9716. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9717. #endif
  9718. /* error cases */
  9719. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9720. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9721. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9722. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9723. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9724. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9725. /* success case */
  9726. #ifdef OPENSSL_EXTRA
  9727. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9728. #else
  9729. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9730. #endif
  9731. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9732. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9733. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9734. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9735. #ifdef OPENSSL_EXTRA
  9736. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9737. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9738. #else
  9739. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9740. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9741. #endif
  9742. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9743. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9744. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9745. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9746. #ifdef OPENSSL_EXTRA
  9747. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9748. #else
  9749. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9750. #endif
  9751. wolfSSL_free(ssl);
  9752. wolfSSL_CTX_free(ctx);
  9753. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9754. #endif
  9755. return EXPECT_RESULT();
  9756. }
  9757. static int test_wolfSSL_UseTruncatedHMAC(void)
  9758. {
  9759. EXPECT_DECLS;
  9760. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9761. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9762. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9763. #ifndef NO_WOLFSSL_SERVER
  9764. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9765. #else
  9766. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9767. #endif
  9768. WOLFSSL *ssl = NULL;
  9769. ExpectNotNull(ctx);
  9770. #ifndef NO_WOLFSSL_SERVER
  9771. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9772. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9773. #endif
  9774. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9775. /* error cases */
  9776. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9777. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9778. /* success case */
  9779. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9780. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9781. wolfSSL_free(ssl);
  9782. wolfSSL_CTX_free(ctx);
  9783. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9784. #endif
  9785. return EXPECT_RESULT();
  9786. }
  9787. static int test_wolfSSL_UseSupportedCurve(void)
  9788. {
  9789. EXPECT_DECLS;
  9790. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9791. !defined(NO_TLS)
  9792. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9793. WOLFSSL *ssl = wolfSSL_new(ctx);
  9794. ExpectNotNull(ctx);
  9795. ExpectNotNull(ssl);
  9796. /* error cases */
  9797. ExpectIntNE(WOLFSSL_SUCCESS,
  9798. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9799. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9800. ExpectIntNE(WOLFSSL_SUCCESS,
  9801. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9802. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9803. /* success case */
  9804. ExpectIntEQ(WOLFSSL_SUCCESS,
  9805. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9806. ExpectIntEQ(WOLFSSL_SUCCESS,
  9807. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9808. wolfSSL_free(ssl);
  9809. wolfSSL_CTX_free(ctx);
  9810. #endif
  9811. return EXPECT_RESULT();
  9812. }
  9813. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9814. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9815. {
  9816. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9817. }
  9818. static void use_ALPN_all(WOLFSSL* ssl)
  9819. {
  9820. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9821. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9822. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9823. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9824. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9825. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9826. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9827. }
  9828. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9829. {
  9830. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9831. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9832. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9833. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9834. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9835. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9836. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9837. }
  9838. static void use_ALPN_one(WOLFSSL* ssl)
  9839. {
  9840. /* spdy/2 */
  9841. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9842. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9843. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9844. }
  9845. static void use_ALPN_unknown(WOLFSSL* ssl)
  9846. {
  9847. /* http/2.0 */
  9848. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9849. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9850. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9851. }
  9852. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9853. {
  9854. /* http/2.0 */
  9855. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9856. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9857. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9858. }
  9859. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9860. {
  9861. /* spdy/3 */
  9862. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9863. char *proto = NULL;
  9864. word16 protoSz = 0;
  9865. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9866. /* check value */
  9867. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9868. if (proto) {
  9869. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9870. }
  9871. }
  9872. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9873. {
  9874. char *proto = NULL;
  9875. word16 protoSz = 0;
  9876. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9877. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9878. /* check value */
  9879. AssertIntEQ(1, (0 == protoSz));
  9880. AssertIntEQ(1, (NULL == proto));
  9881. }
  9882. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9883. {
  9884. /* http/1.1 */
  9885. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9886. char *proto;
  9887. word16 protoSz = 0;
  9888. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9889. /* check value */
  9890. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9891. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9892. }
  9893. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9894. {
  9895. /* spdy/2 */
  9896. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9897. char *proto;
  9898. word16 protoSz = 0;
  9899. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9900. /* check value */
  9901. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9902. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9903. }
  9904. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9905. {
  9906. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9907. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9908. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9909. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9910. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9911. char *clist = NULL;
  9912. word16 clistSz = 0;
  9913. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9914. &clistSz));
  9915. /* check value */
  9916. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9917. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9918. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9919. }
  9920. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9921. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9922. /* ALPN select callback, success with spdy/2 */
  9923. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  9924. unsigned char *outlen, const unsigned char *in,
  9925. unsigned int inlen, void *arg)
  9926. {
  9927. /* spdy/2 */
  9928. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9929. (void)ssl;
  9930. (void)arg;
  9931. /* adding +1 since LEN byte comes first */
  9932. if (inlen < sizeof(proto) + 1) {
  9933. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9934. }
  9935. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  9936. *out = in + 1;
  9937. *outlen = (unsigned char)sizeof(proto);
  9938. return SSL_TLSEXT_ERR_OK;
  9939. }
  9940. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9941. }
  9942. /* ALPN select callback, force failure */
  9943. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  9944. unsigned char *outlen, const unsigned char *in,
  9945. unsigned int inlen, void *arg)
  9946. {
  9947. (void)ssl;
  9948. (void)out;
  9949. (void)outlen;
  9950. (void)in;
  9951. (void)inlen;
  9952. (void)arg;
  9953. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9954. }
  9955. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  9956. {
  9957. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  9958. }
  9959. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  9960. {
  9961. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  9962. }
  9963. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  9964. static int test_wolfSSL_UseALPN_connection(void)
  9965. {
  9966. int res = TEST_SKIPPED;
  9967. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9968. callback_functions client_cb;
  9969. callback_functions server_cb;
  9970. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9971. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9972. client_cb.method = wolfSSLv23_client_method;
  9973. server_cb.method = wolfSSLv23_server_method;
  9974. client_cb.devId = testDevId;
  9975. server_cb.devId = testDevId;
  9976. /* success case same list */
  9977. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9978. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9979. test_wolfSSL_client_server(&client_cb, &server_cb);
  9980. /* success case only one for server */
  9981. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9982. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9983. test_wolfSSL_client_server(&client_cb, &server_cb);
  9984. /* success case only one for client */
  9985. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9986. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9987. test_wolfSSL_client_server(&client_cb, &server_cb);
  9988. /* success case none for client */
  9989. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9990. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9991. test_wolfSSL_client_server(&client_cb, &server_cb);
  9992. /* success case mismatch behavior but option 'continue' set */
  9993. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9994. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9995. test_wolfSSL_client_server(&client_cb, &server_cb);
  9996. /* success case read protocol send by client */
  9997. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9998. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9999. test_wolfSSL_client_server(&client_cb, &server_cb);
  10000. /* mismatch behavior with same list
  10001. * the first and only this one must be taken */
  10002. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10003. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10004. test_wolfSSL_client_server(&client_cb, &server_cb);
  10005. /* default mismatch behavior */
  10006. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10007. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10008. test_wolfSSL_client_server(&client_cb, &server_cb);
  10009. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10010. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10011. /* WOLFSSL-level ALPN select callback tests */
  10012. /* Callback: success (one protocol, spdy/2) */
  10013. client_cb.ctx_ready = NULL;
  10014. client_cb.ssl_ready = use_ALPN_one;
  10015. client_cb.on_result = verify_ALPN_matching_spdy2;
  10016. server_cb.ctx_ready = NULL;
  10017. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10018. server_cb.on_result = verify_ALPN_matching_spdy2;
  10019. test_wolfSSL_client_server(&client_cb, &server_cb);
  10020. /* Callback: failure (one client protocol, spdy/2) */
  10021. client_cb.ctx_ready = NULL;
  10022. client_cb.ssl_ready = use_ALPN_one;
  10023. client_cb.on_result = NULL;
  10024. server_cb.ctx_ready = NULL;
  10025. server_cb.ssl_ready = use_ALPN_failure_callback;
  10026. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10027. test_wolfSSL_client_server(&client_cb, &server_cb);
  10028. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10029. res = TEST_RES_CHECK(1);
  10030. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10031. return res;
  10032. }
  10033. static int test_wolfSSL_UseALPN_params(void)
  10034. {
  10035. EXPECT_DECLS;
  10036. #ifndef NO_WOLFSSL_CLIENT
  10037. /* "http/1.1" */
  10038. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10039. /* "spdy/1" */
  10040. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10041. /* "spdy/2" */
  10042. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10043. /* "spdy/3" */
  10044. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10045. char buff[256];
  10046. word32 idx;
  10047. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10048. WOLFSSL *ssl = wolfSSL_new(ctx);
  10049. ExpectNotNull(ctx);
  10050. ExpectNotNull(ssl);
  10051. /* error cases */
  10052. ExpectIntNE(WOLFSSL_SUCCESS,
  10053. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10054. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10055. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10056. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10057. /* success case */
  10058. /* http1 only */
  10059. ExpectIntEQ(WOLFSSL_SUCCESS,
  10060. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10061. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10062. /* http1, spdy1 */
  10063. XMEMCPY(buff, http1, sizeof(http1));
  10064. idx = sizeof(http1);
  10065. buff[idx++] = ',';
  10066. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10067. idx += sizeof(spdy1);
  10068. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10069. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10070. /* http1, spdy2, spdy1 */
  10071. XMEMCPY(buff, http1, sizeof(http1));
  10072. idx = sizeof(http1);
  10073. buff[idx++] = ',';
  10074. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10075. idx += sizeof(spdy2);
  10076. buff[idx++] = ',';
  10077. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10078. idx += sizeof(spdy1);
  10079. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10080. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10081. /* spdy3, http1, spdy2, spdy1 */
  10082. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10083. idx = sizeof(spdy3);
  10084. buff[idx++] = ',';
  10085. XMEMCPY(buff+idx, http1, sizeof(http1));
  10086. idx += sizeof(http1);
  10087. buff[idx++] = ',';
  10088. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10089. idx += sizeof(spdy2);
  10090. buff[idx++] = ',';
  10091. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10092. idx += sizeof(spdy1);
  10093. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10094. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10095. wolfSSL_free(ssl);
  10096. wolfSSL_CTX_free(ctx);
  10097. #endif
  10098. return EXPECT_RESULT();
  10099. }
  10100. #endif /* HAVE_ALPN */
  10101. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10102. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10103. {
  10104. unsigned char p[] = {
  10105. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10106. 6, 's', 'p', 'd', 'y', '/', '2',
  10107. 6, 's', 'p', 'd', 'y', '/', '1',
  10108. };
  10109. unsigned char p_len = sizeof(p);
  10110. int ret;
  10111. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10112. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10113. AssertIntEQ(ret, 0);
  10114. #else
  10115. AssertIntEQ(ret, SSL_SUCCESS);
  10116. #endif
  10117. }
  10118. static void set_alpn_protos(SSL* ssl)
  10119. {
  10120. unsigned char p[] = {
  10121. 6, 's', 'p', 'd', 'y', '/', '3',
  10122. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10123. 6, 's', 'p', 'd', 'y', '/', '2',
  10124. 6, 's', 'p', 'd', 'y', '/', '1',
  10125. };
  10126. unsigned char p_len = sizeof(p);
  10127. int ret;
  10128. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10129. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10130. AssertIntEQ(ret, 0);
  10131. #else
  10132. AssertIntEQ(ret, SSL_SUCCESS);
  10133. #endif
  10134. }
  10135. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10136. {
  10137. /* "spdy/3" */
  10138. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10139. const unsigned char *proto;
  10140. unsigned int protoSz = 0;
  10141. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10142. /* check value */
  10143. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10144. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10145. }
  10146. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10147. {
  10148. /* "http/1.1" */
  10149. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10150. const unsigned char *proto;
  10151. unsigned int protoSz = 0;
  10152. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10153. /* check value */
  10154. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10155. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10156. }
  10157. static int test_wolfSSL_set_alpn_protos(void)
  10158. {
  10159. int res = TEST_SKIPPED;
  10160. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10161. callback_functions client_cb;
  10162. callback_functions server_cb;
  10163. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10164. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10165. client_cb.method = wolfSSLv23_client_method;
  10166. server_cb.method = wolfSSLv23_server_method;
  10167. client_cb.devId = testDevId;
  10168. server_cb.devId = testDevId;
  10169. /* use CTX_alpn_protos */
  10170. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10171. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10172. test_wolfSSL_client_server(&client_cb, &server_cb);
  10173. /* use set_alpn_protos */
  10174. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10175. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10176. test_wolfSSL_client_server(&client_cb, &server_cb);
  10177. res = TEST_SUCCESS;
  10178. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10179. return res;
  10180. }
  10181. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10182. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10183. {
  10184. EXPECT_DECLS;
  10185. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10186. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10187. WOLFSSL *ssl = wolfSSL_new(ctx);
  10188. ExpectNotNull(ctx);
  10189. ExpectNotNull(ssl);
  10190. /* error cases */
  10191. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10192. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10193. /* success cases */
  10194. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10195. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10196. wolfSSL_free(ssl);
  10197. wolfSSL_CTX_free(ctx);
  10198. #endif
  10199. return EXPECT_RESULT();
  10200. }
  10201. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10202. {
  10203. EXPECT_DECLS;
  10204. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10205. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10206. WOLFSSL *ssl = wolfSSL_new(ctx);
  10207. ExpectNotNull(ctx);
  10208. ExpectNotNull(ssl);
  10209. /* error cases */
  10210. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10211. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10212. /* success cases */
  10213. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10214. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10215. wolfSSL_free(ssl);
  10216. wolfSSL_CTX_free(ctx);
  10217. #endif
  10218. return EXPECT_RESULT();
  10219. }
  10220. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10221. static int test_wolfSSL_SCR_Reconnect(void)
  10222. {
  10223. EXPECT_DECLS;
  10224. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10225. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10226. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10227. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10228. struct test_memio_ctx test_ctx;
  10229. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10230. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10231. byte data;
  10232. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10233. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10234. test_ctx.s_ciphers =
  10235. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10236. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10237. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10238. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10239. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10240. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10241. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10242. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10243. /* WOLFSSL_FATAL_ERROR since it will block */
  10244. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10245. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10246. WOLFSSL_ERROR_WANT_READ);
  10247. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10248. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10249. WOLFSSL_ERROR_WANT_READ);
  10250. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10251. wolfSSL_free(ssl_c);
  10252. ssl_c = NULL;
  10253. wolfSSL_free(ssl_s);
  10254. ssl_s = NULL;
  10255. wolfSSL_CTX_free(ctx_c);
  10256. ctx_c = NULL;
  10257. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10258. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10259. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10260. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10261. wolfSSL_free(ssl_s);
  10262. wolfSSL_free(ssl_c);
  10263. wolfSSL_CTX_free(ctx_s);
  10264. wolfSSL_CTX_free(ctx_c);
  10265. #endif
  10266. return EXPECT_RESULT();
  10267. }
  10268. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10269. (!defined(NO_RSA) || defined(HAVE_ECC))
  10270. /* Called when writing. */
  10271. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10272. {
  10273. (void)ssl;
  10274. (void)buf;
  10275. (void)sz;
  10276. (void)ctx;
  10277. /* Force error return from wolfSSL_accept_TLSv13(). */
  10278. return WANT_WRITE;
  10279. }
  10280. /* Called when reading. */
  10281. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10282. {
  10283. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10284. int len = (int)msg->length;
  10285. (void)ssl;
  10286. (void)sz;
  10287. /* Pass back as much of message as will fit in buffer. */
  10288. if (len > sz)
  10289. len = sz;
  10290. XMEMCPY(buf, msg->buffer, len);
  10291. /* Move over returned data. */
  10292. msg->buffer += len;
  10293. msg->length -= len;
  10294. /* Amount actually copied. */
  10295. return len;
  10296. }
  10297. #endif
  10298. /* Test the detection of duplicate known TLS extensions.
  10299. * Specifically in a ClientHello.
  10300. */
  10301. static int test_tls_ext_duplicate(void)
  10302. {
  10303. EXPECT_DECLS;
  10304. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10305. !defined(NO_FILESYSTEM)
  10306. const unsigned char clientHelloDupTlsExt[] = {
  10307. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10308. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10309. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10310. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10311. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10312. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10313. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10314. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10315. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10316. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10317. 0x00, 0x9e, 0x01, 0x00,
  10318. /* Extensions - duplicate signature algorithms. */
  10319. 0x00, 0x19, 0x00, 0x0d,
  10320. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10321. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10322. /* Supported Versions extension for TLS 1.3. */
  10323. 0x00, 0x2b,
  10324. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10325. };
  10326. WOLFSSL_BUFFER_INFO msg;
  10327. const char* testCertFile;
  10328. const char* testKeyFile;
  10329. WOLFSSL_CTX *ctx = NULL;
  10330. WOLFSSL *ssl = NULL;
  10331. #ifndef NO_RSA
  10332. testCertFile = svrCertFile;
  10333. testKeyFile = svrKeyFile;
  10334. #elif defined(HAVE_ECC)
  10335. testCertFile = eccCertFile;
  10336. testKeyFile = eccKeyFile;
  10337. #endif
  10338. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10339. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10340. WOLFSSL_FILETYPE_PEM));
  10341. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10342. WOLFSSL_FILETYPE_PEM));
  10343. /* Read from 'msg'. */
  10344. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10345. /* No where to send to - dummy sender. */
  10346. wolfSSL_SetIOSend(ctx, DummySend);
  10347. ssl = wolfSSL_new(ctx);
  10348. ExpectNotNull(ssl);
  10349. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10350. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10351. wolfSSL_SetIOReadCtx(ssl, &msg);
  10352. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10353. /* can return duplicate ext error or socket error if the peer closed down
  10354. * while sending alert */
  10355. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10356. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10357. }
  10358. wolfSSL_free(ssl);
  10359. wolfSSL_CTX_free(ctx);
  10360. #endif
  10361. return EXPECT_RESULT();
  10362. }
  10363. /*----------------------------------------------------------------------------*
  10364. | X509 Tests
  10365. *----------------------------------------------------------------------------*/
  10366. static int test_wolfSSL_X509_NAME_get_entry(void)
  10367. {
  10368. EXPECT_DECLS;
  10369. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10370. #if defined(OPENSSL_ALL) || \
  10371. (defined(OPENSSL_EXTRA) && \
  10372. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10373. /* use openssl like name to test mapping */
  10374. X509_NAME_ENTRY* ne = NULL;
  10375. X509_NAME* name = NULL;
  10376. X509* x509 = NULL;
  10377. #ifndef NO_FILESYSTEM
  10378. ASN1_STRING* asn = NULL;
  10379. char* subCN = NULL;
  10380. #endif
  10381. int idx = 0;
  10382. ASN1_OBJECT *object = NULL;
  10383. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10384. defined(WOLFSSL_NGINX)
  10385. #ifndef NO_BIO
  10386. BIO* bio = NULL;
  10387. #endif
  10388. #endif
  10389. #ifndef NO_FILESYSTEM
  10390. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10391. WOLFSSL_FILETYPE_PEM));
  10392. ExpectNotNull(name = X509_get_subject_name(x509));
  10393. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10394. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10395. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10396. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10397. wolfSSL_FreeX509(x509);
  10398. x509 = NULL;
  10399. #endif
  10400. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10401. WOLFSSL_FILETYPE_PEM));
  10402. ExpectNotNull(name = X509_get_subject_name(x509));
  10403. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10404. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10405. defined(WOLFSSL_NGINX)
  10406. #ifndef NO_BIO
  10407. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10408. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10409. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10410. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10411. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10412. BIO_free(bio);
  10413. #endif
  10414. #endif
  10415. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10416. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10417. wolfSSL_FreeX509(x509);
  10418. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10419. #endif /* !NO_CERTS && !NO_RSA */
  10420. return EXPECT_RESULT();
  10421. }
  10422. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10423. static int test_wolfSSL_PKCS12(void)
  10424. {
  10425. EXPECT_DECLS;
  10426. /* .p12 file is encrypted with DES3 */
  10427. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10428. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10429. * Password Key
  10430. */
  10431. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10432. !defined(NO_STDIO_FILESYSTEM) && \
  10433. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10434. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10435. byte buf[6000];
  10436. char file[] = "./certs/test-servercert.p12";
  10437. char order[] = "./certs/ecc-rsa-server.p12";
  10438. #ifdef WC_RC2
  10439. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10440. #endif
  10441. char pass[] = "a password";
  10442. const char goodPsw[] = "wolfSSL test";
  10443. const char badPsw[] = "bad";
  10444. #ifdef HAVE_ECC
  10445. WOLFSSL_X509_NAME *subject = NULL;
  10446. WOLFSSL_X509 *x509 = NULL;
  10447. #endif
  10448. XFILE f = XBADFILE;
  10449. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10450. WOLFSSL_BIO *bio = NULL;
  10451. WOLFSSL_EVP_PKEY *pkey = NULL;
  10452. WC_PKCS12 *pkcs12 = NULL;
  10453. WC_PKCS12 *pkcs12_2 = NULL;
  10454. WOLFSSL_X509 *cert = NULL;
  10455. WOLFSSL_X509 *tmp = NULL;
  10456. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10457. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10458. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10459. WOLFSSL_CTX *ctx = NULL;
  10460. WOLFSSL *ssl = NULL;
  10461. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10462. #endif
  10463. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10464. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10465. if (f != XBADFILE) {
  10466. XFCLOSE(f);
  10467. f = XBADFILE;
  10468. }
  10469. goodPswLen = (int)XSTRLEN(goodPsw);
  10470. badPswLen = (int)XSTRLEN(badPsw);
  10471. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10472. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10473. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10474. ExpectNotNull(pkcs12);
  10475. BIO_free(bio);
  10476. bio = NULL;
  10477. /* check verify MAC directly */
  10478. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10479. /* check verify MAC fail case directly */
  10480. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10481. /* check verify MAC fail case */
  10482. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10483. ExpectNull(pkey);
  10484. ExpectNull(cert);
  10485. /* check parse with no extra certs kept */
  10486. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10487. 1);
  10488. ExpectNotNull(pkey);
  10489. ExpectNotNull(cert);
  10490. wolfSSL_EVP_PKEY_free(pkey);
  10491. pkey = NULL;
  10492. wolfSSL_X509_free(cert);
  10493. cert = NULL;
  10494. /* check parse with extra certs kept */
  10495. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10496. 1);
  10497. ExpectNotNull(pkey);
  10498. ExpectNotNull(cert);
  10499. ExpectNotNull(ca);
  10500. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10501. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10502. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10503. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10504. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10505. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10506. #else
  10507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10508. #endif
  10509. /* Copy stack structure */
  10510. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10511. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10512. /* CTX now owns the tmp_ca stack structure */
  10513. tmp_ca = NULL;
  10514. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10515. ExpectNotNull(tmp_ca);
  10516. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10517. /* Check that the main cert is also set */
  10518. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10519. ExpectNotNull(ssl = SSL_new(ctx));
  10520. ExpectNotNull(SSL_get_certificate(ssl));
  10521. SSL_free(ssl);
  10522. SSL_CTX_free(ctx);
  10523. ctx = NULL;
  10524. #endif
  10525. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10526. /* should be 2 other certs on stack */
  10527. ExpectNotNull(tmp = sk_X509_pop(ca));
  10528. X509_free(tmp);
  10529. ExpectNotNull(tmp = sk_X509_pop(ca));
  10530. X509_free(tmp);
  10531. ExpectNull(sk_X509_pop(ca));
  10532. EVP_PKEY_free(pkey);
  10533. pkey = NULL;
  10534. X509_free(cert);
  10535. cert = NULL;
  10536. sk_X509_pop_free(ca, X509_free);
  10537. ca = NULL;
  10538. /* check PKCS12_create */
  10539. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10540. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10541. SSL_SUCCESS);
  10542. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10543. -1, -1, 100, -1, 0)));
  10544. EVP_PKEY_free(pkey);
  10545. pkey = NULL;
  10546. X509_free(cert);
  10547. cert = NULL;
  10548. sk_X509_pop_free(ca, NULL);
  10549. ca = NULL;
  10550. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10551. SSL_SUCCESS);
  10552. PKCS12_free(pkcs12_2);
  10553. pkcs12_2 = NULL;
  10554. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10555. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10556. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10557. 2000, 1, 0)));
  10558. EVP_PKEY_free(pkey);
  10559. pkey = NULL;
  10560. X509_free(cert);
  10561. cert = NULL;
  10562. sk_X509_pop_free(ca, NULL);
  10563. ca = NULL;
  10564. /* convert to DER then back and parse */
  10565. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10566. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10567. PKCS12_free(pkcs12_2);
  10568. pkcs12_2 = NULL;
  10569. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10570. BIO_free(bio);
  10571. bio = NULL;
  10572. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10573. SSL_SUCCESS);
  10574. /* should be 2 other certs on stack */
  10575. ExpectNotNull(tmp = sk_X509_pop(ca));
  10576. X509_free(tmp);
  10577. ExpectNotNull(tmp = sk_X509_pop(ca));
  10578. X509_free(tmp);
  10579. ExpectNull(sk_X509_pop(ca));
  10580. #ifndef NO_RC4
  10581. PKCS12_free(pkcs12_2);
  10582. pkcs12_2 = NULL;
  10583. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10584. NID_pbe_WithSHA1And128BitRC4,
  10585. NID_pbe_WithSHA1And128BitRC4,
  10586. 2000, 1, 0)));
  10587. EVP_PKEY_free(pkey);
  10588. pkey = NULL;
  10589. X509_free(cert);
  10590. cert = NULL;
  10591. sk_X509_pop_free(ca, NULL);
  10592. ca = NULL;
  10593. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10594. SSL_SUCCESS);
  10595. #endif /* NO_RC4 */
  10596. EVP_PKEY_free(pkey);
  10597. pkey = NULL;
  10598. X509_free(cert);
  10599. cert = NULL;
  10600. PKCS12_free(pkcs12);
  10601. pkcs12 = NULL;
  10602. PKCS12_free(pkcs12_2);
  10603. pkcs12_2 = NULL;
  10604. sk_X509_pop_free(ca, NULL);
  10605. ca = NULL;
  10606. #ifdef HAVE_ECC
  10607. /* test order of parsing */
  10608. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10609. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10610. if (f != XBADFILE) {
  10611. XFCLOSE(f);
  10612. f = XBADFILE;
  10613. }
  10614. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10615. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10616. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10617. WOLFSSL_SUCCESS);
  10618. /* check use of pkey after parse */
  10619. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10620. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10621. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10622. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10623. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10624. #else
  10625. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10626. #endif
  10627. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10628. SSL_CTX_free(ctx);
  10629. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10630. #endif
  10631. ExpectNotNull(pkey);
  10632. ExpectNotNull(cert);
  10633. ExpectNotNull(ca);
  10634. /* compare subject lines of certificates */
  10635. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10636. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10637. SSL_FILETYPE_PEM));
  10638. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10639. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10640. X509_free(x509);
  10641. x509 = NULL;
  10642. /* test expected fail case */
  10643. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10644. SSL_FILETYPE_PEM));
  10645. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10646. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10647. X509_free(x509);
  10648. x509 = NULL;
  10649. X509_free(cert);
  10650. cert = NULL;
  10651. /* get subject line from ca stack */
  10652. ExpectNotNull(cert = sk_X509_pop(ca));
  10653. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10654. /* compare subject from certificate in ca to expected */
  10655. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10656. SSL_FILETYPE_PEM));
  10657. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10658. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10659. EVP_PKEY_free(pkey);
  10660. pkey = NULL;
  10661. X509_free(x509);
  10662. x509 = NULL;
  10663. X509_free(cert);
  10664. cert = NULL;
  10665. BIO_free(bio);
  10666. bio = NULL;
  10667. PKCS12_free(pkcs12);
  10668. pkcs12 = NULL;
  10669. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10670. ca = NULL;
  10671. /* test order of parsing */
  10672. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10673. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10674. if (f != XBADFILE) {
  10675. XFCLOSE(f);
  10676. f = XBADFILE;
  10677. }
  10678. /* check verify MAC fail case */
  10679. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10680. ExpectNull(pkey);
  10681. ExpectNull(cert);
  10682. /* check parse with no extra certs kept */
  10683. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10684. 1);
  10685. ExpectNotNull(pkey);
  10686. ExpectNotNull(cert);
  10687. wolfSSL_EVP_PKEY_free(pkey);
  10688. pkey = NULL;
  10689. wolfSSL_X509_free(cert);
  10690. cert = NULL;
  10691. /* check parse with extra certs kept */
  10692. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10693. 1);
  10694. ExpectNotNull(pkey);
  10695. ExpectNotNull(cert);
  10696. ExpectNotNull(ca);
  10697. wolfSSL_EVP_PKEY_free(pkey);
  10698. pkey = NULL;
  10699. wolfSSL_X509_free(cert);
  10700. cert = NULL;
  10701. sk_X509_pop_free(ca, NULL);
  10702. ca = NULL;
  10703. PKCS12_free(pkcs12);
  10704. pkcs12 = NULL;
  10705. #endif /* HAVE_ECC */
  10706. #ifdef WC_RC2
  10707. /* test PKCS#12 with RC2 encryption */
  10708. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10709. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10710. if (f != XBADFILE) {
  10711. XFCLOSE(f);
  10712. f = XBADFILE;
  10713. }
  10714. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10715. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10716. /* check verify MAC fail case */
  10717. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10718. ExpectNull(pkey);
  10719. ExpectNull(cert);
  10720. /* check parse with not extra certs kept */
  10721. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10722. WOLFSSL_SUCCESS);
  10723. ExpectNotNull(pkey);
  10724. ExpectNotNull(cert);
  10725. wolfSSL_EVP_PKEY_free(pkey);
  10726. pkey = NULL;
  10727. wolfSSL_X509_free(cert);
  10728. cert = NULL;
  10729. /* check parse with extra certs kept */
  10730. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10731. WOLFSSL_SUCCESS);
  10732. ExpectNotNull(pkey);
  10733. ExpectNotNull(cert);
  10734. ExpectNotNull(ca);
  10735. wolfSSL_EVP_PKEY_free(pkey);
  10736. wolfSSL_X509_free(cert);
  10737. sk_X509_pop_free(ca, NULL);
  10738. BIO_free(bio);
  10739. bio = NULL;
  10740. PKCS12_free(pkcs12);
  10741. pkcs12 = NULL;
  10742. #endif /* WC_RC2 */
  10743. /* Test i2d_PKCS12_bio */
  10744. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10745. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10746. if (f != XBADFILE)
  10747. XFCLOSE(f);
  10748. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10749. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10750. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10751. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10752. PKCS12_free(pkcs12);
  10753. BIO_free(bio);
  10754. (void)order;
  10755. #endif /* OPENSSL_EXTRA */
  10756. #endif /* HAVE_FIPS */
  10757. return EXPECT_RESULT();
  10758. }
  10759. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10760. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10761. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10762. #define TEST_PKCS8_ENC
  10763. #endif
  10764. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10765. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10766. /* used to keep track if FailTestCallback was called */
  10767. static int failTestCallbackCalled = 0;
  10768. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10769. {
  10770. (void)passwd;
  10771. (void)sz;
  10772. (void)rw;
  10773. (void)userdata;
  10774. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10775. failTestCallbackCalled = 1;
  10776. return -1;
  10777. }
  10778. #endif
  10779. static int test_wolfSSL_no_password_cb(void)
  10780. {
  10781. EXPECT_DECLS;
  10782. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10783. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10784. WOLFSSL_CTX* ctx = NULL;
  10785. byte buff[FOURK_BUF];
  10786. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10787. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10788. XFILE f = XBADFILE;
  10789. int bytes = 0;
  10790. #ifndef NO_WOLFSSL_CLIENT
  10791. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10792. #else
  10793. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10794. #endif
  10795. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10796. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10797. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10798. if (f != XBADFILE) {
  10799. XFCLOSE(f);
  10800. f = XBADFILE;
  10801. }
  10802. ExpectIntLE(bytes, sizeof(buff));
  10803. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10804. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10805. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10806. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10807. if (f != XBADFILE)
  10808. XFCLOSE(f);
  10809. ExpectIntLE(bytes, sizeof(buff));
  10810. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10811. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10812. wolfSSL_CTX_free(ctx);
  10813. /* Password callback should not be called by default */
  10814. ExpectIntEQ(failTestCallbackCalled, 0);
  10815. #endif
  10816. return EXPECT_RESULT();
  10817. }
  10818. #ifdef TEST_PKCS8_ENC
  10819. /* for PKCS8 test case */
  10820. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10821. {
  10822. int flag = 0;
  10823. (void)rw;
  10824. if (userdata != NULL) {
  10825. flag = *((int*)userdata); /* user set data */
  10826. }
  10827. switch (flag) {
  10828. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10829. * can be anything the user wishes to be passed to the callback
  10830. * associated with the WOLFSSL_CTX */
  10831. XSTRNCPY(passwd, "yassl123", sz);
  10832. return 8;
  10833. default:
  10834. return BAD_FUNC_ARG;
  10835. }
  10836. }
  10837. #endif /* TEST_PKCS8_ENC */
  10838. /* Testing functions dealing with PKCS8 */
  10839. static int test_wolfSSL_PKCS8(void)
  10840. {
  10841. EXPECT_DECLS;
  10842. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10843. !defined(WOLFCRYPT_ONLY)
  10844. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10845. byte buff[FOURK_BUF];
  10846. byte der[FOURK_BUF];
  10847. #ifndef NO_RSA
  10848. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10849. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10850. #endif
  10851. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10852. #ifdef HAVE_ECC
  10853. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10854. #endif
  10855. XFILE f = XBADFILE;
  10856. int bytes = 0;
  10857. WOLFSSL_CTX* ctx = NULL;
  10858. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10859. int ret;
  10860. ecc_key key;
  10861. word32 x = 0;
  10862. #endif
  10863. #ifdef TEST_PKCS8_ENC
  10864. #if !defined(NO_RSA) && !defined(NO_SHA)
  10865. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10866. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10867. #endif
  10868. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10869. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10870. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10871. #endif
  10872. int flag;
  10873. #endif
  10874. (void)der;
  10875. #ifndef NO_WOLFSSL_CLIENT
  10876. #ifndef WOLFSSL_NO_TLS12
  10877. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10878. #else
  10879. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10880. #endif
  10881. #else
  10882. #ifndef WOLFSSL_NO_TLS12
  10883. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10884. #else
  10885. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10886. #endif
  10887. #endif
  10888. #ifdef TEST_PKCS8_ENC
  10889. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10890. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10891. flag = 1; /* used by password callback as return code */
  10892. #if !defined(NO_RSA) && !defined(NO_SHA)
  10893. /* test loading PEM PKCS8 encrypted file */
  10894. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10895. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10896. if (f != XBADFILE) {
  10897. XFCLOSE(f);
  10898. f = XBADFILE;
  10899. }
  10900. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10901. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10902. /* this next case should fail because of password callback return code */
  10903. flag = 0; /* used by password callback as return code */
  10904. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10905. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10906. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10907. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10908. "yassl123"), 0);
  10909. /* test that error value is returned with a bad password */
  10910. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10911. "bad"), 0);
  10912. /* test loading PEM PKCS8 encrypted file */
  10913. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10914. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10915. if (f != XBADFILE) {
  10916. XFCLOSE(f);
  10917. f = XBADFILE;
  10918. }
  10919. flag = 1; /* used by password callback as return code */
  10920. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10921. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10922. /* this next case should fail because of password callback return code */
  10923. flag = 0; /* used by password callback as return code */
  10924. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10925. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10926. #endif /* !NO_RSA && !NO_SHA */
  10927. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10928. /* test loading PEM PKCS8 encrypted ECC Key file */
  10929. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10930. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10931. if (f != XBADFILE) {
  10932. XFCLOSE(f);
  10933. f = XBADFILE;
  10934. }
  10935. flag = 1; /* used by password callback as return code */
  10936. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10937. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10938. /* this next case should fail because of password callback return code */
  10939. flag = 0; /* used by password callback as return code */
  10940. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10941. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10942. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10943. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10944. "yassl123"), 0);
  10945. /* test that error value is returned with a bad password */
  10946. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10947. "bad"), 0);
  10948. /* test loading DER PKCS8 encrypted ECC Key file */
  10949. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10950. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10951. if (f != XBADFILE) {
  10952. XFCLOSE(f);
  10953. f = XBADFILE;
  10954. }
  10955. flag = 1; /* used by password callback as return code */
  10956. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10957. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10958. /* this next case should fail because of password callback return code */
  10959. flag = 0; /* used by password callback as return code */
  10960. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10961. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10962. /* leave flag as "okay" */
  10963. flag = 1;
  10964. #endif /* HAVE_ECC && !NO_SHA */
  10965. #endif /* TEST_PKCS8_ENC */
  10966. #ifndef NO_RSA
  10967. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10968. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10969. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10970. if (f != XBADFILE) {
  10971. XFCLOSE(f);
  10972. f = XBADFILE;
  10973. }
  10974. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10975. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10976. /* test loading PEM PKCS8 private key file (not encrypted) */
  10977. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10978. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10979. if (f != XBADFILE) {
  10980. XFCLOSE(f);
  10981. f = XBADFILE;
  10982. }
  10983. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10984. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10985. #endif /* !NO_RSA */
  10986. /* Test PKCS8 PEM ECC key no crypt */
  10987. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10988. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10989. if (f != XBADFILE) {
  10990. XFCLOSE(f);
  10991. f = XBADFILE;
  10992. }
  10993. #ifdef HAVE_ECC
  10994. /* Test PKCS8 PEM ECC key no crypt */
  10995. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10996. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10997. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10998. /* decrypt PKCS8 PEM to key in DER format */
  10999. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11000. (word32)sizeof(der), NULL)), 0);
  11001. ret = wc_ecc_init(&key);
  11002. if (ret == 0) {
  11003. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  11004. wc_ecc_free(&key);
  11005. }
  11006. ExpectIntEQ(ret, 0);
  11007. #endif
  11008. /* Test PKCS8 DER ECC key no crypt */
  11009. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11010. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11011. if (f != XBADFILE)
  11012. XFCLOSE(f);
  11013. /* Test using a PKCS8 ECC PEM */
  11014. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11015. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11016. #else
  11017. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11018. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11019. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11020. #endif /* HAVE_ECC */
  11021. wolfSSL_CTX_free(ctx);
  11022. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11023. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11024. return EXPECT_RESULT();
  11025. }
  11026. static int test_wolfSSL_PKCS8_ED25519(void)
  11027. {
  11028. EXPECT_DECLS;
  11029. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11030. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11031. defined(HAVE_ED25519_KEY_IMPORT)
  11032. const byte encPrivKey[] = \
  11033. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11034. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11035. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11036. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11037. "2L9W4v7swXkV+X+a1ww=\n"
  11038. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11039. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11040. byte der[FOURK_BUF];
  11041. WOLFSSL_CTX* ctx = NULL;
  11042. int bytes;
  11043. XMEMSET(der, 0, sizeof(der));
  11044. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11045. (word32)sizeof(der), password)), 0);
  11046. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11047. #ifndef NO_WOLFSSL_SERVER
  11048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11049. #else
  11050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11051. #endif
  11052. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11053. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11054. wolfSSL_CTX_free(ctx);
  11055. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11056. #endif
  11057. return EXPECT_RESULT();
  11058. }
  11059. static int test_wolfSSL_PKCS8_ED448(void)
  11060. {
  11061. EXPECT_DECLS;
  11062. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11063. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11064. defined(HAVE_ED448_KEY_IMPORT)
  11065. const byte encPrivKey[] = \
  11066. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11067. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11068. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11069. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11070. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11071. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11072. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11073. byte der[FOURK_BUF];
  11074. WOLFSSL_CTX* ctx = NULL;
  11075. int bytes;
  11076. XMEMSET(der, 0, sizeof(der));
  11077. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11078. (word32)sizeof(der), password)), 0);
  11079. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11080. #ifndef NO_WOLFSSL_SERVER
  11081. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11082. #else
  11083. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11084. #endif
  11085. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11086. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11087. wolfSSL_CTX_free(ctx);
  11088. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11089. #endif
  11090. return EXPECT_RESULT();
  11091. }
  11092. /* Testing functions dealing with PKCS5 */
  11093. static int test_wolfSSL_PKCS5(void)
  11094. {
  11095. EXPECT_DECLS;
  11096. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11097. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11098. const char* passwd = "myfipsPa$$W0rd";
  11099. #else
  11100. const char *passwd = "pass1234";
  11101. #endif
  11102. const unsigned char *salt = (unsigned char *)"salt1234";
  11103. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11104. DYNAMIC_TYPE_TMP_BUFFER);
  11105. int ret = 0;
  11106. ExpectNotNull(out);
  11107. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11108. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11109. WOLFSSL_SUCCESS);
  11110. #ifdef WOLFSSL_SHA512
  11111. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11112. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11113. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11114. #endif
  11115. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11116. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11117. return EXPECT_RESULT();
  11118. }
  11119. /* test parsing URI from certificate */
  11120. static int test_wolfSSL_URI(void)
  11121. {
  11122. EXPECT_DECLS;
  11123. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11124. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11125. defined(OPENSSL_EXTRA))
  11126. WOLFSSL_X509* x509 = NULL;
  11127. const char uri[] = "./certs/client-uri-cert.pem";
  11128. const char urn[] = "./certs/client-absolute-urn.pem";
  11129. const char badUri[] = "./certs/client-relative-uri.pem";
  11130. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11131. WOLFSSL_FILETYPE_PEM));
  11132. wolfSSL_FreeX509(x509);
  11133. x509 = NULL;
  11134. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11135. WOLFSSL_FILETYPE_PEM));
  11136. wolfSSL_FreeX509(x509);
  11137. x509 = NULL;
  11138. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11139. && !defined(WOLFSSL_FPKI)
  11140. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11141. WOLFSSL_FILETYPE_PEM));
  11142. #else
  11143. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11144. WOLFSSL_FILETYPE_PEM));
  11145. #endif
  11146. wolfSSL_FreeX509(x509);
  11147. #endif
  11148. return EXPECT_RESULT();
  11149. }
  11150. static int test_wolfSSL_TBS(void)
  11151. {
  11152. EXPECT_DECLS;
  11153. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11154. && defined(OPENSSL_EXTRA)
  11155. WOLFSSL_X509* x509 = NULL;
  11156. const unsigned char* tbs;
  11157. int tbsSz;
  11158. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11159. WOLFSSL_FILETYPE_PEM));
  11160. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11161. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11162. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11163. ExpectIntEQ(tbsSz, 1003);
  11164. wolfSSL_FreeX509(x509);
  11165. #endif
  11166. return EXPECT_RESULT();
  11167. }
  11168. static int test_wolfSSL_X509_verify(void)
  11169. {
  11170. EXPECT_DECLS;
  11171. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11172. defined(OPENSSL_EXTRA)
  11173. WOLFSSL_X509* ca = NULL;
  11174. WOLFSSL_X509* serv = NULL;
  11175. WOLFSSL_EVP_PKEY* pkey = NULL;
  11176. unsigned char buf[2048];
  11177. const unsigned char* pt = NULL;
  11178. int bufSz;
  11179. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11180. WOLFSSL_FILETYPE_PEM));
  11181. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11182. WOLFSSL_SUCCESS);
  11183. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11184. WOLFSSL_SUCCESS);
  11185. ExpectIntEQ(bufSz, 294);
  11186. bufSz = 2048;
  11187. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11188. WOLFSSL_SUCCESS);
  11189. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11190. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11191. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11192. WOLFSSL_FILETYPE_PEM));
  11193. /* success case */
  11194. pt = buf;
  11195. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11196. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11197. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11198. wolfSSL_EVP_PKEY_free(pkey);
  11199. pkey = NULL;
  11200. /* fail case */
  11201. bufSz = 2048;
  11202. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11203. WOLFSSL_SUCCESS);
  11204. pt = buf;
  11205. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11206. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11207. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11208. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11209. wolfSSL_EVP_PKEY_free(pkey);
  11210. wolfSSL_FreeX509(ca);
  11211. wolfSSL_FreeX509(serv);
  11212. #endif
  11213. return EXPECT_RESULT();
  11214. }
  11215. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11216. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11217. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11218. /* create certificate with version 2 */
  11219. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11220. {
  11221. EXPECT_DECLS;
  11222. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11223. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11224. unsigned char *der = NULL, *key = NULL, *pt;
  11225. char *header = NULL, *name = NULL;
  11226. int derSz;
  11227. long keySz;
  11228. XFILE fp = XBADFILE;
  11229. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11230. time_t t;
  11231. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11232. WOLFSSL_FILETYPE_PEM));
  11233. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11234. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11235. WOLFSSL_SUCCESS);
  11236. if (fp != XBADFILE)
  11237. XFCLOSE(fp);
  11238. pt = key;
  11239. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11240. (const unsigned char**)&pt, keySz));
  11241. /* create the version 2 certificate */
  11242. ExpectNotNull(x509v2 = X509_new());
  11243. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11244. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11245. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11246. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11247. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11248. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11249. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11250. t = time(NULL);
  11251. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11252. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11253. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11254. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11255. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11256. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11257. ExpectIntGT(derSz, 0);
  11258. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11259. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11260. /* TODO: Replace with API call */
  11261. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11262. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11263. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11264. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11265. wolfSSL_X509_free(x509);
  11266. wolfSSL_X509_free(x509v2);
  11267. wolfSSL_EVP_PKEY_free(priv);
  11268. wolfSSL_EVP_PKEY_free(pub);
  11269. wolfSSL_ASN1_TIME_free(notBefore);
  11270. wolfSSL_ASN1_TIME_free(notAfter);
  11271. return EXPECT_RESULT();
  11272. }
  11273. /* override certificate version error */
  11274. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11275. {
  11276. EXPECT_DECLS;
  11277. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11278. ExpectIntEQ(store->error, ASN_VERSION_E);
  11279. #else
  11280. ExpectIntEQ(store->error, 0);
  11281. #endif
  11282. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11283. (void)preverify;
  11284. return EXPECT_RESULT() == TEST_SUCCESS;
  11285. }
  11286. /* set verify callback that will override bad certificate version */
  11287. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11288. {
  11289. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11290. return TEST_SUCCESS;
  11291. }
  11292. #endif
  11293. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11294. {
  11295. EXPECT_DECLS;
  11296. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11297. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11298. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11299. test_ssl_cbf func_cb_client;
  11300. test_ssl_cbf func_cb_server;
  11301. /* test server rejects a client certificate that is not version 3 */
  11302. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11303. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11304. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11305. #ifndef WOLFSSL_NO_TLS12
  11306. func_cb_client.method = wolfTLSv1_2_client_method;
  11307. #else
  11308. func_cb_client.method = wolfTLSv1_3_client_method;
  11309. #endif
  11310. #ifndef WOLFSSL_NO_TLS12
  11311. func_cb_server.method = wolfTLSv1_2_server_method;
  11312. #else
  11313. func_cb_server.method = wolfTLSv1_3_server_method;
  11314. #endif
  11315. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11316. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11317. &func_cb_server, NULL), TEST_FAIL);
  11318. #else
  11319. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11320. &func_cb_server, NULL), TEST_SUCCESS);
  11321. #endif
  11322. #endif
  11323. return EXPECT_RESULT();
  11324. }
  11325. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11326. {
  11327. EXPECT_DECLS;
  11328. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11329. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11330. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11331. test_ssl_cbf func_cb_client;
  11332. test_ssl_cbf func_cb_server;
  11333. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11334. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11335. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11336. func_cb_server.ctx_ready = &test_set_override_x509;
  11337. #ifndef WOLFSSL_NO_TLS12
  11338. func_cb_client.method = wolfTLSv1_2_client_method;
  11339. #else
  11340. func_cb_client.method = wolfTLSv1_3_client_method;
  11341. #endif
  11342. #ifndef WOLFSSL_NO_TLS12
  11343. func_cb_server.method = wolfTLSv1_2_server_method;
  11344. #else
  11345. func_cb_server.method = wolfTLSv1_3_server_method;
  11346. #endif
  11347. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11348. &func_cb_server, NULL), TEST_SUCCESS);
  11349. #endif
  11350. return EXPECT_RESULT();
  11351. }
  11352. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11353. * version allowed.
  11354. * POST: 1 on success.
  11355. */
  11356. static int test_wolfSSL_CTX_SetMinVersion(void)
  11357. {
  11358. int res = TEST_SKIPPED;
  11359. #ifndef NO_WOLFSSL_CLIENT
  11360. int failFlag = WOLFSSL_SUCCESS;
  11361. WOLFSSL_CTX* ctx;
  11362. int itr;
  11363. #ifndef NO_OLD_TLS
  11364. const int versions[] = {
  11365. #ifdef WOLFSSL_ALLOW_TLSV10
  11366. WOLFSSL_TLSV1,
  11367. #endif
  11368. WOLFSSL_TLSV1_1,
  11369. WOLFSSL_TLSV1_2 };
  11370. #elif !defined(WOLFSSL_NO_TLS12)
  11371. const int versions[] = { WOLFSSL_TLSV1_2 };
  11372. #elif defined(WOLFSSL_TLS13)
  11373. const int versions[] = { WOLFSSL_TLSV1_3 };
  11374. #else
  11375. const int versions[0];
  11376. #endif
  11377. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11378. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11379. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11380. != WOLFSSL_SUCCESS) {
  11381. failFlag = WOLFSSL_FAILURE;
  11382. }
  11383. }
  11384. wolfSSL_CTX_free(ctx);
  11385. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11386. #endif
  11387. return res;
  11388. } /* END test_wolfSSL_CTX_SetMinVersion */
  11389. /*----------------------------------------------------------------------------*
  11390. | OCSP Stapling
  11391. *----------------------------------------------------------------------------*/
  11392. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11393. * need to contact the CA, lowering the cost of cert revocation checking.
  11394. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11395. * POST: 1 returned for success.
  11396. */
  11397. static int test_wolfSSL_UseOCSPStapling(void)
  11398. {
  11399. EXPECT_DECLS;
  11400. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11401. !defined(NO_WOLFSSL_CLIENT)
  11402. WOLFSSL_CTX* ctx = NULL;
  11403. WOLFSSL* ssl = NULL;
  11404. #ifndef NO_WOLFSSL_CLIENT
  11405. #ifndef WOLFSSL_NO_TLS12
  11406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11407. #else
  11408. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11409. #endif
  11410. #else
  11411. #ifndef WOLFSSL_NO_TLS12
  11412. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11413. #else
  11414. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11415. #endif
  11416. #endif
  11417. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11418. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11419. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11420. #ifndef NO_WOLFSSL_CLIENT
  11421. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11422. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11423. #else
  11424. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11425. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11426. #endif
  11427. wolfSSL_free(ssl);
  11428. wolfSSL_CTX_free(ctx);
  11429. #endif
  11430. return EXPECT_RESULT();
  11431. } /* END test_wolfSSL_UseOCSPStapling */
  11432. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11433. * stapling eliminates the need to contact the CA and lowers cert revocation
  11434. * check.
  11435. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11436. */
  11437. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11438. {
  11439. EXPECT_DECLS;
  11440. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11441. !defined(NO_WOLFSSL_CLIENT)
  11442. WOLFSSL_CTX* ctx = NULL;
  11443. WOLFSSL* ssl = NULL;
  11444. #ifndef NO_WOLFSSL_CLIENT
  11445. #ifndef WOLFSSL_NO_TLS12
  11446. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11447. #else
  11448. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11449. #endif
  11450. #else
  11451. #ifndef WOLFSSL_NO_TLS12
  11452. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11453. #else
  11454. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11455. #endif
  11456. #endif
  11457. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11458. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11459. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11460. #ifndef NO_WOLFSSL_CLIENT
  11461. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11462. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11463. #else
  11464. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11465. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11466. #endif
  11467. wolfSSL_free(ssl);
  11468. wolfSSL_CTX_free(ctx);
  11469. #endif
  11470. return EXPECT_RESULT();
  11471. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11472. /*----------------------------------------------------------------------------*
  11473. | Multicast Tests
  11474. *----------------------------------------------------------------------------*/
  11475. static int test_wolfSSL_mcast(void)
  11476. {
  11477. EXPECT_DECLS;
  11478. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11479. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11480. WOLFSSL_CTX* ctx = NULL;
  11481. WOLFSSL* ssl = NULL;
  11482. byte preMasterSecret[512];
  11483. byte clientRandom[32];
  11484. byte serverRandom[32];
  11485. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11486. byte buf[256];
  11487. word16 newId;
  11488. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11489. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11490. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11491. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11492. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11493. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11494. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11495. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11496. WOLFSSL_SUCCESS);
  11497. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11498. ExpectIntLE(newId, 100);
  11499. wolfSSL_free(ssl);
  11500. wolfSSL_CTX_free(ctx);
  11501. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11502. * WOLFSSL_SNIFFER) */
  11503. return EXPECT_RESULT();
  11504. }
  11505. /*----------------------------------------------------------------------------*
  11506. | Wolfcrypt
  11507. *----------------------------------------------------------------------------*/
  11508. /*
  11509. * Unit test for the wc_InitBlake2b()
  11510. */
  11511. static int test_wc_InitBlake2b(void)
  11512. {
  11513. EXPECT_DECLS;
  11514. #ifdef HAVE_BLAKE2
  11515. Blake2b blake;
  11516. /* Test good arg. */
  11517. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11518. /* Test bad arg. */
  11519. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11520. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11521. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11522. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11523. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11524. #endif
  11525. return EXPECT_RESULT();
  11526. } /* END test_wc_InitBlake2b*/
  11527. /*
  11528. * Unit test for the wc_InitBlake2b_WithKey()
  11529. */
  11530. static int test_wc_InitBlake2b_WithKey(void)
  11531. {
  11532. EXPECT_DECLS;
  11533. #ifdef HAVE_BLAKE2
  11534. Blake2b blake;
  11535. word32 digestSz = BLAKE2B_KEYBYTES;
  11536. byte key[BLAKE2B_KEYBYTES];
  11537. word32 keylen = BLAKE2B_KEYBYTES;
  11538. XMEMSET(key, 0, sizeof(key));
  11539. /* Test good arg. */
  11540. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11541. /* Test bad args. */
  11542. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11543. BAD_FUNC_ARG);
  11544. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11545. BAD_FUNC_ARG);
  11546. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11547. #endif
  11548. return EXPECT_RESULT();
  11549. } /* END wc_InitBlake2b_WithKey*/
  11550. /*
  11551. * Unit test for the wc_InitBlake2s_WithKey()
  11552. */
  11553. static int test_wc_InitBlake2s_WithKey(void)
  11554. {
  11555. EXPECT_DECLS;
  11556. #ifdef HAVE_BLAKE2S
  11557. Blake2s blake;
  11558. word32 digestSz = BLAKE2S_KEYBYTES;
  11559. byte *key = (byte*)"01234567890123456789012345678901";
  11560. word32 keylen = BLAKE2S_KEYBYTES;
  11561. /* Test good arg. */
  11562. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11563. /* Test bad args. */
  11564. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11565. BAD_FUNC_ARG);
  11566. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11567. BAD_FUNC_ARG);
  11568. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11569. #endif
  11570. return EXPECT_RESULT();
  11571. } /* END wc_InitBlake2s_WithKey*/
  11572. /*
  11573. * Unit test for the wc_InitMd5()
  11574. */
  11575. static int test_wc_InitMd5(void)
  11576. {
  11577. EXPECT_DECLS;
  11578. #ifndef NO_MD5
  11579. wc_Md5 md5;
  11580. /* Test good arg. */
  11581. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11582. /* Test bad arg. */
  11583. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11584. wc_Md5Free(&md5);
  11585. #endif
  11586. return EXPECT_RESULT();
  11587. } /* END test_wc_InitMd5 */
  11588. /*
  11589. * Testing wc_UpdateMd5()
  11590. */
  11591. static int test_wc_Md5Update(void)
  11592. {
  11593. EXPECT_DECLS;
  11594. #ifndef NO_MD5
  11595. wc_Md5 md5;
  11596. byte hash[WC_MD5_DIGEST_SIZE];
  11597. testVector a, b, c;
  11598. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11599. /* Input */
  11600. a.input = "a";
  11601. a.inLen = XSTRLEN(a.input);
  11602. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11603. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11604. /* Update input. */
  11605. a.input = "abc";
  11606. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11607. "\x72";
  11608. a.inLen = XSTRLEN(a.input);
  11609. a.outLen = XSTRLEN(a.output);
  11610. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11611. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11612. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11613. /* Pass in bad values. */
  11614. b.input = NULL;
  11615. b.inLen = 0;
  11616. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11617. c.input = NULL;
  11618. c.inLen = WC_MD5_DIGEST_SIZE;
  11619. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11620. BAD_FUNC_ARG);
  11621. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11622. BAD_FUNC_ARG);
  11623. wc_Md5Free(&md5);
  11624. #endif
  11625. return EXPECT_RESULT();
  11626. } /* END test_wc_Md5Update() */
  11627. /*
  11628. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11629. */
  11630. static int test_wc_Md5Final(void)
  11631. {
  11632. EXPECT_DECLS;
  11633. #ifndef NO_MD5
  11634. /* Instantiate */
  11635. wc_Md5 md5;
  11636. byte* hash_test[3];
  11637. byte hash1[WC_MD5_DIGEST_SIZE];
  11638. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11639. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11640. int times, i;
  11641. /* Initialize */
  11642. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11643. hash_test[0] = hash1;
  11644. hash_test[1] = hash2;
  11645. hash_test[2] = hash3;
  11646. times = sizeof(hash_test)/sizeof(byte*);
  11647. for (i = 0; i < times; i++) {
  11648. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11649. }
  11650. /* Test bad args. */
  11651. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11652. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11653. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11654. wc_Md5Free(&md5);
  11655. #endif
  11656. return EXPECT_RESULT();
  11657. }
  11658. /*
  11659. * Unit test for the wc_InitSha()
  11660. */
  11661. static int test_wc_InitSha(void)
  11662. {
  11663. EXPECT_DECLS;
  11664. #ifndef NO_SHA
  11665. wc_Sha sha;
  11666. /* Test good arg. */
  11667. ExpectIntEQ(wc_InitSha(&sha), 0);
  11668. /* Test bad arg. */
  11669. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11670. wc_ShaFree(&sha);
  11671. #endif
  11672. return EXPECT_RESULT();
  11673. } /* END test_wc_InitSha */
  11674. /*
  11675. * Tesing wc_ShaUpdate()
  11676. */
  11677. static int test_wc_ShaUpdate(void)
  11678. {
  11679. EXPECT_DECLS;
  11680. #ifndef NO_SHA
  11681. wc_Sha sha;
  11682. byte hash[WC_SHA_DIGEST_SIZE];
  11683. testVector a, b, c;
  11684. ExpectIntEQ(wc_InitSha(&sha), 0);
  11685. /* Input. */
  11686. a.input = "a";
  11687. a.inLen = XSTRLEN(a.input);
  11688. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11689. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11690. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11691. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11692. /* Update input. */
  11693. a.input = "abc";
  11694. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11695. "\x6C\x9C\xD0\xD8\x9D";
  11696. a.inLen = XSTRLEN(a.input);
  11697. a.outLen = XSTRLEN(a.output);
  11698. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11699. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11700. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11701. /* Try passing in bad values. */
  11702. b.input = NULL;
  11703. b.inLen = 0;
  11704. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11705. c.input = NULL;
  11706. c.inLen = WC_SHA_DIGEST_SIZE;
  11707. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11708. BAD_FUNC_ARG);
  11709. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11710. BAD_FUNC_ARG);
  11711. wc_ShaFree(&sha);
  11712. #endif
  11713. return EXPECT_RESULT();
  11714. } /* END test_wc_ShaUpdate() */
  11715. /*
  11716. * Unit test on wc_ShaFinal
  11717. */
  11718. static int test_wc_ShaFinal(void)
  11719. {
  11720. EXPECT_DECLS;
  11721. #ifndef NO_SHA
  11722. wc_Sha sha;
  11723. byte* hash_test[3];
  11724. byte hash1[WC_SHA_DIGEST_SIZE];
  11725. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11726. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11727. int times, i;
  11728. /* Initialize*/
  11729. ExpectIntEQ(wc_InitSha(&sha), 0);
  11730. hash_test[0] = hash1;
  11731. hash_test[1] = hash2;
  11732. hash_test[2] = hash3;
  11733. times = sizeof(hash_test)/sizeof(byte*);
  11734. for (i = 0; i < times; i++) {
  11735. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11736. }
  11737. /* Test bad args. */
  11738. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11739. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11740. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11741. wc_ShaFree(&sha);
  11742. #endif
  11743. return EXPECT_RESULT();
  11744. } /* END test_wc_ShaFinal */
  11745. /*
  11746. * Unit test for wc_InitSha256()
  11747. */
  11748. static int test_wc_InitSha256(void)
  11749. {
  11750. EXPECT_DECLS;
  11751. #ifndef NO_SHA256
  11752. wc_Sha256 sha256;
  11753. /* Test good arg. */
  11754. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11755. /* Test bad arg. */
  11756. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11757. wc_Sha256Free(&sha256);
  11758. #endif
  11759. return EXPECT_RESULT();
  11760. } /* END test_wc_InitSha256 */
  11761. /*
  11762. * Unit test for wc_Sha256Update()
  11763. */
  11764. static int test_wc_Sha256Update(void)
  11765. {
  11766. EXPECT_DECLS;
  11767. #ifndef NO_SHA256
  11768. wc_Sha256 sha256;
  11769. byte hash[WC_SHA256_DIGEST_SIZE];
  11770. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11771. testVector a, b, c;
  11772. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11773. /* Input. */
  11774. a.input = "a";
  11775. a.inLen = XSTRLEN(a.input);
  11776. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11777. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11778. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11779. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11780. /* Update input. */
  11781. a.input = "abc";
  11782. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11783. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11784. "\x15\xAD";
  11785. a.inLen = XSTRLEN(a.input);
  11786. a.outLen = XSTRLEN(a.output);
  11787. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11788. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11789. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11790. /* Unaligned check. */
  11791. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11792. 0);
  11793. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11794. /* Try passing in bad values */
  11795. b.input = NULL;
  11796. b.inLen = 0;
  11797. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11798. c.input = NULL;
  11799. c.inLen = WC_SHA256_DIGEST_SIZE;
  11800. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11801. BAD_FUNC_ARG);
  11802. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11803. BAD_FUNC_ARG);
  11804. wc_Sha256Free(&sha256);
  11805. #endif
  11806. return EXPECT_RESULT();
  11807. } /* END test_wc_Sha256Update */
  11808. /*
  11809. * Unit test function for wc_Sha256Final()
  11810. */
  11811. static int test_wc_Sha256Final(void)
  11812. {
  11813. EXPECT_DECLS;
  11814. #ifndef NO_SHA256
  11815. wc_Sha256 sha256;
  11816. byte* hash_test[3];
  11817. byte hash1[WC_SHA256_DIGEST_SIZE];
  11818. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11819. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11820. int times, i;
  11821. /* Initialize */
  11822. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11823. hash_test[0] = hash1;
  11824. hash_test[1] = hash2;
  11825. hash_test[2] = hash3;
  11826. times = sizeof(hash_test) / sizeof(byte*);
  11827. for (i = 0; i < times; i++) {
  11828. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11829. }
  11830. /* Test bad args. */
  11831. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11832. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11833. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11834. wc_Sha256Free(&sha256);
  11835. #endif
  11836. return EXPECT_RESULT();
  11837. } /* END test_wc_Sha256Final */
  11838. /*
  11839. * Unit test function for wc_Sha256FinalRaw()
  11840. */
  11841. static int test_wc_Sha256FinalRaw(void)
  11842. {
  11843. EXPECT_DECLS;
  11844. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11845. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11846. !defined(WOLFSSL_NO_HASH_RAW)
  11847. wc_Sha256 sha256;
  11848. byte* hash_test[3];
  11849. byte hash1[WC_SHA256_DIGEST_SIZE];
  11850. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11851. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11852. int times, i;
  11853. /* Initialize */
  11854. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11855. hash_test[0] = hash1;
  11856. hash_test[1] = hash2;
  11857. hash_test[2] = hash3;
  11858. times = sizeof(hash_test) / sizeof(byte*);
  11859. for (i = 0; i < times; i++) {
  11860. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11861. }
  11862. /* Test bad args. */
  11863. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11864. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11865. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11866. wc_Sha256Free(&sha256);
  11867. #endif
  11868. return EXPECT_RESULT();
  11869. } /* END test_wc_Sha256FinalRaw */
  11870. /*
  11871. * Unit test function for wc_Sha256GetFlags()
  11872. */
  11873. static int test_wc_Sha256GetFlags(void)
  11874. {
  11875. EXPECT_DECLS;
  11876. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11877. wc_Sha256 sha256;
  11878. word32 flags = 0;
  11879. /* Initialize */
  11880. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11881. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11882. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11883. wc_Sha256Free(&sha256);
  11884. #endif
  11885. return EXPECT_RESULT();
  11886. } /* END test_wc_Sha256GetFlags */
  11887. /*
  11888. * Unit test function for wc_Sha256Free()
  11889. */
  11890. static int test_wc_Sha256Free(void)
  11891. {
  11892. EXPECT_DECLS;
  11893. #ifndef NO_SHA256
  11894. wc_Sha256Free(NULL);
  11895. /* Set result to SUCCESS. */
  11896. ExpectTrue(1);
  11897. #endif
  11898. return EXPECT_RESULT();
  11899. } /* END test_wc_Sha256Free */
  11900. /*
  11901. * Unit test function for wc_Sha256GetHash()
  11902. */
  11903. static int test_wc_Sha256GetHash(void)
  11904. {
  11905. EXPECT_DECLS;
  11906. #ifndef NO_SHA256
  11907. wc_Sha256 sha256;
  11908. byte hash1[WC_SHA256_DIGEST_SIZE];
  11909. /* Initialize */
  11910. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11911. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11912. /* test bad arguments*/
  11913. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11914. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11915. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11916. wc_Sha256Free(&sha256);
  11917. #endif
  11918. return EXPECT_RESULT();
  11919. } /* END test_wc_Sha256GetHash */
  11920. /*
  11921. * Unit test function for wc_Sha256Copy()
  11922. */
  11923. static int test_wc_Sha256Copy(void)
  11924. {
  11925. EXPECT_DECLS;
  11926. #ifndef NO_SHA256
  11927. wc_Sha256 sha256;
  11928. wc_Sha256 temp;
  11929. XMEMSET(&sha256, 0, sizeof(sha256));
  11930. XMEMSET(&temp, 0, sizeof(temp));
  11931. /* Initialize */
  11932. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11933. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11934. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11935. /* test bad arguments*/
  11936. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11937. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11938. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11939. wc_Sha256Free(&sha256);
  11940. wc_Sha256Free(&temp);
  11941. #endif
  11942. return EXPECT_RESULT();
  11943. } /* END test_wc_Sha256Copy */
  11944. /*
  11945. * Testing wc_InitSha512()
  11946. */
  11947. static int test_wc_InitSha512(void)
  11948. {
  11949. EXPECT_DECLS;
  11950. #ifdef WOLFSSL_SHA512
  11951. wc_Sha512 sha512;
  11952. /* Test good arg. */
  11953. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11954. /* Test bad arg. */
  11955. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11956. wc_Sha512Free(&sha512);
  11957. #endif
  11958. return EXPECT_RESULT();
  11959. } /* END test_wc_InitSha512 */
  11960. /*
  11961. * wc_Sha512Update() test.
  11962. */
  11963. static int test_wc_Sha512Update(void)
  11964. {
  11965. EXPECT_DECLS;
  11966. #ifdef WOLFSSL_SHA512
  11967. wc_Sha512 sha512;
  11968. byte hash[WC_SHA512_DIGEST_SIZE];
  11969. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11970. testVector a, b, c;
  11971. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11972. /* Input. */
  11973. a.input = "a";
  11974. a.inLen = XSTRLEN(a.input);
  11975. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11976. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11977. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11978. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11979. /* Update input. */
  11980. a.input = "abc";
  11981. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11982. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11983. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11984. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11985. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11986. a.inLen = XSTRLEN(a.input);
  11987. a.outLen = XSTRLEN(a.output);
  11988. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11989. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11990. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11991. /* Unaligned check. */
  11992. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11993. 0);
  11994. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11995. /* Try passing in bad values */
  11996. b.input = NULL;
  11997. b.inLen = 0;
  11998. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11999. c.input = NULL;
  12000. c.inLen = WC_SHA512_DIGEST_SIZE;
  12001. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12002. BAD_FUNC_ARG);
  12003. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12004. BAD_FUNC_ARG);
  12005. wc_Sha512Free(&sha512);
  12006. #endif
  12007. return EXPECT_RESULT();
  12008. } /* END test_wc_Sha512Update */
  12009. #ifdef WOLFSSL_SHA512
  12010. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12011. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12012. /* Performs test for
  12013. * - wc_Sha512Final/wc_Sha512FinalRaw
  12014. * - wc_Sha512_224Final/wc_Sha512_224Final
  12015. * - wc_Sha512_256Final/wc_Sha512_256Final
  12016. * parameter:
  12017. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12018. * WC_HASH_TYPE_SHA512_256
  12019. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12020. *return 0 on success
  12021. */
  12022. static int test_Sha512_Family_Final(int type, int isRaw)
  12023. {
  12024. EXPECT_DECLS;
  12025. wc_Sha512 sha512;
  12026. byte* hash_test[3];
  12027. byte hash1[WC_SHA512_DIGEST_SIZE];
  12028. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12029. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12030. int times, i;
  12031. int(*initFp)(wc_Sha512*);
  12032. int(*finalFp)(wc_Sha512*, byte*);
  12033. void(*freeFp)(wc_Sha512*);
  12034. if (type == WC_HASH_TYPE_SHA512) {
  12035. initFp = wc_InitSha512;
  12036. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12037. !defined(WOLFSSL_NO_HASH_RAW)
  12038. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12039. #else
  12040. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12041. #endif
  12042. freeFp = wc_Sha512Free;
  12043. }
  12044. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12045. #if !defined(WOLFSSL_NOSHA512_224)
  12046. else if (type == WC_HASH_TYPE_SHA512_224) {
  12047. initFp = wc_InitSha512_224;
  12048. #if !defined(WOLFSSL_NO_HASH_RAW)
  12049. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12050. #else
  12051. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12052. #endif
  12053. freeFp = wc_Sha512_224Free;
  12054. }
  12055. #endif
  12056. #if !defined(WOLFSSL_NOSHA512_256)
  12057. else if (type == WC_HASH_TYPE_SHA512_256) {
  12058. initFp = wc_InitSha512_256;
  12059. #if !defined(WOLFSSL_NO_HASH_RAW)
  12060. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12061. #else
  12062. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12063. #endif
  12064. freeFp = wc_Sha512_256Free;
  12065. }
  12066. #endif
  12067. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12068. else
  12069. return TEST_FAIL;
  12070. /* Initialize */
  12071. ExpectIntEQ(initFp(&sha512), 0);
  12072. hash_test[0] = hash1;
  12073. hash_test[1] = hash2;
  12074. hash_test[2] = hash3;
  12075. times = sizeof(hash_test) / sizeof(byte *);
  12076. /* Good test args. */
  12077. for (i = 0; i < times; i++) {
  12078. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12079. }
  12080. /* Test bad args. */
  12081. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12082. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12083. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12084. freeFp(&sha512);
  12085. return EXPECT_RESULT();
  12086. }
  12087. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12088. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12089. #endif /* WOLFSSL_SHA512 */
  12090. /*
  12091. * Unit test function for wc_Sha512Final()
  12092. */
  12093. static int test_wc_Sha512Final(void)
  12094. {
  12095. EXPECT_DECLS;
  12096. #ifdef WOLFSSL_SHA512
  12097. wc_Sha512 sha512;
  12098. byte* hash_test[3];
  12099. byte hash1[WC_SHA512_DIGEST_SIZE];
  12100. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12101. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12102. int times, i;
  12103. /* Initialize */
  12104. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12105. hash_test[0] = hash1;
  12106. hash_test[1] = hash2;
  12107. hash_test[2] = hash3;
  12108. times = sizeof(hash_test) / sizeof(byte *);
  12109. for (i = 0; i < times; i++) {
  12110. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12111. }
  12112. /* Test bad args. */
  12113. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12114. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12115. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12116. wc_Sha512Free(&sha512);
  12117. #endif
  12118. return EXPECT_RESULT();
  12119. } /* END test_wc_Sha512Final */
  12120. /*
  12121. * Unit test function for wc_Sha512GetFlags()
  12122. */
  12123. static int test_wc_Sha512GetFlags(void)
  12124. {
  12125. EXPECT_DECLS;
  12126. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12127. wc_Sha512 sha512;
  12128. word32 flags = 0;
  12129. /* Initialize */
  12130. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12131. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12132. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12133. wc_Sha512Free(&sha512);
  12134. #endif
  12135. return EXPECT_RESULT();
  12136. } /* END test_wc_Sha512GetFlags */
  12137. /*
  12138. * Unit test function for wc_Sha512FinalRaw()
  12139. */
  12140. static int test_wc_Sha512FinalRaw(void)
  12141. {
  12142. EXPECT_DECLS;
  12143. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12144. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12145. !defined(WOLFSSL_NO_HASH_RAW)
  12146. wc_Sha512 sha512;
  12147. byte* hash_test[3];
  12148. byte hash1[WC_SHA512_DIGEST_SIZE];
  12149. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12150. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12151. int times, i;
  12152. /* Initialize */
  12153. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12154. hash_test[0] = hash1;
  12155. hash_test[1] = hash2;
  12156. hash_test[2] = hash3;
  12157. times = sizeof(hash_test) / sizeof(byte*);
  12158. /* Good test args. */
  12159. for (i = 0; i < times; i++) {
  12160. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12161. }
  12162. /* Test bad args. */
  12163. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12164. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12165. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12166. wc_Sha512Free(&sha512);
  12167. #endif
  12168. return EXPECT_RESULT();
  12169. } /* END test_wc_Sha512FinalRaw */
  12170. /*
  12171. * Unit test function for wc_Sha512Free()
  12172. */
  12173. static int test_wc_Sha512Free(void)
  12174. {
  12175. EXPECT_DECLS;
  12176. #ifdef WOLFSSL_SHA512
  12177. wc_Sha512Free(NULL);
  12178. /* Set result to SUCCESS. */
  12179. ExpectTrue(1);
  12180. #endif
  12181. return EXPECT_RESULT();
  12182. } /* END test_wc_Sha512Free */
  12183. #ifdef WOLFSSL_SHA512
  12184. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12185. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12186. static int test_Sha512_Family_GetHash(int type )
  12187. {
  12188. EXPECT_DECLS;
  12189. int(*initFp)(wc_Sha512*);
  12190. int(*ghashFp)(wc_Sha512*, byte*);
  12191. wc_Sha512 sha512;
  12192. byte hash1[WC_SHA512_DIGEST_SIZE];
  12193. if (type == WC_HASH_TYPE_SHA512) {
  12194. initFp = wc_InitSha512;
  12195. ghashFp = wc_Sha512GetHash;
  12196. }
  12197. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12198. #if !defined(WOLFSSL_NOSHA512_224)
  12199. else if (type == WC_HASH_TYPE_SHA512_224) {
  12200. initFp = wc_InitSha512_224;
  12201. ghashFp = wc_Sha512_224GetHash;
  12202. }
  12203. #endif
  12204. #if !defined(WOLFSSL_NOSHA512_256)
  12205. else if (type == WC_HASH_TYPE_SHA512_256) {
  12206. initFp = wc_InitSha512_256;
  12207. ghashFp = wc_Sha512_256GetHash;
  12208. }
  12209. #endif
  12210. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12211. else {
  12212. initFp = NULL;
  12213. ghashFp = NULL;
  12214. }
  12215. if (initFp == NULL || ghashFp == NULL)
  12216. return TEST_FAIL;
  12217. ExpectIntEQ(initFp(&sha512), 0);
  12218. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12219. /* test bad arguments*/
  12220. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12221. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12222. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12223. wc_Sha512Free(&sha512);
  12224. return EXPECT_RESULT();
  12225. }
  12226. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12227. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12228. #endif /* WOLFSSL_SHA512 */
  12229. /*
  12230. * Unit test function for wc_Sha512GetHash()
  12231. */
  12232. static int test_wc_Sha512GetHash(void)
  12233. {
  12234. EXPECT_DECLS;
  12235. #ifdef WOLFSSL_SHA512
  12236. wc_Sha512 sha512;
  12237. byte hash1[WC_SHA512_DIGEST_SIZE];
  12238. /* Initialize */
  12239. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12240. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12241. /* test bad arguments*/
  12242. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12243. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12244. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12245. wc_Sha512Free(&sha512);
  12246. #endif
  12247. return EXPECT_RESULT();
  12248. } /* END test_wc_Sha512GetHash */
  12249. /*
  12250. * Unit test function for wc_Sha512Copy()
  12251. */
  12252. static int test_wc_Sha512Copy(void)
  12253. {
  12254. EXPECT_DECLS;
  12255. #ifdef WOLFSSL_SHA512
  12256. wc_Sha512 sha512;
  12257. wc_Sha512 temp;
  12258. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12259. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12260. /* Initialize */
  12261. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12262. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12263. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12264. /* test bad arguments*/
  12265. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12266. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12267. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12268. wc_Sha512Free(&sha512);
  12269. wc_Sha512Free(&temp);
  12270. #endif
  12271. return EXPECT_RESULT();
  12272. } /* END test_wc_Sha512Copy */
  12273. static int test_wc_InitSha512_224(void)
  12274. {
  12275. EXPECT_DECLS;
  12276. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12277. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12278. wc_Sha512 sha512;
  12279. /* Test good arg. */
  12280. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12281. /* Test bad arg. */
  12282. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12283. wc_Sha512_224Free(&sha512);
  12284. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12285. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12286. return EXPECT_RESULT();
  12287. }
  12288. static int test_wc_Sha512_224Update(void)
  12289. {
  12290. EXPECT_DECLS;
  12291. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12292. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12293. wc_Sha512 sha512;
  12294. byte hash[WC_SHA512_DIGEST_SIZE];
  12295. testVector a, c;
  12296. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12297. /* Input. */
  12298. a.input = "a";
  12299. a.inLen = XSTRLEN(a.input);
  12300. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12301. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12302. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12303. 0);
  12304. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12305. /* Update input. */
  12306. a.input = "abc";
  12307. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12308. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12309. a.inLen = XSTRLEN(a.input);
  12310. a.outLen = XSTRLEN(a.output);
  12311. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12312. 0);
  12313. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12314. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12315. c.input = NULL;
  12316. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12317. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12318. BAD_FUNC_ARG);
  12319. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12320. BAD_FUNC_ARG);
  12321. wc_Sha512_224Free(&sha512);
  12322. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12323. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12324. return EXPECT_RESULT();
  12325. }
  12326. static int test_wc_Sha512_224Final(void)
  12327. {
  12328. EXPECT_DECLS;
  12329. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12330. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12331. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12332. TEST_SUCCESS);
  12333. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12334. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12335. return EXPECT_RESULT();
  12336. }
  12337. static int test_wc_Sha512_224GetFlags(void)
  12338. {
  12339. EXPECT_DECLS;
  12340. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12341. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12342. wc_Sha512 sha512;
  12343. wc_Sha512 copy;
  12344. word32 flags = 0;
  12345. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12346. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12347. /* Initialize */
  12348. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12349. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12350. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12351. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12352. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12353. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12354. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12355. wc_Sha512_224Free(&copy);
  12356. wc_Sha512_224Free(&sha512);
  12357. #endif
  12358. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12359. return EXPECT_RESULT();
  12360. }
  12361. static int test_wc_Sha512_224FinalRaw(void)
  12362. {
  12363. EXPECT_DECLS;
  12364. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12365. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12366. !defined(WOLFSSL_NO_HASH_RAW)
  12367. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12368. TEST_SUCCESS);
  12369. #endif
  12370. return EXPECT_RESULT();
  12371. }
  12372. static int test_wc_Sha512_224Free(void)
  12373. {
  12374. EXPECT_DECLS;
  12375. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12376. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12377. wc_Sha512_224Free(NULL);
  12378. /* Set result to SUCCESS. */
  12379. ExpectTrue(1);
  12380. #endif
  12381. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12382. return EXPECT_RESULT();
  12383. }
  12384. static int test_wc_Sha512_224GetHash(void)
  12385. {
  12386. EXPECT_DECLS;
  12387. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12388. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12389. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12390. TEST_SUCCESS);
  12391. #endif
  12392. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12393. return EXPECT_RESULT();
  12394. }
  12395. static int test_wc_Sha512_224Copy(void)
  12396. {
  12397. EXPECT_DECLS;
  12398. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12399. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12400. wc_Sha512 sha512;
  12401. wc_Sha512 temp;
  12402. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12403. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12404. /* Initialize */
  12405. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12406. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12407. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12408. /* test bad arguments*/
  12409. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12410. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12411. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12412. wc_Sha512_224Free(&sha512);
  12413. wc_Sha512_224Free(&temp);
  12414. #endif
  12415. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12416. return EXPECT_RESULT();
  12417. }
  12418. static int test_wc_InitSha512_256(void)
  12419. {
  12420. EXPECT_DECLS;
  12421. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12422. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12423. wc_Sha512 sha512;
  12424. /* Test good arg. */
  12425. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12426. /* Test bad arg. */
  12427. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12428. wc_Sha512_256Free(&sha512);
  12429. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12430. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12431. return EXPECT_RESULT();
  12432. }
  12433. static int test_wc_Sha512_256Update(void)
  12434. {
  12435. EXPECT_DECLS;
  12436. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12437. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12438. wc_Sha512 sha512;
  12439. byte hash[WC_SHA512_DIGEST_SIZE];
  12440. testVector a, c;
  12441. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12442. /* Input. */
  12443. a.input = "a";
  12444. a.inLen = XSTRLEN(a.input);
  12445. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12446. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12447. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12448. 0);
  12449. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12450. /* Update input. */
  12451. a.input = "abc";
  12452. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12453. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12454. "\x07\xe7\xaf\x23";
  12455. a.inLen = XSTRLEN(a.input);
  12456. a.outLen = XSTRLEN(a.output);
  12457. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12458. 0);
  12459. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12460. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12461. c.input = NULL;
  12462. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12463. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12464. BAD_FUNC_ARG);
  12465. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12466. BAD_FUNC_ARG);
  12467. wc_Sha512_256Free(&sha512);
  12468. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12469. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12470. return EXPECT_RESULT();
  12471. }
  12472. static int test_wc_Sha512_256Final(void)
  12473. {
  12474. EXPECT_DECLS;
  12475. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12476. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12477. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12478. TEST_SUCCESS);
  12479. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12480. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12481. return EXPECT_RESULT();
  12482. }
  12483. static int test_wc_Sha512_256GetFlags(void)
  12484. {
  12485. EXPECT_DECLS;
  12486. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12487. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12488. wc_Sha512 sha512, copy;
  12489. word32 flags = 0;
  12490. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12491. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12492. /* Initialize */
  12493. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12494. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12495. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12496. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12497. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12498. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12499. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12500. wc_Sha512_256Free(&sha512);
  12501. #endif
  12502. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12503. return EXPECT_RESULT();
  12504. }
  12505. static int test_wc_Sha512_256FinalRaw(void)
  12506. {
  12507. EXPECT_DECLS;
  12508. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12509. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12510. !defined(WOLFSSL_NO_HASH_RAW)
  12511. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12512. TEST_SUCCESS);
  12513. #endif
  12514. return EXPECT_RESULT();
  12515. }
  12516. static int test_wc_Sha512_256Free(void)
  12517. {
  12518. EXPECT_DECLS;
  12519. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12520. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12521. wc_Sha512_256Free(NULL);
  12522. /* Set result to SUCCESS. */
  12523. ExpectTrue(1);
  12524. #endif
  12525. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12526. return EXPECT_RESULT();
  12527. }
  12528. static int test_wc_Sha512_256GetHash(void)
  12529. {
  12530. EXPECT_DECLS;
  12531. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12532. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12533. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12534. TEST_SUCCESS);
  12535. #endif
  12536. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12537. return EXPECT_RESULT();
  12538. }
  12539. static int test_wc_Sha512_256Copy(void)
  12540. {
  12541. EXPECT_DECLS;
  12542. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12543. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12544. wc_Sha512 sha512;
  12545. wc_Sha512 temp;
  12546. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12547. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12548. /* Initialize */
  12549. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12550. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12551. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12552. /* test bad arguments*/
  12553. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12554. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12555. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12556. wc_Sha512_256Free(&sha512);
  12557. wc_Sha512_256Free(&temp);
  12558. #endif
  12559. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12560. return EXPECT_RESULT();
  12561. }
  12562. /*
  12563. * Testing wc_InitSha384()
  12564. */
  12565. static int test_wc_InitSha384(void)
  12566. {
  12567. EXPECT_DECLS;
  12568. #ifdef WOLFSSL_SHA384
  12569. wc_Sha384 sha384;
  12570. /* Test good arg. */
  12571. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12572. /* Test bad arg. */
  12573. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12574. wc_Sha384Free(&sha384);
  12575. #endif
  12576. return EXPECT_RESULT();
  12577. } /* END test_wc_InitSha384 */
  12578. /*
  12579. * test wc_Sha384Update()
  12580. */
  12581. static int test_wc_Sha384Update(void)
  12582. {
  12583. EXPECT_DECLS;
  12584. #ifdef WOLFSSL_SHA384
  12585. wc_Sha384 sha384;
  12586. byte hash[WC_SHA384_DIGEST_SIZE];
  12587. testVector a, b, c;
  12588. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12589. /* Input */
  12590. a.input = "a";
  12591. a.inLen = XSTRLEN(a.input);
  12592. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12593. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12594. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12595. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12596. /* Update input. */
  12597. a.input = "abc";
  12598. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12599. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12600. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12601. "\xc8\x25\xa7";
  12602. a.inLen = XSTRLEN(a.input);
  12603. a.outLen = XSTRLEN(a.output);
  12604. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12605. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12606. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12607. /* Pass in bad values. */
  12608. b.input = NULL;
  12609. b.inLen = 0;
  12610. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12611. c.input = NULL;
  12612. c.inLen = WC_SHA384_DIGEST_SIZE;
  12613. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12614. BAD_FUNC_ARG);
  12615. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12616. BAD_FUNC_ARG);
  12617. wc_Sha384Free(&sha384);
  12618. #endif
  12619. return EXPECT_RESULT();
  12620. } /* END test_wc_Sha384Update */
  12621. /*
  12622. * Unit test function for wc_Sha384Final();
  12623. */
  12624. static int test_wc_Sha384Final(void)
  12625. {
  12626. EXPECT_DECLS;
  12627. #ifdef WOLFSSL_SHA384
  12628. wc_Sha384 sha384;
  12629. byte* hash_test[3];
  12630. byte hash1[WC_SHA384_DIGEST_SIZE];
  12631. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12632. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12633. int times, i;
  12634. /* Initialize */
  12635. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12636. hash_test[0] = hash1;
  12637. hash_test[1] = hash2;
  12638. hash_test[2] = hash3;
  12639. times = sizeof(hash_test) / sizeof(byte*);
  12640. /* Good test args. */
  12641. for (i = 0; i < times; i++) {
  12642. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12643. }
  12644. /* Test bad args. */
  12645. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12646. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12647. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12648. wc_Sha384Free(&sha384);
  12649. #endif
  12650. return EXPECT_RESULT();
  12651. } /* END test_wc_Sha384Final */
  12652. /*
  12653. * Unit test function for wc_Sha384GetFlags()
  12654. */
  12655. static int test_wc_Sha384GetFlags(void)
  12656. {
  12657. EXPECT_DECLS;
  12658. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12659. wc_Sha384 sha384;
  12660. word32 flags = 0;
  12661. /* Initialize */
  12662. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12663. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12664. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12665. wc_Sha384Free(&sha384);
  12666. #endif
  12667. return EXPECT_RESULT();
  12668. } /* END test_wc_Sha384GetFlags */
  12669. /*
  12670. * Unit test function for wc_Sha384FinalRaw()
  12671. */
  12672. static int test_wc_Sha384FinalRaw(void)
  12673. {
  12674. EXPECT_DECLS;
  12675. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12676. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12677. !defined(WOLFSSL_NO_HASH_RAW)
  12678. wc_Sha384 sha384;
  12679. byte* hash_test[3];
  12680. byte hash1[WC_SHA384_DIGEST_SIZE];
  12681. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12682. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12683. int times, i;
  12684. /* Initialize */
  12685. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12686. hash_test[0] = hash1;
  12687. hash_test[1] = hash2;
  12688. hash_test[2] = hash3;
  12689. times = sizeof(hash_test) / sizeof(byte*);
  12690. /* Good test args. */
  12691. for (i = 0; i < times; i++) {
  12692. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12693. }
  12694. /* Test bad args. */
  12695. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12696. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12697. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12698. wc_Sha384Free(&sha384);
  12699. #endif
  12700. return EXPECT_RESULT();
  12701. } /* END test_wc_Sha384FinalRaw */
  12702. /*
  12703. * Unit test function for wc_Sha384Free()
  12704. */
  12705. static int test_wc_Sha384Free(void)
  12706. {
  12707. EXPECT_DECLS;
  12708. #ifdef WOLFSSL_SHA384
  12709. wc_Sha384Free(NULL);
  12710. /* Set result to SUCCESS. */
  12711. ExpectTrue(1);
  12712. #endif
  12713. return EXPECT_RESULT();
  12714. } /* END test_wc_Sha384Free */
  12715. /*
  12716. * Unit test function for wc_Sha384GetHash()
  12717. */
  12718. static int test_wc_Sha384GetHash(void)
  12719. {
  12720. EXPECT_DECLS;
  12721. #ifdef WOLFSSL_SHA384
  12722. wc_Sha384 sha384;
  12723. byte hash1[WC_SHA384_DIGEST_SIZE];
  12724. /* Initialize */
  12725. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12726. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12727. /* test bad arguments*/
  12728. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12729. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12730. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12731. wc_Sha384Free(&sha384);
  12732. #endif
  12733. return EXPECT_RESULT();
  12734. } /* END test_wc_Sha384GetHash */
  12735. /*
  12736. * Unit test function for wc_Sha384Copy()
  12737. */
  12738. static int test_wc_Sha384Copy(void)
  12739. {
  12740. EXPECT_DECLS;
  12741. #ifdef WOLFSSL_SHA384
  12742. wc_Sha384 sha384;
  12743. wc_Sha384 temp;
  12744. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12745. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12746. /* Initialize */
  12747. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12748. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12749. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12750. /* test bad arguments*/
  12751. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12752. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12753. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12754. wc_Sha384Free(&sha384);
  12755. wc_Sha384Free(&temp);
  12756. #endif
  12757. return EXPECT_RESULT();
  12758. } /* END test_wc_Sha384Copy */
  12759. /*
  12760. * Testing wc_InitSha224();
  12761. */
  12762. static int test_wc_InitSha224(void)
  12763. {
  12764. EXPECT_DECLS;
  12765. #ifdef WOLFSSL_SHA224
  12766. wc_Sha224 sha224;
  12767. /* Test good arg. */
  12768. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12769. /* Test bad arg. */
  12770. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12771. wc_Sha224Free(&sha224);
  12772. #endif
  12773. return EXPECT_RESULT();
  12774. } /* END test_wc_InitSha224 */
  12775. /*
  12776. * Unit test on wc_Sha224Update
  12777. */
  12778. static int test_wc_Sha224Update(void)
  12779. {
  12780. EXPECT_DECLS;
  12781. #ifdef WOLFSSL_SHA224
  12782. wc_Sha224 sha224;
  12783. byte hash[WC_SHA224_DIGEST_SIZE];
  12784. testVector a, b, c;
  12785. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12786. /* Input. */
  12787. a.input = "a";
  12788. a.inLen = XSTRLEN(a.input);
  12789. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12790. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12791. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12792. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12793. /* Update input. */
  12794. a.input = "abc";
  12795. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12796. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12797. a.inLen = XSTRLEN(a.input);
  12798. a.outLen = XSTRLEN(a.output);
  12799. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12800. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12801. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12802. /* Pass in bad values. */
  12803. b.input = NULL;
  12804. b.inLen = 0;
  12805. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12806. c.input = NULL;
  12807. c.inLen = WC_SHA224_DIGEST_SIZE;
  12808. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12809. BAD_FUNC_ARG);
  12810. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12811. BAD_FUNC_ARG);
  12812. wc_Sha224Free(&sha224);
  12813. #endif
  12814. return EXPECT_RESULT();
  12815. } /* END test_wc_Sha224Update */
  12816. /*
  12817. * Unit test for wc_Sha224Final();
  12818. */
  12819. static int test_wc_Sha224Final(void)
  12820. {
  12821. EXPECT_DECLS;
  12822. #ifdef WOLFSSL_SHA224
  12823. wc_Sha224 sha224;
  12824. byte* hash_test[3];
  12825. byte hash1[WC_SHA224_DIGEST_SIZE];
  12826. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12827. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12828. int times, i;
  12829. /* Initialize */
  12830. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12831. hash_test[0] = hash1;
  12832. hash_test[1] = hash2;
  12833. hash_test[2] = hash3;
  12834. times = sizeof(hash_test) / sizeof(byte*);
  12835. /* Good test args. */
  12836. /* Testing oversized buffers. */
  12837. for (i = 0; i < times; i++) {
  12838. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12839. }
  12840. /* Test bad args. */
  12841. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12842. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12843. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12844. wc_Sha224Free(&sha224);
  12845. #endif
  12846. return EXPECT_RESULT();
  12847. } /* END test_wc_Sha224Final */
  12848. /*
  12849. * Unit test function for wc_Sha224SetFlags()
  12850. */
  12851. static int test_wc_Sha224SetFlags(void)
  12852. {
  12853. EXPECT_DECLS;
  12854. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12855. wc_Sha224 sha224;
  12856. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12857. /* Initialize */
  12858. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12859. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12860. flags = 0;
  12861. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12862. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12863. wc_Sha224Free(&sha224);
  12864. #endif
  12865. return EXPECT_RESULT();
  12866. } /* END test_wc_Sha224SetFlags */
  12867. /*
  12868. * Unit test function for wc_Sha224GetFlags()
  12869. */
  12870. static int test_wc_Sha224GetFlags(void)
  12871. {
  12872. EXPECT_DECLS;
  12873. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12874. wc_Sha224 sha224;
  12875. word32 flags = 0;
  12876. /* Initialize */
  12877. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12878. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12879. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12880. wc_Sha224Free(&sha224);
  12881. #endif
  12882. return EXPECT_RESULT();
  12883. } /* END test_wc_Sha224GetFlags */
  12884. /*
  12885. * Unit test function for wc_Sha224Free()
  12886. */
  12887. static int test_wc_Sha224Free(void)
  12888. {
  12889. EXPECT_DECLS;
  12890. #ifdef WOLFSSL_SHA224
  12891. wc_Sha224Free(NULL);
  12892. /* Set result to SUCCESS. */
  12893. ExpectTrue(1);
  12894. #endif
  12895. return EXPECT_RESULT();
  12896. } /* END test_wc_Sha224Free */
  12897. /*
  12898. * Unit test function for wc_Sha224GetHash()
  12899. */
  12900. static int test_wc_Sha224GetHash(void)
  12901. {
  12902. EXPECT_DECLS;
  12903. #ifdef WOLFSSL_SHA224
  12904. wc_Sha224 sha224;
  12905. byte hash1[WC_SHA224_DIGEST_SIZE];
  12906. /* Initialize */
  12907. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12908. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12909. /* test bad arguments*/
  12910. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12911. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12912. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12913. wc_Sha224Free(&sha224);
  12914. #endif
  12915. return EXPECT_RESULT();
  12916. } /* END test_wc_Sha224GetHash */
  12917. /*
  12918. * Unit test function for wc_Sha224Copy()
  12919. */
  12920. static int test_wc_Sha224Copy(void)
  12921. {
  12922. EXPECT_DECLS;
  12923. #ifdef WOLFSSL_SHA224
  12924. wc_Sha224 sha224;
  12925. wc_Sha224 temp;
  12926. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12927. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12928. /* Initialize */
  12929. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12930. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12931. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12932. /* test bad arguments*/
  12933. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12934. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12935. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12936. wc_Sha224Free(&sha224);
  12937. wc_Sha224Free(&temp);
  12938. #endif
  12939. return EXPECT_RESULT();
  12940. } /* END test_wc_Sha224Copy */
  12941. /*
  12942. * Testing wc_InitRipeMd()
  12943. */
  12944. static int test_wc_InitRipeMd(void)
  12945. {
  12946. EXPECT_DECLS;
  12947. #ifdef WOLFSSL_RIPEMD
  12948. RipeMd ripemd;
  12949. /* Test good arg. */
  12950. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12951. /* Test bad arg. */
  12952. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12953. #endif
  12954. return EXPECT_RESULT();
  12955. } /* END test_wc_InitRipeMd */
  12956. /*
  12957. * Testing wc_RipeMdUpdate()
  12958. */
  12959. static int test_wc_RipeMdUpdate(void)
  12960. {
  12961. EXPECT_DECLS;
  12962. #ifdef WOLFSSL_RIPEMD
  12963. RipeMd ripemd;
  12964. byte hash[RIPEMD_DIGEST_SIZE];
  12965. testVector a, b, c;
  12966. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12967. /* Input */
  12968. a.input = "a";
  12969. a.inLen = XSTRLEN(a.input);
  12970. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12971. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12972. /* Update input. */
  12973. a.input = "abc";
  12974. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12975. "\xb0\x87\xf1\x5a\x0b\xfc";
  12976. a.inLen = XSTRLEN(a.input);
  12977. a.outLen = XSTRLEN(a.output);
  12978. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12979. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12980. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12981. /* Pass in bad values. */
  12982. b.input = NULL;
  12983. b.inLen = 0;
  12984. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12985. c.input = NULL;
  12986. c.inLen = RIPEMD_DIGEST_SIZE;
  12987. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12988. BAD_FUNC_ARG);
  12989. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12990. BAD_FUNC_ARG);
  12991. #endif
  12992. return EXPECT_RESULT();
  12993. } /* END test_wc_RipeMdUdpate */
  12994. /*
  12995. * Unit test function for wc_RipeMdFinal()
  12996. */
  12997. static int test_wc_RipeMdFinal(void)
  12998. {
  12999. EXPECT_DECLS;
  13000. #ifdef WOLFSSL_RIPEMD
  13001. RipeMd ripemd;
  13002. byte* hash_test[3];
  13003. byte hash1[RIPEMD_DIGEST_SIZE];
  13004. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13005. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13006. int times, i;
  13007. /* Initialize */
  13008. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13009. hash_test[0] = hash1;
  13010. hash_test[1] = hash2;
  13011. hash_test[2] = hash3;
  13012. times = sizeof(hash_test) / sizeof(byte*);
  13013. /* Testing oversized buffers. */
  13014. for (i = 0; i < times; i++) {
  13015. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13016. }
  13017. /* Test bad args. */
  13018. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13019. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13020. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13021. #endif
  13022. return EXPECT_RESULT();
  13023. } /* END test_wc_RipeMdFinal */
  13024. /*
  13025. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13026. * wc_InitSha3_512
  13027. */
  13028. static int test_wc_InitSha3(void)
  13029. {
  13030. EXPECT_DECLS;
  13031. #if defined(WOLFSSL_SHA3)
  13032. wc_Sha3 sha3;
  13033. (void)sha3;
  13034. #if !defined(WOLFSSL_NOSHA3_224)
  13035. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13036. /* Test bad args. */
  13037. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13038. wc_Sha3_224_Free(&sha3);
  13039. #endif /* NOSHA3_224 */
  13040. #if !defined(WOLFSSL_NOSHA3_256)
  13041. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13042. /* Test bad args. */
  13043. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13044. wc_Sha3_256_Free(&sha3);
  13045. #endif /* NOSHA3_256 */
  13046. #if !defined(WOLFSSL_NOSHA3_384)
  13047. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13048. /* Test bad args. */
  13049. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13050. wc_Sha3_384_Free(&sha3);
  13051. #endif /* NOSHA3_384 */
  13052. #if !defined(WOLFSSL_NOSHA3_512)
  13053. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13054. /* Test bad args. */
  13055. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13056. wc_Sha3_512_Free(&sha3);
  13057. #endif /* NOSHA3_512 */
  13058. #endif
  13059. return EXPECT_RESULT();
  13060. } /* END test_wc_InitSha3 */
  13061. /*
  13062. * Testing wc_Sha3_Update()
  13063. */
  13064. static int testing_wc_Sha3_Update(void)
  13065. {
  13066. EXPECT_DECLS;
  13067. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13068. !defined(WOLFSSL_AFALG_XILINX)
  13069. wc_Sha3 sha3;
  13070. byte msg[] = "Everybody's working for the weekend.";
  13071. byte msg2[] = "Everybody gets Friday off.";
  13072. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13073. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13074. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13075. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13076. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13077. word32 msglen = sizeof(msg) - 1;
  13078. word32 msg2len = sizeof(msg2);
  13079. word32 msgCmplen = sizeof(msgCmp);
  13080. #if !defined(WOLFSSL_NOSHA3_224)
  13081. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13082. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13083. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13084. ExpectTrue(sha3.i == msglen);
  13085. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13086. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13087. /* Pass bad args. */
  13088. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13089. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13090. wc_Sha3_224_Free(&sha3);
  13091. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13092. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13093. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13094. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13095. wc_Sha3_224_Free(&sha3);
  13096. #endif /* SHA3_224 */
  13097. #if !defined(WOLFSSL_NOSHA3_256)
  13098. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13099. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13100. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13101. ExpectTrue(sha3.i == msglen);
  13102. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13103. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13104. /* Pass bad args. */
  13105. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13106. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13107. wc_Sha3_256_Free(&sha3);
  13108. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13109. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13110. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13111. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13112. wc_Sha3_256_Free(&sha3);
  13113. #endif /* SHA3_256 */
  13114. #if !defined(WOLFSSL_NOSHA3_384)
  13115. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13116. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13117. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13118. ExpectTrue(sha3.i == msglen);
  13119. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13120. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13121. /* Pass bad args. */
  13122. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13123. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13124. wc_Sha3_384_Free(&sha3);
  13125. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13126. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13127. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13128. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13129. wc_Sha3_384_Free(&sha3);
  13130. #endif /* SHA3_384 */
  13131. #if !defined(WOLFSSL_NOSHA3_512)
  13132. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13133. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13134. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13135. ExpectTrue(sha3.i == msglen);
  13136. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13137. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13138. /* Pass bad args. */
  13139. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13140. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13141. wc_Sha3_512_Free(&sha3);
  13142. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13143. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13144. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13145. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13146. wc_Sha3_512_Free(&sha3);
  13147. #endif /* SHA3_512 */
  13148. #endif /* WOLFSSL_SHA3 */
  13149. return EXPECT_RESULT();
  13150. } /* END testing_wc_Sha3_Update */
  13151. /*
  13152. * Testing wc_Sha3_224_Final()
  13153. */
  13154. static int test_wc_Sha3_224_Final(void)
  13155. {
  13156. EXPECT_DECLS;
  13157. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13158. wc_Sha3 sha3;
  13159. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13160. "nopnopq";
  13161. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13162. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13163. "\x64\xea\xd0\xfc\xce\x33";
  13164. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13165. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13166. /* Init stack variables. */
  13167. XMEMSET(hash, 0, sizeof(hash));
  13168. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13169. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13170. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13171. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13172. /* Test bad args. */
  13173. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13174. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13175. wc_Sha3_224_Free(&sha3);
  13176. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13177. /* Init stack variables. */
  13178. XMEMSET(hash, 0, sizeof(hash));
  13179. XMEMSET(hashRet, 0, sizeof(hashRet));
  13180. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13181. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13182. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13183. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13184. /* Test bad args. */
  13185. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13186. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13187. wc_Sha3_224_Free(&sha3);
  13188. #endif
  13189. return EXPECT_RESULT();
  13190. } /* END test_wc_Sha3_224_Final */
  13191. /*
  13192. * Testing wc_Sha3_256_Final()
  13193. */
  13194. static int test_wc_Sha3_256_Final(void)
  13195. {
  13196. EXPECT_DECLS;
  13197. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13198. wc_Sha3 sha3;
  13199. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13200. "nopnopq";
  13201. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13202. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13203. "\xdd\x97\x49\x6d\x33\x76";
  13204. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13205. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13206. /* Init stack variables. */
  13207. XMEMSET(hash, 0, sizeof(hash));
  13208. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13209. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13210. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13211. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13212. /* Test bad args. */
  13213. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13214. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13215. wc_Sha3_256_Free(&sha3);
  13216. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13217. /* Init stack variables. */
  13218. XMEMSET(hash, 0, sizeof(hash));
  13219. XMEMSET(hashRet, 0, sizeof(hashRet));
  13220. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13221. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13222. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13223. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13224. /* Test bad args. */
  13225. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13226. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13227. wc_Sha3_256_Free(&sha3);
  13228. #endif
  13229. return EXPECT_RESULT();
  13230. } /* END test_wc_Sha3_256_Final */
  13231. /*
  13232. * Testing wc_Sha3_384_Final()
  13233. */
  13234. static int test_wc_Sha3_384_Final(void)
  13235. {
  13236. EXPECT_DECLS;
  13237. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13238. wc_Sha3 sha3;
  13239. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13240. "nopnopq";
  13241. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13242. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13243. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13244. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13245. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13246. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13247. /* Init stack variables. */
  13248. XMEMSET(hash, 0, sizeof(hash));
  13249. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13250. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13251. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13252. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13253. /* Test bad args. */
  13254. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13255. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13256. wc_Sha3_384_Free(&sha3);
  13257. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13258. /* Init stack variables. */
  13259. XMEMSET(hash, 0, sizeof(hash));
  13260. XMEMSET(hashRet, 0, sizeof(hashRet));
  13261. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13262. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13263. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13264. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13265. /* Test bad args. */
  13266. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13267. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13268. wc_Sha3_384_Free(&sha3);
  13269. #endif
  13270. return EXPECT_RESULT();
  13271. } /* END test_wc_Sha3_384_Final */
  13272. /*
  13273. * Testing wc_Sha3_512_Final()
  13274. */
  13275. static int test_wc_Sha3_512_Final(void)
  13276. {
  13277. EXPECT_DECLS;
  13278. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13279. !defined(WOLFSSL_NOSHA3_384)
  13280. wc_Sha3 sha3;
  13281. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13282. "nopnopq";
  13283. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13284. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13285. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13286. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13287. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13288. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13289. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13290. /* Init stack variables. */
  13291. XMEMSET(hash, 0, sizeof(hash));
  13292. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13293. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13294. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13295. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13296. /* Test bad args. */
  13297. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13298. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13299. wc_Sha3_512_Free(&sha3);
  13300. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13301. /* Init stack variables. */
  13302. XMEMSET(hash, 0, sizeof(hash));
  13303. XMEMSET(hashRet, 0, sizeof(hashRet));
  13304. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13305. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13306. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13307. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13308. /* Test bad args. */
  13309. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13310. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13311. wc_Sha3_512_Free(&sha3);
  13312. #endif
  13313. return EXPECT_RESULT();
  13314. } /* END test_wc_Sha3_512_Final */
  13315. /*
  13316. * Testing wc_Sha3_224_Copy()
  13317. */
  13318. static int test_wc_Sha3_224_Copy(void)
  13319. {
  13320. EXPECT_DECLS;
  13321. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13322. wc_Sha3 sha3, sha3Cpy;
  13323. const char* msg = TEST_STRING;
  13324. word32 msglen = (word32)TEST_STRING_SZ;
  13325. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13326. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13327. XMEMSET(hash, 0, sizeof(hash));
  13328. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13329. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13330. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13331. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13332. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13333. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13334. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13335. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13336. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13337. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13338. /* Test bad args. */
  13339. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13340. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13341. wc_Sha3_224_Free(&sha3);
  13342. wc_Sha3_224_Free(&sha3Cpy);
  13343. #endif
  13344. return EXPECT_RESULT();
  13345. } /* END test_wc_Sha3_224_Copy */
  13346. /*
  13347. * Testing wc_Sha3_256_Copy()
  13348. */
  13349. static int test_wc_Sha3_256_Copy(void)
  13350. {
  13351. EXPECT_DECLS;
  13352. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13353. wc_Sha3 sha3, sha3Cpy;
  13354. const char* msg = TEST_STRING;
  13355. word32 msglen = (word32)TEST_STRING_SZ;
  13356. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13357. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13358. XMEMSET(hash, 0, sizeof(hash));
  13359. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13360. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13361. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13362. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13363. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13364. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13365. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13366. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13367. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13368. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13369. /* Test bad args. */
  13370. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13371. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13372. wc_Sha3_256_Free(&sha3);
  13373. wc_Sha3_256_Free(&sha3Cpy);
  13374. #endif
  13375. return EXPECT_RESULT();
  13376. } /* END test_wc_Sha3_256_Copy */
  13377. /*
  13378. * Testing wc_Sha3_384_Copy()
  13379. */
  13380. static int test_wc_Sha3_384_Copy(void)
  13381. {
  13382. EXPECT_DECLS;
  13383. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13384. wc_Sha3 sha3, sha3Cpy;
  13385. const char* msg = TEST_STRING;
  13386. word32 msglen = (word32)TEST_STRING_SZ;
  13387. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13388. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13389. XMEMSET(hash, 0, sizeof(hash));
  13390. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13391. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13392. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13393. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13394. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13395. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13396. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13397. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13398. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13399. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13400. /* Test bad args. */
  13401. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13402. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13403. wc_Sha3_384_Free(&sha3);
  13404. wc_Sha3_384_Free(&sha3Cpy);
  13405. #endif
  13406. return EXPECT_RESULT();
  13407. } /* END test_wc_Sha3_384_Copy */
  13408. /*
  13409. * Testing wc_Sha3_512_Copy()
  13410. */
  13411. static int test_wc_Sha3_512_Copy(void)
  13412. {
  13413. EXPECT_DECLS;
  13414. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13415. wc_Sha3 sha3, sha3Cpy;
  13416. const char* msg = TEST_STRING;
  13417. word32 msglen = (word32)TEST_STRING_SZ;
  13418. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13419. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13420. XMEMSET(hash, 0, sizeof(hash));
  13421. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13422. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13423. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13424. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13425. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13426. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13427. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13428. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13429. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13430. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13431. /* Test bad args. */
  13432. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13433. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13434. wc_Sha3_512_Free(&sha3);
  13435. wc_Sha3_512_Free(&sha3Cpy);
  13436. #endif
  13437. return EXPECT_RESULT();
  13438. } /* END test_wc_Sha3_512_Copy */
  13439. /*
  13440. * Unit test function for wc_Sha3_GetFlags()
  13441. */
  13442. static int test_wc_Sha3_GetFlags(void)
  13443. {
  13444. EXPECT_DECLS;
  13445. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13446. wc_Sha3 sha3;
  13447. word32 flags = 0;
  13448. /* Initialize */
  13449. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13450. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13451. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13452. wc_Sha3_224_Free(&sha3);
  13453. #endif
  13454. return EXPECT_RESULT();
  13455. } /* END test_wc_Sha3_GetFlags */
  13456. static int test_wc_InitShake256(void)
  13457. {
  13458. EXPECT_DECLS;
  13459. #ifdef WOLFSSL_SHAKE256
  13460. wc_Shake shake;
  13461. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13462. /* Test bad args. */
  13463. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13464. wc_Shake256_Free(&shake);
  13465. #endif
  13466. return EXPECT_RESULT();
  13467. }
  13468. static int testing_wc_Shake256_Update(void)
  13469. {
  13470. EXPECT_DECLS;
  13471. #ifdef WOLFSSL_SHAKE256
  13472. wc_Shake shake;
  13473. byte msg[] = "Everybody's working for the weekend.";
  13474. byte msg2[] = "Everybody gets Friday off.";
  13475. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13476. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13477. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13478. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13479. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13480. word32 msglen = sizeof(msg) - 1;
  13481. word32 msg2len = sizeof(msg2);
  13482. word32 msgCmplen = sizeof(msgCmp);
  13483. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13484. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13485. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13486. ExpectTrue(shake.i == msglen);
  13487. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13488. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13489. /* Pass bad args. */
  13490. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13491. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13492. wc_Shake256_Free(&shake);
  13493. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13494. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13495. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13496. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13497. wc_Shake256_Free(&shake);
  13498. #endif /* WOLFSSL_SHAKE256 */
  13499. return EXPECT_RESULT();
  13500. }
  13501. static int test_wc_Shake256_Final(void)
  13502. {
  13503. EXPECT_DECLS;
  13504. #ifdef WOLFSSL_SHAKE256
  13505. wc_Shake shake;
  13506. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13507. "nopnopq";
  13508. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13509. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13510. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13511. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13512. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13513. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13514. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13515. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13516. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13517. byte hash[114];
  13518. /* Init stack variables. */
  13519. XMEMSET(hash, 0, sizeof(hash));
  13520. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13521. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13522. 0);
  13523. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13524. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13525. /* Test bad args. */
  13526. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13527. BAD_FUNC_ARG);
  13528. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13529. BAD_FUNC_ARG);
  13530. wc_Shake256_Free(&shake);
  13531. #endif
  13532. return EXPECT_RESULT();
  13533. }
  13534. /*
  13535. * Testing wc_Shake256_Copy()
  13536. */
  13537. static int test_wc_Shake256_Copy(void)
  13538. {
  13539. EXPECT_DECLS;
  13540. #ifdef WOLFSSL_SHAKE256
  13541. wc_Shake shake, shakeCpy;
  13542. const char* msg = TEST_STRING;
  13543. word32 msglen = (word32)TEST_STRING_SZ;
  13544. byte hash[144];
  13545. byte hashCpy[144];
  13546. word32 hashLen = sizeof(hash);
  13547. word32 hashLenCpy = sizeof(hashCpy);
  13548. XMEMSET(hash, 0, sizeof(hash));
  13549. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13550. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13551. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13552. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13553. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13554. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13555. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13556. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13557. /* Test bad args. */
  13558. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13559. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13560. wc_Shake256_Free(&shake);
  13561. wc_Shake256_Free(&shakeCpy);
  13562. #endif
  13563. return EXPECT_RESULT();
  13564. } /* END test_wc_Shake256_Copy */
  13565. /*
  13566. * Unit test function for wc_Shake256Hash()
  13567. */
  13568. static int test_wc_Shake256Hash(void)
  13569. {
  13570. EXPECT_DECLS;
  13571. #ifdef WOLFSSL_SHAKE256
  13572. const byte data[] = { /* Hello World */
  13573. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13574. 0x72,0x6c,0x64
  13575. };
  13576. word32 len = sizeof(data);
  13577. byte hash[144];
  13578. word32 hashLen = sizeof(hash);
  13579. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13580. #endif
  13581. return EXPECT_RESULT();
  13582. } /* END test_wc_Shake256Hash */
  13583. /*
  13584. * Testing wc_InitSm3(), wc_Sm3Free()
  13585. */
  13586. static int test_wc_InitSm3Free(void)
  13587. {
  13588. EXPECT_DECLS;
  13589. #ifdef WOLFSSL_SM3
  13590. wc_Sm3 sm3;
  13591. /* Invalid Parameters */
  13592. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13593. /* Valid Parameters */
  13594. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13595. wc_Sm3Free(NULL);
  13596. wc_Sm3Free(&sm3);
  13597. #endif
  13598. return EXPECT_RESULT();
  13599. } /* END test_wc_InitSm3 */
  13600. /*
  13601. * Testing wc_Sm3Update(), wc_Sm3Final()
  13602. */
  13603. static int test_wc_Sm3UpdateFinal(void)
  13604. {
  13605. EXPECT_DECLS;
  13606. #ifdef WOLFSSL_SM3
  13607. wc_Sm3 sm3;
  13608. byte data[WC_SM3_BLOCK_SIZE * 4];
  13609. byte hash[WC_SM3_DIGEST_SIZE];
  13610. byte calcHash[WC_SM3_DIGEST_SIZE];
  13611. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13612. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13613. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13614. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13615. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13616. };
  13617. word32 chunk;
  13618. word32 i;
  13619. XMEMSET(data, 0, sizeof(data));
  13620. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13621. /* Invalid Parameters */
  13622. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13623. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13624. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13625. /* Valid Parameters */
  13626. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13627. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13628. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13629. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13630. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13631. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13632. /* Ensure too many bytes for lengths. */
  13633. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13634. /* Invalid Parameters */
  13635. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13636. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13637. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13638. /* Valid Parameters */
  13639. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13640. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13641. /* Chunk tests. */
  13642. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13643. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13644. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13645. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13646. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13647. }
  13648. if (i < (word32)sizeof(data)) {
  13649. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13650. 0);
  13651. }
  13652. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13653. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13654. }
  13655. /* Not testing when the low 32-bit length overflows. */
  13656. wc_Sm3Free(&sm3);
  13657. #endif
  13658. return EXPECT_RESULT();
  13659. } /* END test_wc_Sm3Update */
  13660. /*
  13661. * Testing wc_Sm3GetHash()
  13662. */
  13663. static int test_wc_Sm3GetHash(void)
  13664. {
  13665. EXPECT_DECLS;
  13666. #ifdef WOLFSSL_SM3
  13667. wc_Sm3 sm3;
  13668. byte hash[WC_SM3_DIGEST_SIZE];
  13669. byte calcHash[WC_SM3_DIGEST_SIZE];
  13670. byte data[WC_SM3_BLOCK_SIZE];
  13671. XMEMSET(data, 0, sizeof(data));
  13672. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13673. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13674. /* Invalid Parameters */
  13675. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13676. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13677. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13678. /* Valid Parameters */
  13679. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13680. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13681. /* With update. */
  13682. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13683. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13684. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13685. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13686. wc_Sm3Free(&sm3);
  13687. #endif
  13688. return EXPECT_RESULT();
  13689. } /* END test_wc_Sm3Update */
  13690. /*
  13691. * Testing wc_Sm3Copy()
  13692. */
  13693. static int test_wc_Sm3Copy(void)
  13694. {
  13695. EXPECT_DECLS;
  13696. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13697. wc_Sm3 sm3;
  13698. wc_Sm3 sm3Copy;
  13699. byte hash[WC_SM3_DIGEST_SIZE];
  13700. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13701. byte data[WC_SM3_BLOCK_SIZE + 1];
  13702. int i;
  13703. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13704. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13705. /* Invalid Parameters */
  13706. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13707. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13708. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13709. /* Valid Parameters */
  13710. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13711. /* Ensure all parts of data updated during hashing are copied. */
  13712. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13713. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13714. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13715. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13716. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13717. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13718. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13719. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13720. }
  13721. wc_Sm3Free(&sm3Copy);
  13722. wc_Sm3Free(&sm3);
  13723. #endif
  13724. return EXPECT_RESULT();
  13725. } /* END test_wc_Sm3Copy */
  13726. /*
  13727. * Testing wc_Sm3FinalRaw()
  13728. */
  13729. static int test_wc_Sm3FinalRaw(void)
  13730. {
  13731. EXPECT_DECLS;
  13732. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13733. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13734. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13735. !defined(WOLFSSL_NO_HASH_RAW)
  13736. wc_Sm3 sm3;
  13737. byte hash1[WC_SM3_DIGEST_SIZE];
  13738. byte hash2[WC_SM3_DIGEST_SIZE];
  13739. byte hash3[WC_SM3_DIGEST_SIZE];
  13740. byte* hash_test[3] = { hash1, hash2, hash3 };
  13741. int times;
  13742. int i;
  13743. XMEMSET(&sm3, 0, sizeof(sm3));
  13744. /* Initialize */
  13745. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13746. /* Invalid Parameters */
  13747. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13748. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13749. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13750. times = sizeof(hash_test) / sizeof(byte*);
  13751. for (i = 0; i < times; i++) {
  13752. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13753. }
  13754. wc_Sm3Free(&sm3);
  13755. #endif
  13756. return EXPECT_RESULT();
  13757. } /* END test_wc_Sm3FinalRaw */
  13758. /*
  13759. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13760. */
  13761. static int test_wc_Sm3GetSetFlags(void)
  13762. {
  13763. EXPECT_DECLS;
  13764. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13765. wc_Sm3 sm3;
  13766. wc_Sm3 sm3Copy;
  13767. word32 flags = 0;
  13768. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13769. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13770. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13771. ExpectIntEQ(flags, 0);
  13772. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13773. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13774. ExpectIntEQ(flags, 0);
  13775. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13776. ExpectIntEQ(flags, 0);
  13777. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13778. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13779. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13780. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13781. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13782. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13783. wc_Sm3Free(&sm3Copy);
  13784. wc_Sm3Free(&sm3);
  13785. #endif
  13786. return EXPECT_RESULT();
  13787. } /* END test_wc_Sm3Update */
  13788. /*
  13789. * Testing wc_Sm3Hash()
  13790. */
  13791. static int test_wc_Sm3Hash(void)
  13792. {
  13793. EXPECT_DECLS;
  13794. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13795. byte data[WC_SM3_BLOCK_SIZE];
  13796. byte hash[WC_SM3_DIGEST_SIZE];
  13797. /* Invalid parameters. */
  13798. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13799. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13800. /* Valid parameters. */
  13801. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13802. #endif
  13803. return EXPECT_RESULT();
  13804. } /* END test_wc_Sm3Hash */
  13805. /*
  13806. * Test function for wc_HmacSetKey
  13807. */
  13808. static int test_wc_Md5HmacSetKey(void)
  13809. {
  13810. EXPECT_DECLS;
  13811. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13812. Hmac hmac;
  13813. int ret, times, itr;
  13814. const char* keys[]=
  13815. {
  13816. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13817. #ifndef HAVE_FIPS
  13818. "Jefe", /* smaller than minimum FIPS key size */
  13819. #endif
  13820. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13821. };
  13822. times = sizeof(keys) / sizeof(char*);
  13823. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13824. for (itr = 0; itr < times; itr++) {
  13825. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13826. (word32)XSTRLEN(keys[itr]));
  13827. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13828. wc_HmacFree(&hmac);
  13829. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13830. #else
  13831. ExpectIntEQ(ret, 0);
  13832. #endif
  13833. }
  13834. /* Bad args. */
  13835. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13836. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13837. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13838. BAD_FUNC_ARG);
  13839. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13840. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13841. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13842. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13843. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13844. #elif defined(HAVE_FIPS)
  13845. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13846. #else
  13847. ExpectIntEQ(ret, 0);
  13848. #endif
  13849. wc_HmacFree(&hmac);
  13850. #endif
  13851. return EXPECT_RESULT();
  13852. } /* END test_wc_Md5HmacSetKey */
  13853. /*
  13854. * testing wc_HmacSetKey() on wc_Sha hash.
  13855. */
  13856. static int test_wc_ShaHmacSetKey(void)
  13857. {
  13858. EXPECT_DECLS;
  13859. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13860. Hmac hmac;
  13861. int ret, times, itr;
  13862. const char* keys[]=
  13863. {
  13864. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13865. "\x0b\x0b\x0b",
  13866. #ifndef HAVE_FIPS
  13867. "Jefe", /* smaller than minimum FIPS key size */
  13868. #endif
  13869. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13870. "\xAA\xAA\xAA"
  13871. };
  13872. times = sizeof(keys) / sizeof(char*);
  13873. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13874. for (itr = 0; itr < times; itr++) {
  13875. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13876. (word32)XSTRLEN(keys[itr])), 0);
  13877. }
  13878. /* Bad args. */
  13879. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13880. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13881. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13882. BAD_FUNC_ARG);
  13883. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13884. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13885. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13886. #ifdef HAVE_FIPS
  13887. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13888. #else
  13889. ExpectIntEQ(ret, 0);
  13890. #endif
  13891. wc_HmacFree(&hmac);
  13892. #endif
  13893. return EXPECT_RESULT();
  13894. } /* END test_wc_ShaHmacSetKey() */
  13895. /*
  13896. * testing wc_HmacSetKey() on Sha224 hash.
  13897. */
  13898. static int test_wc_Sha224HmacSetKey(void)
  13899. {
  13900. EXPECT_DECLS;
  13901. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13902. Hmac hmac;
  13903. int ret, times, itr;
  13904. const char* keys[]=
  13905. {
  13906. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13907. "\x0b\x0b\x0b",
  13908. #ifndef HAVE_FIPS
  13909. "Jefe", /* smaller than minimum FIPS key size */
  13910. #endif
  13911. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13912. "\xAA\xAA\xAA"
  13913. };
  13914. times = sizeof(keys) / sizeof(char*);
  13915. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13916. for (itr = 0; itr < times; itr++) {
  13917. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13918. (word32)XSTRLEN(keys[itr])), 0);
  13919. }
  13920. /* Bad args. */
  13921. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13922. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13923. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13924. BAD_FUNC_ARG);
  13925. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13926. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13927. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13928. #ifdef HAVE_FIPS
  13929. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13930. #else
  13931. ExpectIntEQ(ret, 0);
  13932. #endif
  13933. wc_HmacFree(&hmac);
  13934. #endif
  13935. return EXPECT_RESULT();
  13936. } /* END test_wc_Sha224HmacSetKey() */
  13937. /*
  13938. * testing wc_HmacSetKey() on Sha256 hash
  13939. */
  13940. static int test_wc_Sha256HmacSetKey(void)
  13941. {
  13942. EXPECT_DECLS;
  13943. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13944. Hmac hmac;
  13945. int ret, times, itr;
  13946. const char* keys[]=
  13947. {
  13948. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13949. "\x0b\x0b\x0b",
  13950. #ifndef HAVE_FIPS
  13951. "Jefe", /* smaller than minimum FIPS key size */
  13952. #endif
  13953. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13954. "\xAA\xAA\xAA"
  13955. };
  13956. times = sizeof(keys) / sizeof(char*);
  13957. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13958. for (itr = 0; itr < times; itr++) {
  13959. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13960. (word32)XSTRLEN(keys[itr])), 0);
  13961. }
  13962. /* Bad args. */
  13963. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13964. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13965. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13966. BAD_FUNC_ARG);
  13967. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13968. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13969. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13970. #ifdef HAVE_FIPS
  13971. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13972. #else
  13973. ExpectIntEQ(ret, 0);
  13974. #endif
  13975. wc_HmacFree(&hmac);
  13976. #endif
  13977. return EXPECT_RESULT();
  13978. } /* END test_wc_Sha256HmacSetKey() */
  13979. /*
  13980. * testing wc_HmacSetKey on Sha384 hash.
  13981. */
  13982. static int test_wc_Sha384HmacSetKey(void)
  13983. {
  13984. EXPECT_DECLS;
  13985. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13986. Hmac hmac;
  13987. int ret, times, itr;
  13988. const char* keys[]=
  13989. {
  13990. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13991. "\x0b\x0b\x0b",
  13992. #ifndef HAVE_FIPS
  13993. "Jefe", /* smaller than minimum FIPS key size */
  13994. #endif
  13995. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13996. "\xAA\xAA\xAA"
  13997. };
  13998. times = sizeof(keys) / sizeof(char*);
  13999. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14000. for (itr = 0; itr < times; itr++) {
  14001. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14002. (word32)XSTRLEN(keys[itr])), 0);
  14003. }
  14004. /* Bad args. */
  14005. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14006. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14007. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14008. BAD_FUNC_ARG);
  14009. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14010. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14011. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14012. #ifdef HAVE_FIPS
  14013. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14014. #else
  14015. ExpectIntEQ(ret, 0);
  14016. #endif
  14017. wc_HmacFree(&hmac);
  14018. #endif
  14019. return EXPECT_RESULT();
  14020. } /* END test_wc_Sha384HmacSetKey() */
  14021. /*
  14022. * testing wc_HmacUpdate on wc_Md5 hash.
  14023. */
  14024. static int test_wc_Md5HmacUpdate(void)
  14025. {
  14026. EXPECT_DECLS;
  14027. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14028. Hmac hmac;
  14029. testVector a, b;
  14030. #ifdef HAVE_FIPS
  14031. const char* keys =
  14032. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14033. #else
  14034. const char* keys = "Jefe";
  14035. #endif
  14036. a.input = "what do ya want for nothing?";
  14037. a.inLen = XSTRLEN(a.input);
  14038. b.input = "Hi There";
  14039. b.inLen = XSTRLEN(b.input);
  14040. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14041. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14042. (word32)XSTRLEN(keys)), 0);
  14043. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14044. /* Update Hmac. */
  14045. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14046. /* Test bad args. */
  14047. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14048. BAD_FUNC_ARG);
  14049. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14050. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14051. wc_HmacFree(&hmac);
  14052. #endif
  14053. return EXPECT_RESULT();
  14054. } /* END test_wc_Md5HmacUpdate */
  14055. /*
  14056. * testing wc_HmacUpdate on SHA hash.
  14057. */
  14058. static int test_wc_ShaHmacUpdate(void)
  14059. {
  14060. EXPECT_DECLS;
  14061. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14062. Hmac hmac;
  14063. testVector a, b;
  14064. #ifdef HAVE_FIPS
  14065. const char* keys =
  14066. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14067. #else
  14068. const char* keys = "Jefe";
  14069. #endif
  14070. a.input = "what do ya want for nothing?";
  14071. a.inLen = XSTRLEN(a.input);
  14072. b.input = "Hi There";
  14073. b.inLen = XSTRLEN(b.input);
  14074. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14075. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14076. (word32)XSTRLEN(keys)), 0);
  14077. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14078. /* Update Hmac. */
  14079. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14080. /* Test bad args. */
  14081. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14082. BAD_FUNC_ARG);
  14083. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14084. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14085. wc_HmacFree(&hmac);
  14086. #endif
  14087. return EXPECT_RESULT();
  14088. } /* END test_wc_ShaHmacUpdate */
  14089. /*
  14090. * testing wc_HmacUpdate on SHA224 hash.
  14091. */
  14092. static int test_wc_Sha224HmacUpdate(void)
  14093. {
  14094. EXPECT_DECLS;
  14095. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14096. Hmac hmac;
  14097. testVector a, b;
  14098. #ifdef HAVE_FIPS
  14099. const char* keys =
  14100. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14101. #else
  14102. const char* keys = "Jefe";
  14103. #endif
  14104. a.input = "what do ya want for nothing?";
  14105. a.inLen = XSTRLEN(a.input);
  14106. b.input = "Hi There";
  14107. b.inLen = XSTRLEN(b.input);
  14108. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14109. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14110. (word32)XSTRLEN(keys)), 0);
  14111. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14112. /* Update Hmac. */
  14113. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14114. /* Test bad args. */
  14115. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14116. BAD_FUNC_ARG);
  14117. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14118. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14119. wc_HmacFree(&hmac);
  14120. #endif
  14121. return EXPECT_RESULT();
  14122. } /* END test_wc_Sha224HmacUpdate */
  14123. /*
  14124. * testing wc_HmacUpdate on SHA256 hash.
  14125. */
  14126. static int test_wc_Sha256HmacUpdate(void)
  14127. {
  14128. EXPECT_DECLS;
  14129. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14130. Hmac hmac;
  14131. testVector a, b;
  14132. #ifdef HAVE_FIPS
  14133. const char* keys =
  14134. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14135. #else
  14136. const char* keys = "Jefe";
  14137. #endif
  14138. a.input = "what do ya want for nothing?";
  14139. a.inLen = XSTRLEN(a.input);
  14140. b.input = "Hi There";
  14141. b.inLen = XSTRLEN(b.input);
  14142. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14143. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14144. (word32)XSTRLEN(keys)), 0);
  14145. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14146. /* Update Hmac. */
  14147. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14148. /* Test bad args. */
  14149. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14150. BAD_FUNC_ARG);
  14151. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14152. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14153. wc_HmacFree(&hmac);
  14154. #endif
  14155. return EXPECT_RESULT();
  14156. } /* END test_wc_Sha256HmacUpdate */
  14157. /*
  14158. * testing wc_HmacUpdate on SHA384 hash.
  14159. */
  14160. static int test_wc_Sha384HmacUpdate(void)
  14161. {
  14162. EXPECT_DECLS;
  14163. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14164. Hmac hmac;
  14165. testVector a, b;
  14166. #ifdef HAVE_FIPS
  14167. const char* keys =
  14168. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14169. #else
  14170. const char* keys = "Jefe";
  14171. #endif
  14172. a.input = "what do ya want for nothing?";
  14173. a.inLen = XSTRLEN(a.input);
  14174. b.input = "Hi There";
  14175. b.inLen = XSTRLEN(b.input);
  14176. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14177. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14178. (word32)XSTRLEN(keys)), 0);
  14179. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14180. /* Update Hmac. */
  14181. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14182. /* Test bad args. */
  14183. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14184. BAD_FUNC_ARG);
  14185. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14186. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14187. wc_HmacFree(&hmac);
  14188. #endif
  14189. return EXPECT_RESULT();
  14190. } /* END test_wc_Sha384HmacUpdate */
  14191. /*
  14192. * Testing wc_HmacFinal() with MD5
  14193. */
  14194. static int test_wc_Md5HmacFinal(void)
  14195. {
  14196. EXPECT_DECLS;
  14197. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14198. Hmac hmac;
  14199. byte hash[WC_MD5_DIGEST_SIZE];
  14200. testVector a;
  14201. const char* key;
  14202. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14203. a.input = "Hi There";
  14204. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14205. "\x9d";
  14206. a.inLen = XSTRLEN(a.input);
  14207. a.outLen = XSTRLEN(a.output);
  14208. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14209. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14210. 0);
  14211. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14212. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14213. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14214. /* Try bad parameters. */
  14215. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14216. #ifndef HAVE_FIPS
  14217. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14218. #endif
  14219. wc_HmacFree(&hmac);
  14220. #endif
  14221. return EXPECT_RESULT();
  14222. } /* END test_wc_Md5HmacFinal */
  14223. /*
  14224. * Testing wc_HmacFinal() with SHA
  14225. */
  14226. static int test_wc_ShaHmacFinal(void)
  14227. {
  14228. EXPECT_DECLS;
  14229. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14230. Hmac hmac;
  14231. byte hash[WC_SHA_DIGEST_SIZE];
  14232. testVector a;
  14233. const char* key;
  14234. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14235. "\x0b\x0b\x0b";
  14236. a.input = "Hi There";
  14237. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14238. "\x8e\xf1\x46\xbe\x00";
  14239. a.inLen = XSTRLEN(a.input);
  14240. a.outLen = XSTRLEN(a.output);
  14241. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14242. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14243. 0);
  14244. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14245. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14246. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14247. /* Try bad parameters. */
  14248. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14249. #ifndef HAVE_FIPS
  14250. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14251. #endif
  14252. wc_HmacFree(&hmac);
  14253. #endif
  14254. return EXPECT_RESULT();
  14255. } /* END test_wc_ShaHmacFinal */
  14256. /*
  14257. * Testing wc_HmacFinal() with SHA224
  14258. */
  14259. static int test_wc_Sha224HmacFinal(void)
  14260. {
  14261. EXPECT_DECLS;
  14262. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14263. Hmac hmac;
  14264. byte hash[WC_SHA224_DIGEST_SIZE];
  14265. testVector a;
  14266. const char* key;
  14267. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14268. "\x0b\x0b\x0b";
  14269. a.input = "Hi There";
  14270. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14271. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14272. a.inLen = XSTRLEN(a.input);
  14273. a.outLen = XSTRLEN(a.output);
  14274. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14275. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14276. (word32)XSTRLEN(key)), 0);
  14277. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14278. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14279. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14280. /* Try bad parameters. */
  14281. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14282. #ifndef HAVE_FIPS
  14283. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14284. #endif
  14285. wc_HmacFree(&hmac);
  14286. #endif
  14287. return EXPECT_RESULT();
  14288. } /* END test_wc_Sha224HmacFinal */
  14289. /*
  14290. * Testing wc_HmacFinal() with SHA256
  14291. */
  14292. static int test_wc_Sha256HmacFinal(void)
  14293. {
  14294. EXPECT_DECLS;
  14295. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14296. Hmac hmac;
  14297. byte hash[WC_SHA256_DIGEST_SIZE];
  14298. testVector a;
  14299. const char* key;
  14300. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14301. "\x0b\x0b\x0b";
  14302. a.input = "Hi There";
  14303. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14304. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14305. "\xcf\xf7";
  14306. a.inLen = XSTRLEN(a.input);
  14307. a.outLen = XSTRLEN(a.output);
  14308. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14309. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14310. (word32)XSTRLEN(key)), 0);
  14311. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14312. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14313. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14314. /* Try bad parameters. */
  14315. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14316. #ifndef HAVE_FIPS
  14317. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14318. #endif
  14319. wc_HmacFree(&hmac);
  14320. #endif
  14321. return EXPECT_RESULT();
  14322. } /* END test_wc_Sha256HmacFinal */
  14323. /*
  14324. * Testing wc_HmacFinal() with SHA384
  14325. */
  14326. static int test_wc_Sha384HmacFinal(void)
  14327. {
  14328. EXPECT_DECLS;
  14329. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14330. Hmac hmac;
  14331. byte hash[WC_SHA384_DIGEST_SIZE];
  14332. testVector a;
  14333. const char* key;
  14334. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14335. "\x0b\x0b\x0b";
  14336. a.input = "Hi There";
  14337. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14338. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14339. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14340. "\xfa\x9c\xb6";
  14341. a.inLen = XSTRLEN(a.input);
  14342. a.outLen = XSTRLEN(a.output);
  14343. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14344. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14345. (word32)XSTRLEN(key)), 0);
  14346. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14347. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14348. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14349. /* Try bad parameters. */
  14350. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14351. #ifndef HAVE_FIPS
  14352. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14353. #endif
  14354. wc_HmacFree(&hmac);
  14355. #endif
  14356. return EXPECT_RESULT();
  14357. } /* END test_wc_Sha384HmacFinal */
  14358. /*
  14359. * Testing wc_InitCmac()
  14360. */
  14361. static int test_wc_InitCmac(void)
  14362. {
  14363. EXPECT_DECLS;
  14364. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14365. Cmac cmac1;
  14366. Cmac cmac2;
  14367. Cmac cmac3;
  14368. /* AES 128 key. */
  14369. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14370. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14371. /* AES 192 key. */
  14372. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14373. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14374. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14375. /* AES 256 key. */
  14376. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14377. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14378. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14379. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14380. word32 key1Sz = (word32)sizeof(key1) - 1;
  14381. word32 key2Sz = (word32)sizeof(key2) - 1;
  14382. word32 key3Sz = (word32)sizeof(key3) - 1;
  14383. int type = WC_CMAC_AES;
  14384. (void)key1;
  14385. (void)key1Sz;
  14386. (void)key2;
  14387. (void)key2Sz;
  14388. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14389. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14390. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14391. #ifdef WOLFSSL_AES_128
  14392. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14393. #endif
  14394. #ifdef WOLFSSL_AES_192
  14395. wc_AesFree(&cmac1.aes);
  14396. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14397. #endif
  14398. #ifdef WOLFSSL_AES_256
  14399. wc_AesFree(&cmac2.aes);
  14400. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14401. #endif
  14402. wc_AesFree(&cmac3.aes);
  14403. /* Test bad args. */
  14404. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14405. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14406. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14407. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14408. #endif
  14409. return EXPECT_RESULT();
  14410. } /* END test_wc_InitCmac */
  14411. /*
  14412. * Testing wc_CmacUpdate()
  14413. */
  14414. static int test_wc_CmacUpdate(void)
  14415. {
  14416. EXPECT_DECLS;
  14417. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14418. Cmac cmac;
  14419. byte key[] = {
  14420. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14421. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14422. };
  14423. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14424. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14425. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14426. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14427. "\xf4";
  14428. word32 inSz = (word32)sizeof(in) - 1;
  14429. word32 keySz = (word32)sizeof(key);
  14430. int type = WC_CMAC_AES;
  14431. XMEMSET(&cmac, 0, sizeof(Cmac));
  14432. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14433. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14434. /* Test bad args. */
  14435. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14436. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14437. wc_AesFree(&cmac.aes);
  14438. #endif
  14439. return EXPECT_RESULT();
  14440. } /* END test_wc_CmacUpdate */
  14441. /*
  14442. * Testing wc_CmacFinal()
  14443. */
  14444. static int test_wc_CmacFinal(void)
  14445. {
  14446. EXPECT_DECLS;
  14447. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14448. Cmac cmac;
  14449. byte key[] = {
  14450. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14451. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14452. };
  14453. byte msg[] = {
  14454. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14455. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14456. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14457. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14458. 0xf4
  14459. };
  14460. /* Test vectors from CMACGenAES128.rsp from
  14461. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14462. * Per RFC4493 truncation of lsb is possible.
  14463. */
  14464. byte expMac[] = {
  14465. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14466. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14467. };
  14468. byte mac[AES_BLOCK_SIZE];
  14469. word32 msgSz = (word32)sizeof(msg);
  14470. word32 keySz = (word32)sizeof(key);
  14471. word32 macSz = sizeof(mac);
  14472. word32 badMacSz = 17;
  14473. int expMacSz = sizeof(expMac);
  14474. int type = WC_CMAC_AES;
  14475. XMEMSET(&cmac, 0, sizeof(Cmac));
  14476. XMEMSET(mac, 0, macSz);
  14477. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14478. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14479. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14480. /* Pass in bad args. */
  14481. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14482. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14483. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14484. /* For the last call, use the API with implicit wc_CmacFree(). */
  14485. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14486. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14487. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14488. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14489. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14490. /* Pass in bad args. */
  14491. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14492. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14493. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14494. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14495. #endif
  14496. return EXPECT_RESULT();
  14497. } /* END test_wc_CmacFinal */
  14498. /*
  14499. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14500. */
  14501. static int test_wc_AesCmacGenerate(void)
  14502. {
  14503. EXPECT_DECLS;
  14504. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14505. byte key[] = {
  14506. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14507. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14508. };
  14509. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14510. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14511. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14512. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14513. byte mac[AES_BLOCK_SIZE];
  14514. word32 keySz = sizeof(key);
  14515. word32 macSz = sizeof(mac);
  14516. word32 msgSz = sizeof(msg) - 1;
  14517. word32 expMacSz = sizeof(expMac) - 1;
  14518. XMEMSET(mac, 0, macSz);
  14519. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14520. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14521. /* Pass in bad args. */
  14522. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14523. BAD_FUNC_ARG);
  14524. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14525. BAD_FUNC_ARG);
  14526. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14527. BAD_FUNC_ARG);
  14528. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14529. BAD_FUNC_ARG);
  14530. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14531. /* Test bad args. */
  14532. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14533. BAD_FUNC_ARG);
  14534. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14535. BAD_FUNC_ARG);
  14536. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14537. BAD_FUNC_ARG);
  14538. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14539. BAD_FUNC_ARG);
  14540. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14541. BAD_FUNC_ARG);
  14542. #endif
  14543. return EXPECT_RESULT();
  14544. } /* END test_wc_AesCmacGenerate */
  14545. /*
  14546. * Testing streaming AES-GCM API.
  14547. */
  14548. static int test_wc_AesGcmStream(void)
  14549. {
  14550. EXPECT_DECLS;
  14551. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14552. defined(WOLFSSL_AESGCM_STREAM)
  14553. int i;
  14554. WC_RNG rng[1];
  14555. Aes aesEnc[1];
  14556. Aes aesDec[1];
  14557. byte tag[AES_BLOCK_SIZE];
  14558. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14559. byte out[AES_BLOCK_SIZE * 3 + 2];
  14560. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14561. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14562. byte key[AES_128_KEY_SIZE] = { 0, };
  14563. byte iv[AES_IV_SIZE] = { 1, };
  14564. byte ivOut[AES_IV_SIZE];
  14565. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14566. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14567. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14568. };
  14569. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14570. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14571. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14572. };
  14573. static const byte expTag[AES_BLOCK_SIZE] = {
  14574. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14575. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14576. };
  14577. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14578. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14579. XMEMSET(&aesDec, 0, sizeof(Aes));
  14580. /* Create a random for generating IV/nonce. */
  14581. ExpectIntEQ(wc_InitRng(rng), 0);
  14582. /* Initialize data structures. */
  14583. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14584. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14585. /* BadParameters to streaming init. */
  14586. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14587. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14588. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14589. BAD_FUNC_ARG);
  14590. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14591. BAD_FUNC_ARG);
  14592. /* Bad parameters to encrypt update. */
  14593. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14594. BAD_FUNC_ARG);
  14595. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14596. BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14598. BAD_FUNC_ARG);
  14599. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14600. BAD_FUNC_ARG);
  14601. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14602. BAD_FUNC_ARG);
  14603. /* Bad parameters to decrypt update. */
  14604. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14605. BAD_FUNC_ARG);
  14606. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14607. BAD_FUNC_ARG);
  14608. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14609. BAD_FUNC_ARG);
  14610. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14611. BAD_FUNC_ARG);
  14612. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14613. BAD_FUNC_ARG);
  14614. /* Bad parameters to encrypt final. */
  14615. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14616. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14617. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14618. BAD_FUNC_ARG);
  14619. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14620. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14621. BAD_FUNC_ARG);
  14622. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14623. BAD_FUNC_ARG);
  14624. /* Bad parameters to decrypt final. */
  14625. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14626. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14627. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14628. BAD_FUNC_ARG);
  14629. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14630. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14631. BAD_FUNC_ARG);
  14632. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14633. BAD_FUNC_ARG);
  14634. /* Check calling final before setting key fails. */
  14635. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14636. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14637. /* Check calling update before setting key else fails. */
  14638. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14639. MISSING_KEY);
  14640. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14641. MISSING_KEY);
  14642. /* Set key but not IV. */
  14643. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14644. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14645. /* Check calling final before setting IV fails. */
  14646. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14647. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14648. /* Check calling update before setting IV else fails. */
  14649. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14650. MISSING_IV);
  14651. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14652. MISSING_IV);
  14653. /* Set IV using fixed part IV and external IV APIs. */
  14654. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14655. rng), 0);
  14656. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14657. GCM_NONCE_MID_SZ), 0);
  14658. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14659. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14660. /* Encrypt and decrypt data. */
  14661. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14662. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14663. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14664. /* Finalize and check tag matches. */
  14665. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14666. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14667. /* Set key and IV through streaming init API. */
  14668. wc_AesFree(aesEnc);
  14669. wc_AesFree(aesDec);
  14670. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14671. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14672. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14673. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14674. /* Encrypt/decrypt one block and AAD of one block. */
  14675. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14676. AES_BLOCK_SIZE), 0);
  14677. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14678. AES_BLOCK_SIZE), 0);
  14679. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14680. /* Finalize and check tag matches. */
  14681. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14682. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14683. /* Set key and IV through streaming init API. */
  14684. wc_AesFree(aesEnc);
  14685. wc_AesFree(aesDec);
  14686. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14687. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14688. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14689. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14690. /* No data to encrypt/decrypt one byte of AAD. */
  14691. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14692. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14693. /* Finalize and check tag matches. */
  14694. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14695. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14696. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14697. /* Set key and IV through streaming init API. */
  14698. wc_AesFree(aesEnc);
  14699. wc_AesFree(aesDec);
  14700. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14701. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14702. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14703. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14704. /* Encrypt/decrypt one byte and no AAD. */
  14705. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14706. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14707. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14708. /* Finalize and check tag matches. */
  14709. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14710. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14711. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14712. /* Set key and IV through streaming init API. */
  14713. wc_AesFree(aesEnc);
  14714. wc_AesFree(aesDec);
  14715. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14716. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14717. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14718. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14719. /* Encryption AES is one byte at a time */
  14720. for (i = 0; i < (int)sizeof(aad); i++) {
  14721. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14722. 0);
  14723. }
  14724. for (i = 0; i < (int)sizeof(in); i++) {
  14725. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14726. 0);
  14727. }
  14728. /* Decryption AES is two bytes at a time */
  14729. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14730. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14731. 0);
  14732. }
  14733. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14734. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14735. 0), 0);
  14736. }
  14737. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14738. /* Finalize and check tag matches. */
  14739. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14740. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14741. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14742. /* Check streaming encryption can be decrypted with one shot. */
  14743. wc_AesFree(aesDec);
  14744. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14745. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14746. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14747. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14748. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14749. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14750. wc_AesFree(aesEnc);
  14751. wc_AesFree(aesDec);
  14752. wc_FreeRng(rng);
  14753. #endif
  14754. return EXPECT_RESULT();
  14755. } /* END test_wc_AesGcmStream */
  14756. /*
  14757. * Testing streaming SM4 API.
  14758. */
  14759. static int test_wc_Sm4(void)
  14760. {
  14761. int res = TEST_SKIPPED;
  14762. #ifdef WOLFSSL_SM4
  14763. EXPECT_DECLS;
  14764. wc_Sm4 sm4;
  14765. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14766. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14767. unsigned char key[SM4_KEY_SIZE];
  14768. #endif
  14769. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14770. unsigned char iv[SM4_IV_SIZE];
  14771. #endif
  14772. /* Invalid parameters - wc_Sm4Init */
  14773. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14774. /* Valid cases - wc_Sm4Init */
  14775. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14776. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14777. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14778. XMEMSET(key, 0, sizeof(key));
  14779. /* Invalid parameters - wc_Sm4SetKey. */
  14780. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14781. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14782. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14783. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14784. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14785. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14786. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14787. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14788. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14789. /* Valid cases - wc_Sm4SetKey. */
  14790. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14791. #endif
  14792. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14793. XMEMSET(iv, 0, sizeof(iv));
  14794. /* Invalid parameters - wc_Sm4SetIV. */
  14795. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14796. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14797. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14798. /* Valid cases - wc_Sm4SetIV. */
  14799. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14800. #endif
  14801. /* Valid cases - wc_Sm4Free */
  14802. wc_Sm4Free(NULL);
  14803. wc_Sm4Free(&sm4);
  14804. res = EXPECT_RESULT();
  14805. #endif
  14806. return res;
  14807. } /* END test_wc_Sm4 */
  14808. /*
  14809. * Testing block based SM4-ECB API.
  14810. */
  14811. static int test_wc_Sm4Ecb(void)
  14812. {
  14813. int res = TEST_SKIPPED;
  14814. #ifdef WOLFSSL_SM4_ECB
  14815. EXPECT_DECLS;
  14816. wc_Sm4 sm4;
  14817. unsigned char key[SM4_KEY_SIZE];
  14818. unsigned char in[SM4_BLOCK_SIZE * 2];
  14819. unsigned char out[SM4_BLOCK_SIZE * 2];
  14820. unsigned char out2[SM4_BLOCK_SIZE];
  14821. XMEMSET(key, 0, sizeof(key));
  14822. XMEMSET(in, 0, sizeof(in));
  14823. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14824. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14825. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14826. /* Tested in test_wc_Sm4. */
  14827. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14828. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14829. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14830. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14831. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14832. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14833. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14834. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14835. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14836. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14837. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14838. /* Valid cases - wc_Sm4EcbEncrypt. */
  14839. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14840. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14841. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14842. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14843. /* In and out are same pointer. */
  14844. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14845. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14846. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14847. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14848. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14849. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14850. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14851. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14852. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14853. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14854. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14855. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14856. /* Valid cases - wc_Sm4EcbDecrypt. */
  14857. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14858. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14859. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14860. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14861. /* In and out are same pointer. */
  14862. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14863. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14864. wc_Sm4Free(&sm4);
  14865. res = EXPECT_RESULT();
  14866. #endif
  14867. return res;
  14868. } /* END test_wc_Sm4Ecb */
  14869. /*
  14870. * Testing block based SM4-CBC API.
  14871. */
  14872. static int test_wc_Sm4Cbc(void)
  14873. {
  14874. int res = TEST_SKIPPED;
  14875. #ifdef WOLFSSL_SM4_CBC
  14876. EXPECT_DECLS;
  14877. wc_Sm4 sm4;
  14878. unsigned char key[SM4_KEY_SIZE];
  14879. unsigned char iv[SM4_IV_SIZE];
  14880. unsigned char in[SM4_BLOCK_SIZE * 2];
  14881. unsigned char out[SM4_BLOCK_SIZE * 2];
  14882. unsigned char out2[SM4_BLOCK_SIZE];
  14883. XMEMSET(key, 0, sizeof(key));
  14884. XMEMSET(iv, 0, sizeof(iv));
  14885. XMEMSET(in, 0, sizeof(in));
  14886. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14887. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14888. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14889. /* Tested in test_wc_Sm4. */
  14890. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14891. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14892. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14893. /* Tested in test_wc_Sm4. */
  14894. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14895. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14896. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14897. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14898. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14899. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14901. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14902. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14903. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14904. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14905. /* Valid cases - wc_Sm4CbcEncrypt. */
  14906. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14907. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14908. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14909. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14910. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14911. /* In and out are same pointer. */
  14912. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14913. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14914. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14915. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14916. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14918. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14919. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14920. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14921. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14922. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14923. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14924. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14926. /* Valid cases - wc_Sm4CbcDecrypt. */
  14927. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14928. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14929. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14930. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14931. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14932. /* In and out are same pointer. */
  14933. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14934. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14935. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14936. wc_Sm4Free(&sm4);
  14937. res = EXPECT_RESULT();
  14938. #endif
  14939. return res;
  14940. } /* END test_wc_Sm4Cbc */
  14941. /*
  14942. * Testing streaming SM4-CTR API.
  14943. */
  14944. static int test_wc_Sm4Ctr(void)
  14945. {
  14946. int res = TEST_SKIPPED;
  14947. #ifdef WOLFSSL_SM4_CTR
  14948. EXPECT_DECLS;
  14949. wc_Sm4 sm4;
  14950. unsigned char key[SM4_KEY_SIZE];
  14951. unsigned char iv[SM4_IV_SIZE];
  14952. unsigned char in[SM4_BLOCK_SIZE * 4];
  14953. unsigned char out[SM4_BLOCK_SIZE * 4];
  14954. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14955. word32 chunk;
  14956. word32 i;
  14957. XMEMSET(key, 0, sizeof(key));
  14958. XMEMSET(iv, 0, sizeof(iv));
  14959. XMEMSET(in, 0, sizeof(in));
  14960. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14961. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14962. /* Tested in test_wc_Sm4. */
  14963. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14964. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14965. /* Tested in test_wc_Sm4. */
  14966. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14967. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14968. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14969. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14970. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14971. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14972. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14973. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14974. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14975. /* Valid cases - wc_Sm4CtrEncrypt. */
  14976. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14977. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14978. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14979. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14980. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14981. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14982. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14983. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14984. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14985. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14986. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14987. /* In and out are same pointer. Also check encrypt of cipher text produces
  14988. * plaintext.
  14989. */
  14990. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14991. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14992. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14993. /* Chunking tests. */
  14994. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14995. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14996. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14997. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14998. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14999. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15000. }
  15001. if (i < (word32)sizeof(in)) {
  15002. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15003. (word32)sizeof(in) - i), 0);
  15004. }
  15005. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15006. }
  15007. for (i = 0; i < (word32)sizeof(iv); i++) {
  15008. iv[i] = 0xff;
  15009. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15010. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15011. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15012. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15013. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15014. }
  15015. wc_Sm4Free(&sm4);
  15016. res = EXPECT_RESULT();
  15017. #endif
  15018. return res;
  15019. } /* END test_wc_Sm4Ctr */
  15020. /*
  15021. * Testing stream SM4-GCM API.
  15022. */
  15023. static int test_wc_Sm4Gcm(void)
  15024. {
  15025. int res = TEST_SKIPPED;
  15026. #ifdef WOLFSSL_SM4_GCM
  15027. EXPECT_DECLS;
  15028. wc_Sm4 sm4;
  15029. unsigned char key[SM4_KEY_SIZE];
  15030. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15031. unsigned char in[SM4_BLOCK_SIZE * 2];
  15032. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15033. unsigned char out[SM4_BLOCK_SIZE * 2];
  15034. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15035. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15036. unsigned char tag[SM4_BLOCK_SIZE];
  15037. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15038. word32 i;
  15039. XMEMSET(key, 0, sizeof(key));
  15040. XMEMSET(nonce, 0, sizeof(nonce));
  15041. XMEMSET(in, 0, sizeof(in));
  15042. XMEMSET(in2, 0, sizeof(in2));
  15043. XMEMSET(aad, 0, sizeof(aad));
  15044. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15045. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15046. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15047. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15048. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15049. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15050. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15051. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15052. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15053. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15054. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15055. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15056. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15057. /* Valid parameters - wc_Sm4GcmSetKey. */
  15058. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15059. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15060. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15061. 0), BAD_FUNC_ARG);
  15062. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15063. 0), BAD_FUNC_ARG);
  15064. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15065. 0), BAD_FUNC_ARG);
  15066. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15067. 0), BAD_FUNC_ARG);
  15068. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15069. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15070. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15071. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15072. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15073. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15074. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15075. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15077. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15078. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15079. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15080. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15081. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15082. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15083. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15084. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15085. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15086. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15087. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15088. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15089. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15090. 0), BAD_FUNC_ARG);
  15091. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15092. 0), BAD_FUNC_ARG);
  15093. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15094. 0), BAD_FUNC_ARG);
  15095. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15096. 0), BAD_FUNC_ARG);
  15097. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15098. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15099. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15100. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15101. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15102. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15103. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15104. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15105. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15106. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15107. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15108. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15109. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15110. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15111. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15112. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15113. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15114. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15115. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15116. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15117. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15118. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15119. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15120. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15121. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15122. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15123. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15124. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15125. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15126. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15127. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15128. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15129. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15130. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15131. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15132. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15133. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15134. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15135. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15136. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15137. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15138. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15139. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15140. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15141. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15142. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15143. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15144. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15145. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15146. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15147. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15148. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15149. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15150. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15151. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15152. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15153. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15154. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15155. SM4_GCM_AUTH_E);
  15156. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15157. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15158. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15159. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15160. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15161. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15162. }
  15163. /* Check different in/out sizes. */
  15164. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15165. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15166. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15167. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15168. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15169. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15170. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15171. XMEMCPY(out2, out, i - 1);
  15172. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15173. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15174. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15175. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15176. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15177. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15178. }
  15179. /* Force the counter to roll over in first byte. */
  15180. {
  15181. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15182. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15183. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15184. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15185. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15186. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15187. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15188. }
  15189. wc_Sm4Free(&sm4);
  15190. res = EXPECT_RESULT();
  15191. #endif
  15192. return res;
  15193. } /* END test_wc_Sm4Gcm */
  15194. /*
  15195. * Testing stream SM4-CCM API.
  15196. */
  15197. static int test_wc_Sm4Ccm(void)
  15198. {
  15199. int res = TEST_SKIPPED;
  15200. #ifdef WOLFSSL_SM4_CCM
  15201. EXPECT_DECLS;
  15202. wc_Sm4 sm4;
  15203. unsigned char key[SM4_KEY_SIZE];
  15204. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15205. unsigned char in[SM4_BLOCK_SIZE * 2];
  15206. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15207. unsigned char out[SM4_BLOCK_SIZE * 2];
  15208. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15209. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15210. unsigned char tag[SM4_BLOCK_SIZE];
  15211. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15212. word32 i;
  15213. XMEMSET(key, 0, sizeof(key));
  15214. XMEMSET(nonce, 0, sizeof(nonce));
  15215. XMEMSET(in, 0, sizeof(in));
  15216. XMEMSET(in2, 0, sizeof(in2));
  15217. XMEMSET(aad, 0, sizeof(aad));
  15218. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15219. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15220. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15221. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15222. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15223. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15224. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15225. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15226. 0), BAD_FUNC_ARG);
  15227. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15228. 0), BAD_FUNC_ARG);
  15229. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15230. 0), BAD_FUNC_ARG);
  15231. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15232. 0), BAD_FUNC_ARG);
  15233. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15234. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15235. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15236. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15237. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15238. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15239. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15240. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15241. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15242. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15243. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15244. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15245. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15246. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15247. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15248. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15249. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15250. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15251. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15252. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15253. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15254. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15255. 0), BAD_FUNC_ARG);
  15256. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15257. 0), BAD_FUNC_ARG);
  15258. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15259. 0), BAD_FUNC_ARG);
  15260. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15261. 0), BAD_FUNC_ARG);
  15262. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15263. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15264. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15265. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15266. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15267. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15268. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15269. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15270. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15271. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15272. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15273. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15274. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15275. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15276. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15277. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15278. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15279. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15280. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15281. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15282. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15283. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15284. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15285. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15286. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15287. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15288. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15289. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15290. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15291. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15292. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15293. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15294. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15295. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15296. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15297. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15298. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15299. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15300. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15301. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15302. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15303. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15304. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15305. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15306. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15307. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15308. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15309. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15310. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15311. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15312. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15313. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15314. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15315. }
  15316. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15317. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15318. SM4_CCM_AUTH_E);
  15319. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15320. for (i = 0; i < 4; i++) {
  15321. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15322. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15323. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15324. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15325. }
  15326. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15327. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15328. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15329. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15330. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15331. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15332. }
  15333. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15334. * Even values in range 4..SM4_BLOCK_SIZE.
  15335. */
  15336. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15337. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15338. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15339. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15340. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15341. }
  15342. /* Check different in/out sizes. */
  15343. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15344. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15345. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15346. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15347. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15348. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15349. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15350. XMEMCPY(out2, out, i - 1);
  15351. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15352. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15353. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15354. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15355. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15356. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15357. }
  15358. /* Force the counter to roll over in first byte. */
  15359. {
  15360. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15361. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15362. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15363. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15364. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15365. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15366. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15367. }
  15368. wc_Sm4Free(&sm4);
  15369. res = EXPECT_RESULT();
  15370. #endif
  15371. return res;
  15372. } /* END test_wc_Sm4Ccm */
  15373. /*
  15374. * unit test for wc_Des3_SetIV()
  15375. */
  15376. static int test_wc_Des3_SetIV(void)
  15377. {
  15378. EXPECT_DECLS;
  15379. #ifndef NO_DES3
  15380. Des3 des;
  15381. const byte key[] = {
  15382. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15383. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15384. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15385. };
  15386. const byte iv[] = {
  15387. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15388. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15389. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15390. };
  15391. XMEMSET(&des, 0, sizeof(Des3));
  15392. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15393. /* DES_ENCRYPTION or DES_DECRYPTION */
  15394. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15395. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15396. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15397. /* Test explicitly wc_Des3_SetIV() */
  15398. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15399. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15400. #endif
  15401. wc_Des3Free(&des);
  15402. #endif
  15403. return EXPECT_RESULT();
  15404. } /* END test_wc_Des3_SetIV */
  15405. /*
  15406. * unit test for wc_Des3_SetKey()
  15407. */
  15408. static int test_wc_Des3_SetKey(void)
  15409. {
  15410. EXPECT_DECLS;
  15411. #ifndef NO_DES3
  15412. Des3 des;
  15413. const byte key[] = {
  15414. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15415. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15416. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15417. };
  15418. const byte iv[] = {
  15419. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15420. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15421. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15422. };
  15423. XMEMSET(&des, 0, sizeof(Des3));
  15424. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15425. /* DES_ENCRYPTION or DES_DECRYPTION */
  15426. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15427. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15428. /* Test bad args. */
  15429. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15430. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15431. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15432. /* Default case. Should return 0. */
  15433. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15434. wc_Des3Free(&des);
  15435. #endif
  15436. return EXPECT_RESULT();
  15437. } /* END test_wc_Des3_SetKey */
  15438. /*
  15439. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15440. */
  15441. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15442. {
  15443. EXPECT_DECLS;
  15444. #ifndef NO_DES3
  15445. Des3 des;
  15446. byte cipher[24];
  15447. byte plain[24];
  15448. const byte key[] = {
  15449. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15450. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15451. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15452. };
  15453. const byte iv[] = {
  15454. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15455. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15456. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15457. };
  15458. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15459. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15460. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15461. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15462. };
  15463. XMEMSET(&des, 0, sizeof(Des3));
  15464. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15465. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15466. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15467. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15468. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15469. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15470. /* Pass in bad args. */
  15471. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15472. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15473. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15474. BAD_FUNC_ARG);
  15475. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15476. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15477. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15478. wc_Des3Free(&des);
  15479. #endif
  15480. return EXPECT_RESULT();
  15481. } /* END wc_Des3_CbcEncrypt */
  15482. /*
  15483. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15484. */
  15485. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15486. {
  15487. EXPECT_DECLS;
  15488. #ifndef NO_DES3
  15489. word32 vectorSz, cipherSz;
  15490. byte cipher[24];
  15491. byte plain[24];
  15492. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15493. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15494. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15495. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15496. };
  15497. byte key[] = {
  15498. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15499. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15500. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15501. };
  15502. byte iv[] = {
  15503. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15504. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15505. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15506. };
  15507. vectorSz = sizeof(byte) * 24;
  15508. cipherSz = sizeof(byte) * 24;
  15509. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15510. 0);
  15511. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15512. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15513. /* pass in bad args. */
  15514. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15515. BAD_FUNC_ARG);
  15516. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15517. BAD_FUNC_ARG);
  15518. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15519. BAD_FUNC_ARG);
  15520. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15521. 0);
  15522. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15523. BAD_FUNC_ARG);
  15524. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15525. BAD_FUNC_ARG);
  15526. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15527. BAD_FUNC_ARG);
  15528. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15529. 0);
  15530. #endif
  15531. return EXPECT_RESULT();
  15532. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15533. /*
  15534. * Unit test for wc_Des3_EcbEncrypt
  15535. */
  15536. static int test_wc_Des3_EcbEncrypt(void)
  15537. {
  15538. EXPECT_DECLS;
  15539. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15540. Des3 des;
  15541. byte cipher[24];
  15542. word32 cipherSz = sizeof(cipher);
  15543. const byte key[] = {
  15544. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15545. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15546. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15547. };
  15548. const byte iv[] = {
  15549. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15550. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15551. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15552. };
  15553. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15554. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15555. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15556. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15557. };
  15558. XMEMSET(&des, 0, sizeof(Des3));
  15559. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15560. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15561. /* Bad Cases */
  15562. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15563. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15564. BAD_FUNC_ARG);
  15565. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15566. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15567. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15568. /* Good Cases */
  15569. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15570. wc_Des3Free(&des);
  15571. #endif
  15572. return EXPECT_RESULT();
  15573. } /* END test_wc_Des3_EcbEncrypt */
  15574. /*
  15575. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15576. */
  15577. static int test_wc_Chacha_SetKey(void)
  15578. {
  15579. EXPECT_DECLS;
  15580. #ifdef HAVE_CHACHA
  15581. ChaCha ctx;
  15582. const byte key[] = {
  15583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15585. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15586. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15587. };
  15588. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15589. byte cipher[128];
  15590. XMEMSET(cipher, 0, sizeof(cipher));
  15591. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15592. /* Test bad args. */
  15593. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15594. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15595. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15596. /* Test bad args. */
  15597. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15598. #endif
  15599. return EXPECT_RESULT();
  15600. } /* END test_wc_Chacha_SetKey */
  15601. /*
  15602. * unit test for wc_Poly1305SetKey()
  15603. */
  15604. static int test_wc_Poly1305SetKey(void)
  15605. {
  15606. EXPECT_DECLS;
  15607. #ifdef HAVE_POLY1305
  15608. Poly1305 ctx;
  15609. const byte key[] =
  15610. {
  15611. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15615. };
  15616. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15617. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15618. /* Test bad args. */
  15619. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15620. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15621. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15622. #endif
  15623. return EXPECT_RESULT();
  15624. } /* END test_wc_Poly1305_SetKey() */
  15625. /*
  15626. * Testing wc_Chacha_Process()
  15627. */
  15628. static int test_wc_Chacha_Process(void)
  15629. {
  15630. EXPECT_DECLS;
  15631. #ifdef HAVE_CHACHA
  15632. ChaCha enc, dec;
  15633. byte cipher[128];
  15634. byte plain[128];
  15635. const byte key[] =
  15636. {
  15637. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15638. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15639. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15640. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15641. };
  15642. const char* input = "Everybody gets Friday off.";
  15643. word32 keySz = sizeof(key)/sizeof(byte);
  15644. unsigned long int inlen = XSTRLEN(input);
  15645. /* Initialize stack variables. */
  15646. XMEMSET(cipher, 0, 128);
  15647. XMEMSET(plain, 0, 128);
  15648. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15649. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15650. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15651. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15652. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15653. 0);
  15654. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15655. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15656. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15657. /* test checking and using leftovers, currently just in C code */
  15658. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15659. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15660. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15661. (word32)inlen - 2), 0);
  15662. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15663. (byte*)input + (inlen - 2), 2), 0);
  15664. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15665. (word32)inlen - 2), 0);
  15666. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15667. (byte*)input + (inlen - 2), 2), 0);
  15668. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15669. /* check edge cases with counter increment */
  15670. {
  15671. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15672. const byte expected[] = {
  15673. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15674. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15675. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15676. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15677. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15678. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15679. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15680. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15681. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15682. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15683. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15684. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15685. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15686. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15687. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15688. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15689. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15690. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15691. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15692. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15693. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15694. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15695. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15696. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15697. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15698. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15699. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15700. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15701. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15702. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15703. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15704. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15705. };
  15706. const byte iv2[] = {
  15707. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15708. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15709. };
  15710. byte input2[256];
  15711. int i;
  15712. for (i = 0; i < 256; i++)
  15713. input2[i] = i;
  15714. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15715. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15716. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15717. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15718. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15719. /* partial */
  15720. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15721. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15722. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15723. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15724. }
  15725. #endif
  15726. /* Test bad args. */
  15727. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15728. BAD_FUNC_ARG);
  15729. #endif
  15730. return EXPECT_RESULT();
  15731. } /* END test_wc_Chacha_Process */
  15732. /*
  15733. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15734. */
  15735. static int test_wc_ChaCha20Poly1305_aead(void)
  15736. {
  15737. EXPECT_DECLS;
  15738. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15739. const byte key[] = {
  15740. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15741. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15742. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15743. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15744. };
  15745. const byte plaintext[] = {
  15746. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15747. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15748. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15749. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15750. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15751. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15752. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15753. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15754. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15755. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15756. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15757. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15758. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15759. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15760. 0x74, 0x2e
  15761. };
  15762. const byte iv[] = {
  15763. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15764. 0x44, 0x45, 0x46, 0x47
  15765. };
  15766. const byte aad[] = { /* additional data */
  15767. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15768. 0xc4, 0xc5, 0xc6, 0xc7
  15769. };
  15770. const byte cipher[] = { /* expected output from operation */
  15771. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15772. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15773. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15774. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15775. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15776. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15777. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15778. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15779. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15780. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15781. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15782. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15783. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15784. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15785. 0x61, 0x16
  15786. };
  15787. const byte authTag[] = { /* expected output from operation */
  15788. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15789. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15790. };
  15791. byte generatedCiphertext[272];
  15792. byte generatedPlaintext[272];
  15793. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15794. /* Initialize stack variables. */
  15795. XMEMSET(generatedCiphertext, 0, 272);
  15796. XMEMSET(generatedPlaintext, 0, 272);
  15797. /* Test Encrypt */
  15798. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15799. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15800. 0);
  15801. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15802. sizeof(cipher)/sizeof(byte)), 0);
  15803. /* Test bad args. */
  15804. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15805. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15806. BAD_FUNC_ARG);
  15807. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15808. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15809. BAD_FUNC_ARG);
  15810. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15811. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15812. BAD_FUNC_ARG);
  15813. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15814. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15815. BAD_FUNC_ARG);
  15816. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15817. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15818. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15819. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15820. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15821. sizeof(cipher), authTag, generatedPlaintext), 0);
  15822. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15823. sizeof(plaintext)/sizeof(byte)), 0);
  15824. /* Test bad args. */
  15825. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15826. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15827. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15828. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15829. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15830. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15831. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15832. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15833. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15834. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15835. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15836. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15837. #endif
  15838. return EXPECT_RESULT();
  15839. } /* END test_wc_ChaCha20Poly1305_aead */
  15840. /*
  15841. * Testing function for wc_Rc2SetKey().
  15842. */
  15843. static int test_wc_Rc2SetKey(void)
  15844. {
  15845. EXPECT_DECLS;
  15846. #ifdef WC_RC2
  15847. Rc2 rc2;
  15848. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15849. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15850. /* valid key and IV */
  15851. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15852. iv, 40), 0);
  15853. /* valid key, no IV */
  15854. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15855. NULL, 40), 0);
  15856. /* bad arguments */
  15857. /* null Rc2 struct */
  15858. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15859. iv, 40), BAD_FUNC_ARG);
  15860. /* null key */
  15861. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15862. iv, 40), BAD_FUNC_ARG);
  15863. /* key size == 0 */
  15864. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15865. /* key size > 128 */
  15866. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15867. /* effective bits == 0 */
  15868. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15869. iv, 0), WC_KEY_SIZE_E);
  15870. /* effective bits > 1024 */
  15871. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15872. iv, 1025), WC_KEY_SIZE_E);
  15873. #endif
  15874. return EXPECT_RESULT();
  15875. } /* END test_wc_Rc2SetKey */
  15876. /*
  15877. * Testing function for wc_Rc2SetIV().
  15878. */
  15879. static int test_wc_Rc2SetIV(void)
  15880. {
  15881. EXPECT_DECLS;
  15882. #ifdef WC_RC2
  15883. Rc2 rc2;
  15884. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15885. /* valid IV */
  15886. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15887. /* valid NULL IV */
  15888. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15889. /* bad arguments */
  15890. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15891. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15892. #endif
  15893. return EXPECT_RESULT();
  15894. } /* END test_wc_Rc2SetIV */
  15895. /*
  15896. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15897. */
  15898. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15899. {
  15900. EXPECT_DECLS;
  15901. #ifdef WC_RC2
  15902. Rc2 rc2;
  15903. int effectiveKeyBits = 63;
  15904. byte cipher[RC2_BLOCK_SIZE];
  15905. byte plain[RC2_BLOCK_SIZE];
  15906. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15907. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15908. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15909. XMEMSET(cipher, 0, sizeof(cipher));
  15910. XMEMSET(plain, 0, sizeof(plain));
  15911. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15912. NULL, effectiveKeyBits), 0);
  15913. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15914. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15915. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15916. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15917. /* Rc2EcbEncrypt bad arguments */
  15918. /* null Rc2 struct */
  15919. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15920. BAD_FUNC_ARG);
  15921. /* null out buffer */
  15922. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15923. BAD_FUNC_ARG);
  15924. /* null input buffer */
  15925. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15926. BAD_FUNC_ARG);
  15927. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15928. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15929. /* Rc2EcbDecrypt bad arguments */
  15930. /* null Rc2 struct */
  15931. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15932. BAD_FUNC_ARG);
  15933. /* null out buffer */
  15934. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15935. BAD_FUNC_ARG);
  15936. /* null input buffer */
  15937. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15938. BAD_FUNC_ARG);
  15939. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15940. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15941. #endif
  15942. return EXPECT_RESULT();
  15943. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15944. /*
  15945. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15946. */
  15947. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15948. {
  15949. EXPECT_DECLS;
  15950. #ifdef WC_RC2
  15951. Rc2 rc2;
  15952. int effectiveKeyBits = 63;
  15953. byte cipher[RC2_BLOCK_SIZE*2];
  15954. byte plain[RC2_BLOCK_SIZE*2];
  15955. /* vector taken from test.c */
  15956. byte key[] = {
  15957. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15958. };
  15959. byte iv[] = {
  15960. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15961. };
  15962. byte input[] = {
  15963. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15964. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15965. };
  15966. byte output[] = {
  15967. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15968. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15969. };
  15970. XMEMSET(cipher, 0, sizeof(cipher));
  15971. XMEMSET(plain, 0, sizeof(plain));
  15972. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15973. iv, effectiveKeyBits), 0);
  15974. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15975. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15976. /* reset IV for decrypt */
  15977. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15978. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15979. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15980. /* Rc2CbcEncrypt bad arguments */
  15981. /* null Rc2 struct */
  15982. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15983. BAD_FUNC_ARG);
  15984. /* null out buffer */
  15985. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15986. BAD_FUNC_ARG);
  15987. /* null input buffer */
  15988. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15989. BAD_FUNC_ARG);
  15990. /* Rc2CbcDecrypt bad arguments */
  15991. /* in size is 0 */
  15992. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15993. /* null Rc2 struct */
  15994. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15995. BAD_FUNC_ARG);
  15996. /* null out buffer */
  15997. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15998. BAD_FUNC_ARG);
  15999. /* null input buffer */
  16000. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16001. BAD_FUNC_ARG);
  16002. #endif
  16003. return EXPECT_RESULT();
  16004. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16005. /*
  16006. * Testing function for wc_AesSetIV
  16007. */
  16008. static int test_wc_AesSetIV(void)
  16009. {
  16010. int res = TEST_SKIPPED;
  16011. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16012. Aes aes;
  16013. int ret = 0;
  16014. byte key16[] =
  16015. {
  16016. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16017. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16018. };
  16019. byte iv1[] = "1234567890abcdef";
  16020. byte iv2[] = "0987654321fedcba";
  16021. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16022. if (ret != 0)
  16023. return ret;
  16024. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16025. iv1, AES_ENCRYPTION);
  16026. if (ret == 0) {
  16027. ret = wc_AesSetIV(&aes, iv2);
  16028. }
  16029. /* Test bad args. */
  16030. if (ret == 0) {
  16031. ret = wc_AesSetIV(NULL, iv1);
  16032. if (ret == BAD_FUNC_ARG) {
  16033. /* NULL iv should return 0. */
  16034. ret = wc_AesSetIV(&aes, NULL);
  16035. }
  16036. else {
  16037. ret = WOLFSSL_FATAL_ERROR;
  16038. }
  16039. }
  16040. wc_AesFree(&aes);
  16041. res = TEST_RES_CHECK(ret == 0);
  16042. #endif
  16043. return res;
  16044. } /* test_wc_AesSetIV */
  16045. /*
  16046. * Testing function for wc_AesSetKey().
  16047. */
  16048. static int test_wc_AesSetKey(void)
  16049. {
  16050. EXPECT_DECLS;
  16051. #ifndef NO_AES
  16052. Aes aes;
  16053. byte key16[] = {
  16054. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16055. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16056. };
  16057. #ifdef WOLFSSL_AES_192
  16058. byte key24[] = {
  16059. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16060. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16061. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16062. };
  16063. #endif
  16064. #ifdef WOLFSSL_AES_256
  16065. byte key32[] = {
  16066. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16067. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16068. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16069. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16070. };
  16071. #endif
  16072. byte badKey16[] = {
  16073. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16074. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16075. };
  16076. byte iv[] = "1234567890abcdef";
  16077. XMEMSET(&aes, 0, sizeof(Aes));
  16078. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16079. #ifdef WOLFSSL_AES_128
  16080. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16081. iv, AES_ENCRYPTION), 0);
  16082. #endif
  16083. #ifdef WOLFSSL_AES_192
  16084. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16085. iv, AES_ENCRYPTION), 0);
  16086. #endif
  16087. #ifdef WOLFSSL_AES_256
  16088. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16089. iv, AES_ENCRYPTION), 0);
  16090. #endif
  16091. /* Pass in bad args. */
  16092. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16093. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16094. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16095. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16096. BAD_FUNC_ARG);
  16097. wc_AesFree(&aes);
  16098. #endif
  16099. return EXPECT_RESULT();
  16100. } /* END test_wc_AesSetKey */
  16101. /*
  16102. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16103. * and wc_AesCbcDecryptWithKey()
  16104. */
  16105. static int test_wc_AesCbcEncryptDecrypt(void)
  16106. {
  16107. EXPECT_DECLS;
  16108. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16109. defined(WOLFSSL_AES_256)
  16110. Aes aes;
  16111. byte key32[] = {
  16112. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16113. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16114. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16115. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16116. };
  16117. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16118. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16119. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16120. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16121. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16122. };
  16123. byte iv[] = "1234567890abcdef";
  16124. byte enc[sizeof(vector)];
  16125. byte dec[sizeof(vector)];
  16126. byte dec2[sizeof(vector)];
  16127. /* Init stack variables. */
  16128. XMEMSET(&aes, 0, sizeof(Aes));
  16129. XMEMSET(enc, 0, sizeof(enc));
  16130. XMEMSET(dec, 0, sizeof(vector));
  16131. XMEMSET(dec2, 0, sizeof(vector));
  16132. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16133. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16134. AES_ENCRYPTION), 0);
  16135. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16136. /* Re init for decrypt and set flag. */
  16137. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16138. AES_DECRYPTION), 0);
  16139. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16140. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16141. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16142. sizeof(key32)/sizeof(byte), iv), 0);
  16143. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16144. /* Pass in bad args */
  16145. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16146. BAD_FUNC_ARG);
  16147. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16148. BAD_FUNC_ARG);
  16149. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16150. BAD_FUNC_ARG);
  16151. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16152. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16153. BAD_LENGTH_E);
  16154. #endif
  16155. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16156. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16157. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16158. "mode (v2), skip test");
  16159. #else
  16160. /* Test passing in size of 0 */
  16161. XMEMSET(enc, 0, sizeof(enc));
  16162. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16163. /* Check enc was not modified */
  16164. {
  16165. int i;
  16166. for (i = 0; i < (int)sizeof(enc); i++)
  16167. ExpectIntEQ(enc[i], 0);
  16168. }
  16169. #endif
  16170. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16171. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16172. BAD_FUNC_ARG);
  16173. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16174. BAD_FUNC_ARG);
  16175. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16176. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16177. BAD_LENGTH_E);
  16178. #else
  16179. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16180. BAD_FUNC_ARG);
  16181. #endif
  16182. /* Test passing in size of 0 */
  16183. XMEMSET(dec, 0, sizeof(dec));
  16184. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16185. /* Check dec was not modified */
  16186. {
  16187. int i;
  16188. for (i = 0; i < (int)sizeof(dec); i++)
  16189. ExpectIntEQ(dec[i], 0);
  16190. }
  16191. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16192. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16193. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16194. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16195. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16196. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16197. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16198. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16199. wc_AesFree(&aes);
  16200. #endif
  16201. return EXPECT_RESULT();
  16202. } /* END test_wc_AesCbcEncryptDecrypt */
  16203. /*
  16204. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16205. */
  16206. static int test_wc_AesCtrEncryptDecrypt(void)
  16207. {
  16208. EXPECT_DECLS;
  16209. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16210. Aes aesEnc;
  16211. Aes aesDec;
  16212. byte key32[] = {
  16213. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16214. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16215. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16216. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16217. };
  16218. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16219. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16220. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16221. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16222. };
  16223. byte iv[] = "1234567890abcdef";
  16224. byte enc[AES_BLOCK_SIZE * 2];
  16225. byte dec[AES_BLOCK_SIZE * 2];
  16226. /* Init stack variables. */
  16227. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16228. XMEMSET(&aesDec, 0, sizeof(Aes));
  16229. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16230. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16231. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16232. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16233. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16234. AES_ENCRYPTION), 0);
  16235. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16236. sizeof(vector)/sizeof(byte)), 0);
  16237. /* Decrypt with wc_AesCtrEncrypt() */
  16238. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16239. AES_ENCRYPTION), 0);
  16240. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16241. 0);
  16242. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16243. /* Test bad args. */
  16244. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16245. BAD_FUNC_ARG);
  16246. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16247. BAD_FUNC_ARG);
  16248. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16249. BAD_FUNC_ARG);
  16250. wc_AesFree(&aesEnc);
  16251. wc_AesFree(&aesDec);
  16252. #endif
  16253. return EXPECT_RESULT();
  16254. } /* END test_wc_AesCtrEncryptDecrypt */
  16255. /*
  16256. * test function for wc_AesGcmSetKey()
  16257. */
  16258. static int test_wc_AesGcmSetKey(void)
  16259. {
  16260. EXPECT_DECLS;
  16261. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16262. Aes aes;
  16263. #ifdef WOLFSSL_AES_128
  16264. byte key16[] = {
  16265. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16267. };
  16268. #endif
  16269. #ifdef WOLFSSL_AES_192
  16270. byte key24[] = {
  16271. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16272. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16273. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16274. };
  16275. #endif
  16276. #ifdef WOLFSSL_AES_256
  16277. byte key32[] = {
  16278. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16279. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16280. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16281. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16282. };
  16283. #endif
  16284. byte badKey16[] = {
  16285. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16286. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16287. };
  16288. byte badKey24[] = {
  16289. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16290. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16291. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16292. };
  16293. byte badKey32[] = {
  16294. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16295. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16296. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16297. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16298. };
  16299. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16300. #ifdef WOLFSSL_AES_128
  16301. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16302. #endif
  16303. #ifdef WOLFSSL_AES_192
  16304. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16305. #endif
  16306. #ifdef WOLFSSL_AES_256
  16307. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16308. #endif
  16309. /* Pass in bad args. */
  16310. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16311. BAD_FUNC_ARG);
  16312. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16313. BAD_FUNC_ARG);
  16314. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16315. BAD_FUNC_ARG);
  16316. wc_AesFree(&aes);
  16317. #endif
  16318. return EXPECT_RESULT();
  16319. } /* END test_wc_AesGcmSetKey */
  16320. /*
  16321. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16322. */
  16323. static int test_wc_AesGcmEncryptDecrypt(void)
  16324. {
  16325. EXPECT_DECLS;
  16326. /* WOLFSSL_AFALG requires 12 byte IV */
  16327. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16328. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16329. Aes aes;
  16330. byte key32[] = {
  16331. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16332. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16333. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16334. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16335. };
  16336. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16337. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16338. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16339. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16340. };
  16341. const byte a[] = {
  16342. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16343. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16344. 0xab, 0xad, 0xda, 0xd2
  16345. };
  16346. byte iv[] = "1234567890a";
  16347. byte longIV[] = "1234567890abcdefghij";
  16348. byte enc[sizeof(vector)];
  16349. byte resultT[AES_BLOCK_SIZE];
  16350. byte dec[sizeof(vector)];
  16351. /* Init stack variables. */
  16352. XMEMSET(&aes, 0, sizeof(Aes));
  16353. XMEMSET(enc, 0, sizeof(vector));
  16354. XMEMSET(dec, 0, sizeof(vector));
  16355. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16356. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16357. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16358. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16359. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16360. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16361. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16362. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16363. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16364. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16365. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16366. BAD_FUNC_ARG);
  16367. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16368. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16369. BAD_FUNC_ARG);
  16370. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16371. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16372. BAD_FUNC_ARG);
  16373. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16374. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16375. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16376. /* FIPS does not check the lower bound of ivSz */
  16377. #else
  16378. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16379. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16380. #endif
  16381. /* This case is now considered good. Long IVs are now allowed.
  16382. * Except for the original FIPS release, it still has an upper
  16383. * bound on the IV length. */
  16384. #if (!defined(HAVE_FIPS) || \
  16385. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16386. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16387. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16388. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16389. 0);
  16390. #else
  16391. (void)longIV;
  16392. #endif /* Old FIPS */
  16393. /* END wc_AesGcmEncrypt */
  16394. #ifdef HAVE_AES_DECRYPT
  16395. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16396. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16397. BAD_FUNC_ARG);
  16398. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16399. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16400. BAD_FUNC_ARG);
  16401. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16402. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16403. BAD_FUNC_ARG);
  16404. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16405. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16406. BAD_FUNC_ARG);
  16407. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16408. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16409. BAD_FUNC_ARG);
  16410. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16411. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16412. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16413. AES_GCM_AUTH_E);
  16414. #else
  16415. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16416. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16417. BAD_FUNC_ARG);
  16418. #endif
  16419. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16420. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16421. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16422. /* FIPS does not check the lower bound of ivSz */
  16423. #else
  16424. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16425. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16426. #endif
  16427. #endif /* HAVE_AES_DECRYPT */
  16428. wc_AesFree(&aes);
  16429. #endif
  16430. return EXPECT_RESULT();
  16431. } /* END test_wc_AesGcmEncryptDecrypt */
  16432. /*
  16433. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16434. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16435. */
  16436. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16437. {
  16438. EXPECT_DECLS;
  16439. #if (!defined(HAVE_FIPS) || \
  16440. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16441. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16442. const byte key[] = {
  16443. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16444. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16445. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16446. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16447. };
  16448. const byte in[] = {
  16449. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16450. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16451. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16452. };
  16453. const byte aad[] = {
  16454. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16455. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16456. 0xab, 0xad, 0xda, 0xd2
  16457. };
  16458. Aes aesEnc;
  16459. Aes aesDec;
  16460. byte iv[] = "1234567890abcdefghij";
  16461. byte out[sizeof(in)];
  16462. byte plain[sizeof(in)];
  16463. byte tag[AES_BLOCK_SIZE];
  16464. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16465. XMEMSET(&aesDec, 0, sizeof(Aes));
  16466. XMEMSET(out, 0, sizeof(out));
  16467. XMEMSET(plain, 0, sizeof(plain));
  16468. XMEMSET(tag, 0, sizeof(tag));
  16469. /* Perform one-shot encryption using long IV */
  16470. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16471. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16472. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16473. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16474. /* Perform streaming decryption using long IV */
  16475. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16476. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16477. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16478. sizeof(aad)), 0);
  16479. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16480. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16481. /* Free resources */
  16482. wc_AesFree(&aesEnc);
  16483. wc_AesFree(&aesDec);
  16484. #endif
  16485. return EXPECT_RESULT();
  16486. } /* END wc_AesGcmMixedEncDecLongIV */
  16487. /*
  16488. * unit test for wc_GmacSetKey()
  16489. */
  16490. static int test_wc_GmacSetKey(void)
  16491. {
  16492. EXPECT_DECLS;
  16493. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16494. Gmac gmac;
  16495. byte key16[] = {
  16496. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16497. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16498. };
  16499. #ifdef WOLFSSL_AES_192
  16500. byte key24[] = {
  16501. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16502. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16503. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16504. };
  16505. #endif
  16506. #ifdef WOLFSSL_AES_256
  16507. byte key32[] = {
  16508. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16509. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16510. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16511. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16512. };
  16513. #endif
  16514. byte badKey16[] = {
  16515. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16516. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16517. };
  16518. byte badKey24[] = {
  16519. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16520. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16521. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16522. };
  16523. byte badKey32[] = {
  16524. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16525. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16526. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16527. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16528. };
  16529. XMEMSET(&gmac, 0, sizeof(Gmac));
  16530. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16531. #ifdef WOLFSSL_AES_128
  16532. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16533. #endif
  16534. #ifdef WOLFSSL_AES_192
  16535. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16536. #endif
  16537. #ifdef WOLFSSL_AES_256
  16538. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16539. #endif
  16540. /* Pass in bad args. */
  16541. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16542. BAD_FUNC_ARG);
  16543. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16544. BAD_FUNC_ARG);
  16545. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16546. BAD_FUNC_ARG);
  16547. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16548. BAD_FUNC_ARG);
  16549. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16550. BAD_FUNC_ARG);
  16551. wc_AesFree(&gmac.aes);
  16552. #endif
  16553. return EXPECT_RESULT();
  16554. } /* END test_wc_GmacSetKey */
  16555. /*
  16556. * unit test for wc_GmacUpdate
  16557. */
  16558. static int test_wc_GmacUpdate(void)
  16559. {
  16560. EXPECT_DECLS;
  16561. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16562. Gmac gmac;
  16563. #ifdef WOLFSSL_AES_128
  16564. const byte key16[] = {
  16565. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16566. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16567. };
  16568. #endif
  16569. #ifdef WOLFSSL_AES_192
  16570. byte key24[] = {
  16571. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16572. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16573. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16574. };
  16575. #endif
  16576. #ifdef WOLFSSL_AES_256
  16577. byte key32[] = {
  16578. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16579. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16580. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16581. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16582. };
  16583. #endif
  16584. #ifdef WOLFSSL_AES_128
  16585. const byte authIn[] = {
  16586. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16587. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16588. };
  16589. #endif
  16590. #ifdef WOLFSSL_AES_192
  16591. const byte authIn2[] = {
  16592. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16593. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16594. };
  16595. #endif
  16596. const byte authIn3[] = {
  16597. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16598. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16599. };
  16600. #ifdef WOLFSSL_AES_128
  16601. const byte tag1[] = { /* Known. */
  16602. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16603. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16604. };
  16605. #endif
  16606. #ifdef WOLFSSL_AES_192
  16607. const byte tag2[] = { /* Known */
  16608. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16609. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16610. };
  16611. #endif
  16612. const byte tag3[] = { /* Known */
  16613. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16614. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16615. };
  16616. #ifdef WOLFSSL_AES_128
  16617. const byte iv[] = {
  16618. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16619. 0xe2, 0x8c, 0x8f, 0x16
  16620. };
  16621. #endif
  16622. #ifdef WOLFSSL_AES_192
  16623. const byte iv2[] = {
  16624. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16625. 0x7e, 0x1a, 0x6f, 0xbc
  16626. };
  16627. #endif
  16628. const byte iv3[] = {
  16629. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16630. 0xc3, 0xfb, 0x6c, 0x8a
  16631. };
  16632. byte tagOut[16];
  16633. byte tagOut2[24];
  16634. byte tagOut3[32];
  16635. /* Init stack variables. */
  16636. XMEMSET(&gmac, 0, sizeof(Gmac));
  16637. XMEMSET(tagOut, 0, sizeof(tagOut));
  16638. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16639. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16640. #ifdef WOLFSSL_AES_128
  16641. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16642. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16643. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16644. tagOut, sizeof(tag1)), 0);
  16645. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16646. wc_AesFree(&gmac.aes);
  16647. #endif
  16648. #ifdef WOLFSSL_AES_192
  16649. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16650. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16651. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16652. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16653. tagOut2, sizeof(tag2)), 0);
  16654. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16655. wc_AesFree(&gmac.aes);
  16656. #endif
  16657. #ifdef WOLFSSL_AES_256
  16658. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16659. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16660. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16661. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16662. tagOut3, sizeof(tag3)), 0);
  16663. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16664. wc_AesFree(&gmac.aes);
  16665. #endif
  16666. /* Pass bad args. */
  16667. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16668. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16669. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16670. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16671. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16672. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16673. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16674. wc_AesFree(&gmac.aes);
  16675. #endif
  16676. return EXPECT_RESULT();
  16677. } /* END test_wc_GmacUpdate */
  16678. /*
  16679. * testing wc_CamelliaSetKey
  16680. */
  16681. static int test_wc_CamelliaSetKey(void)
  16682. {
  16683. EXPECT_DECLS;
  16684. #ifdef HAVE_CAMELLIA
  16685. Camellia camellia;
  16686. /*128-bit key*/
  16687. static const byte key16[] = {
  16688. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16689. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16690. };
  16691. /* 192-bit key */
  16692. static const byte key24[] = {
  16693. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16694. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16695. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16696. };
  16697. /* 256-bit key */
  16698. static const byte key32[] = {
  16699. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16700. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16701. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16702. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16703. };
  16704. static const byte iv[] = {
  16705. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16706. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16707. };
  16708. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16709. 0);
  16710. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16711. NULL), 0);
  16712. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16713. 0);
  16714. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16715. NULL), 0);
  16716. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16717. 0);
  16718. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16719. NULL), 0);
  16720. /* Bad args. */
  16721. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16722. BAD_FUNC_ARG);
  16723. #endif
  16724. return EXPECT_RESULT();
  16725. } /* END test_wc_CammeliaSetKey */
  16726. /*
  16727. * Testing wc_CamelliaSetIV()
  16728. */
  16729. static int test_wc_CamelliaSetIV(void)
  16730. {
  16731. EXPECT_DECLS;
  16732. #ifdef HAVE_CAMELLIA
  16733. Camellia camellia;
  16734. static const byte iv[] = {
  16735. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16736. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16737. };
  16738. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16739. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16740. /* Bad args. */
  16741. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16742. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16743. #endif
  16744. return EXPECT_RESULT();
  16745. } /* END test_wc_CamelliaSetIV*/
  16746. /*
  16747. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16748. */
  16749. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16750. {
  16751. EXPECT_DECLS;
  16752. #ifdef HAVE_CAMELLIA
  16753. Camellia camellia;
  16754. static const byte key24[] = {
  16755. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16756. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16757. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16758. };
  16759. static const byte iv[] = {
  16760. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16761. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16762. };
  16763. static const byte plainT[] = {
  16764. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16765. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16766. };
  16767. byte enc[sizeof(plainT)];
  16768. byte dec[sizeof(enc)];
  16769. /* Init stack variables.*/
  16770. XMEMSET(enc, 0, 16);
  16771. XMEMSET(enc, 0, 16);
  16772. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16773. 0);
  16774. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16775. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16776. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16777. /* Pass bad args. */
  16778. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16779. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16780. BAD_FUNC_ARG);
  16781. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16782. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16783. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16784. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16785. #endif
  16786. return EXPECT_RESULT();
  16787. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16788. /*
  16789. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16790. */
  16791. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16792. {
  16793. EXPECT_DECLS;
  16794. #ifdef HAVE_CAMELLIA
  16795. Camellia camellia;
  16796. static const byte key24[] = {
  16797. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16798. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16799. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16800. };
  16801. static const byte plainT[] = {
  16802. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16803. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16804. };
  16805. byte enc[CAMELLIA_BLOCK_SIZE];
  16806. byte dec[CAMELLIA_BLOCK_SIZE];
  16807. /* Init stack variables. */
  16808. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16809. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16810. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16811. NULL), 0);
  16812. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16813. CAMELLIA_BLOCK_SIZE), 0);
  16814. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16815. NULL), 0);
  16816. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16817. 0);
  16818. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16819. /* Pass in bad args. */
  16820. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16821. BAD_FUNC_ARG);
  16822. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16823. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16824. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16825. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16826. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16827. BAD_FUNC_ARG);
  16828. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16829. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16830. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16831. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16832. #endif
  16833. return EXPECT_RESULT();
  16834. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16835. /*
  16836. * Testing wc_Arc4SetKey()
  16837. */
  16838. static int test_wc_Arc4SetKey(void)
  16839. {
  16840. EXPECT_DECLS;
  16841. #ifndef NO_RC4
  16842. Arc4 arc;
  16843. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16844. int keyLen = 8;
  16845. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16846. /* Test bad args. */
  16847. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16848. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16849. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16850. #endif
  16851. return EXPECT_RESULT();
  16852. } /* END test_wc_Arc4SetKey */
  16853. /*
  16854. * Testing wc_Arc4Process for ENC/DEC.
  16855. */
  16856. static int test_wc_Arc4Process(void)
  16857. {
  16858. EXPECT_DECLS;
  16859. #ifndef NO_RC4
  16860. Arc4 enc;
  16861. Arc4 dec;
  16862. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16863. int keyLen = 8;
  16864. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16865. byte cipher[8];
  16866. byte plain[8];
  16867. /* Init stack variables */
  16868. XMEMSET(&enc, 0, sizeof(Arc4));
  16869. XMEMSET(&dec, 0, sizeof(Arc4));
  16870. XMEMSET(cipher, 0, sizeof(cipher));
  16871. XMEMSET(plain, 0, sizeof(plain));
  16872. /* Use for async. */
  16873. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16874. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16875. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16876. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16877. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16878. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16879. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16880. /* Bad args. */
  16881. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16882. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16883. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16884. wc_Arc4Free(&enc);
  16885. wc_Arc4Free(&dec);
  16886. #endif
  16887. return EXPECT_RESULT();
  16888. } /* END test_wc_Arc4Process */
  16889. /*
  16890. * Testing wc_Init RsaKey()
  16891. */
  16892. static int test_wc_InitRsaKey(void)
  16893. {
  16894. EXPECT_DECLS;
  16895. #ifndef NO_RSA
  16896. RsaKey key;
  16897. XMEMSET(&key, 0, sizeof(RsaKey));
  16898. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16899. /* Test bad args. */
  16900. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16901. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16902. #endif
  16903. return EXPECT_RESULT();
  16904. } /* END test_wc_InitRsaKey */
  16905. /*
  16906. * Testing wc_RsaPrivateKeyDecode()
  16907. */
  16908. static int test_wc_RsaPrivateKeyDecode(void)
  16909. {
  16910. EXPECT_DECLS;
  16911. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16912. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16913. RsaKey key;
  16914. byte* tmp = NULL;
  16915. word32 idx = 0;
  16916. int bytes = 0;
  16917. XMEMSET(&key, 0, sizeof(RsaKey));
  16918. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16919. DYNAMIC_TYPE_TMP_BUFFER));
  16920. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16921. if (tmp != NULL) {
  16922. #ifdef USE_CERT_BUFFERS_1024
  16923. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16924. bytes = sizeof_client_key_der_1024;
  16925. #else
  16926. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16927. bytes = sizeof_client_key_der_2048;
  16928. #endif /* Use cert buffers. */
  16929. }
  16930. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16931. /* Test bad args. */
  16932. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16933. BAD_FUNC_ARG);
  16934. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16935. BAD_FUNC_ARG);
  16936. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16937. BAD_FUNC_ARG);
  16938. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16939. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16940. #endif
  16941. return EXPECT_RESULT();
  16942. } /* END test_wc_RsaPrivateKeyDecode */
  16943. /*
  16944. * Testing wc_RsaPublicKeyDecode()
  16945. */
  16946. static int test_wc_RsaPublicKeyDecode(void)
  16947. {
  16948. EXPECT_DECLS;
  16949. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16950. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16951. RsaKey keyPub;
  16952. byte* tmp = NULL;
  16953. word32 idx = 0;
  16954. int bytes = 0;
  16955. word32 keySz = 0;
  16956. word32 tstKeySz = 0;
  16957. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16958. XFILE f = XBADFILE;
  16959. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16960. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16961. byte buf[4096];
  16962. #endif
  16963. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16964. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16965. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16966. if (tmp != NULL) {
  16967. #ifdef USE_CERT_BUFFERS_1024
  16968. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16969. bytes = sizeof_client_keypub_der_1024;
  16970. keySz = 1024;
  16971. #else
  16972. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16973. bytes = sizeof_client_keypub_der_2048;
  16974. keySz = 2048;
  16975. #endif
  16976. }
  16977. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16978. /* Pass in bad args. */
  16979. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16980. BAD_FUNC_ARG);
  16981. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16982. BAD_FUNC_ARG);
  16983. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16984. BAD_FUNC_ARG);
  16985. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16986. /* Test for getting modulus key size */
  16987. idx = 0;
  16988. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16989. &tstKeySz, NULL, NULL), 0);
  16990. ExpectIntEQ(tstKeySz, keySz/8);
  16991. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16992. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16993. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16994. if (f != XBADFILE) {
  16995. XFCLOSE(f);
  16996. f = XBADFILE;
  16997. }
  16998. idx = 0;
  16999. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17000. NULL), 0);
  17001. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17002. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17003. if (f != XBADFILE)
  17004. XFCLOSE(f);
  17005. idx = 0;
  17006. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17007. NULL), 0);
  17008. #endif
  17009. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17010. #endif
  17011. return EXPECT_RESULT();
  17012. } /* END test_wc_RsaPublicKeyDecode */
  17013. /*
  17014. * Testing wc_RsaPublicKeyDecodeRaw()
  17015. */
  17016. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17017. {
  17018. EXPECT_DECLS;
  17019. #if !defined(NO_RSA)
  17020. RsaKey key;
  17021. const byte n = 0x23;
  17022. const byte e = 0x03;
  17023. int nSz = sizeof(n);
  17024. int eSz = sizeof(e);
  17025. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17026. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17027. /* Pass in bad args. */
  17028. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17029. BAD_FUNC_ARG);
  17030. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17031. BAD_FUNC_ARG);
  17032. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17033. BAD_FUNC_ARG);
  17034. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17035. #endif
  17036. return EXPECT_RESULT();
  17037. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17038. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17039. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17040. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17041. * trying until it gets a probable prime. */
  17042. #ifdef HAVE_FIPS
  17043. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17044. {
  17045. int ret;
  17046. for (;;) {
  17047. ret = wc_MakeRsaKey(key, size, e, rng);
  17048. if (ret != PRIME_GEN_E) break;
  17049. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17050. "trying again.\n");
  17051. }
  17052. return ret;
  17053. }
  17054. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17055. #else
  17056. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17057. #endif
  17058. #endif
  17059. /*
  17060. * Testing wc_MakeRsaKey()
  17061. */
  17062. static int test_wc_MakeRsaKey(void)
  17063. {
  17064. EXPECT_DECLS;
  17065. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17066. RsaKey genKey;
  17067. WC_RNG rng;
  17068. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17069. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17070. int bits = 1024;
  17071. #else
  17072. int bits = 2048;
  17073. #endif
  17074. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17075. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17076. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17077. ExpectIntEQ(wc_InitRng(&rng), 0);
  17078. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17079. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17080. /* Test bad args. */
  17081. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17082. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17083. BAD_FUNC_ARG);
  17084. /* e < 3 */
  17085. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17086. /* e & 1 == 0 */
  17087. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17088. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17089. #endif
  17090. return EXPECT_RESULT();
  17091. } /* END test_wc_MakeRsaKey */
  17092. /*
  17093. * Test the bounds checking on the cipher text versus the key modulus.
  17094. * 1. Make a new RSA key.
  17095. * 2. Set c to 1.
  17096. * 3. Decrypt c into k. (error)
  17097. * 4. Copy the key modulus to c and sub 1 from the copy.
  17098. * 5. Decrypt c into k. (error)
  17099. * Valid bounds test cases are covered by all the other RSA tests.
  17100. */
  17101. static int test_RsaDecryptBoundsCheck(void)
  17102. {
  17103. EXPECT_DECLS;
  17104. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17105. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17106. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17107. WC_RNG rng;
  17108. RsaKey key;
  17109. byte flatC[256];
  17110. word32 flatCSz;
  17111. byte out[256];
  17112. word32 outSz = sizeof(out);
  17113. XMEMSET(&key, 0, sizeof(RsaKey));
  17114. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17115. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17116. ExpectIntEQ(wc_InitRng(&rng), 0);
  17117. if (EXPECT_SUCCESS()) {
  17118. const byte* derKey;
  17119. word32 derKeySz;
  17120. word32 idx = 0;
  17121. #ifdef USE_CERT_BUFFERS_1024
  17122. derKey = server_key_der_1024;
  17123. derKeySz = (word32)sizeof_server_key_der_1024;
  17124. flatCSz = 128;
  17125. #else
  17126. derKey = server_key_der_2048;
  17127. derKeySz = (word32)sizeof_server_key_der_2048;
  17128. flatCSz = 256;
  17129. #endif
  17130. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17131. }
  17132. if (EXPECT_SUCCESS()) {
  17133. XMEMSET(flatC, 0, flatCSz);
  17134. flatC[flatCSz-1] = 1;
  17135. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17136. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17137. if (EXPECT_SUCCESS()) {
  17138. mp_int c;
  17139. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17140. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17141. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17142. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17143. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17144. mp_clear(&c);
  17145. }
  17146. }
  17147. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17148. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17149. #endif
  17150. return EXPECT_RESULT();
  17151. } /* END test_wc_RsaDecryptBoundsCheck */
  17152. /*
  17153. * Testing wc_SetKeyUsage()
  17154. */
  17155. static int test_wc_SetKeyUsage(void)
  17156. {
  17157. EXPECT_DECLS;
  17158. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17159. Cert myCert;
  17160. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17161. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17162. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17163. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17164. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17165. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17166. /* Test bad args. */
  17167. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17168. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17169. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17170. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17171. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17172. KEYUSAGE_E);
  17173. #endif
  17174. return EXPECT_RESULT();
  17175. } /* END test_wc_SetKeyUsage */
  17176. /*
  17177. * Testing wc_CheckProbablePrime()
  17178. */
  17179. static int test_wc_CheckProbablePrime(void)
  17180. {
  17181. EXPECT_DECLS;
  17182. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17183. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17184. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17185. RsaKey key;
  17186. WC_RNG rng;
  17187. byte e[3];
  17188. word32 eSz = (word32)sizeof(e);
  17189. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17190. word32 nSz = (word32)sizeof(n);
  17191. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17192. word32 dSz = (word32)sizeof(d);
  17193. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17194. word32 pSz = (word32)sizeof(p);
  17195. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17196. word32 qSz = (word32)sizeof(q);
  17197. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17198. int* isPrime;
  17199. int test[5];
  17200. isPrime = test;
  17201. XMEMSET(&key, 0, sizeof(RsaKey));
  17202. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17203. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17204. ExpectIntEQ(wc_InitRng(&rng), 0);
  17205. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17206. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17207. WC_RSA_EXPONENT, &rng), 0);
  17208. PRIVATE_KEY_UNLOCK();
  17209. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17210. &qSz), 0);
  17211. PRIVATE_KEY_LOCK();
  17212. /* Bad cases */
  17213. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17214. BAD_FUNC_ARG);
  17215. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17216. BAD_FUNC_ARG);
  17217. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17218. BAD_FUNC_ARG);
  17219. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17220. BAD_FUNC_ARG);
  17221. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17222. BAD_FUNC_ARG);
  17223. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17224. BAD_FUNC_ARG);
  17225. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17226. BAD_FUNC_ARG);
  17227. /* Good case */
  17228. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17229. 0);
  17230. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17231. wc_FreeRng(&rng);
  17232. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17233. #endif
  17234. return EXPECT_RESULT();
  17235. } /* END test_wc_CheckProbablePrime */
  17236. /*
  17237. * Testing wc_RsaPSS_Verify()
  17238. */
  17239. static int test_wc_RsaPSS_Verify(void)
  17240. {
  17241. EXPECT_DECLS;
  17242. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17243. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17244. RsaKey key;
  17245. WC_RNG rng;
  17246. int sz = 256;
  17247. const char* szMessage = "This is the string to be signed";
  17248. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17249. unsigned char pDecrypted[2048/8];
  17250. byte* pt = pDecrypted;
  17251. word32 outLen = sizeof(pDecrypted);
  17252. XMEMSET(&key, 0, sizeof(RsaKey));
  17253. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17254. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17255. ExpectIntEQ(wc_InitRng(&rng), 0);
  17256. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17257. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17258. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17259. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17260. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17261. /* Bad cases */
  17262. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  17263. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17264. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17265. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17266. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17267. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17268. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17269. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17270. /* Good case */
  17271. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17272. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17273. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17274. wc_FreeRng(&rng);
  17275. #endif
  17276. return EXPECT_RESULT();
  17277. } /* END test_wc_RsaPSS_Verify */
  17278. /*
  17279. * Testing wc_RsaPSS_VerifyCheck()
  17280. */
  17281. static int test_wc_RsaPSS_VerifyCheck(void)
  17282. {
  17283. EXPECT_DECLS;
  17284. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17285. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17286. RsaKey key;
  17287. WC_RNG rng;
  17288. int sz = 256; /* 2048/8 */
  17289. byte digest[32];
  17290. word32 digestSz = sizeof(digest);
  17291. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17292. word32 pSignatureSz = sizeof(pSignature);
  17293. unsigned char pDecrypted[2048/8];
  17294. byte* pt = pDecrypted;
  17295. word32 outLen = sizeof(pDecrypted);
  17296. XMEMSET(&key, 0, sizeof(RsaKey));
  17297. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17298. XMEMSET(digest, 0, sizeof(digest));
  17299. XMEMSET(pSignature, 0, sizeof(pSignature));
  17300. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17301. ExpectIntEQ(wc_InitRng(&rng), 0);
  17302. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17303. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17304. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17305. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17306. 0);
  17307. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17308. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17309. /* Bad cases */
  17310. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  17311. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17312. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17313. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17314. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  17315. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17316. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17317. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17318. /* Good case */
  17319. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  17320. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17321. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17322. wc_FreeRng(&rng);
  17323. #endif
  17324. return EXPECT_RESULT();
  17325. } /* END test_wc_RsaPSS_VerifyCheck */
  17326. /*
  17327. * Testing wc_RsaPSS_VerifyCheckInline()
  17328. */
  17329. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17330. {
  17331. EXPECT_DECLS;
  17332. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17333. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17334. RsaKey key;
  17335. WC_RNG rng;
  17336. int sz = 256;
  17337. byte digest[32];
  17338. word32 digestSz = sizeof(digest);
  17339. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17340. unsigned char pDecrypted[2048/8];
  17341. byte* pt = pDecrypted;
  17342. XMEMSET(&key, 0, sizeof(RsaKey));
  17343. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17344. XMEMSET(digest, 0, sizeof(digest));
  17345. XMEMSET(pSignature, 0, sizeof(pSignature));
  17346. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17347. ExpectIntEQ(wc_InitRng(&rng), 0);
  17348. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17349. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17350. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17351. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17352. 0);
  17353. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17354. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17355. /* Bad Cases */
  17356. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  17357. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17358. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17359. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17360. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17361. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17362. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17363. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17364. /* Good case */
  17365. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17366. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17367. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17368. wc_FreeRng(&rng);
  17369. #endif
  17370. return EXPECT_RESULT();
  17371. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17372. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17373. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17374. {
  17375. (void)flag;
  17376. (void)type;
  17377. (void)file;
  17378. (void)line;
  17379. }
  17380. #endif
  17381. /*
  17382. * Testing wc_LockMutex_ex
  17383. */
  17384. static int test_wc_LockMutex_ex(void)
  17385. {
  17386. EXPECT_DECLS;
  17387. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17388. int flag = CRYPTO_LOCK;
  17389. int type = 0;
  17390. const char* file = "./test-LockMutex_ex.txt";
  17391. int line = 0;
  17392. /* without SetMutexCb */
  17393. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17394. /* with SetMutexCb */
  17395. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17396. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17397. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17398. #endif
  17399. return EXPECT_RESULT();
  17400. } /* End test_wc_LockMutex_ex*/
  17401. /*
  17402. * Testing wc_SetMutexCb
  17403. */
  17404. static int test_wc_SetMutexCb(void)
  17405. {
  17406. EXPECT_DECLS;
  17407. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17408. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17409. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17410. #endif
  17411. return EXPECT_RESULT();
  17412. } /* End test_wc_SetMutexCb*/
  17413. /*
  17414. * Testing wc_RsaKeyToDer()
  17415. */
  17416. static int test_wc_RsaKeyToDer(void)
  17417. {
  17418. EXPECT_DECLS;
  17419. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17420. RsaKey genKey;
  17421. WC_RNG rng;
  17422. byte* der = NULL;
  17423. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17424. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17425. int bits = 1024;
  17426. word32 derSz = 611;
  17427. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17428. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17429. #else
  17430. int bits = 2048;
  17431. word32 derSz = 1196;
  17432. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17433. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17434. #endif
  17435. XMEMSET(&rng, 0, sizeof(rng));
  17436. XMEMSET(&genKey, 0, sizeof(genKey));
  17437. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17438. /* Init structures. */
  17439. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17440. ExpectIntEQ(wc_InitRng(&rng), 0);
  17441. /* Make key. */
  17442. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17443. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17444. /* Pass good/bad args. */
  17445. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17446. /* Get just the output length */
  17447. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17448. /* Try Public Key. */
  17449. genKey.type = 0;
  17450. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17451. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17452. /* Put back to Private Key */
  17453. genKey.type = 1;
  17454. #endif
  17455. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17456. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17457. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17458. #endif
  17459. return EXPECT_RESULT();
  17460. } /* END test_wc_RsaKeyToDer */
  17461. /*
  17462. * Testing wc_RsaKeyToPublicDer()
  17463. */
  17464. static int test_wc_RsaKeyToPublicDer(void)
  17465. {
  17466. EXPECT_DECLS;
  17467. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17468. RsaKey key;
  17469. WC_RNG rng;
  17470. byte* der = NULL;
  17471. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17472. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17473. int bits = 1024;
  17474. word32 derLen = 162;
  17475. #else
  17476. int bits = 2048;
  17477. word32 derLen = 294;
  17478. #endif
  17479. int ret;
  17480. XMEMSET(&rng, 0, sizeof(rng));
  17481. XMEMSET(&key, 0, sizeof(key));
  17482. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17483. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17484. ExpectIntEQ(wc_InitRng(&rng), 0);
  17485. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17486. /* test getting size only */
  17487. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17488. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17489. /* test getting size only */
  17490. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17491. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17492. /* Pass in bad args. */
  17493. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17494. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17495. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17496. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17497. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17498. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17499. #endif
  17500. return EXPECT_RESULT();
  17501. } /* END test_wc_RsaKeyToPublicDer */
  17502. /*
  17503. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17504. */
  17505. static int test_wc_RsaPublicEncryptDecrypt(void)
  17506. {
  17507. EXPECT_DECLS;
  17508. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17509. RsaKey key;
  17510. WC_RNG rng;
  17511. const char inStr[] = TEST_STRING;
  17512. const word32 plainLen = (word32)TEST_STRING_SZ;
  17513. const word32 inLen = (word32)TEST_STRING_SZ;
  17514. int bits = TEST_RSA_BITS;
  17515. const word32 cipherLen = TEST_RSA_BYTES;
  17516. word32 cipherLenResult = cipherLen;
  17517. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17518. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17519. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17520. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17521. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17522. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17523. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17524. ExpectNotNull(in);
  17525. ExpectNotNull(plain);
  17526. ExpectNotNull(cipher);
  17527. #endif
  17528. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17529. /* Initialize stack structures. */
  17530. XMEMSET(&key, 0, sizeof(RsaKey));
  17531. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17532. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17533. ExpectIntEQ(wc_InitRng(&rng), 0);
  17534. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17535. /* Encrypt. */
  17536. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  17537. cipherLen, &key, &rng), 0);
  17538. /* Pass bad args - tested in another testing function.*/
  17539. /* Decrypt */
  17540. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17541. /* Bind rng */
  17542. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17543. #endif
  17544. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17545. &key), 0);
  17546. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17547. /* Pass bad args - tested in another testing function.*/
  17548. WC_FREE_VAR(in, NULL);
  17549. WC_FREE_VAR(plain, NULL);
  17550. WC_FREE_VAR(cipher, NULL);
  17551. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17552. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17553. #endif
  17554. return EXPECT_RESULT();
  17555. } /* END test_wc_RsaPublicEncryptDecrypt */
  17556. /*
  17557. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17558. */
  17559. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17560. {
  17561. EXPECT_DECLS;
  17562. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17563. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17564. RsaKey key;
  17565. WC_RNG rng;
  17566. const char inStr[] = TEST_STRING;
  17567. const word32 inLen = (word32)TEST_STRING_SZ;
  17568. const word32 plainSz = (word32)TEST_STRING_SZ;
  17569. byte* res = NULL;
  17570. int idx = 0;
  17571. int bits = TEST_RSA_BITS;
  17572. const word32 cipherSz = TEST_RSA_BYTES;
  17573. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17574. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17575. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17576. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17577. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17578. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17579. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17580. ExpectNotNull(in);
  17581. ExpectNotNull(plain);
  17582. ExpectNotNull(cipher);
  17583. #endif
  17584. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17585. /* Initialize stack structures. */
  17586. XMEMSET(&key, 0, sizeof(RsaKey));
  17587. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17588. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17589. ExpectIntEQ(wc_InitRng(&rng), 0);
  17590. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17591. /* Encrypt */
  17592. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17593. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17594. /* Pass bad args - tested in another testing function.*/
  17595. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17596. /* Decrypt */
  17597. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17598. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17599. #endif
  17600. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17601. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17602. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17603. /* Pass bad args - tested in another testing function.*/
  17604. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17605. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17606. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17607. #endif
  17608. WC_FREE_VAR(in, NULL);
  17609. WC_FREE_VAR(plain, NULL);
  17610. WC_FREE_VAR(cipher, NULL);
  17611. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17612. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17613. #endif
  17614. return EXPECT_RESULT();
  17615. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17616. /*
  17617. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17618. */
  17619. static int test_wc_RsaSSL_SignVerify(void)
  17620. {
  17621. EXPECT_DECLS;
  17622. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17623. RsaKey key;
  17624. WC_RNG rng;
  17625. const char inStr[] = TEST_STRING;
  17626. const word32 plainSz = (word32)TEST_STRING_SZ;
  17627. const word32 inLen = (word32)TEST_STRING_SZ;
  17628. word32 idx = 0;
  17629. int bits = TEST_RSA_BITS;
  17630. const word32 outSz = TEST_RSA_BYTES;
  17631. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17632. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17633. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17634. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17635. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17636. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17637. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17638. ExpectNotNull(in);
  17639. ExpectNotNull(out);
  17640. ExpectNotNull(plain);
  17641. #endif
  17642. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17643. XMEMSET(&key, 0, sizeof(RsaKey));
  17644. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17645. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17646. ExpectIntEQ(wc_InitRng(&rng), 0);
  17647. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17648. /* Sign. */
  17649. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17650. idx = (int)outSz;
  17651. /* Test bad args. */
  17652. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17653. BAD_FUNC_ARG);
  17654. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17655. BAD_FUNC_ARG);
  17656. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17657. BAD_FUNC_ARG);
  17658. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17659. BAD_FUNC_ARG);
  17660. /* Verify. */
  17661. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17662. /* Pass bad args. */
  17663. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17664. BAD_FUNC_ARG);
  17665. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17666. BAD_FUNC_ARG);
  17667. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17668. BAD_FUNC_ARG);
  17669. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17670. BAD_FUNC_ARG);
  17671. WC_FREE_VAR(in, NULL);
  17672. WC_FREE_VAR(out, NULL);
  17673. WC_FREE_VAR(plain, NULL);
  17674. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17675. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17676. #endif
  17677. return EXPECT_RESULT();
  17678. } /* END test_wc_RsaSSL_SignVerify */
  17679. /*
  17680. * Testing wc_RsaEncryptSize()
  17681. */
  17682. static int test_wc_RsaEncryptSize(void)
  17683. {
  17684. EXPECT_DECLS;
  17685. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17686. RsaKey key;
  17687. WC_RNG rng;
  17688. XMEMSET(&key, 0, sizeof(RsaKey));
  17689. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17690. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17691. ExpectIntEQ(wc_InitRng(&rng), 0);
  17692. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17693. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17694. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17695. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17696. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17697. #endif
  17698. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17699. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17700. /* Pass in bad arg. */
  17701. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17702. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17703. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17704. #endif
  17705. return EXPECT_RESULT();
  17706. } /* END test_wc_RsaEncryptSize*/
  17707. /*
  17708. * Testing wc_RsaFlattenPublicKey()
  17709. */
  17710. static int test_wc_RsaFlattenPublicKey(void)
  17711. {
  17712. EXPECT_DECLS;
  17713. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17714. RsaKey key;
  17715. WC_RNG rng;
  17716. byte e[256];
  17717. byte n[256];
  17718. word32 eSz = sizeof(e);
  17719. word32 nSz = sizeof(n);
  17720. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17721. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17722. int bits = 1024;
  17723. #else
  17724. int bits = 2048;
  17725. #endif
  17726. XMEMSET(&key, 0, sizeof(RsaKey));
  17727. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17728. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17729. ExpectIntEQ(wc_InitRng(&rng), 0);
  17730. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17731. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17732. /* Pass bad args. */
  17733. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17734. BAD_FUNC_ARG);
  17735. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17736. BAD_FUNC_ARG);
  17737. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17738. BAD_FUNC_ARG);
  17739. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17740. BAD_FUNC_ARG);
  17741. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17742. BAD_FUNC_ARG);
  17743. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17744. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17745. #endif
  17746. return EXPECT_RESULT();
  17747. } /* END test_wc_RsaFlattenPublicKey */
  17748. /*
  17749. * unit test for wc_AesCcmSetKey
  17750. */
  17751. static int test_wc_AesCcmSetKey(void)
  17752. {
  17753. EXPECT_DECLS;
  17754. #ifdef HAVE_AESCCM
  17755. Aes aes;
  17756. const byte key16[] = {
  17757. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17758. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17759. };
  17760. const byte key24[] = {
  17761. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17762. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17764. };
  17765. const byte key32[] = {
  17766. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17767. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17768. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17769. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17770. };
  17771. XMEMSET(&aes, 0, sizeof(Aes));
  17772. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17773. #ifdef WOLFSSL_AES_128
  17774. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17775. #endif
  17776. #ifdef WOLFSSL_AES_192
  17777. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17778. #endif
  17779. #ifdef WOLFSSL_AES_256
  17780. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17781. #endif
  17782. /* Test bad args. */
  17783. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17784. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17785. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17786. wc_AesFree(&aes);
  17787. #endif
  17788. return EXPECT_RESULT();
  17789. } /* END test_wc_AesCcmSetKey */
  17790. /*
  17791. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17792. */
  17793. static int test_wc_AesCcmEncryptDecrypt(void)
  17794. {
  17795. EXPECT_DECLS;
  17796. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17797. Aes aes;
  17798. const byte key16[] = {
  17799. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17800. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17801. };
  17802. /* plaintext */
  17803. const byte plainT[] = {
  17804. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17805. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17806. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17807. };
  17808. /* nonce */
  17809. const byte iv[] = {
  17810. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17811. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17812. };
  17813. const byte c[] = { /* cipher text. */
  17814. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17815. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17816. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17817. };
  17818. const byte t[] = { /* Auth tag */
  17819. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17820. };
  17821. const byte authIn[] = {
  17822. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17823. };
  17824. byte cipherOut[sizeof(plainT)];
  17825. byte authTag[sizeof(t)];
  17826. #ifdef HAVE_AES_DECRYPT
  17827. byte plainOut[sizeof(cipherOut)];
  17828. #endif
  17829. XMEMSET(&aes, 0, sizeof(Aes));
  17830. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17831. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17832. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17833. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17834. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17835. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17836. #ifdef HAVE_AES_DECRYPT
  17837. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17838. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17839. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17840. #endif
  17841. /* Pass in bad args. Encrypt*/
  17842. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17843. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17844. BAD_FUNC_ARG);
  17845. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17846. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17847. BAD_FUNC_ARG);
  17848. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17849. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17850. BAD_FUNC_ARG);
  17851. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17852. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17853. BAD_FUNC_ARG);
  17854. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17855. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17856. BAD_FUNC_ARG);
  17857. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17858. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17859. BAD_FUNC_ARG);
  17860. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17861. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17862. BAD_FUNC_ARG);
  17863. #ifdef HAVE_AES_DECRYPT
  17864. /* Pass in bad args. Decrypt*/
  17865. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17866. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17867. BAD_FUNC_ARG);
  17868. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17869. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17870. BAD_FUNC_ARG);
  17871. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17872. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17873. BAD_FUNC_ARG);
  17874. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17875. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17876. BAD_FUNC_ARG);
  17877. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17878. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17879. BAD_FUNC_ARG);
  17880. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17881. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17882. BAD_FUNC_ARG);
  17883. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17884. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17885. BAD_FUNC_ARG);
  17886. #endif
  17887. wc_AesFree(&aes);
  17888. #endif /* HAVE_AESCCM */
  17889. return EXPECT_RESULT();
  17890. } /* END test_wc_AesCcmEncryptDecrypt */
  17891. #if defined(WOLFSSL_AES_EAX) && \
  17892. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17893. /*
  17894. * Testing test_wc_AesEaxVectors()
  17895. */
  17896. static int test_wc_AesEaxVectors(void)
  17897. {
  17898. EXPECT_DECLS;
  17899. typedef struct {
  17900. byte key[AES_256_KEY_SIZE];
  17901. int key_length;
  17902. byte iv[AES_BLOCK_SIZE];
  17903. int iv_length;
  17904. byte aad[AES_BLOCK_SIZE * 2];
  17905. int aad_length;
  17906. byte msg[AES_BLOCK_SIZE * 5];
  17907. int msg_length;
  17908. byte ct[AES_BLOCK_SIZE * 5];
  17909. int ct_length;
  17910. byte tag[AES_BLOCK_SIZE];
  17911. int tag_length;
  17912. int valid;
  17913. } AadVector;
  17914. /* Test vectors obtained from Google wycheproof project
  17915. * https://github.com/google/wycheproof
  17916. * from testvectors/aes_eax_test.json
  17917. */
  17918. const AadVector vectors[] = {
  17919. {
  17920. /* key, key length */
  17921. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17922. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17923. /* iv, iv length */
  17924. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17925. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17926. /* aad, aad length */
  17927. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17928. /* msg, msg length */
  17929. {0x00}, 0,
  17930. /* ct, ct length */
  17931. {0x00}, 0,
  17932. /* tag, tag length */
  17933. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17934. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17935. /* valid */
  17936. 1,
  17937. },
  17938. {
  17939. /* key, key length */
  17940. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17941. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17942. /* iv, iv length */
  17943. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17944. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17945. /* aad, aad length */
  17946. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17947. /* msg, msg length */
  17948. {0xf7, 0xfb}, 2,
  17949. /* ct, ct length */
  17950. {0x19, 0xdd}, 2,
  17951. /* tag, tag length */
  17952. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17953. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17954. /* valid */
  17955. 1,
  17956. },
  17957. {
  17958. /* key, key length */
  17959. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17960. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17961. /* iv, iv length */
  17962. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17963. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17964. /* aad, aad length */
  17965. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17966. /* msg, msg length */
  17967. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17968. /* ct, ct length */
  17969. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17970. /* tag, tag length */
  17971. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17972. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17973. /* valid */
  17974. 1,
  17975. },
  17976. {
  17977. /* key, key length */
  17978. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17979. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17980. /* iv, iv length */
  17981. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17982. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17983. /* aad, aad length */
  17984. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17985. /* msg, msg length */
  17986. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17987. /* ct, ct length */
  17988. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17989. /* tag, tag length */
  17990. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17991. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17992. /* valid */
  17993. 1,
  17994. },
  17995. {
  17996. /* key, key length */
  17997. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17998. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17999. /* iv, iv length */
  18000. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18001. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18002. /* aad, aad length */
  18003. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18004. /* msg, msg length */
  18005. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18006. /* ct, ct length */
  18007. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18008. /* tag, tag length */
  18009. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18010. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18011. /* valid */
  18012. 1,
  18013. },
  18014. {
  18015. /* key, key length */
  18016. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18017. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18018. /* iv, iv length */
  18019. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18020. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18021. /* aad, aad length */
  18022. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18023. /* msg, msg length */
  18024. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18025. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18026. /* ct, ct length */
  18027. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18028. 0x0e, 0x72, 0x84, 0x14}, 12,
  18029. /* tag, tag length */
  18030. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18031. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18032. /* valid */
  18033. 1,
  18034. },
  18035. {
  18036. /* key, key length */
  18037. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18038. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18039. /* iv, iv length */
  18040. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18041. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18042. /* aad, aad length */
  18043. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18044. /* msg, msg length */
  18045. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18046. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18047. 0x36}, 17,
  18048. /* ct, ct length */
  18049. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18050. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18051. 0x30}, 17,
  18052. /* tag, tag length */
  18053. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18054. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18055. /* valid */
  18056. 1,
  18057. },
  18058. {
  18059. /* key, key length */
  18060. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18061. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18062. /* iv, iv length */
  18063. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18064. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18065. /* aad, aad length */
  18066. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18067. /* msg, msg length */
  18068. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18069. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18070. 0x2d, 0x56}, 18,
  18071. /* ct, ct length */
  18072. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18073. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18074. 0x8c, 0xc3}, 18,
  18075. /* tag, tag length */
  18076. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18077. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18078. /* valid */
  18079. 1,
  18080. },
  18081. {
  18082. /* key, key length */
  18083. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18084. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18085. /* iv, iv length */
  18086. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18087. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18088. /* aad, aad length */
  18089. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18090. /* msg, msg length */
  18091. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18092. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18093. 0xef, 0x11}, 18,
  18094. /* ct, ct length */
  18095. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18096. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18097. 0xb1, 0xe8}, 18,
  18098. /* tag, tag length */
  18099. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18100. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18101. /* valid */
  18102. 1,
  18103. },
  18104. {
  18105. /* key, key length */
  18106. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18107. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18108. /* iv, iv length */
  18109. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18110. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18111. /* aad, aad length */
  18112. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18113. /* msg, msg length */
  18114. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18115. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18116. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18117. /* ct, ct length */
  18118. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18119. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18120. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18121. /* tag, tag length */
  18122. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18123. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18124. /* valid */
  18125. 1,
  18126. },
  18127. {
  18128. /* key, key length */
  18129. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18130. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18131. /* iv, iv length */
  18132. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18133. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18134. /* aad, aad length */
  18135. {0x00}, 0,
  18136. /* msg, msg length */
  18137. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18139. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18140. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18141. /* ct, ct length */
  18142. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18143. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18144. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18145. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18146. /* tag, tag length */
  18147. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18148. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18149. /* valid */
  18150. 1,
  18151. },
  18152. {
  18153. /* key, key length */
  18154. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18155. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18156. /* iv, iv length */
  18157. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18158. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18159. /* aad, aad length */
  18160. {0x00}, 0,
  18161. /* msg, msg length */
  18162. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18164. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18165. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18166. /* ct, ct length */
  18167. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18168. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18169. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18170. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18171. /* tag, tag length */
  18172. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18173. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18174. /* valid */
  18175. 1,
  18176. },
  18177. {
  18178. /* key, key length */
  18179. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18180. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18181. /* iv, iv length */
  18182. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18183. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18184. /* aad, aad length */
  18185. {0x00}, 0,
  18186. /* msg, msg length */
  18187. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18188. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18189. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18190. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18191. /* ct, ct length */
  18192. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18193. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18194. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18195. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18196. /* tag, tag length */
  18197. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18198. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18199. /* valid */
  18200. 1,
  18201. },
  18202. {
  18203. /* key, key length */
  18204. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18205. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18206. /* iv, iv length */
  18207. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18208. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18209. /* aad, aad length */
  18210. {0x00}, 0,
  18211. /* msg, msg length */
  18212. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18214. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18215. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18216. /* ct, ct length */
  18217. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18218. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18219. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18220. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18221. /* tag, tag length */
  18222. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18223. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18224. /* valid */
  18225. 1,
  18226. },
  18227. {
  18228. /* key, key length */
  18229. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18230. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18231. /* iv, iv length */
  18232. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18233. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18234. /* aad, aad length */
  18235. {0x00}, 0,
  18236. /* msg, msg length */
  18237. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18239. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18240. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18241. /* ct, ct length */
  18242. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18243. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18244. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18245. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18246. /* tag, tag length */
  18247. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18248. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18249. /* valid */
  18250. 1,
  18251. },
  18252. {
  18253. /* key, key length */
  18254. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18255. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18256. /* iv, iv length */
  18257. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18258. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18259. /* aad, aad length */
  18260. {0x00}, 0,
  18261. /* msg, msg length */
  18262. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18263. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18264. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18265. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18266. /* ct, ct length */
  18267. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18268. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18269. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18270. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18271. /* tag, tag length */
  18272. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18273. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18274. /* valid */
  18275. 1,
  18276. },
  18277. {
  18278. /* key, key length */
  18279. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18280. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18281. /* iv, iv length */
  18282. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18283. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18284. /* aad, aad length */
  18285. {0x00}, 0,
  18286. /* msg, msg length */
  18287. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18289. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18290. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18291. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18292. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18293. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18294. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18295. /* ct, ct length */
  18296. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18297. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18298. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18299. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18300. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18301. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18302. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18303. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18304. /* tag, tag length */
  18305. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18306. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18307. /* valid */
  18308. 1,
  18309. },
  18310. {
  18311. /* key, key length */
  18312. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18313. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18314. /* iv, iv length */
  18315. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18316. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18317. /* aad, aad length */
  18318. {0x00}, 0,
  18319. /* msg, msg length */
  18320. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18322. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18323. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18324. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18325. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18326. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18327. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18328. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18329. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18330. /* ct, ct length */
  18331. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18332. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18333. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18334. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18335. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18336. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18337. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18338. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18339. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18340. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18341. /* tag, tag length */
  18342. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18343. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18344. /* valid */
  18345. 1,
  18346. },
  18347. {
  18348. /* key, key length */
  18349. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18350. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18351. /* iv, iv length */
  18352. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18353. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18354. /* aad, aad length */
  18355. {0x00}, 0,
  18356. /* msg, msg length */
  18357. {0x00}, 0,
  18358. /* ct, ct length */
  18359. {0x00}, 0,
  18360. /* tag, tag length */
  18361. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18362. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18363. /* valid */
  18364. 1,
  18365. },
  18366. {
  18367. /* key, key length */
  18368. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18369. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18370. /* iv, iv length */
  18371. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18372. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18373. /* aad, aad length */
  18374. {0x00}, 0,
  18375. /* msg, msg length */
  18376. {0x01}, 1,
  18377. /* ct, ct length */
  18378. {0x1d}, 1,
  18379. /* tag, tag length */
  18380. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18381. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18382. /* valid */
  18383. 1,
  18384. },
  18385. {
  18386. /* key, key length */
  18387. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18388. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18389. /* iv, iv length */
  18390. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18391. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18392. /* aad, aad length */
  18393. {0x00}, 0,
  18394. /* msg, msg length */
  18395. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18396. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18397. /* ct, ct length */
  18398. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18399. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18400. /* tag, tag length */
  18401. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18402. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18403. /* valid */
  18404. 1,
  18405. },
  18406. {
  18407. /* key, key length */
  18408. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18409. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18410. /* iv, iv length */
  18411. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18412. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18413. /* aad, aad length */
  18414. {0x00}, 0,
  18415. /* msg, msg length */
  18416. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18417. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18418. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18419. /* ct, ct length */
  18420. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18421. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18422. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18423. /* tag, tag length */
  18424. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18425. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18426. /* valid */
  18427. 1,
  18428. },
  18429. {
  18430. /* key, key length */
  18431. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18432. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18433. /* iv, iv length */
  18434. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18435. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18436. /* aad, aad length */
  18437. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18438. /* msg, msg length */
  18439. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18440. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18441. /* ct, ct length */
  18442. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18443. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18444. /* tag, tag length */
  18445. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18446. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18447. /* valid */
  18448. 1,
  18449. },
  18450. {
  18451. /* key, key length */
  18452. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18453. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18454. /* iv, iv length */
  18455. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18456. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18457. /* aad, aad length */
  18458. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18459. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18460. /* msg, msg length */
  18461. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18462. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18463. /* ct, ct length */
  18464. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18465. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18466. /* tag, tag length */
  18467. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18468. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18469. /* valid */
  18470. 1,
  18471. },
  18472. {
  18473. /* key, key length */
  18474. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18475. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18476. /* iv, iv length */
  18477. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18478. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18479. /* aad, aad length */
  18480. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18481. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18482. /* msg, msg length */
  18483. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18484. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18485. /* ct, ct length */
  18486. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18487. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18488. /* tag, tag length */
  18489. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18490. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18491. /* valid */
  18492. 1,
  18493. },
  18494. {
  18495. /* key, key length */
  18496. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18497. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18498. /* iv, iv length */
  18499. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18500. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18501. /* aad, aad length */
  18502. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18503. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18504. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18505. /* msg, msg length */
  18506. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18507. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18508. /* ct, ct length */
  18509. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18510. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18511. /* tag, tag length */
  18512. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18513. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18514. /* valid */
  18515. 1,
  18516. },
  18517. {
  18518. /* key, key length */
  18519. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18520. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18521. /* iv, iv length */
  18522. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18523. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18524. /* aad, aad length */
  18525. {0x00}, 0,
  18526. /* msg, msg length */
  18527. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18528. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18529. /* ct, ct length */
  18530. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18531. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18532. /* tag, tag length */
  18533. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18534. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18535. /* valid */
  18536. 0,
  18537. },
  18538. {
  18539. /* key, key length */
  18540. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18541. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18542. /* iv, iv length */
  18543. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18544. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18545. /* aad, aad length */
  18546. {0x00}, 0,
  18547. /* msg, msg length */
  18548. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18549. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18550. /* ct, ct length */
  18551. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18552. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18553. /* tag, tag length */
  18554. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18555. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18556. /* valid */
  18557. 0,
  18558. },
  18559. {
  18560. /* key, key length */
  18561. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18562. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18563. /* iv, iv length */
  18564. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18565. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18566. /* aad, aad length */
  18567. {0x00}, 0,
  18568. /* msg, msg length */
  18569. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18570. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18571. /* ct, ct length */
  18572. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18573. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18574. /* tag, tag length */
  18575. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18576. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18577. /* valid */
  18578. 0,
  18579. },
  18580. {
  18581. /* key, key length */
  18582. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18583. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18584. /* iv, iv length */
  18585. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18586. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18587. /* aad, aad length */
  18588. {0x00}, 0,
  18589. /* msg, msg length */
  18590. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18591. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18592. /* ct, ct length */
  18593. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18594. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18595. /* tag, tag length */
  18596. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18597. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18598. /* valid */
  18599. 0,
  18600. },
  18601. {
  18602. /* key, key length */
  18603. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18604. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18605. /* iv, iv length */
  18606. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18607. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18608. /* aad, aad length */
  18609. {0x00}, 0,
  18610. /* msg, msg length */
  18611. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18612. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18613. /* ct, ct length */
  18614. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18615. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18616. /* tag, tag length */
  18617. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18618. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18619. /* valid */
  18620. 0,
  18621. },
  18622. {
  18623. /* key, key length */
  18624. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18625. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18626. /* iv, iv length */
  18627. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18628. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18629. /* aad, aad length */
  18630. {0x00}, 0,
  18631. /* msg, msg length */
  18632. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18633. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18634. /* ct, ct length */
  18635. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18636. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18637. /* tag, tag length */
  18638. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18639. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18640. /* valid */
  18641. 0,
  18642. },
  18643. {
  18644. /* key, key length */
  18645. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18646. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18647. /* iv, iv length */
  18648. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18649. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18650. /* aad, aad length */
  18651. {0x00}, 0,
  18652. /* msg, msg length */
  18653. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18654. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18655. /* ct, ct length */
  18656. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18657. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18658. /* tag, tag length */
  18659. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18660. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18661. /* valid */
  18662. 0,
  18663. },
  18664. {
  18665. /* key, key length */
  18666. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18667. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18668. /* iv, iv length */
  18669. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18670. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18671. /* aad, aad length */
  18672. {0x00}, 0,
  18673. /* msg, msg length */
  18674. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18675. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18676. /* ct, ct length */
  18677. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18678. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18679. /* tag, tag length */
  18680. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18681. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18682. /* valid */
  18683. 0,
  18684. },
  18685. {
  18686. /* key, key length */
  18687. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18688. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18689. /* iv, iv length */
  18690. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18691. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18692. /* aad, aad length */
  18693. {0x00}, 0,
  18694. /* msg, msg length */
  18695. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18696. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18697. /* ct, ct length */
  18698. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18699. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18700. /* tag, tag length */
  18701. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18702. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18703. /* valid */
  18704. 0,
  18705. },
  18706. {
  18707. /* key, key length */
  18708. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18709. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18710. /* iv, iv length */
  18711. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18712. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18713. /* aad, aad length */
  18714. {0x00}, 0,
  18715. /* msg, msg length */
  18716. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18717. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18718. /* ct, ct length */
  18719. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18720. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18721. /* tag, tag length */
  18722. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18723. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18724. /* valid */
  18725. 0,
  18726. },
  18727. {
  18728. /* key, key length */
  18729. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18730. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18731. /* iv, iv length */
  18732. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18733. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18734. /* aad, aad length */
  18735. {0x00}, 0,
  18736. /* msg, msg length */
  18737. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18738. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18739. /* ct, ct length */
  18740. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18741. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18742. /* tag, tag length */
  18743. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18744. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18745. /* valid */
  18746. 0,
  18747. },
  18748. {
  18749. /* key, key length */
  18750. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18751. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18752. /* iv, iv length */
  18753. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18754. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18755. /* aad, aad length */
  18756. {0x00}, 0,
  18757. /* msg, msg length */
  18758. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18759. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18760. /* ct, ct length */
  18761. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18762. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18763. /* tag, tag length */
  18764. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18765. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18766. /* valid */
  18767. 0,
  18768. },
  18769. {
  18770. /* key, key length */
  18771. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18772. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18773. /* iv, iv length */
  18774. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18775. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18776. /* aad, aad length */
  18777. {0x00}, 0,
  18778. /* msg, msg length */
  18779. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18780. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18781. /* ct, ct length */
  18782. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18783. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18784. /* tag, tag length */
  18785. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18786. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18787. /* valid */
  18788. 0,
  18789. },
  18790. {
  18791. /* key, key length */
  18792. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18793. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18794. /* iv, iv length */
  18795. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18796. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18797. /* aad, aad length */
  18798. {0x00}, 0,
  18799. /* msg, msg length */
  18800. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18801. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18802. /* ct, ct length */
  18803. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18804. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18805. /* tag, tag length */
  18806. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18807. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18808. /* valid */
  18809. 0,
  18810. },
  18811. {
  18812. /* key, key length */
  18813. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18814. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18815. /* iv, iv length */
  18816. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18817. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18818. /* aad, aad length */
  18819. {0x00}, 0,
  18820. /* msg, msg length */
  18821. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18822. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18823. /* ct, ct length */
  18824. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18825. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18826. /* tag, tag length */
  18827. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18828. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18829. /* valid */
  18830. 0,
  18831. },
  18832. {
  18833. /* key, key length */
  18834. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18835. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18836. /* iv, iv length */
  18837. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18838. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18839. /* aad, aad length */
  18840. {0x00}, 0,
  18841. /* msg, msg length */
  18842. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18843. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18844. /* ct, ct length */
  18845. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18846. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18847. /* tag, tag length */
  18848. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18849. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18850. /* valid */
  18851. 0,
  18852. },
  18853. {
  18854. /* key, key length */
  18855. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18856. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18857. /* iv, iv length */
  18858. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18859. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18860. /* aad, aad length */
  18861. {0x00}, 0,
  18862. /* msg, msg length */
  18863. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18864. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18865. /* ct, ct length */
  18866. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18867. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18868. /* tag, tag length */
  18869. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18870. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18871. /* valid */
  18872. 0,
  18873. },
  18874. {
  18875. /* key, key length */
  18876. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18877. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18878. /* iv, iv length */
  18879. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18880. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18881. /* aad, aad length */
  18882. {0x00}, 0,
  18883. /* msg, msg length */
  18884. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18885. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18886. /* ct, ct length */
  18887. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18888. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18889. /* tag, tag length */
  18890. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18891. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18892. /* valid */
  18893. 0,
  18894. },
  18895. {
  18896. /* key, key length */
  18897. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18898. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18899. /* iv, iv length */
  18900. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18901. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18902. /* aad, aad length */
  18903. {0x00}, 0,
  18904. /* msg, msg length */
  18905. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18906. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18907. /* ct, ct length */
  18908. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18909. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18910. /* tag, tag length */
  18911. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18912. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18913. /* valid */
  18914. 0,
  18915. },
  18916. {
  18917. /* key, key length */
  18918. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18919. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18920. /* iv, iv length */
  18921. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18922. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18923. /* aad, aad length */
  18924. {0x00}, 0,
  18925. /* msg, msg length */
  18926. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18927. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18928. /* ct, ct length */
  18929. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18930. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18931. /* tag, tag length */
  18932. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18933. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18934. /* valid */
  18935. 0,
  18936. },
  18937. {
  18938. /* key, key length */
  18939. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18940. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18941. /* iv, iv length */
  18942. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18943. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18944. /* aad, aad length */
  18945. {0x00}, 0,
  18946. /* msg, msg length */
  18947. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18948. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18949. /* ct, ct length */
  18950. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18951. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18952. /* tag, tag length */
  18953. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18954. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18955. /* valid */
  18956. 0,
  18957. },
  18958. {
  18959. /* key, key length */
  18960. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18961. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18962. /* iv, iv length */
  18963. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18964. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18965. /* aad, aad length */
  18966. {0x00}, 0,
  18967. /* msg, msg length */
  18968. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18969. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18970. /* ct, ct length */
  18971. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18972. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18973. /* tag, tag length */
  18974. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18975. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18976. /* valid */
  18977. 0,
  18978. },
  18979. {
  18980. /* key, key length */
  18981. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18982. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18983. /* iv, iv length */
  18984. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18985. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18986. /* aad, aad length */
  18987. {0x00}, 0,
  18988. /* msg, msg length */
  18989. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18990. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18991. /* ct, ct length */
  18992. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18993. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18994. /* tag, tag length */
  18995. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18996. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18997. /* valid */
  18998. 0,
  18999. },
  19000. {
  19001. /* key, key length */
  19002. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19003. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19004. /* iv, iv length */
  19005. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19006. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19007. /* aad, aad length */
  19008. {0x00}, 0,
  19009. /* msg, msg length */
  19010. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19011. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19012. /* ct, ct length */
  19013. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19014. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19015. /* tag, tag length */
  19016. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19018. /* valid */
  19019. 0,
  19020. },
  19021. {
  19022. /* key, key length */
  19023. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19024. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19025. /* iv, iv length */
  19026. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19027. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19028. /* aad, aad length */
  19029. {0x00}, 0,
  19030. /* msg, msg length */
  19031. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19032. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19033. /* ct, ct length */
  19034. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19035. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19036. /* tag, tag length */
  19037. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19038. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19039. /* valid */
  19040. 0,
  19041. },
  19042. {
  19043. /* key, key length */
  19044. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19045. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19046. /* iv, iv length */
  19047. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19048. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19049. /* aad, aad length */
  19050. {0x00}, 0,
  19051. /* msg, msg length */
  19052. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19053. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19054. /* ct, ct length */
  19055. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19056. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19057. /* tag, tag length */
  19058. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19059. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19060. /* valid */
  19061. 0,
  19062. },
  19063. {
  19064. /* key, key length */
  19065. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19066. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19067. /* iv, iv length */
  19068. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19069. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19070. /* aad, aad length */
  19071. {0x00}, 0,
  19072. /* msg, msg length */
  19073. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19074. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19075. /* ct, ct length */
  19076. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19077. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19078. /* tag, tag length */
  19079. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19080. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19081. /* valid */
  19082. 0,
  19083. },
  19084. };
  19085. byte ciphertext[sizeof(vectors[0].ct)];
  19086. byte authtag[sizeof(vectors[0].tag)];
  19087. int i;
  19088. int len;
  19089. int ret;
  19090. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19091. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19092. len = sizeof(authtag);
  19093. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19094. ciphertext,
  19095. vectors[i].msg, vectors[i].msg_length,
  19096. vectors[i].iv, vectors[i].iv_length,
  19097. authtag, len,
  19098. vectors[i].aad, vectors[i].aad_length),
  19099. 0);
  19100. /* check ciphertext matches vector */
  19101. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19102. 0);
  19103. /* check that computed tag matches vector only for vectors marked as valid */
  19104. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19105. if (vectors[i].valid) {
  19106. ExpectIntEQ(ret, 0);
  19107. }
  19108. else {
  19109. ExpectIntNE(ret, 0);
  19110. }
  19111. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19112. /* Decrypt, checking that the computed auth tags match */
  19113. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19114. ciphertext,
  19115. vectors[i].ct, vectors[i].ct_length,
  19116. vectors[i].iv, vectors[i].iv_length,
  19117. authtag, len,
  19118. vectors[i].aad, vectors[i].aad_length),
  19119. 0);
  19120. /* check decrypted ciphertext matches vector plaintext */
  19121. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19122. 0);
  19123. }
  19124. return EXPECT_RESULT();
  19125. } /* END test_wc_AesEaxVectors */
  19126. /*
  19127. * Testing test_wc_AesEaxEncryptAuth()
  19128. */
  19129. static int test_wc_AesEaxEncryptAuth(void)
  19130. {
  19131. EXPECT_DECLS;
  19132. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19133. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19134. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19135. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19136. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19137. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19138. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19139. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19140. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19141. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19142. byte ciphertext[sizeof(msg)];
  19143. byte authtag[AES_BLOCK_SIZE];
  19144. int i;
  19145. int len;
  19146. len = sizeof(authtag);
  19147. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19148. ciphertext,
  19149. msg, sizeof(msg),
  19150. iv, sizeof(iv),
  19151. authtag, len,
  19152. aad, sizeof(aad)),
  19153. 0);
  19154. /* Test null checking */
  19155. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19156. ciphertext,
  19157. msg, sizeof(msg),
  19158. iv, sizeof(iv),
  19159. authtag, len,
  19160. aad, sizeof(aad)),
  19161. BAD_FUNC_ARG);
  19162. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19163. NULL,
  19164. msg, sizeof(msg),
  19165. iv, sizeof(iv),
  19166. authtag, len,
  19167. aad, sizeof(aad)),
  19168. BAD_FUNC_ARG);
  19169. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19170. ciphertext,
  19171. NULL, sizeof(msg),
  19172. iv, sizeof(iv),
  19173. authtag, len,
  19174. aad, sizeof(aad)),
  19175. BAD_FUNC_ARG);
  19176. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19177. ciphertext,
  19178. msg, sizeof(msg),
  19179. NULL, sizeof(iv),
  19180. authtag, len,
  19181. aad, sizeof(aad)),
  19182. BAD_FUNC_ARG);
  19183. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19184. ciphertext,
  19185. msg, sizeof(msg),
  19186. iv, sizeof(iv),
  19187. NULL, len,
  19188. aad, sizeof(aad)),
  19189. BAD_FUNC_ARG);
  19190. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19191. ciphertext,
  19192. msg, sizeof(msg),
  19193. iv, sizeof(iv),
  19194. authtag, len,
  19195. NULL, sizeof(aad)),
  19196. BAD_FUNC_ARG);
  19197. /* Test bad key lengths */
  19198. for (i = 0; i <= 32; i++) {
  19199. int exp_ret;
  19200. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19201. || i == AES_256_KEY_SIZE) {
  19202. exp_ret = 0;
  19203. }
  19204. else {
  19205. exp_ret = BAD_FUNC_ARG;
  19206. }
  19207. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  19208. ciphertext,
  19209. msg, sizeof(msg),
  19210. iv, sizeof(iv),
  19211. authtag, len,
  19212. aad, sizeof(aad)),
  19213. exp_ret);
  19214. }
  19215. /* Test auth tag size out of range */
  19216. len = AES_BLOCK_SIZE + 1;
  19217. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19218. ciphertext,
  19219. msg, sizeof(msg),
  19220. iv, sizeof(iv),
  19221. authtag, len,
  19222. aad, sizeof(aad)),
  19223. BAD_FUNC_ARG);
  19224. return EXPECT_RESULT();
  19225. } /* END test_wc_AesEaxEncryptAuth() */
  19226. /*
  19227. * Testing test_wc_AesEaxDecryptAuth()
  19228. */
  19229. static int test_wc_AesEaxDecryptAuth(void)
  19230. {
  19231. EXPECT_DECLS;
  19232. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19233. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19234. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19235. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19236. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19237. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19238. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19239. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19240. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19241. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19242. /* Garbage tag that should always fail for above aad */
  19243. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19244. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19245. byte plaintext[sizeof(ct)];
  19246. int i;
  19247. int len;
  19248. len = sizeof(tag);
  19249. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19250. plaintext,
  19251. ct, sizeof(ct),
  19252. iv, sizeof(iv),
  19253. tag, len,
  19254. aad, sizeof(aad)),
  19255. AES_EAX_AUTH_E);
  19256. /* Test null checking */
  19257. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19258. plaintext,
  19259. ct, sizeof(ct),
  19260. iv, sizeof(iv),
  19261. tag, len,
  19262. aad, sizeof(aad)),
  19263. BAD_FUNC_ARG);
  19264. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19265. NULL,
  19266. ct, sizeof(ct),
  19267. iv, sizeof(iv),
  19268. tag, len,
  19269. aad, sizeof(aad)),
  19270. BAD_FUNC_ARG);
  19271. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19272. plaintext,
  19273. NULL, sizeof(ct),
  19274. iv, sizeof(iv),
  19275. tag, len,
  19276. aad, sizeof(aad)),
  19277. BAD_FUNC_ARG);
  19278. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19279. plaintext,
  19280. ct, sizeof(ct),
  19281. NULL, sizeof(iv),
  19282. tag, len,
  19283. aad, sizeof(aad)),
  19284. BAD_FUNC_ARG);
  19285. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19286. plaintext,
  19287. ct, sizeof(ct),
  19288. iv, sizeof(iv),
  19289. NULL, len,
  19290. aad, sizeof(aad)),
  19291. BAD_FUNC_ARG);
  19292. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19293. plaintext,
  19294. ct, sizeof(ct),
  19295. iv, sizeof(iv),
  19296. tag, len,
  19297. NULL, sizeof(aad)),
  19298. BAD_FUNC_ARG);
  19299. /* Test bad key lengths */
  19300. for (i = 0; i <= 32; i++) {
  19301. int exp_ret;
  19302. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19303. || i == AES_256_KEY_SIZE) {
  19304. exp_ret = AES_EAX_AUTH_E;
  19305. }
  19306. else {
  19307. exp_ret = BAD_FUNC_ARG;
  19308. }
  19309. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  19310. plaintext,
  19311. ct, sizeof(ct),
  19312. iv, sizeof(iv),
  19313. tag, len,
  19314. aad, sizeof(aad)),
  19315. exp_ret);
  19316. }
  19317. /* Test auth tag size out of range */
  19318. len = AES_BLOCK_SIZE + 1;
  19319. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19320. plaintext,
  19321. ct, sizeof(ct),
  19322. iv, sizeof(iv),
  19323. tag, len,
  19324. aad, sizeof(aad)),
  19325. BAD_FUNC_ARG);
  19326. return EXPECT_RESULT();
  19327. } /* END test_wc_AesEaxDecryptAuth() */
  19328. #endif /* WOLFSSL_AES_EAX &&
  19329. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19330. */
  19331. /*
  19332. * Testing wc_InitDsaKey()
  19333. */
  19334. static int test_wc_InitDsaKey(void)
  19335. {
  19336. EXPECT_DECLS;
  19337. #ifndef NO_DSA
  19338. DsaKey key;
  19339. XMEMSET(&key, 0, sizeof(DsaKey));
  19340. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19341. /* Pass in bad args. */
  19342. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19343. wc_FreeDsaKey(&key);
  19344. #endif
  19345. return EXPECT_RESULT();
  19346. } /* END test_wc_InitDsaKey */
  19347. /*
  19348. * Testing wc_DsaSign() and wc_DsaVerify()
  19349. */
  19350. static int test_wc_DsaSignVerify(void)
  19351. {
  19352. EXPECT_DECLS;
  19353. #if !defined(NO_DSA)
  19354. DsaKey key;
  19355. WC_RNG rng;
  19356. wc_Sha sha;
  19357. byte signature[DSA_SIG_SIZE];
  19358. byte hash[WC_SHA_DIGEST_SIZE];
  19359. word32 idx = 0;
  19360. word32 bytes;
  19361. int answer;
  19362. #ifdef USE_CERT_BUFFERS_1024
  19363. byte tmp[ONEK_BUF];
  19364. XMEMSET(tmp, 0, sizeof(tmp));
  19365. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19366. bytes = sizeof_dsa_key_der_1024;
  19367. #elif defined(USE_CERT_BUFFERS_2048)
  19368. byte tmp[TWOK_BUF];
  19369. XMEMSET(tmp, 0, sizeof(tmp));
  19370. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19371. bytes = sizeof_dsa_key_der_2048;
  19372. #else
  19373. byte tmp[TWOK_BUF];
  19374. XFILE fp = XBADFILE;
  19375. XMEMSET(tmp, 0, sizeof(tmp));
  19376. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19377. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19378. if (fp != XBADFILE)
  19379. XFCLOSE(fp);
  19380. #endif /* END USE_CERT_BUFFERS_1024 */
  19381. ExpectIntEQ(wc_InitSha(&sha), 0);
  19382. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19383. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19384. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19385. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19386. ExpectIntEQ(wc_InitRng(&rng), 0);
  19387. /* Sign. */
  19388. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19389. /* Test bad args. */
  19390. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19391. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19392. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19393. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19394. /* Verify. */
  19395. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19396. ExpectIntEQ(answer, 1);
  19397. /* Pass in bad args. */
  19398. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19399. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19400. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19401. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19402. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19403. /* hard set q to 0 and test fail case */
  19404. mp_free(&key.q);
  19405. mp_init(&key.q);
  19406. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19407. mp_set(&key.q, 1);
  19408. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19409. #endif
  19410. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19411. wc_FreeDsaKey(&key);
  19412. wc_ShaFree(&sha);
  19413. #endif
  19414. return EXPECT_RESULT();
  19415. } /* END test_wc_DsaSign */
  19416. /*
  19417. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19418. */
  19419. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19420. {
  19421. EXPECT_DECLS;
  19422. #if !defined(NO_DSA)
  19423. DsaKey key;
  19424. word32 bytes = 0;
  19425. word32 idx = 0;
  19426. int ret = 0;
  19427. #ifdef USE_CERT_BUFFERS_1024
  19428. byte tmp[ONEK_BUF];
  19429. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19430. bytes = sizeof_dsa_key_der_1024;
  19431. #elif defined(USE_CERT_BUFFERS_2048)
  19432. byte tmp[TWOK_BUF];
  19433. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19434. bytes = sizeof_dsa_key_der_2048;
  19435. #else
  19436. byte tmp[TWOK_BUF];
  19437. XFILE fp = XBADFILE;
  19438. XMEMSET(tmp, 0, sizeof(tmp));
  19439. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19440. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19441. if (fp != XBADFILE)
  19442. XFCLOSE(fp);
  19443. #endif /* END USE_CERT_BUFFERS_1024 */
  19444. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19445. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19446. /* Test bad args. */
  19447. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19448. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19449. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19450. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19451. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19452. wc_FreeDsaKey(&key);
  19453. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19454. idx = 0; /* Reset */
  19455. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19456. /* Test bad args. */
  19457. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19458. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19459. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19460. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19461. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19462. wc_FreeDsaKey(&key);
  19463. #endif /* !NO_DSA */
  19464. return EXPECT_RESULT();
  19465. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19466. /*
  19467. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19468. */
  19469. static int test_wc_MakeDsaKey(void)
  19470. {
  19471. EXPECT_DECLS;
  19472. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19473. DsaKey genKey;
  19474. WC_RNG rng;
  19475. XMEMSET(&genKey, 0, sizeof(genKey));
  19476. XMEMSET(&rng, 0, sizeof(rng));
  19477. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19478. ExpectIntEQ(wc_InitRng(&rng), 0);
  19479. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19480. /* Test bad args. */
  19481. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19482. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19483. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19484. BAD_FUNC_ARG);
  19485. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19486. /* Test bad args. */
  19487. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19488. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19489. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19490. wc_FreeDsaKey(&genKey);
  19491. #endif
  19492. return EXPECT_RESULT();
  19493. } /* END test_wc_MakeDsaKey */
  19494. /*
  19495. * Testing wc_DsaKeyToDer()
  19496. */
  19497. static int test_wc_DsaKeyToDer(void)
  19498. {
  19499. EXPECT_DECLS;
  19500. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19501. DsaKey key;
  19502. word32 bytes;
  19503. word32 idx = 0;
  19504. #ifdef USE_CERT_BUFFERS_1024
  19505. byte tmp[ONEK_BUF];
  19506. byte der[ONEK_BUF];
  19507. XMEMSET(tmp, 0, sizeof(tmp));
  19508. XMEMSET(der, 0, sizeof(der));
  19509. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19510. bytes = sizeof_dsa_key_der_1024;
  19511. #elif defined(USE_CERT_BUFFERS_2048)
  19512. byte tmp[TWOK_BUF];
  19513. byte der[TWOK_BUF];
  19514. XMEMSET(tmp, 0, sizeof(tmp));
  19515. XMEMSET(der, 0, sizeof(der));
  19516. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19517. bytes = sizeof_dsa_key_der_2048;
  19518. #else
  19519. byte tmp[TWOK_BUF];
  19520. byte der[TWOK_BUF];
  19521. XFILE fp = XBADFILE;
  19522. XMEMSET(tmp, 0, sizeof(tmp));
  19523. XMEMSET(der, 0, sizeof(der));
  19524. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19525. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19526. if (fp != XBADFILE)
  19527. XFCLOSE(fp);
  19528. #endif /* END USE_CERT_BUFFERS_1024 */
  19529. XMEMSET(&key, 0, sizeof(DsaKey));
  19530. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19531. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19532. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19533. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19534. /* Test bad args. */
  19535. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19536. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19537. wc_FreeDsaKey(&key);
  19538. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19539. return EXPECT_RESULT();
  19540. } /* END test_wc_DsaKeyToDer */
  19541. /*
  19542. * Testing wc_DsaKeyToPublicDer()
  19543. * (indirectly testing setDsaPublicKey())
  19544. */
  19545. static int test_wc_DsaKeyToPublicDer(void)
  19546. {
  19547. EXPECT_DECLS;
  19548. #ifndef HAVE_SELFTEST
  19549. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19550. DsaKey key;
  19551. WC_RNG rng;
  19552. byte* der = NULL;
  19553. word32 sz = 0;
  19554. word32 idx = 0;
  19555. XMEMSET(&key, 0, sizeof(DsaKey));
  19556. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19557. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19558. DYNAMIC_TYPE_TMP_BUFFER));
  19559. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19560. ExpectIntEQ(wc_InitRng(&rng), 0);
  19561. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19562. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19563. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19564. wc_FreeDsaKey(&key);
  19565. idx = 0;
  19566. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19567. /* Test without the SubjectPublicKeyInfo header */
  19568. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19569. wc_FreeDsaKey(&key);
  19570. idx = 0;
  19571. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19572. /* Test bad args. */
  19573. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19574. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19575. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19576. wc_FreeDsaKey(&key);
  19577. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19578. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19579. #endif /* !HAVE_SELFTEST */
  19580. return EXPECT_RESULT();
  19581. } /* END test_wc_DsaKeyToPublicDer */
  19582. /*
  19583. * Testing wc_DsaImportParamsRaw()
  19584. */
  19585. static int test_wc_DsaImportParamsRaw(void)
  19586. {
  19587. EXPECT_DECLS;
  19588. #if !defined(NO_DSA)
  19589. DsaKey key;
  19590. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19591. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19592. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19593. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19594. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19595. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19596. "80a0093113a8bd73";
  19597. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19598. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19599. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19600. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19601. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19602. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19603. "76341a7e7d9";
  19604. /* invalid p and q parameters */
  19605. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19606. const char* invalidQ = "96c5390a";
  19607. XMEMSET(&key, 0, sizeof(DsaKey));
  19608. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19609. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19610. /* test bad args */
  19611. /* null key struct */
  19612. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19613. /* null param pointers */
  19614. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19615. /* illegal p length */
  19616. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19617. /* illegal q length */
  19618. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19619. wc_FreeDsaKey(&key);
  19620. #endif
  19621. return EXPECT_RESULT();
  19622. } /* END test_wc_DsaImportParamsRaw */
  19623. /*
  19624. * Testing wc_DsaImportParamsRawCheck()
  19625. */
  19626. static int test_wc_DsaImportParamsRawCheck(void)
  19627. {
  19628. EXPECT_DECLS;
  19629. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19630. DsaKey key;
  19631. int trusted = 0;
  19632. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19633. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19634. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19635. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19636. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19637. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19638. "80a0093113a8bd73";
  19639. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19640. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19641. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19642. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19643. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19644. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19645. "76341a7e7d9";
  19646. /* invalid p and q parameters */
  19647. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19648. const char* invalidQ = "96c5390a";
  19649. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19650. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19651. /* test bad args */
  19652. /* null key struct */
  19653. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19654. BAD_FUNC_ARG);
  19655. /* null param pointers */
  19656. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19657. NULL), BAD_FUNC_ARG);
  19658. /* illegal p length */
  19659. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19660. BAD_FUNC_ARG);
  19661. /* illegal q length */
  19662. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19663. BAD_FUNC_ARG);
  19664. wc_FreeDsaKey(&key);
  19665. #endif
  19666. return EXPECT_RESULT();
  19667. } /* END test_wc_DsaImportParamsRawCheck */
  19668. /*
  19669. * Testing wc_DsaExportParamsRaw()
  19670. */
  19671. static int test_wc_DsaExportParamsRaw(void)
  19672. {
  19673. EXPECT_DECLS;
  19674. #if !defined(NO_DSA)
  19675. DsaKey key;
  19676. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19677. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19678. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19679. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19680. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19681. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19682. "80a0093113a8bd73";
  19683. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19684. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19685. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19686. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19687. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19688. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19689. "76341a7e7d9";
  19690. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19691. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19692. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19693. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19694. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19695. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19696. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19697. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19698. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19699. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19700. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19701. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19702. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19703. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19704. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19705. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19706. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19707. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19708. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19709. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19710. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19711. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19712. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19713. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19714. byte pOut[MAX_DSA_PARAM_SIZE];
  19715. byte qOut[MAX_DSA_PARAM_SIZE];
  19716. byte gOut[MAX_DSA_PARAM_SIZE];
  19717. word32 pOutSz;
  19718. word32 qOutSz;
  19719. word32 gOutSz;
  19720. XMEMSET(&key, 0, sizeof(DsaKey));
  19721. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19722. /* first test using imported raw parameters, for expected */
  19723. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19724. pOutSz = sizeof(pOut);
  19725. qOutSz = sizeof(qOut);
  19726. gOutSz = sizeof(gOut);
  19727. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19728. &gOutSz), 0);
  19729. /* validate exported parameters are correct */
  19730. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19731. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19732. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19733. /* test bad args */
  19734. /* null key struct */
  19735. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19736. &gOutSz), BAD_FUNC_ARG);
  19737. /* null output pointers */
  19738. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19739. &gOutSz), LENGTH_ONLY_E);
  19740. /* null output size pointers */
  19741. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19742. NULL), BAD_FUNC_ARG);
  19743. /* p output buffer size too small */
  19744. pOutSz = 1;
  19745. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19746. &gOutSz), BUFFER_E);
  19747. pOutSz = sizeof(pOut);
  19748. /* q output buffer size too small */
  19749. qOutSz = 1;
  19750. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19751. &gOutSz), BUFFER_E);
  19752. qOutSz = sizeof(qOut);
  19753. /* g output buffer size too small */
  19754. gOutSz = 1;
  19755. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19756. &gOutSz), BUFFER_E);
  19757. wc_FreeDsaKey(&key);
  19758. #endif
  19759. return EXPECT_RESULT();
  19760. } /* END test_wc_DsaExportParamsRaw */
  19761. /*
  19762. * Testing wc_DsaExportKeyRaw()
  19763. */
  19764. static int test_wc_DsaExportKeyRaw(void)
  19765. {
  19766. EXPECT_DECLS;
  19767. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19768. DsaKey key;
  19769. WC_RNG rng;
  19770. byte xOut[MAX_DSA_PARAM_SIZE];
  19771. byte yOut[MAX_DSA_PARAM_SIZE];
  19772. word32 xOutSz, yOutSz;
  19773. XMEMSET(&key, 0, sizeof(key));
  19774. XMEMSET(&rng, 0, sizeof(rng));
  19775. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19776. ExpectIntEQ(wc_InitRng(&rng), 0);
  19777. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19778. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19779. /* try successful export */
  19780. xOutSz = sizeof(xOut);
  19781. yOutSz = sizeof(yOut);
  19782. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19783. /* test bad args */
  19784. /* null key struct */
  19785. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19786. BAD_FUNC_ARG);
  19787. /* null output pointers */
  19788. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19789. LENGTH_ONLY_E);
  19790. /* null output size pointers */
  19791. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19792. BAD_FUNC_ARG);
  19793. /* x output buffer size too small */
  19794. xOutSz = 1;
  19795. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19796. BUFFER_E);
  19797. xOutSz = sizeof(xOut);
  19798. /* y output buffer size too small */
  19799. yOutSz = 1;
  19800. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19801. BUFFER_E);
  19802. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19803. wc_FreeDsaKey(&key);
  19804. #endif
  19805. return EXPECT_RESULT();
  19806. } /* END test_wc_DsaExportParamsRaw */
  19807. /*
  19808. * Testing wc_ed25519_make_key().
  19809. */
  19810. static int test_wc_ed25519_make_key(void)
  19811. {
  19812. EXPECT_DECLS;
  19813. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19814. ed25519_key key;
  19815. WC_RNG rng;
  19816. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19817. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19818. XMEMSET(&key, 0, sizeof(ed25519_key));
  19819. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19820. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19821. ExpectIntEQ(wc_InitRng(&rng), 0);
  19822. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19823. ECC_PRIV_KEY_E);
  19824. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19825. ECC_PRIV_KEY_E);
  19826. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19827. /* Test bad args. */
  19828. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19829. BAD_FUNC_ARG);
  19830. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19831. BAD_FUNC_ARG);
  19832. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19833. BAD_FUNC_ARG);
  19834. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19835. BAD_FUNC_ARG);
  19836. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19837. wc_ed25519_free(&key);
  19838. #endif
  19839. return EXPECT_RESULT();
  19840. } /* END test_wc_ed25519_make_key */
  19841. /*
  19842. * Testing wc_ed25519_init()
  19843. */
  19844. static int test_wc_ed25519_init(void)
  19845. {
  19846. EXPECT_DECLS;
  19847. #if defined(HAVE_ED25519)
  19848. ed25519_key key;
  19849. XMEMSET(&key, 0, sizeof(ed25519_key));
  19850. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19851. /* Test bad args. */
  19852. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19853. wc_ed25519_free(&key);
  19854. #endif
  19855. return EXPECT_RESULT();
  19856. } /* END test_wc_ed25519_init */
  19857. /*
  19858. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19859. */
  19860. static int test_wc_ed25519_sign_msg(void)
  19861. {
  19862. EXPECT_DECLS;
  19863. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19864. WC_RNG rng;
  19865. ed25519_key key;
  19866. byte msg[] = "Everybody gets Friday off.\n";
  19867. byte sig[ED25519_SIG_SIZE+1];
  19868. word32 msglen = sizeof(msg);
  19869. word32 siglen = ED25519_SIG_SIZE;
  19870. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19871. #ifdef HAVE_ED25519_VERIFY
  19872. int verify_ok = 0; /*1 = Verify success.*/
  19873. #endif
  19874. /* Initialize stack variables. */
  19875. XMEMSET(&key, 0, sizeof(ed25519_key));
  19876. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19877. XMEMSET(sig, 0, sizeof(sig));
  19878. /* Initialize key. */
  19879. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19880. ExpectIntEQ(wc_InitRng(&rng), 0);
  19881. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19882. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19883. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19884. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19885. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19886. /* Test bad args. */
  19887. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19888. BAD_FUNC_ARG);
  19889. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19890. BAD_FUNC_ARG);
  19891. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19892. BAD_FUNC_ARG);
  19893. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19894. BAD_FUNC_ARG);
  19895. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19896. BUFFER_E);
  19897. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19898. badSigLen -= 1;
  19899. #ifdef HAVE_ED25519_VERIFY
  19900. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19901. &key), 0);
  19902. ExpectIntEQ(verify_ok, 1);
  19903. /* Test bad args. */
  19904. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19905. &verify_ok, &key), BAD_FUNC_ARG);
  19906. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19907. &verify_ok, &key), BAD_FUNC_ARG);
  19908. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19909. &key), BAD_FUNC_ARG);
  19910. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19911. &key), BAD_FUNC_ARG);
  19912. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19913. BAD_FUNC_ARG);
  19914. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19915. NULL), BAD_FUNC_ARG);
  19916. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19917. &key), BAD_FUNC_ARG);
  19918. #endif /* Verify. */
  19919. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19920. wc_ed25519_free(&key);
  19921. #endif
  19922. return EXPECT_RESULT();
  19923. } /* END test_wc_ed25519_sign_msg */
  19924. /*
  19925. * Testing wc_ed25519_import_public()
  19926. */
  19927. static int test_wc_ed25519_import_public(void)
  19928. {
  19929. EXPECT_DECLS;
  19930. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19931. ed25519_key pubKey;
  19932. WC_RNG rng;
  19933. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19934. word32 inlen = sizeof(in);
  19935. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19936. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19937. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19938. ExpectIntEQ(wc_InitRng(&rng), 0);
  19939. #ifdef HAVE_ED25519_MAKE_KEY
  19940. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19941. #endif
  19942. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19943. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19944. /* Test bad args. */
  19945. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19946. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19947. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19948. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19949. wc_ed25519_free(&pubKey);
  19950. #endif
  19951. return EXPECT_RESULT();
  19952. } /* END wc_ed25519_import_public */
  19953. /*
  19954. * Testing wc_ed25519_import_private_key()
  19955. */
  19956. static int test_wc_ed25519_import_private_key(void)
  19957. {
  19958. EXPECT_DECLS;
  19959. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19960. ed25519_key key;
  19961. WC_RNG rng;
  19962. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19963. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19964. word32 privKeySz = sizeof(privKey);
  19965. word32 pubKeySz = sizeof(pubKey);
  19966. #ifdef HAVE_ED25519_KEY_EXPORT
  19967. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19968. word32 bothKeysSz = sizeof(bothKeys);
  19969. #endif
  19970. XMEMSET(&key, 0, sizeof(ed25519_key));
  19971. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19972. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19973. ExpectIntEQ(wc_InitRng(&rng), 0);
  19974. #ifdef HAVE_ED25519_MAKE_KEY
  19975. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19976. #endif
  19977. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19978. pubKeySz, &key, 1), 0);
  19979. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19980. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19981. #ifdef HAVE_ED25519_KEY_EXPORT
  19982. PRIVATE_KEY_UNLOCK();
  19983. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19984. PRIVATE_KEY_LOCK();
  19985. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19986. &key, 1), 0);
  19987. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19988. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19989. #endif
  19990. /* Test bad args. */
  19991. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19992. &key), BAD_FUNC_ARG);
  19993. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19994. pubKeySz, &key), BAD_FUNC_ARG);
  19995. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19996. pubKeySz, NULL), BAD_FUNC_ARG);
  19997. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19998. pubKeySz, &key), BAD_FUNC_ARG);
  19999. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20000. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20001. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20002. &key), BAD_FUNC_ARG);
  20003. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20004. wc_ed25519_free(&key);
  20005. #endif
  20006. return EXPECT_RESULT();
  20007. } /* END test_wc_ed25519_import_private_key */
  20008. /*
  20009. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20010. */
  20011. static int test_wc_ed25519_export(void)
  20012. {
  20013. EXPECT_DECLS;
  20014. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20015. ed25519_key key;
  20016. WC_RNG rng;
  20017. byte priv[ED25519_PRV_KEY_SIZE];
  20018. byte pub[ED25519_PUB_KEY_SIZE];
  20019. word32 privSz = sizeof(priv);
  20020. word32 pubSz = sizeof(pub);
  20021. #ifndef HAVE_ED25519_MAKE_KEY
  20022. const byte privKey[] = {
  20023. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20024. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20025. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20026. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20027. };
  20028. const byte pubKey[] = {
  20029. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20030. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20031. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20032. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20033. };
  20034. #endif
  20035. XMEMSET(&key, 0, sizeof(ed25519_key));
  20036. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20037. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20038. ExpectIntEQ(wc_InitRng(&rng), 0);
  20039. #ifdef HAVE_ED25519_MAKE_KEY
  20040. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20041. #else
  20042. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20043. pubKey, sizeof(pubKey), &key, 1), 0);
  20044. #endif
  20045. PRIVATE_KEY_UNLOCK();
  20046. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20047. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20048. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20049. /* Test bad args. */
  20050. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20051. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20052. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20053. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20054. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20055. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20056. /* Test bad args. */
  20057. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20058. BAD_FUNC_ARG);
  20059. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20060. BAD_FUNC_ARG);
  20061. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20062. BAD_FUNC_ARG);
  20063. PRIVATE_KEY_LOCK();
  20064. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20065. wc_ed25519_free(&key);
  20066. #endif
  20067. return EXPECT_RESULT();
  20068. } /* END test_wc_ed25519_export */
  20069. /*
  20070. * Testing wc_ed25519_size()
  20071. */
  20072. static int test_wc_ed25519_size(void)
  20073. {
  20074. EXPECT_DECLS;
  20075. #if defined(HAVE_ED25519)
  20076. ed25519_key key;
  20077. WC_RNG rng;
  20078. #ifndef HAVE_ED25519_MAKE_KEY
  20079. const byte privKey[] = {
  20080. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20081. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20082. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20083. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20084. };
  20085. const byte pubKey[] = {
  20086. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20087. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20088. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20089. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20090. };
  20091. #endif
  20092. XMEMSET(&key, 0, sizeof(ed25519_key));
  20093. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20094. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20095. ExpectIntEQ(wc_InitRng(&rng), 0);
  20096. #ifdef HAVE_ED25519_MAKE_KEY
  20097. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20098. #else
  20099. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20100. pubKey, sizeof(pubKey), &key, 1), 0);
  20101. #endif
  20102. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20103. /* Test bad args. */
  20104. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20106. /* Test bad args. */
  20107. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20108. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20109. /* Test bad args. */
  20110. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20111. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20112. /* Test bad args. */
  20113. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20114. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20115. wc_ed25519_free(&key);
  20116. #endif
  20117. return EXPECT_RESULT();
  20118. } /* END test_wc_ed25519_size */
  20119. /*
  20120. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20121. */
  20122. static int test_wc_ed25519_exportKey(void)
  20123. {
  20124. EXPECT_DECLS;
  20125. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20126. WC_RNG rng;
  20127. ed25519_key key;
  20128. byte priv[ED25519_PRV_KEY_SIZE];
  20129. byte pub[ED25519_PUB_KEY_SIZE];
  20130. byte privOnly[ED25519_PRV_KEY_SIZE];
  20131. word32 privSz = sizeof(priv);
  20132. word32 pubSz = sizeof(pub);
  20133. word32 privOnlySz = sizeof(privOnly);
  20134. #ifndef HAVE_ED25519_MAKE_KEY
  20135. const byte privKey[] = {
  20136. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20137. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20138. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20139. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20140. };
  20141. const byte pubKey[] = {
  20142. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20143. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20144. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20145. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20146. };
  20147. #endif
  20148. XMEMSET(&key, 0, sizeof(ed25519_key));
  20149. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20150. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20151. ExpectIntEQ(wc_InitRng(&rng), 0);
  20152. #ifdef HAVE_ED25519_MAKE_KEY
  20153. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20154. #else
  20155. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20156. pubKey, sizeof(pubKey), &key, 1), 0);
  20157. #endif
  20158. PRIVATE_KEY_UNLOCK();
  20159. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20160. /* Test bad args. */
  20161. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20162. BAD_FUNC_ARG);
  20163. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20164. BAD_FUNC_ARG);
  20165. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20166. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20167. /* Test bad args. */
  20168. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20169. BAD_FUNC_ARG);
  20170. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20171. BAD_FUNC_ARG);
  20172. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20173. BAD_FUNC_ARG);
  20174. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20175. BAD_FUNC_ARG);
  20176. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20177. BAD_FUNC_ARG);
  20178. PRIVATE_KEY_LOCK();
  20179. /* Cross check output. */
  20180. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20181. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20182. wc_ed25519_free(&key);
  20183. #endif
  20184. return EXPECT_RESULT();
  20185. } /* END test_wc_ed25519_exportKey */
  20186. /*
  20187. * Testing wc_Ed25519PublicKeyToDer
  20188. */
  20189. static int test_wc_Ed25519PublicKeyToDer(void)
  20190. {
  20191. EXPECT_DECLS;
  20192. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20193. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20194. ed25519_key key;
  20195. byte derBuf[1024];
  20196. XMEMSET(&key, 0, sizeof(ed25519_key));
  20197. /* Test bad args */
  20198. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20199. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20200. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20201. wc_ed25519_free(&key);
  20202. /* Test good args */
  20203. if (EXPECT_SUCCESS()) {
  20204. WC_RNG rng;
  20205. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20206. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20207. ExpectIntEQ(wc_InitRng(&rng), 0);
  20208. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20209. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20210. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20211. wc_ed25519_free(&key);
  20212. }
  20213. #endif
  20214. return EXPECT_RESULT();
  20215. } /* END testing wc_Ed25519PublicKeyToDer */
  20216. /*
  20217. * Testing wc_curve25519_init and wc_curve25519_free.
  20218. */
  20219. static int test_wc_curve25519_init(void)
  20220. {
  20221. EXPECT_DECLS;
  20222. #if defined(HAVE_CURVE25519)
  20223. curve25519_key key;
  20224. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20225. /* Test bad args for wc_curve25519_init */
  20226. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20227. /* Test good args for wc_curve_25519_free */
  20228. wc_curve25519_free(&key);
  20229. /* Test bad args for wc_curve25519 free. */
  20230. wc_curve25519_free(NULL);
  20231. #endif
  20232. return EXPECT_RESULT();
  20233. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20234. /*
  20235. * Testing test_wc_curve25519_size.
  20236. */
  20237. static int test_wc_curve25519_size(void)
  20238. {
  20239. EXPECT_DECLS;
  20240. #if defined(HAVE_CURVE25519)
  20241. curve25519_key key;
  20242. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20243. /* Test good args for wc_curve25519_size */
  20244. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20245. /* Test bad args for wc_curve25519_size */
  20246. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20247. wc_curve25519_free(&key);
  20248. #endif
  20249. return EXPECT_RESULT();
  20250. } /* END test_wc_curve25519_size*/
  20251. /*
  20252. * Testing test_wc_curve25519_export_key_raw().
  20253. */
  20254. static int test_wc_curve25519_export_key_raw(void)
  20255. {
  20256. EXPECT_DECLS;
  20257. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20258. curve25519_key key;
  20259. WC_RNG rng;
  20260. byte privateKey[CURVE25519_KEYSIZE];
  20261. byte publicKey[CURVE25519_KEYSIZE];
  20262. word32 prvkSz;
  20263. word32 pubkSz;
  20264. byte prik[CURVE25519_KEYSIZE];
  20265. byte pubk[CURVE25519_KEYSIZE];
  20266. word32 prksz;
  20267. word32 pbksz;
  20268. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20269. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20270. ExpectIntEQ(wc_InitRng(&rng), 0);
  20271. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20272. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20273. prvkSz = CURVE25519_KEYSIZE;
  20274. pubkSz = CURVE25519_KEYSIZE;
  20275. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20276. publicKey, &pubkSz), BAD_FUNC_ARG);
  20277. prvkSz = CURVE25519_KEYSIZE;
  20278. pubkSz = CURVE25519_KEYSIZE;
  20279. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20280. &pubkSz), BAD_FUNC_ARG);
  20281. prvkSz = CURVE25519_KEYSIZE;
  20282. pubkSz = CURVE25519_KEYSIZE;
  20283. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20284. publicKey, &pubkSz), BAD_FUNC_ARG);
  20285. /* prvkSz = CURVE25519_KEYSIZE; */
  20286. pubkSz = CURVE25519_KEYSIZE;
  20287. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20288. NULL, &pubkSz), BAD_FUNC_ARG);
  20289. prvkSz = CURVE25519_KEYSIZE;
  20290. pubkSz = CURVE25519_KEYSIZE;
  20291. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20292. publicKey, NULL), BAD_FUNC_ARG);
  20293. /* cross-testing */
  20294. prksz = CURVE25519_KEYSIZE;
  20295. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20296. pbksz = CURVE25519_KEYSIZE;
  20297. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20298. prvkSz = CURVE25519_KEYSIZE;
  20299. /* pubkSz = CURVE25519_KEYSIZE; */
  20300. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20301. publicKey, &pubkSz), 0);
  20302. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20303. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20304. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20305. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20306. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20307. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20308. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20309. wc_curve25519_free(&key);
  20310. #endif
  20311. return EXPECT_RESULT();
  20312. } /* end of test_wc_curve25519_export_key_raw */
  20313. /*
  20314. * Testing test_wc_curve25519_export_key_raw_ex().
  20315. */
  20316. static int test_wc_curve25519_export_key_raw_ex(void)
  20317. {
  20318. EXPECT_DECLS;
  20319. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20320. curve25519_key key;
  20321. WC_RNG rng;
  20322. byte privateKey[CURVE25519_KEYSIZE];
  20323. byte publicKey[CURVE25519_KEYSIZE];
  20324. word32 prvkSz;
  20325. word32 pubkSz;
  20326. byte prik[CURVE25519_KEYSIZE];
  20327. byte pubk[CURVE25519_KEYSIZE];
  20328. word32 prksz;
  20329. word32 pbksz;
  20330. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20331. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20332. ExpectIntEQ(wc_InitRng(&rng), 0);
  20333. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20334. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20335. prvkSz = CURVE25519_KEYSIZE;
  20336. pubkSz = CURVE25519_KEYSIZE;
  20337. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20338. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20339. prvkSz = CURVE25519_KEYSIZE;
  20340. pubkSz = CURVE25519_KEYSIZE;
  20341. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20342. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20343. prvkSz = CURVE25519_KEYSIZE;
  20344. pubkSz = CURVE25519_KEYSIZE;
  20345. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20346. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20347. /* prvkSz = CURVE25519_KEYSIZE; */
  20348. pubkSz = CURVE25519_KEYSIZE;
  20349. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20350. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20351. prvkSz = CURVE25519_KEYSIZE;
  20352. pubkSz = CURVE25519_KEYSIZE;
  20353. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20354. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20355. prvkSz = CURVE25519_KEYSIZE;
  20356. /* pubkSz = CURVE25519_KEYSIZE; */
  20357. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20358. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20359. prvkSz = CURVE25519_KEYSIZE;
  20360. pubkSz = CURVE25519_KEYSIZE;
  20361. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20362. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20363. prvkSz = CURVE25519_KEYSIZE;
  20364. pubkSz = CURVE25519_KEYSIZE;
  20365. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20366. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20367. /* prvkSz = CURVE25519_KEYSIZE; */
  20368. pubkSz = CURVE25519_KEYSIZE;
  20369. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20370. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20371. prvkSz = CURVE25519_KEYSIZE;
  20372. pubkSz = CURVE25519_KEYSIZE;
  20373. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20374. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20375. /* illegal value for endian */
  20376. prvkSz = CURVE25519_KEYSIZE;
  20377. /* pubkSz = CURVE25519_KEYSIZE; */
  20378. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20379. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20380. /* cross-testing */
  20381. prksz = CURVE25519_KEYSIZE;
  20382. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20383. pbksz = CURVE25519_KEYSIZE;
  20384. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20385. prvkSz = CURVE25519_KEYSIZE;
  20386. /* pubkSz = CURVE25519_KEYSIZE; */
  20387. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20388. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20389. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20390. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20391. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20392. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20393. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20394. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20395. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20396. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20397. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20398. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20399. /* try once with another endian */
  20400. prvkSz = CURVE25519_KEYSIZE;
  20401. pubkSz = CURVE25519_KEYSIZE;
  20402. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20403. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20404. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20405. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20406. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20407. wc_curve25519_free(&key);
  20408. #endif
  20409. return EXPECT_RESULT();
  20410. } /* end of test_wc_curve25519_export_key_raw_ex */
  20411. /*
  20412. * Testing wc_curve25519_make_key
  20413. */
  20414. static int test_wc_curve25519_make_key(void)
  20415. {
  20416. EXPECT_DECLS;
  20417. #if defined(HAVE_CURVE25519)
  20418. curve25519_key key;
  20419. WC_RNG rng;
  20420. int keysize;
  20421. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20422. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20423. ExpectIntEQ(wc_InitRng(&rng), 0);
  20424. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20425. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20426. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20427. /* test bad cases*/
  20428. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20429. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20430. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20431. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20432. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20433. wc_curve25519_free(&key);
  20434. #endif
  20435. return EXPECT_RESULT();
  20436. } /* END test_wc_curve25519_make_key*/
  20437. /*
  20438. * Testing wc_curve25519_shared_secret_ex
  20439. */
  20440. static int test_wc_curve25519_shared_secret_ex(void)
  20441. {
  20442. EXPECT_DECLS;
  20443. #if defined(HAVE_CURVE25519)
  20444. curve25519_key private_key;
  20445. curve25519_key public_key;
  20446. WC_RNG rng;
  20447. byte out[CURVE25519_KEYSIZE];
  20448. word32 outLen = sizeof(out);
  20449. int endian = EC25519_BIG_ENDIAN;
  20450. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20451. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20452. ExpectIntEQ(wc_InitRng(&rng), 0);
  20453. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20454. 0);
  20455. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20456. 0);
  20457. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20458. &outLen, endian), 0);
  20459. /* test bad cases*/
  20460. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20461. BAD_FUNC_ARG);
  20462. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20463. endian), BAD_FUNC_ARG);
  20464. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20465. endian), BAD_FUNC_ARG);
  20466. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20467. &outLen, endian), BAD_FUNC_ARG);
  20468. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20469. NULL, endian), BAD_FUNC_ARG);
  20470. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20471. * increasing to 0x8f checks for error being returned*/
  20472. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20473. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20474. &outLen, endian), ECC_BAD_ARG_E);
  20475. outLen = outLen - 2;
  20476. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20477. &outLen, endian), BAD_FUNC_ARG);
  20478. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20479. wc_curve25519_free(&private_key);
  20480. wc_curve25519_free(&public_key);
  20481. #endif
  20482. return EXPECT_RESULT();
  20483. } /* END test_wc_curve25519_shared_secret_ex*/
  20484. /*
  20485. * Testing wc_curve25519_make_pub
  20486. */
  20487. static int test_wc_curve25519_make_pub(void)
  20488. {
  20489. EXPECT_DECLS;
  20490. #ifdef HAVE_CURVE25519
  20491. curve25519_key key;
  20492. WC_RNG rng;
  20493. byte out[CURVE25519_KEYSIZE];
  20494. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20495. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20496. ExpectIntEQ(wc_InitRng(&rng), 0);
  20497. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20498. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20499. (int)sizeof(key.k), key.k), 0);
  20500. /* test bad cases*/
  20501. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20502. (int)sizeof out, out), ECC_BAD_ARG_E);
  20503. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20504. NULL), ECC_BAD_ARG_E);
  20505. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20506. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20507. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20508. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20509. /* verify clamping test */
  20510. key.k[0] |= ~248;
  20511. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20512. key.k), ECC_BAD_ARG_E);
  20513. key.k[0] &= 248;
  20514. /* repeat the expected-to-succeed test. */
  20515. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20516. key.k), 0);
  20517. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20518. wc_curve25519_free(&key);
  20519. #endif
  20520. return EXPECT_RESULT();
  20521. } /* END test_wc_curve25519_make_pub */
  20522. /*
  20523. * Testing test_wc_curve25519_export_public_ex
  20524. */
  20525. static int test_wc_curve25519_export_public_ex(void)
  20526. {
  20527. EXPECT_DECLS;
  20528. #if defined(HAVE_CURVE25519)
  20529. curve25519_key key;
  20530. WC_RNG rng;
  20531. byte out[CURVE25519_KEYSIZE];
  20532. word32 outLen = sizeof(out);
  20533. int endian = EC25519_BIG_ENDIAN;
  20534. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20535. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20536. ExpectIntEQ(wc_InitRng(&rng), 0);
  20537. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20538. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20539. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20540. /* test bad cases*/
  20541. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20542. BAD_FUNC_ARG);
  20543. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20544. BAD_FUNC_ARG);
  20545. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20546. BAD_FUNC_ARG);
  20547. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20548. BAD_FUNC_ARG);
  20549. outLen = outLen - 2;
  20550. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20551. ECC_BAD_ARG_E);
  20552. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20553. wc_curve25519_free(&key);
  20554. #endif
  20555. return EXPECT_RESULT();
  20556. } /* END test_wc_curve25519_export_public_ex*/
  20557. /*
  20558. * Testing test_wc_curve25519_import_private_raw_ex
  20559. */
  20560. static int test_wc_curve25519_import_private_raw_ex(void)
  20561. {
  20562. EXPECT_DECLS;
  20563. #if defined(HAVE_CURVE25519)
  20564. curve25519_key key;
  20565. WC_RNG rng;
  20566. byte priv[CURVE25519_KEYSIZE];
  20567. byte pub[CURVE25519_KEYSIZE];
  20568. word32 privSz = sizeof(priv);
  20569. word32 pubSz = sizeof(pub);
  20570. int endian = EC25519_BIG_ENDIAN;
  20571. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20572. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20573. ExpectIntEQ(wc_InitRng(&rng), 0);
  20574. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20575. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20576. endian), 0);
  20577. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20578. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20579. &key, endian), 0);
  20580. /* test bad cases*/
  20581. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20582. endian), BAD_FUNC_ARG);
  20583. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20584. &key, endian), BAD_FUNC_ARG);
  20585. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20586. &key, endian), BAD_FUNC_ARG);
  20587. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20588. NULL, endian), BAD_FUNC_ARG);
  20589. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20590. &key, endian), ECC_BAD_ARG_E);
  20591. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20592. &key, endian), ECC_BAD_ARG_E);
  20593. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20594. &key, EC25519_LITTLE_ENDIAN), 0);
  20595. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20596. wc_curve25519_free(&key);
  20597. #endif
  20598. return EXPECT_RESULT();
  20599. } /* END test_wc_curve25519_import_private_raw_ex*/
  20600. /*
  20601. * Testing test_wc_curve25519_import_private
  20602. */
  20603. static int test_wc_curve25519_import_private(void)
  20604. {
  20605. EXPECT_DECLS;
  20606. #if defined(HAVE_CURVE25519)
  20607. curve25519_key key;
  20608. WC_RNG rng;
  20609. byte priv[CURVE25519_KEYSIZE];
  20610. word32 privSz = sizeof(priv);
  20611. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20612. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20613. ExpectIntEQ(wc_InitRng(&rng), 0);
  20614. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20615. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20616. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20617. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20618. wc_curve25519_free(&key);
  20619. #endif
  20620. return EXPECT_RESULT();
  20621. } /* END test_wc_curve25519_import*/
  20622. /*
  20623. * Testing test_wc_curve25519_export_private_raw_ex
  20624. */
  20625. static int test_wc_curve25519_export_private_raw_ex(void)
  20626. {
  20627. EXPECT_DECLS;
  20628. #if defined(HAVE_CURVE25519)
  20629. curve25519_key key;
  20630. byte out[CURVE25519_KEYSIZE];
  20631. word32 outLen = sizeof(out);
  20632. int endian = EC25519_BIG_ENDIAN;
  20633. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20634. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20635. 0);
  20636. /* test bad cases*/
  20637. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20638. BAD_FUNC_ARG);
  20639. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20640. BAD_FUNC_ARG);
  20641. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20642. endian), BAD_FUNC_ARG);
  20643. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20644. BAD_FUNC_ARG);
  20645. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20646. EC25519_LITTLE_ENDIAN), 0);
  20647. outLen = outLen - 2;
  20648. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20649. ECC_BAD_ARG_E);
  20650. wc_curve25519_free(&key);
  20651. #endif
  20652. return EXPECT_RESULT();
  20653. } /* END test_wc_curve25519_export_private_raw_ex*/
  20654. /*
  20655. * Testing wc_ed448_make_key().
  20656. */
  20657. static int test_wc_ed448_make_key(void)
  20658. {
  20659. EXPECT_DECLS;
  20660. #if defined(HAVE_ED448)
  20661. ed448_key key;
  20662. WC_RNG rng;
  20663. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20664. XMEMSET(&key, 0, sizeof(ed448_key));
  20665. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20666. ExpectIntEQ(wc_ed448_init(&key), 0);
  20667. ExpectIntEQ(wc_InitRng(&rng), 0);
  20668. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20669. ECC_PRIV_KEY_E);
  20670. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20671. /* Test bad args. */
  20672. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20673. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20674. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20675. BAD_FUNC_ARG);
  20676. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20677. BAD_FUNC_ARG);
  20678. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20679. wc_ed448_free(&key);
  20680. #endif
  20681. return EXPECT_RESULT();
  20682. } /* END test_wc_ed448_make_key */
  20683. /*
  20684. * Testing wc_ed448_init()
  20685. */
  20686. static int test_wc_ed448_init(void)
  20687. {
  20688. EXPECT_DECLS;
  20689. #if defined(HAVE_ED448)
  20690. ed448_key key;
  20691. XMEMSET(&key, 0, sizeof(ed448_key));
  20692. ExpectIntEQ(wc_ed448_init(&key), 0);
  20693. /* Test bad args. */
  20694. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20695. wc_ed448_free(&key);
  20696. #endif
  20697. return EXPECT_RESULT();
  20698. } /* END test_wc_ed448_init */
  20699. /*
  20700. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20701. */
  20702. static int test_wc_ed448_sign_msg(void)
  20703. {
  20704. EXPECT_DECLS;
  20705. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20706. ed448_key key;
  20707. WC_RNG rng;
  20708. byte msg[] = "Everybody gets Friday off.\n";
  20709. byte sig[ED448_SIG_SIZE];
  20710. word32 msglen = sizeof(msg);
  20711. word32 siglen = sizeof(sig);
  20712. word32 badSigLen = sizeof(sig) - 1;
  20713. #ifdef HAVE_ED448_VERIFY
  20714. int verify_ok = 0; /*1 = Verify success.*/
  20715. #endif
  20716. /* Initialize stack variables. */
  20717. XMEMSET(&key, 0, sizeof(ed448_key));
  20718. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20719. XMEMSET(sig, 0, siglen);
  20720. /* Initialize key. */
  20721. ExpectIntEQ(wc_ed448_init(&key), 0);
  20722. ExpectIntEQ(wc_InitRng(&rng), 0);
  20723. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20724. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20725. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20726. /* Test bad args. */
  20727. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20728. BAD_FUNC_ARG);
  20729. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20730. BAD_FUNC_ARG);
  20731. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20732. BAD_FUNC_ARG);
  20733. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20734. BAD_FUNC_ARG);
  20735. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20736. BUFFER_E);
  20737. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20738. badSigLen -= 1;
  20739. #ifdef HAVE_ED448_VERIFY
  20740. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20741. NULL, 0), 0);
  20742. ExpectIntEQ(verify_ok, 1);
  20743. /* Test bad args. */
  20744. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20745. &key, NULL, 0), BAD_FUNC_ARG);
  20746. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20747. &key, NULL, 0), BAD_FUNC_ARG);
  20748. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20749. &key, NULL, 0), BAD_FUNC_ARG);
  20750. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20751. &key, NULL, 0), BAD_FUNC_ARG);
  20752. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20753. &key, NULL, 0), BAD_FUNC_ARG);
  20754. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20755. NULL, NULL, 0), BAD_FUNC_ARG);
  20756. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20757. &key, NULL, 0), BAD_FUNC_ARG);
  20758. #endif /* Verify. */
  20759. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20760. wc_ed448_free(&key);
  20761. #endif
  20762. return EXPECT_RESULT();
  20763. } /* END test_wc_ed448_sign_msg */
  20764. /*
  20765. * Testing wc_ed448_import_public()
  20766. */
  20767. static int test_wc_ed448_import_public(void)
  20768. {
  20769. EXPECT_DECLS;
  20770. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20771. ed448_key pubKey;
  20772. WC_RNG rng;
  20773. const byte in[] =
  20774. "Ed448PublicKeyUnitTest.................................\n";
  20775. word32 inlen = sizeof(in);
  20776. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20777. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20778. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20779. ExpectIntEQ(wc_InitRng(&rng), 0);
  20780. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20781. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20782. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20783. /* Test bad args. */
  20784. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20785. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20786. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20787. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20788. wc_ed448_free(&pubKey);
  20789. #endif
  20790. return EXPECT_RESULT();
  20791. } /* END wc_ed448_import_public */
  20792. /*
  20793. * Testing wc_ed448_import_private_key()
  20794. */
  20795. static int test_wc_ed448_import_private_key(void)
  20796. {
  20797. EXPECT_DECLS;
  20798. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20799. ed448_key key;
  20800. WC_RNG rng;
  20801. const byte privKey[] =
  20802. "Ed448PrivateKeyUnitTest................................\n";
  20803. const byte pubKey[] =
  20804. "Ed448PublicKeyUnitTest.................................\n";
  20805. word32 privKeySz = sizeof(privKey);
  20806. word32 pubKeySz = sizeof(pubKey);
  20807. #ifdef HAVE_ED448_KEY_EXPORT
  20808. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20809. word32 bothKeysSz = sizeof(bothKeys);
  20810. #endif
  20811. XMEMSET(&key, 0, sizeof(ed448_key));
  20812. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20813. ExpectIntEQ(wc_ed448_init(&key), 0);
  20814. ExpectIntEQ(wc_InitRng(&rng), 0);
  20815. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20816. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20817. pubKeySz, &key, 1), 0);
  20818. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20819. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20820. #ifdef HAVE_ED448_KEY_EXPORT
  20821. PRIVATE_KEY_UNLOCK();
  20822. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20823. PRIVATE_KEY_LOCK();
  20824. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20825. &key, 1), 0);
  20826. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20827. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20828. #endif
  20829. /* Test bad args. */
  20830. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20831. &key), BAD_FUNC_ARG);
  20832. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20833. &key), BAD_FUNC_ARG);
  20834. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20835. pubKeySz, NULL), BAD_FUNC_ARG);
  20836. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20837. pubKeySz, &key), BAD_FUNC_ARG);
  20838. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20839. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20840. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20841. BAD_FUNC_ARG);
  20842. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20843. wc_ed448_free(&key);
  20844. #endif
  20845. return EXPECT_RESULT();
  20846. } /* END test_wc_ed448_import_private_key */
  20847. /*
  20848. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20849. */
  20850. static int test_wc_ed448_export(void)
  20851. {
  20852. EXPECT_DECLS;
  20853. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20854. ed448_key key;
  20855. WC_RNG rng;
  20856. byte priv[ED448_PRV_KEY_SIZE];
  20857. byte pub[ED448_PUB_KEY_SIZE];
  20858. word32 privSz = sizeof(priv);
  20859. word32 pubSz = sizeof(pub);
  20860. XMEMSET(&key, 0, sizeof(ed448_key));
  20861. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20862. ExpectIntEQ(wc_ed448_init(&key), 0);
  20863. ExpectIntEQ(wc_InitRng(&rng), 0);
  20864. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20865. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20866. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20867. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20868. /* Test bad args. */
  20869. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20870. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20871. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20872. PRIVATE_KEY_UNLOCK();
  20873. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20874. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20875. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20876. /* Test bad args. */
  20877. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20878. BAD_FUNC_ARG);
  20879. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20880. BAD_FUNC_ARG);
  20881. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20882. PRIVATE_KEY_LOCK();
  20883. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20884. wc_ed448_free(&key);
  20885. #endif
  20886. return EXPECT_RESULT();
  20887. } /* END test_wc_ed448_export */
  20888. /*
  20889. * Testing wc_ed448_size()
  20890. */
  20891. static int test_wc_ed448_size(void)
  20892. {
  20893. EXPECT_DECLS;
  20894. #if defined(HAVE_ED448)
  20895. ed448_key key;
  20896. WC_RNG rng;
  20897. XMEMSET(&key, 0, sizeof(ed448_key));
  20898. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20899. ExpectIntEQ(wc_ed448_init(&key), 0);
  20900. ExpectIntEQ(wc_InitRng(&rng), 0);
  20901. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20902. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20903. /* Test bad args. */
  20904. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20905. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20906. /* Test bad args. */
  20907. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20908. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20909. /* Test bad args. */
  20910. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20911. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20912. /* Test bad args. */
  20913. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20914. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20915. wc_ed448_free(&key);
  20916. #endif
  20917. return EXPECT_RESULT();
  20918. } /* END test_wc_ed448_size */
  20919. /*
  20920. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20921. */
  20922. static int test_wc_ed448_exportKey(void)
  20923. {
  20924. EXPECT_DECLS;
  20925. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20926. ed448_key key;
  20927. WC_RNG rng;
  20928. byte priv[ED448_PRV_KEY_SIZE];
  20929. byte pub[ED448_PUB_KEY_SIZE];
  20930. byte privOnly[ED448_PRV_KEY_SIZE];
  20931. word32 privSz = sizeof(priv);
  20932. word32 pubSz = sizeof(pub);
  20933. word32 privOnlySz = sizeof(privOnly);
  20934. XMEMSET(&key, 0, sizeof(ed448_key));
  20935. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20936. ExpectIntEQ(wc_ed448_init(&key), 0);
  20937. ExpectIntEQ(wc_InitRng(&rng), 0);
  20938. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20939. PRIVATE_KEY_UNLOCK();
  20940. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20941. /* Test bad args. */
  20942. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20943. BAD_FUNC_ARG);
  20944. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20945. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20946. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20947. /* Test bad args. */
  20948. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20949. BAD_FUNC_ARG);
  20950. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20951. BAD_FUNC_ARG);
  20952. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20953. BAD_FUNC_ARG);
  20954. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20955. BAD_FUNC_ARG);
  20956. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20957. BAD_FUNC_ARG);
  20958. PRIVATE_KEY_LOCK();
  20959. /* Cross check output. */
  20960. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20961. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20962. wc_ed448_free(&key);
  20963. #endif
  20964. return EXPECT_RESULT();
  20965. } /* END test_wc_ed448_exportKey */
  20966. /*
  20967. * Testing wc_Ed448PublicKeyToDer
  20968. */
  20969. static int test_wc_Ed448PublicKeyToDer(void)
  20970. {
  20971. EXPECT_DECLS;
  20972. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20973. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20974. ed448_key key;
  20975. byte derBuf[1024];
  20976. XMEMSET(&key, 0, sizeof(ed448_key));
  20977. /* Test bad args */
  20978. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20979. ExpectIntEQ(wc_ed448_init(&key), 0);
  20980. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20981. wc_ed448_free(&key);
  20982. /* Test good args */
  20983. if (EXPECT_SUCCESS()) {
  20984. WC_RNG rng;
  20985. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20986. ExpectIntEQ(wc_ed448_init(&key), 0);
  20987. ExpectIntEQ(wc_InitRng(&rng), 0);
  20988. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20989. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20990. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20991. wc_ed448_free(&key);
  20992. }
  20993. #endif
  20994. return EXPECT_RESULT();
  20995. } /* END testing wc_Ed448PublicKeyToDer */
  20996. /*
  20997. * Testing wc_curve448_init and wc_curve448_free.
  20998. */
  20999. static int test_wc_curve448_init(void)
  21000. {
  21001. EXPECT_DECLS;
  21002. #if defined(HAVE_CURVE448)
  21003. curve448_key key;
  21004. /* Test bad args for wc_curve448_init */
  21005. ExpectIntEQ(wc_curve448_init(&key), 0);
  21006. /* Test bad args for wc_curve448_init */
  21007. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21008. /* Test good args for wc_curve_448_free */
  21009. wc_curve448_free(&key);
  21010. /* Test bad args for wc_curve448_free */
  21011. wc_curve448_free(NULL);
  21012. #endif
  21013. return EXPECT_RESULT();
  21014. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21015. /*
  21016. * Testing wc_curve448_make_key
  21017. */
  21018. static int test_wc_curve448_make_key(void)
  21019. {
  21020. EXPECT_DECLS;
  21021. #if defined(HAVE_CURVE448)
  21022. curve448_key key;
  21023. WC_RNG rng;
  21024. int keysize;
  21025. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21026. ExpectIntEQ(wc_curve448_init(&key), 0);
  21027. ExpectIntEQ(wc_InitRng(&rng), 0);
  21028. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21029. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21030. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21031. /* test bad cases */
  21032. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21033. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21034. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21035. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21036. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21037. wc_curve448_free(&key);
  21038. #endif
  21039. return EXPECT_RESULT();
  21040. } /* END test_wc_curve448_make_key*/
  21041. /*
  21042. * Testing test_wc_curve448_shared_secret_ex
  21043. */
  21044. static int test_wc_curve448_shared_secret_ex(void)
  21045. {
  21046. EXPECT_DECLS;
  21047. #if defined(HAVE_CURVE448)
  21048. curve448_key private_key;
  21049. curve448_key public_key;
  21050. WC_RNG rng;
  21051. byte out[CURVE448_KEY_SIZE];
  21052. word32 outLen = sizeof(out);
  21053. int endian = EC448_BIG_ENDIAN;
  21054. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21055. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21056. ExpectIntEQ(wc_InitRng(&rng), 0);
  21057. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21058. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21059. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21060. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21061. &outLen, endian), 0);
  21062. /* test bad cases */
  21063. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21064. BAD_FUNC_ARG);
  21065. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21066. endian), BAD_FUNC_ARG);
  21067. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21068. endian), BAD_FUNC_ARG);
  21069. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21070. &outLen, endian), BAD_FUNC_ARG);
  21071. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21072. NULL, endian), BAD_FUNC_ARG);
  21073. outLen = outLen - 2;
  21074. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21075. &outLen, endian), BAD_FUNC_ARG);
  21076. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21077. wc_curve448_free(&private_key);
  21078. wc_curve448_free(&public_key);
  21079. #endif
  21080. return EXPECT_RESULT();
  21081. } /* END test_wc_curve448_shared_secret_ex*/
  21082. /*
  21083. * Testing test_wc_curve448_export_public_ex
  21084. */
  21085. static int test_wc_curve448_export_public_ex(void)
  21086. {
  21087. EXPECT_DECLS;
  21088. #if defined(HAVE_CURVE448)
  21089. WC_RNG rng;
  21090. curve448_key key;
  21091. byte out[CURVE448_KEY_SIZE];
  21092. word32 outLen = sizeof(out);
  21093. int endian = EC448_BIG_ENDIAN;
  21094. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21095. ExpectIntEQ(wc_curve448_init(&key), 0);
  21096. ExpectIntEQ(wc_InitRng(&rng), 0);
  21097. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21098. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21099. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21100. /* test bad cases*/
  21101. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21102. BAD_FUNC_ARG);
  21103. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21104. BAD_FUNC_ARG);
  21105. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21106. BAD_FUNC_ARG);
  21107. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21108. BAD_FUNC_ARG);
  21109. outLen = outLen - 2;
  21110. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21111. ECC_BAD_ARG_E);
  21112. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21113. wc_curve448_free(&key);
  21114. #endif
  21115. return EXPECT_RESULT();
  21116. } /* END test_wc_curve448_export_public_ex*/
  21117. /*
  21118. * Testing test_wc_curve448_export_private_raw_ex
  21119. */
  21120. static int test_wc_curve448_export_private_raw_ex(void)
  21121. {
  21122. EXPECT_DECLS;
  21123. #if defined(HAVE_CURVE448)
  21124. curve448_key key;
  21125. byte out[CURVE448_KEY_SIZE];
  21126. word32 outLen = sizeof(out);
  21127. int endian = EC448_BIG_ENDIAN;
  21128. ExpectIntEQ(wc_curve448_init(&key), 0);
  21129. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21130. 0);
  21131. /* test bad cases*/
  21132. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21133. BAD_FUNC_ARG);
  21134. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21135. BAD_FUNC_ARG);
  21136. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21137. BAD_FUNC_ARG);
  21138. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21139. BAD_FUNC_ARG);
  21140. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21141. EC448_LITTLE_ENDIAN), 0);
  21142. outLen = outLen - 2;
  21143. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21144. ECC_BAD_ARG_E);
  21145. wc_curve448_free(&key);
  21146. #endif
  21147. return EXPECT_RESULT();
  21148. } /* END test_wc_curve448_export_private_raw_ex*/
  21149. /*
  21150. * Testing test_wc_curve448_import_private_raw_ex
  21151. */
  21152. static int test_wc_curve448_import_private_raw_ex(void)
  21153. {
  21154. EXPECT_DECLS;
  21155. #if defined(HAVE_CURVE448)
  21156. curve448_key key;
  21157. WC_RNG rng;
  21158. byte priv[CURVE448_KEY_SIZE];
  21159. byte pub[CURVE448_KEY_SIZE];
  21160. word32 privSz = sizeof(priv);
  21161. word32 pubSz = sizeof(pub);
  21162. int endian = EC448_BIG_ENDIAN;
  21163. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21164. ExpectIntEQ(wc_curve448_init(&key), 0);
  21165. ExpectIntEQ(wc_InitRng(&rng), 0);
  21166. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21167. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21168. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21169. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21170. &key, endian), 0);
  21171. /* test bad cases */
  21172. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21173. BAD_FUNC_ARG);
  21174. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21175. &key, endian), BAD_FUNC_ARG);
  21176. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21177. &key, endian), BAD_FUNC_ARG);
  21178. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21179. NULL, endian), BAD_FUNC_ARG);
  21180. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21181. &key, endian), ECC_BAD_ARG_E);
  21182. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21183. &key, endian), ECC_BAD_ARG_E);
  21184. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21185. &key, EC448_LITTLE_ENDIAN), 0);
  21186. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21187. wc_curve448_free(&key);
  21188. #endif
  21189. return EXPECT_RESULT();
  21190. } /* END test_wc_curve448_import_private_raw_ex*/
  21191. /*
  21192. * Testing test_curve448_export_key_raw
  21193. */
  21194. static int test_wc_curve448_export_key_raw(void)
  21195. {
  21196. EXPECT_DECLS;
  21197. #if defined(HAVE_CURVE448)
  21198. curve448_key key;
  21199. WC_RNG rng;
  21200. byte priv[CURVE448_KEY_SIZE];
  21201. byte pub[CURVE448_KEY_SIZE];
  21202. word32 privSz = sizeof(priv);
  21203. word32 pubSz = sizeof(pub);
  21204. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21205. ExpectIntEQ(wc_curve448_init(&key), 0);
  21206. ExpectIntEQ(wc_InitRng(&rng), 0);
  21207. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21208. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21209. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21210. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21211. 0);
  21212. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21213. wc_curve448_free(&key);
  21214. #endif
  21215. return EXPECT_RESULT();
  21216. } /* END test_wc_curve448_import_private_raw_ex*/
  21217. /*
  21218. * Testing test_wc_curve448_import_private
  21219. */
  21220. static int test_wc_curve448_import_private(void)
  21221. {
  21222. EXPECT_DECLS;
  21223. #if defined(HAVE_CURVE448)
  21224. curve448_key key;
  21225. WC_RNG rng;
  21226. byte priv[CURVE448_KEY_SIZE];
  21227. word32 privSz = sizeof(priv);
  21228. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21229. ExpectIntEQ(wc_curve448_init(&key), 0);
  21230. ExpectIntEQ(wc_InitRng(&rng), 0);
  21231. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21232. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21233. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21234. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21235. wc_curve448_free(&key);
  21236. #endif
  21237. return EXPECT_RESULT();
  21238. } /* END test_wc_curve448_import*/
  21239. /*
  21240. * Testing test_wc_curve448_size.
  21241. */
  21242. static int test_wc_curve448_size(void)
  21243. {
  21244. EXPECT_DECLS;
  21245. #if defined(HAVE_CURVE448)
  21246. curve448_key key;
  21247. ExpectIntEQ(wc_curve448_init(&key), 0);
  21248. /* Test good args for wc_curve448_size */
  21249. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21250. /* Test bad args for wc_curve448_size */
  21251. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21252. wc_curve448_free(&key);
  21253. #endif
  21254. return EXPECT_RESULT();
  21255. } /* END test_wc_curve448_size*/
  21256. /*
  21257. * Testing wc_ecc_make_key.
  21258. */
  21259. static int test_wc_ecc_make_key(void)
  21260. {
  21261. EXPECT_DECLS;
  21262. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21263. ecc_key key;
  21264. WC_RNG rng;
  21265. int ret;
  21266. XMEMSET(&key, 0, sizeof(ecc_key));
  21267. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21268. ExpectIntEQ(wc_ecc_init(&key), 0);
  21269. ExpectIntEQ(wc_InitRng(&rng), 0);
  21270. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21271. #if defined(WOLFSSL_ASYNC_CRYPT)
  21272. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21273. #endif
  21274. ExpectIntEQ(ret, 0);
  21275. /* Pass in bad args. */
  21276. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21277. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21278. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21279. wc_ecc_free(&key);
  21280. #ifdef FP_ECC
  21281. wc_ecc_fp_free();
  21282. #endif
  21283. #endif
  21284. return EXPECT_RESULT();
  21285. } /* END test_wc_ecc_make_key */
  21286. /*
  21287. * Testing wc_ecc_init()
  21288. */
  21289. static int test_wc_ecc_init(void)
  21290. {
  21291. EXPECT_DECLS;
  21292. #ifdef HAVE_ECC
  21293. ecc_key key;
  21294. XMEMSET(&key, 0, sizeof(ecc_key));
  21295. ExpectIntEQ(wc_ecc_init(&key), 0);
  21296. /* Pass in bad args. */
  21297. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21298. wc_ecc_free(&key);
  21299. #endif
  21300. return EXPECT_RESULT();
  21301. } /* END test_wc_ecc_init */
  21302. /*
  21303. * Testing wc_ecc_check_key()
  21304. */
  21305. static int test_wc_ecc_check_key(void)
  21306. {
  21307. EXPECT_DECLS;
  21308. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21309. ecc_key key;
  21310. WC_RNG rng;
  21311. int ret;
  21312. XMEMSET(&rng, 0, sizeof(rng));
  21313. XMEMSET(&key, 0, sizeof(key));
  21314. ExpectIntEQ(wc_ecc_init(&key), 0);
  21315. ExpectIntEQ(wc_InitRng(&rng), 0);
  21316. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21317. #if defined(WOLFSSL_ASYNC_CRYPT)
  21318. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21319. #endif
  21320. ExpectIntEQ(ret, 0);
  21321. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21322. /* Pass in bad args. */
  21323. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21324. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21325. wc_ecc_free(&key);
  21326. #ifdef FP_ECC
  21327. wc_ecc_fp_free();
  21328. #endif
  21329. #endif
  21330. return EXPECT_RESULT();
  21331. } /* END test_wc_ecc_check_key */
  21332. /*
  21333. * Testing wc_ecc_get_generator()
  21334. */
  21335. static int test_wc_ecc_get_generator(void)
  21336. {
  21337. EXPECT_DECLS;
  21338. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21339. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21340. ecc_point* pt = NULL;
  21341. ExpectNotNull(pt = wc_ecc_new_point());
  21342. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21343. MP_OKAY);
  21344. /* Test bad args. */
  21345. /* Returns Zero for bad arg. */
  21346. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21347. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21348. MP_OKAY);
  21349. /* If we ever get to 1000 curves increase this number */
  21350. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21351. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21352. wc_ecc_del_point(pt);
  21353. #endif
  21354. return EXPECT_RESULT();
  21355. } /* END test_wc_ecc_get_generator */
  21356. /*
  21357. * Testing wc_ecc_size()
  21358. */
  21359. static int test_wc_ecc_size(void)
  21360. {
  21361. EXPECT_DECLS;
  21362. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21363. WC_RNG rng;
  21364. ecc_key key;
  21365. int ret;
  21366. XMEMSET(&key, 0, sizeof(ecc_key));
  21367. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21368. ExpectIntEQ(wc_ecc_init(&key), 0);
  21369. ExpectIntEQ(wc_InitRng(&rng), 0);
  21370. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21371. #if defined(WOLFSSL_ASYNC_CRYPT)
  21372. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21373. #endif
  21374. ExpectIntEQ(ret, 0);
  21375. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21376. /* Test bad args. */
  21377. /* Returns Zero for bad arg. */
  21378. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21379. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21380. wc_ecc_free(&key);
  21381. #endif
  21382. return EXPECT_RESULT();
  21383. } /* END test_wc_ecc_size */
  21384. static int test_wc_ecc_params(void)
  21385. {
  21386. EXPECT_DECLS;
  21387. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21388. It was added after certifications */
  21389. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21390. const ecc_set_type* ecc_set;
  21391. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21392. /* Test for SECP256R1 curve */
  21393. int curve_id = ECC_SECP256R1;
  21394. int curve_idx;
  21395. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21396. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21397. ExpectIntEQ(ecc_set->id, curve_id);
  21398. #endif
  21399. /* Test case when SECP256R1 is not enabled */
  21400. /* Test that we get curve params for index 0 */
  21401. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21402. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21403. return EXPECT_RESULT();
  21404. }
  21405. /*
  21406. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21407. */
  21408. static int test_wc_ecc_signVerify_hash(void)
  21409. {
  21410. EXPECT_DECLS;
  21411. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21412. ecc_key key;
  21413. WC_RNG rng;
  21414. int ret;
  21415. #ifdef HAVE_ECC_VERIFY
  21416. int verify = 0;
  21417. #endif
  21418. word32 siglen = ECC_BUFSIZE;
  21419. byte sig[ECC_BUFSIZE];
  21420. byte adjustedSig[ECC_BUFSIZE+1];
  21421. byte digest[] = TEST_STRING;
  21422. word32 digestlen = (word32)TEST_STRING_SZ;
  21423. /* Init stack var */
  21424. XMEMSET(&key, 0, sizeof(ecc_key));
  21425. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21426. XMEMSET(sig, 0, siglen);
  21427. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21428. /* Init structs. */
  21429. ExpectIntEQ(wc_ecc_init(&key), 0);
  21430. ExpectIntEQ(wc_InitRng(&rng), 0);
  21431. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21432. #if defined(WOLFSSL_ASYNC_CRYPT)
  21433. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21434. #endif
  21435. ExpectIntEQ(ret, 0);
  21436. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21437. 0);
  21438. /* Check bad args. */
  21439. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21440. ECC_BAD_ARG_E);
  21441. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21442. ECC_BAD_ARG_E);
  21443. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21444. ECC_BAD_ARG_E);
  21445. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21446. ECC_BAD_ARG_E);
  21447. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21448. ECC_BAD_ARG_E);
  21449. #ifdef HAVE_ECC_VERIFY
  21450. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21451. &key), 0);
  21452. ExpectIntEQ(verify, 1);
  21453. /* test check on length of signature passed in */
  21454. XMEMCPY(adjustedSig, sig, siglen);
  21455. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21456. #ifndef NO_STRICT_ECDSA_LEN
  21457. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21458. &verify, &key), 0);
  21459. #else
  21460. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21461. * is allowed */
  21462. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21463. &verify, &key), 0);
  21464. #endif
  21465. /* Test bad args. */
  21466. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21467. &key), ECC_BAD_ARG_E);
  21468. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21469. ECC_BAD_ARG_E);
  21470. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21471. ECC_BAD_ARG_E);
  21472. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21473. NULL), ECC_BAD_ARG_E);
  21474. #endif /* HAVE_ECC_VERIFY */
  21475. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21476. wc_ecc_free(&key);
  21477. #ifdef FP_ECC
  21478. wc_ecc_fp_free();
  21479. #endif
  21480. #endif
  21481. return EXPECT_RESULT();
  21482. } /* END test_wc_ecc_sign_hash */
  21483. /*
  21484. * Testing wc_ecc_shared_secret()
  21485. */
  21486. static int test_wc_ecc_shared_secret(void)
  21487. {
  21488. EXPECT_DECLS;
  21489. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21490. ecc_key key;
  21491. ecc_key pubKey;
  21492. WC_RNG rng;
  21493. #if defined(NO_ECC256)
  21494. int ret;
  21495. #endif
  21496. byte out[KEY32];
  21497. int keySz = sizeof(out);
  21498. word32 outlen = (word32)sizeof(out);
  21499. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21500. const char* qx =
  21501. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21502. const char* qy =
  21503. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21504. const char* d =
  21505. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21506. const char* curveName = "SECP256R1";
  21507. const byte expected_shared_secret[] =
  21508. {
  21509. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21510. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21511. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21512. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21513. };
  21514. #endif
  21515. PRIVATE_KEY_UNLOCK();
  21516. /* Initialize variables. */
  21517. XMEMSET(&key, 0, sizeof(ecc_key));
  21518. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21519. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21520. XMEMSET(out, 0, keySz);
  21521. ExpectIntEQ(wc_ecc_init(&key), 0);
  21522. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21523. ExpectIntEQ(wc_InitRng(&rng), 0);
  21524. #if !defined(NO_ECC256)
  21525. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21526. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21527. #else
  21528. ret = wc_ecc_make_key(&rng, keySz, &key);
  21529. #if defined(WOLFSSL_ASYNC_CRYPT)
  21530. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21531. #endif
  21532. ExpectIntEQ(ret, 0);
  21533. ret = wc_ecc_make_key(&rng, keySz, &key);
  21534. #if defined(WOLFSSL_ASYNC_CRYPT)
  21535. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21536. #endif
  21537. ExpectIntEQ(ret, 0);
  21538. #endif
  21539. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21540. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21541. !defined(HAVE_SELFTEST)
  21542. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21543. #endif
  21544. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21545. #if !defined(NO_ECC256)
  21546. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21547. #endif
  21548. /* Test bad args. */
  21549. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21550. BAD_FUNC_ARG);
  21551. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21552. BAD_FUNC_ARG);
  21553. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21554. BAD_FUNC_ARG);
  21555. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21556. BAD_FUNC_ARG);
  21557. /* Invalid length */
  21558. outlen = 1;
  21559. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21560. BUFFER_E);
  21561. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21562. wc_ecc_free(&pubKey);
  21563. wc_ecc_free(&key);
  21564. #ifdef FP_ECC
  21565. wc_ecc_fp_free();
  21566. #endif
  21567. PRIVATE_KEY_LOCK();
  21568. #endif
  21569. return EXPECT_RESULT();
  21570. } /* END tests_wc_ecc_shared_secret */
  21571. /*
  21572. * testint wc_ecc_export_x963()
  21573. */
  21574. static int test_wc_ecc_export_x963(void)
  21575. {
  21576. EXPECT_DECLS;
  21577. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21578. ecc_key key;
  21579. WC_RNG rng;
  21580. byte out[ECC_ASN963_MAX_BUF_SZ];
  21581. word32 outlen = sizeof(out);
  21582. int ret;
  21583. PRIVATE_KEY_UNLOCK();
  21584. /* Initialize variables. */
  21585. XMEMSET(&key, 0, sizeof(ecc_key));
  21586. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21587. XMEMSET(out, 0, outlen);
  21588. ExpectIntEQ(wc_ecc_init(&key), 0);
  21589. ExpectIntEQ(wc_InitRng(&rng), 0);
  21590. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21591. #if defined(WOLFSSL_ASYNC_CRYPT)
  21592. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21593. #endif
  21594. ExpectIntEQ(ret, 0);
  21595. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21596. /* Test bad args. */
  21597. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21598. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21599. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21600. key.idx = -4;
  21601. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21602. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21603. wc_ecc_free(&key);
  21604. #ifdef FP_ECC
  21605. wc_ecc_fp_free();
  21606. #endif
  21607. PRIVATE_KEY_LOCK();
  21608. #endif
  21609. return EXPECT_RESULT();
  21610. } /* END test_wc_ecc_export_x963 */
  21611. /*
  21612. * Testing wc_ecc_export_x963_ex()
  21613. * compile with --enable-compkey will use compression.
  21614. */
  21615. static int test_wc_ecc_export_x963_ex(void)
  21616. {
  21617. EXPECT_DECLS;
  21618. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21619. ecc_key key;
  21620. WC_RNG rng;
  21621. int ret;
  21622. byte out[ECC_ASN963_MAX_BUF_SZ];
  21623. word32 outlen = sizeof(out);
  21624. #ifdef HAVE_COMP_KEY
  21625. word32 badOutLen = 5;
  21626. #endif
  21627. /* Init stack variables. */
  21628. XMEMSET(&key, 0, sizeof(ecc_key));
  21629. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21630. XMEMSET(out, 0, outlen);
  21631. ExpectIntEQ(wc_ecc_init(&key), 0);
  21632. ExpectIntEQ(wc_InitRng(&rng), 0);
  21633. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21634. #if defined(WOLFSSL_ASYNC_CRYPT)
  21635. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21636. #endif
  21637. ExpectIntEQ(ret, 0);
  21638. #ifdef HAVE_COMP_KEY
  21639. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21640. #else
  21641. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21642. #endif
  21643. /* Test bad args. */
  21644. #ifdef HAVE_COMP_KEY
  21645. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21646. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21647. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21648. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21649. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21650. #else
  21651. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21652. LENGTH_ONLY_E);
  21653. #endif
  21654. key.idx = -4;
  21655. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21656. #else
  21657. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21658. ECC_BAD_ARG_E);
  21659. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21660. LENGTH_ONLY_E);
  21661. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21662. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21663. ECC_BAD_ARG_E);
  21664. key.idx = -4;
  21665. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21666. ECC_BAD_ARG_E);
  21667. #endif
  21668. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21669. wc_ecc_free(&key);
  21670. #ifdef FP_ECC
  21671. wc_ecc_fp_free();
  21672. #endif
  21673. #endif
  21674. return EXPECT_RESULT();
  21675. } /* END test_wc_ecc_export_x963_ex */
  21676. /*
  21677. * testing wc_ecc_import_x963()
  21678. */
  21679. static int test_wc_ecc_import_x963(void)
  21680. {
  21681. EXPECT_DECLS;
  21682. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21683. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21684. ecc_key pubKey;
  21685. ecc_key key;
  21686. WC_RNG rng;
  21687. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21688. word32 x963Len = (word32)sizeof(x963);
  21689. int ret;
  21690. /* Init stack variables. */
  21691. XMEMSET(&key, 0, sizeof(ecc_key));
  21692. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21693. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21694. XMEMSET(x963, 0, x963Len);
  21695. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21696. ExpectIntEQ(wc_ecc_init(&key), 0);
  21697. ExpectIntEQ(wc_InitRng(&rng), 0);
  21698. #if FIPS_VERSION3_GE(6,0,0)
  21699. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21700. #else
  21701. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21702. #endif
  21703. #if defined(WOLFSSL_ASYNC_CRYPT)
  21704. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21705. #endif
  21706. ExpectIntEQ(ret, 0);
  21707. PRIVATE_KEY_UNLOCK();
  21708. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21709. PRIVATE_KEY_LOCK();
  21710. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21711. /* Test bad args. */
  21712. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21713. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21714. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21715. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21716. wc_ecc_free(&key);
  21717. wc_ecc_free(&pubKey);
  21718. #ifdef FP_ECC
  21719. wc_ecc_fp_free();
  21720. #endif
  21721. #endif
  21722. return EXPECT_RESULT();
  21723. } /* END wc_ecc_import_x963 */
  21724. /*
  21725. * testing wc_ecc_import_private_key()
  21726. */
  21727. static int test_wc_ecc_import_private_key(void)
  21728. {
  21729. EXPECT_DECLS;
  21730. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21731. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21732. ecc_key key;
  21733. ecc_key keyImp;
  21734. WC_RNG rng;
  21735. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21736. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21737. word32 privKeySz = (word32)sizeof(privKey);
  21738. word32 x963KeySz = (word32)sizeof(x963Key);
  21739. int ret;
  21740. /* Init stack variables. */
  21741. XMEMSET(&key, 0, sizeof(ecc_key));
  21742. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21743. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21744. XMEMSET(privKey, 0, privKeySz);
  21745. XMEMSET(x963Key, 0, x963KeySz);
  21746. ExpectIntEQ(wc_ecc_init(&key), 0);
  21747. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21748. ExpectIntEQ(wc_InitRng(&rng), 0);
  21749. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21750. #if defined(WOLFSSL_ASYNC_CRYPT)
  21751. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21752. #endif
  21753. ExpectIntEQ(ret, 0);
  21754. PRIVATE_KEY_UNLOCK();
  21755. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21756. PRIVATE_KEY_LOCK();
  21757. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21758. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21759. x963KeySz, &keyImp), 0);
  21760. /* Pass in bad args. */
  21761. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21762. x963KeySz, NULL), BAD_FUNC_ARG);
  21763. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21764. &keyImp), BAD_FUNC_ARG);
  21765. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21766. wc_ecc_free(&keyImp);
  21767. wc_ecc_free(&key);
  21768. #ifdef FP_ECC
  21769. wc_ecc_fp_free();
  21770. #endif
  21771. #endif
  21772. return EXPECT_RESULT();
  21773. } /* END test_wc_ecc_import_private_key */
  21774. /*
  21775. * Testing wc_ecc_export_private_only()
  21776. */
  21777. static int test_wc_ecc_export_private_only(void)
  21778. {
  21779. EXPECT_DECLS;
  21780. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21781. ecc_key key;
  21782. WC_RNG rng;
  21783. byte out[ECC_PRIV_KEY_BUF];
  21784. word32 outlen = sizeof(out);
  21785. int ret;
  21786. /* Init stack variables. */
  21787. XMEMSET(&key, 0, sizeof(ecc_key));
  21788. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21789. XMEMSET(out, 0, outlen);
  21790. ExpectIntEQ(wc_ecc_init(&key), 0);
  21791. ExpectIntEQ(wc_InitRng(&rng), 0);
  21792. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21793. #if defined(WOLFSSL_ASYNC_CRYPT)
  21794. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21795. #endif
  21796. ExpectIntEQ(ret, 0);
  21797. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21798. /* Pass in bad args. */
  21799. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21800. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21801. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21802. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21803. wc_ecc_free(&key);
  21804. #ifdef FP_ECC
  21805. wc_ecc_fp_free();
  21806. #endif
  21807. #endif
  21808. return EXPECT_RESULT();
  21809. } /* END test_wc_ecc_export_private_only */
  21810. /*
  21811. * Testing wc_ecc_rs_to_sig()
  21812. */
  21813. static int test_wc_ecc_rs_to_sig(void)
  21814. {
  21815. EXPECT_DECLS;
  21816. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21817. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21818. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21819. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21820. const char* zeroStr = "0";
  21821. byte sig[ECC_MAX_SIG_SIZE];
  21822. word32 siglen = (word32)sizeof(sig);
  21823. /* R and S max size is the order of curve. 2^192.*/
  21824. int keySz = KEY24;
  21825. byte r[KEY24];
  21826. byte s[KEY24];
  21827. word32 rlen = (word32)sizeof(r);
  21828. word32 slen = (word32)sizeof(s);
  21829. /* Init stack variables. */
  21830. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21831. XMEMSET(r, 0, keySz);
  21832. XMEMSET(s, 0, keySz);
  21833. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21834. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21835. /* Test bad args. */
  21836. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21837. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21838. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21839. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21840. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21841. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21842. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21843. ECC_BAD_ARG_E);
  21844. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21845. ECC_BAD_ARG_E);
  21846. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21847. ECC_BAD_ARG_E);
  21848. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21849. ECC_BAD_ARG_E);
  21850. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21851. ECC_BAD_ARG_E);
  21852. #endif
  21853. return EXPECT_RESULT();
  21854. } /* END test_wc_ecc_rs_to_sig */
  21855. static int test_wc_ecc_import_raw(void)
  21856. {
  21857. EXPECT_DECLS;
  21858. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21859. ecc_key key;
  21860. const char* qx =
  21861. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21862. const char* qy =
  21863. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21864. const char* d =
  21865. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21866. const char* curveName = "SECP256R1";
  21867. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21868. const char* kNullStr = "";
  21869. int ret;
  21870. #endif
  21871. XMEMSET(&key, 0, sizeof(ecc_key));
  21872. ExpectIntEQ(wc_ecc_init(&key), 0);
  21873. /* Test good import */
  21874. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21875. /* Test bad args. */
  21876. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21877. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21878. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21879. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21880. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21881. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21882. wc_ecc_free(&key);
  21883. #endif
  21884. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21885. curveName), 0);
  21886. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21887. #endif
  21888. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21889. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21890. wc_ecc_free(&key);
  21891. #endif
  21892. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21893. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21894. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21895. #else
  21896. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21897. #endif
  21898. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21899. wc_ecc_free(&key);
  21900. #endif
  21901. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21902. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21903. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21904. #else
  21905. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21906. #endif
  21907. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21908. wc_ecc_free(&key);
  21909. #endif
  21910. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21911. #endif
  21912. wc_ecc_free(&key);
  21913. #endif
  21914. return EXPECT_RESULT();
  21915. } /* END test_wc_ecc_import_raw */
  21916. static int test_wc_ecc_import_unsigned(void)
  21917. {
  21918. EXPECT_DECLS;
  21919. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21920. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21921. HAVE_FIPS_VERSION >= 2))
  21922. ecc_key key;
  21923. const byte qx[] = {
  21924. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21925. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21926. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21927. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21928. };
  21929. const byte qy[] = {
  21930. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21931. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21932. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21933. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21934. };
  21935. const byte d[] = {
  21936. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21937. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21938. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21939. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21940. };
  21941. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21942. const byte nullBytes[32] = {0};
  21943. int ret;
  21944. #endif
  21945. int curveId = ECC_SECP256R1;
  21946. XMEMSET(&key, 0, sizeof(ecc_key));
  21947. ExpectIntEQ(wc_ecc_init(&key), 0);
  21948. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21949. curveId), 0);
  21950. /* Test bad args. */
  21951. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21952. curveId), BAD_FUNC_ARG);
  21953. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21954. curveId), BAD_FUNC_ARG);
  21955. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21956. curveId), BAD_FUNC_ARG);
  21957. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21958. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21959. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21960. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21961. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21962. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21963. #endif
  21964. wc_ecc_free(&key);
  21965. #endif
  21966. return EXPECT_RESULT();
  21967. } /* END test_wc_ecc_import_unsigned */
  21968. /*
  21969. * Testing wc_ecc_sig_size()
  21970. */
  21971. static int test_wc_ecc_sig_size(void)
  21972. {
  21973. EXPECT_DECLS;
  21974. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21975. ecc_key key;
  21976. WC_RNG rng;
  21977. int keySz = KEY16;
  21978. int ret;
  21979. XMEMSET(&rng, 0, sizeof(rng));
  21980. XMEMSET(&key, 0, sizeof(key));
  21981. ExpectIntEQ(wc_ecc_init(&key), 0);
  21982. ExpectIntEQ(wc_InitRng(&rng), 0);
  21983. ret = wc_ecc_make_key(&rng, keySz, &key);
  21984. #if defined(WOLFSSL_ASYNC_CRYPT)
  21985. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21986. #endif
  21987. ExpectIntEQ(ret, 0);
  21988. ExpectIntLE(wc_ecc_sig_size(&key),
  21989. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21990. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21991. wc_ecc_free(&key);
  21992. #endif
  21993. return EXPECT_RESULT();
  21994. } /* END test_wc_ecc_sig_size */
  21995. /*
  21996. * Testing wc_ecc_ctx_new()
  21997. */
  21998. static int test_wc_ecc_ctx_new(void)
  21999. {
  22000. EXPECT_DECLS;
  22001. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22002. WC_RNG rng;
  22003. ecEncCtx* cli = NULL;
  22004. ecEncCtx* srv = NULL;
  22005. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22006. ExpectIntEQ(wc_InitRng(&rng), 0);
  22007. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22008. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22009. wc_ecc_ctx_free(cli);
  22010. cli = NULL;
  22011. wc_ecc_ctx_free(srv);
  22012. /* Test bad args. */
  22013. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22014. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22015. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22016. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22017. wc_ecc_ctx_free(cli);
  22018. #endif
  22019. return EXPECT_RESULT();
  22020. } /* END test_wc_ecc_ctx_new */
  22021. /*
  22022. * Tesing wc_ecc_reset()
  22023. */
  22024. static int test_wc_ecc_ctx_reset(void)
  22025. {
  22026. EXPECT_DECLS;
  22027. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22028. ecEncCtx* ctx = NULL;
  22029. WC_RNG rng;
  22030. XMEMSET(&rng, 0, sizeof(rng));
  22031. ExpectIntEQ(wc_InitRng(&rng), 0);
  22032. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22033. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22034. /* Pass in bad args. */
  22035. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22036. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22037. wc_ecc_ctx_free(ctx);
  22038. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22039. #endif
  22040. return EXPECT_RESULT();
  22041. } /* END test_wc_ecc_ctx_reset */
  22042. /*
  22043. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22044. */
  22045. static int test_wc_ecc_ctx_set_peer_salt(void)
  22046. {
  22047. EXPECT_DECLS;
  22048. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22049. WC_RNG rng;
  22050. ecEncCtx* cliCtx = NULL;
  22051. ecEncCtx* servCtx = NULL;
  22052. const byte* cliSalt = NULL;
  22053. const byte* servSalt = NULL;
  22054. XMEMSET(&rng, 0, sizeof(rng));
  22055. ExpectIntEQ(wc_InitRng(&rng), 0);
  22056. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22057. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22058. /* Test bad args. */
  22059. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22060. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22061. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22062. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22063. /* Test bad args. */
  22064. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22065. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22066. wc_ecc_ctx_free(cliCtx);
  22067. wc_ecc_ctx_free(servCtx);
  22068. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22069. #endif
  22070. return EXPECT_RESULT();
  22071. } /* END test_wc_ecc_ctx_set_peer_salt */
  22072. /*
  22073. * Testing wc_ecc_ctx_set_info()
  22074. */
  22075. static int test_wc_ecc_ctx_set_info(void)
  22076. {
  22077. EXPECT_DECLS;
  22078. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22079. ecEncCtx* ctx = NULL;
  22080. WC_RNG rng;
  22081. const char* optInfo = "Optional Test Info.";
  22082. int optInfoSz = (int)XSTRLEN(optInfo);
  22083. const char* badOptInfo = NULL;
  22084. XMEMSET(&rng, 0, sizeof(rng));
  22085. ExpectIntEQ(wc_InitRng(&rng), 0);
  22086. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22087. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22088. /* Test bad args. */
  22089. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22090. BAD_FUNC_ARG);
  22091. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22092. BAD_FUNC_ARG);
  22093. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22094. wc_ecc_ctx_free(ctx);
  22095. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22096. #endif
  22097. return EXPECT_RESULT();
  22098. } /* END test_wc_ecc_ctx_set_info */
  22099. /*
  22100. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22101. */
  22102. static int test_wc_ecc_encryptDecrypt(void)
  22103. {
  22104. EXPECT_DECLS;
  22105. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22106. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22107. ecc_key srvKey;
  22108. ecc_key cliKey;
  22109. ecc_key tmpKey;
  22110. WC_RNG rng;
  22111. int ret;
  22112. const char* msg = "EccBlock Size 16";
  22113. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22114. #ifdef WOLFSSL_ECIES_OLD
  22115. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22116. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22117. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22118. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22119. #else
  22120. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22121. WC_SHA256_DIGEST_SIZE];
  22122. #endif
  22123. word32 outSz = (word32)sizeof(out);
  22124. byte plain[sizeof("EccBlock Size 16")];
  22125. word32 plainSz = (word32)sizeof(plain);
  22126. int keySz = KEY20;
  22127. /* Init stack variables. */
  22128. XMEMSET(out, 0, outSz);
  22129. XMEMSET(plain, 0, plainSz);
  22130. XMEMSET(&rng, 0, sizeof(rng));
  22131. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22132. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22133. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22134. ExpectIntEQ(wc_InitRng(&rng), 0);
  22135. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22136. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22137. #if defined(WOLFSSL_ASYNC_CRYPT)
  22138. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22139. #endif
  22140. ExpectIntEQ(ret, 0);
  22141. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22142. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22143. #if defined(WOLFSSL_ASYNC_CRYPT)
  22144. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22145. #endif
  22146. ExpectIntEQ(ret, 0);
  22147. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22148. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22149. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22150. !defined(HAVE_SELFTEST)
  22151. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22152. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22153. #endif
  22154. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22155. &outSz, NULL), 0);
  22156. /* Test bad args. */
  22157. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22158. NULL), BAD_FUNC_ARG);
  22159. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22160. NULL), BAD_FUNC_ARG);
  22161. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22162. NULL), BAD_FUNC_ARG);
  22163. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22164. &outSz, NULL), BAD_FUNC_ARG);
  22165. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22166. NULL), BAD_FUNC_ARG);
  22167. #ifdef WOLFSSL_ECIES_OLD
  22168. tmpKey.dp = cliKey.dp;
  22169. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22170. #endif
  22171. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22172. NULL), 0);
  22173. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22174. NULL), BAD_FUNC_ARG);
  22175. #ifdef WOLFSSL_ECIES_OLD
  22176. /* NULL parameter allowed in new implementations - public key comes from
  22177. * the message. */
  22178. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22179. NULL), BAD_FUNC_ARG);
  22180. #endif
  22181. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22182. NULL), BAD_FUNC_ARG);
  22183. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22184. NULL), BAD_FUNC_ARG);
  22185. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22186. BAD_FUNC_ARG);
  22187. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22188. wc_ecc_free(&tmpKey);
  22189. wc_ecc_free(&srvKey);
  22190. wc_ecc_free(&cliKey);
  22191. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22192. #endif
  22193. return EXPECT_RESULT();
  22194. } /* END test_wc_ecc_encryptDecrypt */
  22195. /*
  22196. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22197. */
  22198. static int test_wc_ecc_del_point(void)
  22199. {
  22200. EXPECT_DECLS;
  22201. #if defined(HAVE_ECC)
  22202. ecc_point* pt = NULL;
  22203. ExpectNotNull(pt = wc_ecc_new_point());
  22204. wc_ecc_del_point(pt);
  22205. #endif
  22206. return EXPECT_RESULT();
  22207. } /* END test_wc_ecc_del_point */
  22208. /*
  22209. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22210. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22211. * and wc_ecc_cmp_point()
  22212. */
  22213. static int test_wc_ecc_pointFns(void)
  22214. {
  22215. EXPECT_DECLS;
  22216. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22217. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22218. !defined(WOLFSSL_ATECC608A)
  22219. ecc_key key;
  22220. WC_RNG rng;
  22221. int ret;
  22222. ecc_point* point = NULL;
  22223. ecc_point* cpypt = NULL;
  22224. int idx = 0;
  22225. int keySz = KEY32;
  22226. byte der[DER_SZ(KEY32)];
  22227. word32 derlenChk = 0;
  22228. word32 derSz = DER_SZ(KEY32);
  22229. /* Init stack variables. */
  22230. XMEMSET(der, 0, derSz);
  22231. XMEMSET(&key, 0, sizeof(ecc_key));
  22232. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22233. ExpectIntEQ(wc_InitRng(&rng), 0);
  22234. ExpectIntEQ(wc_ecc_init(&key), 0);
  22235. ret = wc_ecc_make_key(&rng, keySz, &key);
  22236. #if defined(WOLFSSL_ASYNC_CRYPT)
  22237. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22238. #endif
  22239. ExpectIntEQ(ret, 0);
  22240. ExpectNotNull(point = wc_ecc_new_point());
  22241. ExpectNotNull(cpypt = wc_ecc_new_point());
  22242. /* Export */
  22243. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22244. &derlenChk), LENGTH_ONLY_E);
  22245. /* Check length value. */
  22246. ExpectIntEQ(derSz, derlenChk);
  22247. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22248. &derSz), 0);
  22249. /* Test bad args. */
  22250. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22251. ECC_BAD_ARG_E);
  22252. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22253. ECC_BAD_ARG_E);
  22254. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22255. NULL), ECC_BAD_ARG_E);
  22256. /* Import */
  22257. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22258. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22259. /* Test bad args. */
  22260. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22261. ECC_BAD_ARG_E);
  22262. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22263. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22264. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22265. ECC_BAD_ARG_E);
  22266. /* Copy */
  22267. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22268. /* Test bad args. */
  22269. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22270. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22271. /* Compare point */
  22272. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22273. /* Test bad args. */
  22274. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22275. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22276. /* At infinity if return == 1, otherwise return == 0. */
  22277. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22278. /* Test bad args. */
  22279. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22280. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22281. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22282. #ifdef USE_ECC_B_PARAM
  22283. /* On curve if ret == 0 */
  22284. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22285. /* Test bad args. */
  22286. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22287. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22288. #endif /* USE_ECC_B_PARAM */
  22289. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22290. /* Free */
  22291. wc_ecc_del_point(point);
  22292. wc_ecc_del_point(cpypt);
  22293. wc_ecc_free(&key);
  22294. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22295. #endif
  22296. return EXPECT_RESULT();
  22297. } /* END test_wc_ecc_pointFns */
  22298. /*
  22299. * Testing wc_ecc_shared_secret_ssh()
  22300. */
  22301. static int test_wc_ecc_shared_secret_ssh(void)
  22302. {
  22303. EXPECT_DECLS;
  22304. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22305. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22306. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22307. !defined(WOLFSSL_CRYPTOCELL)
  22308. ecc_key key;
  22309. ecc_key key2;
  22310. WC_RNG rng;
  22311. int ret;
  22312. int keySz = KEY32;
  22313. #if FIPS_VERSION3_GE(6,0,0)
  22314. int key2Sz = KEY28;
  22315. #else
  22316. int key2Sz = KEY24;
  22317. #endif
  22318. byte secret[KEY32];
  22319. word32 secretLen = keySz;
  22320. /* Init stack variables. */
  22321. XMEMSET(&key, 0, sizeof(ecc_key));
  22322. XMEMSET(&key2, 0, sizeof(ecc_key));
  22323. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22324. XMEMSET(secret, 0, secretLen);
  22325. /* Make keys */
  22326. ExpectIntEQ(wc_ecc_init(&key), 0);
  22327. ExpectIntEQ(wc_InitRng(&rng), 0);
  22328. ret = wc_ecc_make_key(&rng, keySz, &key);
  22329. #if defined(WOLFSSL_ASYNC_CRYPT)
  22330. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22331. #endif
  22332. ExpectIntEQ(ret, 0);
  22333. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22334. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22335. ExpectIntEQ(wc_InitRng(&rng), 0);
  22336. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22337. #if defined(WOLFSSL_ASYNC_CRYPT)
  22338. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22339. #endif
  22340. ExpectIntEQ(ret, 0);
  22341. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22342. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22343. !defined(HAVE_SELFTEST)
  22344. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22345. #endif
  22346. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22347. &secretLen), 0);
  22348. /* Pass in bad args. */
  22349. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22350. &secretLen), BAD_FUNC_ARG);
  22351. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22352. BAD_FUNC_ARG);
  22353. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22354. BAD_FUNC_ARG);
  22355. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22356. BAD_FUNC_ARG);
  22357. key.type = ECC_PUBLICKEY;
  22358. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22359. &secretLen), ECC_BAD_ARG_E);
  22360. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22361. wc_ecc_free(&key);
  22362. wc_ecc_free(&key2);
  22363. #ifdef FP_ECC
  22364. wc_ecc_fp_free();
  22365. #endif
  22366. #endif
  22367. return EXPECT_RESULT();
  22368. } /* END test_wc_ecc_shared_secret_ssh */
  22369. /*
  22370. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22371. */
  22372. static int test_wc_ecc_verify_hash_ex(void)
  22373. {
  22374. EXPECT_DECLS;
  22375. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22376. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22377. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22378. ecc_key key;
  22379. WC_RNG rng;
  22380. int ret;
  22381. mp_int r;
  22382. mp_int s;
  22383. mp_int z;
  22384. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22385. unsigned char iHash[] = "Everyone gets Friday off.......";
  22386. unsigned char shortHash[] = TEST_STRING;
  22387. word32 hashlen = sizeof(hash);
  22388. word32 iHashLen = sizeof(iHash);
  22389. word32 shortHashLen = sizeof(shortHash);
  22390. int keySz = KEY32;
  22391. int verify_ok = 0;
  22392. XMEMSET(&key, 0, sizeof(ecc_key));
  22393. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22394. XMEMSET(&r, 0, sizeof(mp_int));
  22395. XMEMSET(&s, 0, sizeof(mp_int));
  22396. XMEMSET(&z, 0, sizeof(mp_int));
  22397. /* Initialize r, s and z. */
  22398. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22399. ExpectIntEQ(wc_ecc_init(&key), 0);
  22400. ExpectIntEQ(wc_InitRng(&rng), 0);
  22401. ret = wc_ecc_make_key(&rng, keySz, &key);
  22402. #if defined(WOLFSSL_ASYNC_CRYPT)
  22403. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22404. #endif
  22405. ExpectIntEQ(ret, 0);
  22406. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22407. /* verify_ok should be 1. */
  22408. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22409. 0);
  22410. ExpectIntEQ(verify_ok, 1);
  22411. /* verify_ok should be 0 */
  22412. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22413. &key), 0);
  22414. ExpectIntEQ(verify_ok, 0);
  22415. /* verify_ok should be 0. */
  22416. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22417. &verify_ok, &key), 0);
  22418. ExpectIntEQ(verify_ok, 0);
  22419. /* Test bad args. */
  22420. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22421. ECC_BAD_ARG_E);
  22422. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22423. ECC_BAD_ARG_E);
  22424. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22425. ECC_BAD_ARG_E);
  22426. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22427. ECC_BAD_ARG_E);
  22428. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22429. ECC_BAD_ARG_E);
  22430. /* Test bad args. */
  22431. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22432. &verify_ok, &key), ECC_BAD_ARG_E);
  22433. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22434. &verify_ok, &key), ECC_BAD_ARG_E);
  22435. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22436. &verify_ok, &key), MP_ZERO_E);
  22437. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22438. &verify_ok, &key), MP_ZERO_E);
  22439. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22440. &verify_ok, &key), MP_ZERO_E);
  22441. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22442. &key), ECC_BAD_ARG_E);
  22443. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22444. &key), ECC_BAD_ARG_E);
  22445. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22446. &verify_ok, NULL), ECC_BAD_ARG_E);
  22447. wc_ecc_free(&key);
  22448. mp_free(&r);
  22449. mp_free(&s);
  22450. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22451. #endif
  22452. return EXPECT_RESULT();
  22453. } /* END test_wc_ecc_verify_hash_ex */
  22454. /*
  22455. * Testing wc_ecc_mulmod()
  22456. */
  22457. static int test_wc_ecc_mulmod(void)
  22458. {
  22459. EXPECT_DECLS;
  22460. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22461. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22462. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22463. ecc_key key1;
  22464. ecc_key key2;
  22465. ecc_key key3;
  22466. WC_RNG rng;
  22467. int ret;
  22468. XMEMSET(&key1, 0, sizeof(ecc_key));
  22469. XMEMSET(&key2, 0, sizeof(ecc_key));
  22470. XMEMSET(&key3, 0, sizeof(ecc_key));
  22471. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22472. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22473. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22474. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22475. ExpectIntEQ(wc_InitRng(&rng), 0);
  22476. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22477. #if defined(WOLFSSL_ASYNC_CRYPT)
  22478. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22479. #endif
  22480. ExpectIntEQ(ret, 0);
  22481. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22482. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22483. key1.dp->Af, ECC_SECP256R1), 0);
  22484. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22485. key1.dp->prime, ECC_SECP256R1), 0);
  22486. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22487. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22488. 1), 0);
  22489. /* Test bad args. */
  22490. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22491. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22492. ECC_BAD_ARG_E);
  22493. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22494. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22495. ECC_BAD_ARG_E);
  22496. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22497. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22498. ECC_BAD_ARG_E);
  22499. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22500. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22501. wc_ecc_free(&key1);
  22502. wc_ecc_free(&key2);
  22503. wc_ecc_free(&key3);
  22504. #ifdef FP_ECC
  22505. wc_ecc_fp_free();
  22506. #endif
  22507. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22508. return EXPECT_RESULT();
  22509. } /* END test_wc_ecc_mulmod */
  22510. /*
  22511. * Testing wc_ecc_is_valid_idx()
  22512. */
  22513. static int test_wc_ecc_is_valid_idx(void)
  22514. {
  22515. EXPECT_DECLS;
  22516. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22517. ecc_key key;
  22518. WC_RNG rng;
  22519. int ret;
  22520. int iVal = -2;
  22521. int iVal2 = 3000;
  22522. XMEMSET(&key, 0, sizeof(ecc_key));
  22523. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22524. ExpectIntEQ(wc_ecc_init(&key), 0);
  22525. ExpectIntEQ(wc_InitRng(&rng), 0);
  22526. ret = wc_ecc_make_key(&rng, 32, &key);
  22527. #if defined(WOLFSSL_ASYNC_CRYPT)
  22528. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22529. #endif
  22530. ExpectIntEQ(ret, 0);
  22531. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22532. /* Test bad args. */
  22533. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22534. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22535. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22536. wc_ecc_free(&key);
  22537. #ifdef FP_ECC
  22538. wc_ecc_fp_free();
  22539. #endif
  22540. #endif
  22541. return EXPECT_RESULT();
  22542. } /* END test_wc_ecc_is_valid_idx */
  22543. /*
  22544. * Testing wc_ecc_get_curve_id_from_oid()
  22545. */
  22546. static int test_wc_ecc_get_curve_id_from_oid(void)
  22547. {
  22548. EXPECT_DECLS;
  22549. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22550. !defined(HAVE_FIPS)
  22551. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22552. word32 len = sizeof(oid);
  22553. /* Bad Cases */
  22554. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22555. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22556. /* Good Case */
  22557. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22558. #endif
  22559. return EXPECT_RESULT();
  22560. } /* END test_wc_ecc_get_curve_id_from_oid */
  22561. /*
  22562. * Testing wc_ecc_sig_size_calc()
  22563. */
  22564. static int test_wc_ecc_sig_size_calc(void)
  22565. {
  22566. EXPECT_DECLS;
  22567. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22568. ecc_key key;
  22569. WC_RNG rng;
  22570. int sz = 0;
  22571. int ret;
  22572. XMEMSET(&key, 0, sizeof(ecc_key));
  22573. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22574. ExpectIntEQ(wc_ecc_init(&key), 0);
  22575. ExpectIntEQ(wc_InitRng(&rng), 0);
  22576. ret = wc_ecc_make_key(&rng, 16, &key);
  22577. #if defined(WOLFSSL_ASYNC_CRYPT)
  22578. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22579. #endif
  22580. #if FIPS_VERSION3_GE(6,0,0)
  22581. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22582. #else
  22583. ExpectIntEQ(ret, 0);
  22584. #endif
  22585. #if FIPS_VERSION3_LT(6,0,0)
  22586. sz = key.dp->size;
  22587. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22588. #else
  22589. (void) sz;
  22590. #endif
  22591. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22592. wc_ecc_free(&key);
  22593. #endif
  22594. return EXPECT_RESULT();
  22595. } /* END test_wc_ecc_sig_size_calc */
  22596. /*
  22597. * Testing wc_ecc_sm2_make_key()
  22598. */
  22599. static int test_wc_ecc_sm2_make_key(void)
  22600. {
  22601. int res = TEST_SKIPPED;
  22602. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22603. EXPECT_DECLS;
  22604. WC_RNG rng[1];
  22605. ecc_key key[1];
  22606. XMEMSET(rng, 0, sizeof(*rng));
  22607. XMEMSET(key, 0, sizeof(*key));
  22608. ExpectIntEQ(wc_InitRng(rng), 0);
  22609. ExpectIntEQ(wc_ecc_init(key), 0);
  22610. /* Test invalid parameters. */
  22611. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22612. BAD_FUNC_ARG);
  22613. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22614. BAD_FUNC_ARG);
  22615. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22616. BAD_FUNC_ARG);
  22617. /* Test valid parameters. */
  22618. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22619. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22620. wc_ecc_free(key);
  22621. wc_FreeRng(rng);
  22622. #ifdef FP_ECC
  22623. wc_ecc_fp_free();
  22624. #endif
  22625. res = EXPECT_RESULT();
  22626. #endif
  22627. return res;
  22628. }
  22629. /*
  22630. * Testing wc_ecc_sm2_shared_secret()
  22631. */
  22632. static int test_wc_ecc_sm2_shared_secret(void)
  22633. {
  22634. int res = TEST_SKIPPED;
  22635. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22636. EXPECT_DECLS;
  22637. WC_RNG rng[1];
  22638. ecc_key keyA[1];
  22639. ecc_key keyB[1];
  22640. byte outA[32];
  22641. byte outB[32];
  22642. word32 outALen = 32;
  22643. word32 outBLen = 32;
  22644. XMEMSET(rng, 0, sizeof(*rng));
  22645. XMEMSET(keyA, 0, sizeof(*keyA));
  22646. XMEMSET(keyB, 0, sizeof(*keyB));
  22647. ExpectIntEQ(wc_InitRng(rng), 0);
  22648. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22649. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22650. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22651. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22652. #ifdef ECC_TIMING_RESISTANT
  22653. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22654. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22655. #endif
  22656. /* Test invalid parameters. */
  22657. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22658. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22659. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22660. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22661. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22662. BAD_FUNC_ARG);
  22663. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22664. BAD_FUNC_ARG);
  22665. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22666. BAD_FUNC_ARG);
  22667. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22668. BAD_FUNC_ARG);
  22669. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22670. /* Test valid parameters. */
  22671. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22672. ExpectIntLE(outALen, 32);
  22673. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22674. ExpectIntLE(outBLen, 32);
  22675. ExpectIntEQ(outALen, outBLen);
  22676. ExpectBufEQ(outA, outB, outALen);
  22677. wc_ecc_free(keyB);
  22678. wc_ecc_free(keyA);
  22679. wc_FreeRng(rng);
  22680. #ifdef FP_ECC
  22681. wc_ecc_fp_free();
  22682. #endif
  22683. res = EXPECT_RESULT();
  22684. #endif
  22685. return res;
  22686. }
  22687. /*
  22688. * Testing wc_ecc_sm2_create_digest()
  22689. */
  22690. static int test_wc_ecc_sm2_create_digest(void)
  22691. {
  22692. int res = TEST_SKIPPED;
  22693. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22694. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22695. EXPECT_DECLS;
  22696. ecc_key key[1];
  22697. enum wc_HashType hashType;
  22698. unsigned char pub[] = {
  22699. 0x04,
  22700. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22701. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22702. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22703. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22704. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22705. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22706. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22707. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22708. };
  22709. unsigned char id[] = {
  22710. 0x01, 0x02, 0x03,
  22711. };
  22712. unsigned char msg[] = {
  22713. 0x01, 0x02, 0x03,
  22714. };
  22715. unsigned char hash[32];
  22716. #ifdef WOLFSSL_SM3
  22717. unsigned char expHash[32] = {
  22718. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22719. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22720. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22721. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22722. };
  22723. #else
  22724. unsigned char expHash[32] = {
  22725. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22726. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22727. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22728. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22729. };
  22730. #endif
  22731. #ifdef WOLFSSL_SM3
  22732. hashType = WC_HASH_TYPE_SM3;
  22733. #else
  22734. hashType = WC_HASH_TYPE_SHA256;
  22735. #endif
  22736. XMEMSET(key, 0, sizeof(*key));
  22737. ExpectIntEQ(wc_ecc_init(key), 0);
  22738. /* Test with no curve set. */
  22739. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22740. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22741. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22742. /* Test invalid parameters. */
  22743. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22744. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22745. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22746. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22747. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22748. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22749. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22750. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22751. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22752. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22753. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22754. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22755. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22756. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22757. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22758. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22759. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22760. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22761. /* Bad hash type. */
  22762. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22763. -1, hash, 0, key), BAD_FUNC_ARG);
  22764. /* Bad hash size. */
  22765. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22766. hashType, hash, 0, key), BUFFER_E);
  22767. /* Test valid parameters. */
  22768. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22769. hashType, hash, sizeof(hash), key), 0);
  22770. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22771. wc_ecc_free(key);
  22772. res = EXPECT_RESULT();
  22773. #endif
  22774. return res;
  22775. }
  22776. /*
  22777. * Testing wc_ecc_sm2_verify_hash_ex()
  22778. */
  22779. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22780. {
  22781. int res = TEST_SKIPPED;
  22782. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22783. defined(WOLFSSL_PUBLIC_MP)
  22784. EXPECT_DECLS;
  22785. ecc_key key[1];
  22786. mp_int r[1];
  22787. mp_int s[1];
  22788. int verified;
  22789. unsigned char pub[] = {
  22790. 0x04,
  22791. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22792. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22793. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22794. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22795. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22796. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22797. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22798. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22799. };
  22800. unsigned char hash[] = {
  22801. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22802. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22803. 0x50, 0x69, 0x5B, 0x20
  22804. };
  22805. unsigned char rData[] = {
  22806. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22807. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22808. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22809. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22810. };
  22811. unsigned char sData[] = {
  22812. 0x1D,
  22813. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22814. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22815. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22816. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22817. };
  22818. unsigned char rBadData[] = {
  22819. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22820. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22821. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22822. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22823. };
  22824. XMEMSET(key, 0, sizeof(*key));
  22825. XMEMSET(r, 0, sizeof(*r));
  22826. XMEMSET(s, 0, sizeof(*s));
  22827. ExpectIntEQ(mp_init(r), 0);
  22828. ExpectIntEQ(mp_init(s), 0);
  22829. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22830. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22831. ExpectIntEQ(wc_ecc_init(key), 0);
  22832. /* Test with no curve set. */
  22833. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22834. &verified, key), BAD_FUNC_ARG);
  22835. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22836. /* Test invalid parameters. */
  22837. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22838. NULL, NULL), BAD_FUNC_ARG);
  22839. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22840. NULL, NULL), BAD_FUNC_ARG);
  22841. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22842. NULL, NULL), BAD_FUNC_ARG);
  22843. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22844. NULL, NULL), BAD_FUNC_ARG);
  22845. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22846. &verified, NULL), BAD_FUNC_ARG);
  22847. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22848. NULL, key), BAD_FUNC_ARG);
  22849. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22850. &verified, key), BAD_FUNC_ARG);
  22851. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22852. &verified, key), BAD_FUNC_ARG);
  22853. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22854. &verified, key), BAD_FUNC_ARG);
  22855. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22856. NULL, key), BAD_FUNC_ARG);
  22857. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22858. &verified, NULL), BAD_FUNC_ARG);
  22859. /* Make key not on the SM2 curve. */
  22860. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22861. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22862. &verified, key), BAD_FUNC_ARG);
  22863. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22864. /* Test valid parameters. */
  22865. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22866. &verified, key), 0);
  22867. ExpectIntEQ(verified, 1);
  22868. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22869. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22870. &verified, key), 0);
  22871. ExpectIntEQ(verified, 0);
  22872. mp_free(s);
  22873. mp_free(r);
  22874. wc_ecc_free(key);
  22875. #ifdef FP_ECC
  22876. wc_ecc_fp_free();
  22877. #endif
  22878. res = EXPECT_RESULT();
  22879. #endif
  22880. return res;
  22881. }
  22882. /*
  22883. * Testing wc_ecc_sm2_verify_hash()
  22884. */
  22885. static int test_wc_ecc_sm2_verify_hash(void)
  22886. {
  22887. int res = TEST_SKIPPED;
  22888. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22889. EXPECT_DECLS;
  22890. ecc_key key[1];
  22891. int verified;
  22892. unsigned char pub[] = {
  22893. 0x04,
  22894. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22895. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22896. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22897. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22898. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22899. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22900. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22901. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22902. };
  22903. unsigned char hash[] = {
  22904. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22905. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22906. 0x50, 0x69, 0x5B, 0x20
  22907. };
  22908. unsigned char sig[] = {
  22909. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22910. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22911. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22912. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22913. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22914. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22915. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22916. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22917. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22918. };
  22919. unsigned char sigBad[] = {
  22920. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22921. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22922. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22923. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22924. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22925. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22926. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22927. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22928. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22929. };
  22930. XMEMSET(key, 0, sizeof(*key));
  22931. ExpectIntEQ(wc_ecc_init(key), 0);
  22932. /* Test with no curve set. */
  22933. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22934. &verified, key), BAD_FUNC_ARG);
  22935. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22936. /* Test invalid parameters. */
  22937. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22938. NULL, NULL), BAD_FUNC_ARG);
  22939. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22940. NULL, NULL), BAD_FUNC_ARG);
  22941. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22942. NULL, NULL), BAD_FUNC_ARG);
  22943. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22944. &verified, NULL), BAD_FUNC_ARG);
  22945. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22946. NULL, key), BAD_FUNC_ARG);
  22947. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22948. &verified, key), BAD_FUNC_ARG);
  22949. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22950. &verified, key), BAD_FUNC_ARG);
  22951. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22952. NULL, key), BAD_FUNC_ARG);
  22953. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22954. &verified, NULL), BAD_FUNC_ARG);
  22955. /* Make key not on the SM2 curve. */
  22956. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22957. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22958. &verified, key), BAD_FUNC_ARG);
  22959. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22960. /* Test valid parameters. */
  22961. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22962. &verified, key), 0);
  22963. ExpectIntEQ(verified, 1);
  22964. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22965. sizeof(hash), &verified, key), 0);
  22966. ExpectIntEQ(verified, 0);
  22967. wc_ecc_free(key);
  22968. #ifdef FP_ECC
  22969. wc_ecc_fp_free();
  22970. #endif
  22971. res = EXPECT_RESULT();
  22972. #endif
  22973. return res;
  22974. }
  22975. /*
  22976. * Testing wc_ecc_sm2_verify_hash_ex()
  22977. */
  22978. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22979. {
  22980. int res = TEST_SKIPPED;
  22981. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22982. defined(WOLFSSL_PUBLIC_MP)
  22983. EXPECT_DECLS;
  22984. WC_RNG rng[1];
  22985. ecc_key key[1];
  22986. mp_int r[1];
  22987. mp_int s[1];
  22988. unsigned char hash[32];
  22989. #ifdef HAVE_ECC_VERIFY
  22990. int verified;
  22991. #endif
  22992. XMEMSET(rng, 0, sizeof(*rng));
  22993. XMEMSET(key, 0, sizeof(*key));
  22994. XMEMSET(r, 0, sizeof(*r));
  22995. XMEMSET(s, 0, sizeof(*s));
  22996. ExpectIntEQ(wc_InitRng(rng), 0);
  22997. ExpectIntEQ(mp_init(r), 0);
  22998. ExpectIntEQ(mp_init(s), 0);
  22999. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23000. ExpectIntEQ(wc_ecc_init(key), 0);
  23001. /* Test with no curve set. */
  23002. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23003. BAD_FUNC_ARG);
  23004. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23005. /* Test invalid parameters. */
  23006. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23007. NULL), BAD_FUNC_ARG);
  23008. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23009. NULL), BAD_FUNC_ARG);
  23010. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23011. NULL), BAD_FUNC_ARG);
  23012. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23013. NULL), BAD_FUNC_ARG);
  23014. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23015. NULL), BAD_FUNC_ARG);
  23016. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23017. s), BAD_FUNC_ARG);
  23018. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23019. BAD_FUNC_ARG);
  23020. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23021. BAD_FUNC_ARG);
  23022. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23023. BAD_FUNC_ARG);
  23024. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23025. BAD_FUNC_ARG);
  23026. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23027. BAD_FUNC_ARG);
  23028. /* Make key not on the SM2 curve. */
  23029. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23030. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23031. BAD_FUNC_ARG);
  23032. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23033. #ifdef WOLFSSL_SP_MATH_ALL
  23034. {
  23035. mp_int smallR[1];
  23036. sp_init_size(smallR, 1);
  23037. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23038. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23039. smallR, s), 0);
  23040. }
  23041. #endif
  23042. /* Test valid parameters. */
  23043. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23044. 0);
  23045. #ifdef HAVE_ECC_VERIFY
  23046. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23047. key), 0);
  23048. ExpectIntEQ(verified, 1);
  23049. #endif
  23050. mp_free(s);
  23051. mp_free(r);
  23052. wc_ecc_free(key);
  23053. wc_FreeRng(rng);
  23054. #ifdef FP_ECC
  23055. wc_ecc_fp_free();
  23056. #endif
  23057. res = EXPECT_RESULT();
  23058. #endif
  23059. return res;
  23060. }
  23061. /*
  23062. * Testing wc_ecc_sm2_verify_hash()
  23063. */
  23064. static int test_wc_ecc_sm2_sign_hash(void)
  23065. {
  23066. int res = TEST_SKIPPED;
  23067. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23068. EXPECT_DECLS;
  23069. WC_RNG rng[1];
  23070. ecc_key key[1];
  23071. unsigned char hash[32];
  23072. unsigned char sig[72];
  23073. word32 sigSz = sizeof(sig);
  23074. #ifdef HAVE_ECC_VERIFY
  23075. int verified;
  23076. #endif
  23077. XMEMSET(rng, 0, sizeof(*rng));
  23078. XMEMSET(key, 0, sizeof(*key));
  23079. ExpectIntEQ(wc_InitRng(rng), 0);
  23080. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23081. ExpectIntEQ(wc_ecc_init(key), 0);
  23082. /* Test with no curve set. */
  23083. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23084. BAD_FUNC_ARG);
  23085. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23086. /* Test invalid parameters. */
  23087. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23088. NULL), BAD_FUNC_ARG);
  23089. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23090. NULL), BAD_FUNC_ARG);
  23091. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23092. NULL), BAD_FUNC_ARG);
  23093. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23094. NULL), BAD_FUNC_ARG);
  23095. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23096. NULL), BAD_FUNC_ARG);
  23097. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23098. key), BAD_FUNC_ARG);
  23099. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23100. key), BAD_FUNC_ARG);
  23101. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23102. key), BAD_FUNC_ARG);
  23103. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23104. key), BAD_FUNC_ARG);
  23105. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23106. key), BAD_FUNC_ARG);
  23107. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23108. NULL), BAD_FUNC_ARG);
  23109. /* Make key not on the SM2 curve. */
  23110. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23111. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23112. BAD_FUNC_ARG);
  23113. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23114. /* Test valid parameters. */
  23115. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23116. 0);
  23117. #ifdef HAVE_ECC_VERIFY
  23118. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23119. &verified, key), 0);
  23120. ExpectIntEQ(verified, 1);
  23121. #endif
  23122. wc_ecc_free(key);
  23123. wc_FreeRng(rng);
  23124. #ifdef FP_ECC
  23125. wc_ecc_fp_free();
  23126. #endif
  23127. res = EXPECT_RESULT();
  23128. #endif
  23129. return res;
  23130. }
  23131. /*
  23132. * Testing ToTraditional
  23133. */
  23134. static int test_ToTraditional(void)
  23135. {
  23136. EXPECT_DECLS;
  23137. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23138. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23139. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23140. XFILE f = XBADFILE;
  23141. byte input[TWOK_BUF];
  23142. word32 sz = 0;
  23143. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23144. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23145. if (f != XBADFILE)
  23146. XFCLOSE(f);
  23147. /* Good case */
  23148. ExpectIntGT(ToTraditional(input, sz), 0);
  23149. /* Bad cases */
  23150. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23151. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23152. #ifdef WOLFSSL_ASN_TEMPLATE
  23153. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23154. #else
  23155. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23156. #endif
  23157. #endif
  23158. return EXPECT_RESULT();
  23159. } /* End test_ToTraditional*/
  23160. /*
  23161. * Testing wc_EccPrivateKeyToDer
  23162. */
  23163. static int test_wc_EccPrivateKeyToDer(void)
  23164. {
  23165. EXPECT_DECLS;
  23166. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23167. byte output[ONEK_BUF];
  23168. ecc_key eccKey;
  23169. WC_RNG rng;
  23170. word32 inLen;
  23171. word32 outLen = 0;
  23172. int ret;
  23173. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23174. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23175. ExpectIntEQ(wc_InitRng(&rng), 0);
  23176. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23177. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23178. #if defined(WOLFSSL_ASYNC_CRYPT)
  23179. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23180. #endif
  23181. ExpectIntEQ(ret, 0);
  23182. inLen = (word32)sizeof(output);
  23183. /* Bad Cases */
  23184. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23185. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23186. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23187. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23188. /* Good Case */
  23189. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23190. wc_ecc_free(&eccKey);
  23191. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23192. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23193. {
  23194. /* test importing private only into a PKEY struct */
  23195. EC_KEY* ec = NULL;
  23196. EVP_PKEY* pkey = NULL;
  23197. const unsigned char* der;
  23198. der = output;
  23199. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23200. der = output;
  23201. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23202. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23203. if (EXPECT_FAIL()) {
  23204. EC_KEY_free(ec);
  23205. }
  23206. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23207. }
  23208. #endif
  23209. #endif
  23210. return EXPECT_RESULT();
  23211. } /* End test_wc_EccPrivateKeyToDer*/
  23212. /*
  23213. * Testing wc_DhPublicKeyDecode
  23214. */
  23215. static int test_wc_DhPublicKeyDecode(void)
  23216. {
  23217. EXPECT_DECLS;
  23218. #ifndef NO_DH
  23219. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23220. DhKey key;
  23221. word32 inOutIdx;
  23222. XMEMSET(&key, 0, sizeof(DhKey));
  23223. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23224. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23225. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23226. BAD_FUNC_ARG);
  23227. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23228. BAD_FUNC_ARG);
  23229. inOutIdx = 0;
  23230. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23231. BAD_FUNC_ARG);
  23232. inOutIdx = 0;
  23233. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23234. BAD_FUNC_ARG);
  23235. inOutIdx = 0;
  23236. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23237. sizeof_dh_pub_key_der_2048), 0);
  23238. ExpectIntNE(key.p.used, 0);
  23239. ExpectIntNE(key.g.used, 0);
  23240. ExpectIntEQ(key.q.used, 0);
  23241. ExpectIntNE(key.pub.used, 0);
  23242. ExpectIntEQ(key.priv.used, 0);
  23243. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23244. #endif
  23245. #endif /* !NO_DH */
  23246. return EXPECT_RESULT();
  23247. }
  23248. /*
  23249. * Testing wc_Ed25519KeyToDer
  23250. */
  23251. static int test_wc_Ed25519KeyToDer(void)
  23252. {
  23253. EXPECT_DECLS;
  23254. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23255. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23256. byte output[ONEK_BUF];
  23257. ed25519_key ed25519Key;
  23258. WC_RNG rng;
  23259. word32 inLen;
  23260. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23261. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23262. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23263. ExpectIntEQ(wc_InitRng(&rng), 0);
  23264. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23265. inLen = (word32)sizeof(output);
  23266. /* Bad Cases */
  23267. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23268. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23269. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23270. /* Good Cases */
  23271. /* length only */
  23272. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23273. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23274. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23275. wc_ed25519_free(&ed25519Key);
  23276. #endif
  23277. return EXPECT_RESULT();
  23278. } /* End test_wc_Ed25519KeyToDer*/
  23279. /*
  23280. * Testing wc_Ed25519PrivateKeyToDer
  23281. */
  23282. static int test_wc_Ed25519PrivateKeyToDer(void)
  23283. {
  23284. EXPECT_DECLS;
  23285. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23286. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23287. byte output[ONEK_BUF];
  23288. ed25519_key ed25519PrivKey;
  23289. WC_RNG rng;
  23290. word32 inLen;
  23291. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23292. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23293. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23294. ExpectIntEQ(wc_InitRng(&rng), 0);
  23295. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23296. 0);
  23297. inLen = (word32)sizeof(output);
  23298. /* Bad Cases */
  23299. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23300. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23301. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23302. BAD_FUNC_ARG);
  23303. /* Good Cases */
  23304. /* length only */
  23305. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23306. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23307. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23308. wc_ed25519_free(&ed25519PrivKey);
  23309. #endif
  23310. return EXPECT_RESULT();
  23311. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23312. /*
  23313. * Testing wc_Ed448KeyToDer
  23314. */
  23315. static int test_wc_Ed448KeyToDer(void)
  23316. {
  23317. EXPECT_DECLS;
  23318. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23319. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23320. byte output[ONEK_BUF];
  23321. ed448_key ed448Key;
  23322. WC_RNG rng;
  23323. word32 inLen;
  23324. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23325. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23326. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23327. ExpectIntEQ(wc_InitRng(&rng), 0);
  23328. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23329. inLen = (word32)sizeof(output);
  23330. /* Bad Cases */
  23331. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23332. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23333. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23334. /* Good Cases */
  23335. /* length only */
  23336. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23337. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23338. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23339. wc_ed448_free(&ed448Key);
  23340. #endif
  23341. return EXPECT_RESULT();
  23342. } /* End test_wc_Ed448KeyToDer*/
  23343. /*
  23344. * Testing wc_Ed448PrivateKeyToDer
  23345. */
  23346. static int test_wc_Ed448PrivateKeyToDer(void)
  23347. {
  23348. EXPECT_DECLS;
  23349. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23350. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23351. byte output[ONEK_BUF];
  23352. ed448_key ed448PrivKey;
  23353. WC_RNG rng;
  23354. word32 inLen;
  23355. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23356. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23357. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23358. ExpectIntEQ(wc_InitRng(&rng), 0);
  23359. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23360. 0);
  23361. inLen = (word32)sizeof(output);
  23362. /* Bad Cases */
  23363. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23364. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23365. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23366. BAD_FUNC_ARG);
  23367. /* Good cases */
  23368. /* length only */
  23369. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23370. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23371. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23372. wc_ed448_free(&ed448PrivKey);
  23373. #endif
  23374. return EXPECT_RESULT();
  23375. } /* End test_wc_Ed448PrivateKeyToDer*/
  23376. /*
  23377. * Testing wc_SetSubjectBuffer
  23378. */
  23379. static int test_wc_SetSubjectBuffer(void)
  23380. {
  23381. EXPECT_DECLS;
  23382. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23383. Cert cert;
  23384. XFILE file = XBADFILE;
  23385. byte* der = NULL;
  23386. word32 derSz;
  23387. derSz = FOURK_BUF;
  23388. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23389. DYNAMIC_TYPE_TMP_BUFFER));
  23390. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23391. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23392. if (file != XBADFILE)
  23393. XFCLOSE(file);
  23394. ExpectIntEQ(wc_InitCert(&cert), 0);
  23395. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  23396. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  23397. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23398. #endif
  23399. return EXPECT_RESULT();
  23400. } /* End test_wc_SetSubjectBuffer*/
  23401. /*
  23402. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23403. */
  23404. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23405. {
  23406. EXPECT_DECLS;
  23407. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23408. WC_RNG rng;
  23409. Cert cert;
  23410. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23411. RsaKey rsaKey;
  23412. int bits = 2048;
  23413. #endif
  23414. #if defined(HAVE_ECC)
  23415. ecc_key eccKey;
  23416. int ret;
  23417. #endif
  23418. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23419. ed25519_key ed25519Key;
  23420. #endif
  23421. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23422. ed448_key ed448Key;
  23423. #endif
  23424. #ifndef HAVE_FIPS
  23425. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23426. #else
  23427. ExpectIntEQ(wc_InitRng(&rng), 0);
  23428. #endif
  23429. ExpectIntEQ(wc_InitCert(&cert), 0);
  23430. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23431. /* RSA */
  23432. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23433. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23434. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23435. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23436. 0);
  23437. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23438. #endif
  23439. #if defined(HAVE_ECC)
  23440. /* ECC */
  23441. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23442. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23443. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23444. #if defined(WOLFSSL_ASYNC_CRYPT)
  23445. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23446. #endif
  23447. ExpectIntEQ(ret, 0);
  23448. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23449. 0);
  23450. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23451. #endif
  23452. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23453. /* ED25519 */
  23454. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23455. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23456. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23457. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23458. &ed25519Key), 0);
  23459. wc_ed25519_free(&ed25519Key);
  23460. #endif
  23461. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23462. /* ED448 */
  23463. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23464. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23465. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23466. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23467. &ed448Key), 0);
  23468. wc_ed448_free(&ed448Key);
  23469. #endif
  23470. wc_FreeRng(&rng);
  23471. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23472. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23473. return EXPECT_RESULT();
  23474. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23475. /*
  23476. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23477. */
  23478. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23479. {
  23480. EXPECT_DECLS;
  23481. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23482. WC_RNG rng;
  23483. Cert cert;
  23484. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23485. RsaKey rsaKey;
  23486. int bits = 2048;
  23487. #endif
  23488. #if defined(HAVE_ECC)
  23489. ecc_key eccKey;
  23490. int ret;
  23491. #endif
  23492. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23493. ed25519_key ed25519Key;
  23494. #endif
  23495. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23496. ed448_key ed448Key;
  23497. #endif
  23498. #ifndef HAVE_FIPS
  23499. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23500. #else
  23501. ExpectIntEQ(wc_InitRng(&rng), 0);
  23502. #endif
  23503. ExpectIntEQ(wc_InitCert(&cert), 0);
  23504. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23505. /* RSA */
  23506. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23507. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23508. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23509. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23510. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23511. #endif
  23512. #if defined(HAVE_ECC)
  23513. /* ECC */
  23514. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23515. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23516. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23517. #if defined(WOLFSSL_ASYNC_CRYPT)
  23518. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23519. #endif
  23520. ExpectIntEQ(ret, 0);
  23521. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23522. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23523. #endif
  23524. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23525. /* ED25519 */
  23526. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23527. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23528. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23529. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23530. &ed25519Key), 0);
  23531. wc_ed25519_free(&ed25519Key);
  23532. #endif
  23533. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23534. /* ED448 */
  23535. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23536. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23537. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23538. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23539. 0);
  23540. wc_ed448_free(&ed448Key);
  23541. #endif
  23542. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23543. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23544. return EXPECT_RESULT();
  23545. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23546. /*
  23547. * Testing wc_PKCS7_New()
  23548. */
  23549. static int test_wc_PKCS7_New(void)
  23550. {
  23551. EXPECT_DECLS;
  23552. #if defined(HAVE_PKCS7)
  23553. PKCS7* pkcs7 = NULL;
  23554. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23555. wc_PKCS7_Free(pkcs7);
  23556. #endif
  23557. return EXPECT_RESULT();
  23558. } /* END test-wc_PKCS7_New */
  23559. /*
  23560. * Testing wc_PKCS7_Init()
  23561. */
  23562. static int test_wc_PKCS7_Init(void)
  23563. {
  23564. EXPECT_DECLS;
  23565. #if defined(HAVE_PKCS7)
  23566. PKCS7* pkcs7 = NULL;
  23567. void* heap = NULL;
  23568. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23569. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23570. /* Pass in bad args. */
  23571. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23572. wc_PKCS7_Free(pkcs7);
  23573. #endif
  23574. return EXPECT_RESULT();
  23575. } /* END test-wc_PKCS7_Init */
  23576. /*
  23577. * Testing wc_PKCS7_InitWithCert()
  23578. */
  23579. static int test_wc_PKCS7_InitWithCert(void)
  23580. {
  23581. EXPECT_DECLS;
  23582. #if defined(HAVE_PKCS7)
  23583. PKCS7* pkcs7 = NULL;
  23584. #ifndef NO_RSA
  23585. #if defined(USE_CERT_BUFFERS_2048)
  23586. unsigned char cert[sizeof(client_cert_der_2048)];
  23587. int certSz = (int)sizeof(cert);
  23588. XMEMSET(cert, 0, certSz);
  23589. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23590. #elif defined(USE_CERT_BUFFERS_1024)
  23591. unsigned char cert[sizeof(client_cert_der_1024)];
  23592. int certSz = (int)sizeof(cert);
  23593. XMEMSET(cert, 0, certSz);
  23594. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23595. #else
  23596. unsigned char cert[ONEK_BUF];
  23597. XFILE fp = XBADFILE;
  23598. int certSz;
  23599. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23600. XBADFILE);
  23601. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23602. fp), 0);
  23603. if (fp != XBADFILE)
  23604. XFCLOSE(fp);
  23605. #endif
  23606. #elif defined(HAVE_ECC)
  23607. #if defined(USE_CERT_BUFFERS_256)
  23608. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23609. int certSz = (int)sizeof(cert);
  23610. XMEMSET(cert, 0, certSz);
  23611. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23612. #else
  23613. unsigned char cert[ONEK_BUF];
  23614. XFILE fp = XBADFILE;
  23615. int certSz;
  23616. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23617. XBADFILE);
  23618. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23619. fp), 0);
  23620. if (fp != XBADFILE)
  23621. XFCLOSE(fp);
  23622. #endif
  23623. #else
  23624. #error PKCS7 requires ECC or RSA
  23625. #endif
  23626. #ifdef HAVE_ECC
  23627. {
  23628. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23629. static unsigned char certWithInvalidEccKey[] = {
  23630. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23631. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23632. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23633. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23634. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23635. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23636. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23637. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23638. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23639. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23640. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23641. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23642. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23643. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23644. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23645. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23646. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23647. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23648. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23649. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23650. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23651. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23652. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23653. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23654. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23655. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23656. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23657. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23658. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23659. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23660. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23661. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23662. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23663. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23664. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23665. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23666. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23667. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23668. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23669. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23670. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23671. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23672. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23673. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23674. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23675. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23676. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23677. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23678. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23679. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23680. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23681. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23682. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23683. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23684. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23685. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23686. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23687. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23688. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23689. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23690. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23691. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23692. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23693. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23694. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23695. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23696. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23697. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23698. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23699. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23700. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23701. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23702. 0x08, 0xA8, 0xB4
  23703. };
  23704. #endif
  23705. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23706. /* If initialization is not successful, it's free'd in init func. */
  23707. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23708. 0);
  23709. wc_PKCS7_Free(pkcs7);
  23710. pkcs7 = NULL;
  23711. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23712. /* Valid initialization usage. */
  23713. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23714. /* Pass in bad args. No need free for null checks, free at end.*/
  23715. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23716. BAD_FUNC_ARG);
  23717. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23718. BAD_FUNC_ARG);
  23719. #ifdef HAVE_ECC
  23720. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23721. sizeof(certWithInvalidEccKey)), 0);
  23722. }
  23723. #endif
  23724. wc_PKCS7_Free(pkcs7);
  23725. #endif
  23726. return EXPECT_RESULT();
  23727. } /* END test_wc_PKCS7_InitWithCert */
  23728. /*
  23729. * Testing wc_PKCS7_EncodeData()
  23730. */
  23731. static int test_wc_PKCS7_EncodeData(void)
  23732. {
  23733. EXPECT_DECLS;
  23734. #if defined(HAVE_PKCS7)
  23735. PKCS7* pkcs7 = NULL;
  23736. byte output[FOURK_BUF];
  23737. byte data[] = "My encoded DER cert.";
  23738. #ifndef NO_RSA
  23739. #if defined(USE_CERT_BUFFERS_2048)
  23740. unsigned char cert[sizeof(client_cert_der_2048)];
  23741. unsigned char key[sizeof(client_key_der_2048)];
  23742. int certSz = (int)sizeof(cert);
  23743. int keySz = (int)sizeof(key);
  23744. XMEMSET(cert, 0, certSz);
  23745. XMEMSET(key, 0, keySz);
  23746. XMEMCPY(cert, client_cert_der_2048, certSz);
  23747. XMEMCPY(key, client_key_der_2048, keySz);
  23748. #elif defined(USE_CERT_BUFFERS_1024)
  23749. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23750. unsigned char key[sizeof_client_key_der_1024];
  23751. int certSz = (int)sizeof(cert);
  23752. int keySz = (int)sizeof(key);
  23753. XMEMSET(cert, 0, certSz);
  23754. XMEMSET(key, 0, keySz);
  23755. XMEMCPY(cert, client_cert_der_1024, certSz);
  23756. XMEMCPY(key, client_key_der_1024, keySz);
  23757. #else
  23758. unsigned char cert[ONEK_BUF];
  23759. unsigned char key[ONEK_BUF];
  23760. XFILE fp = XBADFILE;
  23761. int certSz;
  23762. int keySz;
  23763. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23764. XBADFILE);
  23765. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23766. fp), 0);
  23767. if (fp != XBADFILE) {
  23768. XFCLOSE(fp);
  23769. fp = XBADFILE;
  23770. }
  23771. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23772. XBADFILE);
  23773. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23774. 0);
  23775. if (fp != XBADFILE)
  23776. XFCLOSE(fp);
  23777. #endif
  23778. #elif defined(HAVE_ECC)
  23779. #if defined(USE_CERT_BUFFERS_256)
  23780. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23781. unsigned char key[sizeof(ecc_clikey_der_256)];
  23782. int certSz = (int)sizeof(cert);
  23783. int keySz = (int)sizeof(key);
  23784. XMEMSET(cert, 0, certSz);
  23785. XMEMSET(key, 0, keySz);
  23786. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23787. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23788. #else
  23789. unsigned char cert[ONEK_BUF];
  23790. unsigned char key[ONEK_BUF];
  23791. XFILE fp = XBADFILE;
  23792. int certSz, keySz;
  23793. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23794. XBADFILE);
  23795. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23796. fp), 0);
  23797. if (fp != XBADFILE) {
  23798. XFCLOSE(fp);
  23799. fp = XBADFILE;
  23800. }
  23801. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23802. XBADFILE);
  23803. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23804. 0);
  23805. if (fp != XBADFILE)
  23806. XFCLOSE(fp);
  23807. #endif
  23808. #endif
  23809. XMEMSET(output, 0, sizeof(output));
  23810. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23811. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23812. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23813. if (pkcs7 != NULL) {
  23814. pkcs7->content = data;
  23815. pkcs7->contentSz = sizeof(data);
  23816. pkcs7->privateKey = key;
  23817. pkcs7->privateKeySz = keySz;
  23818. }
  23819. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23820. /* Test bad args. */
  23821. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23822. BAD_FUNC_ARG);
  23823. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23824. BAD_FUNC_ARG);
  23825. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23826. wc_PKCS7_Free(pkcs7);
  23827. #endif
  23828. return EXPECT_RESULT();
  23829. } /* END test_wc_PKCS7_EncodeData */
  23830. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23831. !defined(NO_RSA) && !defined(NO_SHA256)
  23832. /* RSA sign raw digest callback */
  23833. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23834. byte* out, word32 outSz, byte* privateKey,
  23835. word32 privateKeySz, int devid, int hashOID)
  23836. {
  23837. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23838. byte digInfoEncoding[] = {
  23839. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23840. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23841. 0x00, 0x04, 0x20
  23842. };
  23843. int ret;
  23844. byte digestInfo[ONEK_BUF];
  23845. byte sig[FOURK_BUF];
  23846. word32 digestInfoSz = 0;
  23847. word32 idx = 0;
  23848. RsaKey rsa;
  23849. /* SHA-256 required only for this example callback due to above
  23850. * digInfoEncoding[] */
  23851. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23852. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23853. (hashOID != SHA256h)) {
  23854. return -1;
  23855. }
  23856. /* build DigestInfo */
  23857. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23858. digestInfoSz += sizeof(digInfoEncoding);
  23859. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23860. digestInfoSz += digestSz;
  23861. /* set up RSA key */
  23862. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23863. if (ret != 0) {
  23864. return ret;
  23865. }
  23866. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23867. /* sign DigestInfo */
  23868. if (ret == 0) {
  23869. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23870. &rsa, pkcs7->rng);
  23871. if (ret > 0) {
  23872. if (ret > (int)outSz) {
  23873. /* output buffer too small */
  23874. ret = -1;
  23875. }
  23876. else {
  23877. /* success, ret holds sig size */
  23878. XMEMCPY(out, sig, ret);
  23879. }
  23880. }
  23881. }
  23882. wc_FreeRsaKey(&rsa);
  23883. return ret;
  23884. }
  23885. #endif
  23886. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  23887. typedef struct encodeSignedDataStream {
  23888. byte out[FOURK_BUF*3];
  23889. int idx;
  23890. word32 outIdx;
  23891. } encodeSignedDataStream;
  23892. /* content is 8k of partially created bundle */
  23893. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  23894. {
  23895. int ret = 0;
  23896. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23897. if (strm->outIdx < pkcs7->contentSz) {
  23898. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  23899. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  23900. *content = strm->out + strm->outIdx;
  23901. strm->outIdx += ret;
  23902. }
  23903. (void)pkcs7;
  23904. return ret;
  23905. }
  23906. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  23907. void* ctx)
  23908. {
  23909. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23910. XMEMCPY(strm->out + strm->idx, output, outputSz);
  23911. strm->idx += outputSz;
  23912. (void)pkcs7;
  23913. return 0;
  23914. }
  23915. #endif
  23916. /*
  23917. * Testing wc_PKCS7_EncodeSignedData()
  23918. */
  23919. static int test_wc_PKCS7_EncodeSignedData(void)
  23920. {
  23921. EXPECT_DECLS;
  23922. #if defined(HAVE_PKCS7)
  23923. PKCS7* pkcs7 = NULL;
  23924. WC_RNG rng;
  23925. byte output[FOURK_BUF];
  23926. byte badOut[1];
  23927. word32 outputSz = (word32)sizeof(output);
  23928. word32 badOutSz = 0;
  23929. byte data[] = "Test data to encode.";
  23930. #ifndef NO_RSA
  23931. #if defined(USE_CERT_BUFFERS_2048)
  23932. byte key[sizeof(client_key_der_2048)];
  23933. byte cert[sizeof(client_cert_der_2048)];
  23934. word32 keySz = (word32)sizeof(key);
  23935. word32 certSz = (word32)sizeof(cert);
  23936. XMEMSET(key, 0, keySz);
  23937. XMEMSET(cert, 0, certSz);
  23938. XMEMCPY(key, client_key_der_2048, keySz);
  23939. XMEMCPY(cert, client_cert_der_2048, certSz);
  23940. #elif defined(USE_CERT_BUFFERS_1024)
  23941. byte key[sizeof_client_key_der_1024];
  23942. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23943. word32 keySz = (word32)sizeof(key);
  23944. word32 certSz = (word32)sizeof(cert);
  23945. XMEMSET(key, 0, keySz);
  23946. XMEMSET(cert, 0, certSz);
  23947. XMEMCPY(key, client_key_der_1024, keySz);
  23948. XMEMCPY(cert, client_cert_der_1024, certSz);
  23949. #else
  23950. unsigned char cert[ONEK_BUF];
  23951. unsigned char key[ONEK_BUF];
  23952. XFILE fp = XBADFILE;
  23953. int certSz;
  23954. int keySz;
  23955. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23956. XBADFILE);
  23957. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23958. fp), 0);
  23959. if (fp != XBADFILE) {
  23960. XFCLOSE(fp);
  23961. fp = XBADFILE;
  23962. }
  23963. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23964. XBADFILE);
  23965. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23966. 0);
  23967. if (fp != XBADFILE)
  23968. XFCLOSE(fp);
  23969. #endif
  23970. #elif defined(HAVE_ECC)
  23971. #if defined(USE_CERT_BUFFERS_256)
  23972. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23973. unsigned char key[sizeof(ecc_clikey_der_256)];
  23974. int certSz = (int)sizeof(cert);
  23975. int keySz = (int)sizeof(key);
  23976. XMEMSET(cert, 0, certSz);
  23977. XMEMSET(key, 0, keySz);
  23978. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23979. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23980. #else
  23981. unsigned char cert[ONEK_BUF];
  23982. unsigned char key[ONEK_BUF];
  23983. XFILE fp = XBADFILE;
  23984. int certSz;
  23985. int keySz;
  23986. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23987. XBADFILE);
  23988. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23989. if (fp != XBADFILE) {
  23990. XFCLOSE(fp);
  23991. fp = XBADFILE;
  23992. }
  23993. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23994. XBADFILE);
  23995. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23996. if (fp != XBADFILE)
  23997. XFCLOSE(fp);
  23998. #endif
  23999. #endif
  24000. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24001. XMEMSET(output, 0, outputSz);
  24002. ExpectIntEQ(wc_InitRng(&rng), 0);
  24003. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24004. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24005. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24006. if (pkcs7 != NULL) {
  24007. pkcs7->content = data;
  24008. pkcs7->contentSz = (word32)sizeof(data);
  24009. pkcs7->privateKey = key;
  24010. pkcs7->privateKeySz = (word32)sizeof(key);
  24011. pkcs7->encryptOID = RSAk;
  24012. #ifdef NO_SHA
  24013. pkcs7->hashOID = SHA256h;
  24014. #else
  24015. pkcs7->hashOID = SHAh;
  24016. #endif
  24017. pkcs7->rng = &rng;
  24018. }
  24019. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24020. wc_PKCS7_Free(pkcs7);
  24021. pkcs7 = NULL;
  24022. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24023. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24024. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24025. #ifdef ASN_BER_TO_DER
  24026. wc_PKCS7_Free(pkcs7);
  24027. /* reinitialize and test setting stream mode */
  24028. {
  24029. int signedSz = 0;
  24030. encodeSignedDataStream strm;
  24031. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24032. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24033. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24034. if (pkcs7 != NULL) {
  24035. pkcs7->content = data;
  24036. pkcs7->contentSz = (word32)sizeof(data);
  24037. pkcs7->privateKey = key;
  24038. pkcs7->privateKeySz = (word32)sizeof(key);
  24039. pkcs7->encryptOID = RSAk;
  24040. #ifdef NO_SHA
  24041. pkcs7->hashOID = SHA256h;
  24042. #else
  24043. pkcs7->hashOID = SHAh;
  24044. #endif
  24045. pkcs7->rng = &rng;
  24046. }
  24047. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24048. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24049. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24050. BAD_FUNC_ARG);
  24051. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24052. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24053. outputSz), 0);
  24054. wc_PKCS7_Free(pkcs7);
  24055. pkcs7 = NULL;
  24056. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24057. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24058. /* use exact signed buffer size since BER encoded */
  24059. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  24060. wc_PKCS7_Free(pkcs7);
  24061. /* now try with using callbacks for IO */
  24062. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24063. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24064. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24065. if (pkcs7 != NULL) {
  24066. pkcs7->contentSz = FOURK_BUF*2;
  24067. pkcs7->privateKey = key;
  24068. pkcs7->privateKeySz = (word32)sizeof(key);
  24069. pkcs7->encryptOID = RSAk;
  24070. #ifdef NO_SHA
  24071. pkcs7->hashOID = SHA256h;
  24072. #else
  24073. pkcs7->hashOID = SHAh;
  24074. #endif
  24075. pkcs7->rng = &rng;
  24076. }
  24077. XMEMSET(&strm, 0, sizeof(strm));
  24078. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24079. StreamOutputCB, (void*)&strm), 0);
  24080. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24081. wc_PKCS7_Free(pkcs7);
  24082. pkcs7 = NULL;
  24083. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24084. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24085. /* use exact signed buffer size since BER encoded */
  24086. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
  24087. }
  24088. #endif
  24089. #ifndef NO_PKCS7_STREAM
  24090. wc_PKCS7_Free(pkcs7);
  24091. pkcs7 = NULL;
  24092. {
  24093. word32 z;
  24094. int ret;
  24095. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24096. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24097. /* test for streaming mode */
  24098. ret = -1;
  24099. for (z = 0; z < outputSz && ret != 0; z++) {
  24100. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24101. if (ret < 0){
  24102. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24103. }
  24104. }
  24105. ExpectIntEQ(ret, 0);
  24106. ExpectIntNE(pkcs7->contentSz, 0);
  24107. ExpectNotNull(pkcs7->contentDynamic);
  24108. }
  24109. #endif /* !NO_PKCS7_STREAM */
  24110. /* Pass in bad args. */
  24111. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24112. BAD_FUNC_ARG);
  24113. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24114. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24115. badOutSz), BAD_FUNC_ARG);
  24116. if (pkcs7 != NULL) {
  24117. pkcs7->hashOID = 0; /* bad hashOID */
  24118. }
  24119. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24120. BAD_FUNC_ARG);
  24121. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24122. !defined(NO_RSA) && !defined(NO_SHA256)
  24123. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24124. * Example callback assumes SHA-256, so only run test if compiled in. */
  24125. wc_PKCS7_Free(pkcs7);
  24126. pkcs7 = NULL;
  24127. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24128. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24129. if (pkcs7 != NULL) {
  24130. pkcs7->content = data;
  24131. pkcs7->contentSz = (word32)sizeof(data);
  24132. pkcs7->privateKey = key;
  24133. pkcs7->privateKeySz = (word32)sizeof(key);
  24134. pkcs7->encryptOID = RSAk;
  24135. pkcs7->hashOID = SHA256h;
  24136. pkcs7->rng = &rng;
  24137. }
  24138. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24139. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24140. #endif
  24141. wc_PKCS7_Free(pkcs7);
  24142. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24143. #endif
  24144. return EXPECT_RESULT();
  24145. } /* END test_wc_PKCS7_EncodeSignedData */
  24146. /*
  24147. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24148. */
  24149. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24150. {
  24151. EXPECT_DECLS;
  24152. #if defined(HAVE_PKCS7)
  24153. int i;
  24154. PKCS7* pkcs7 = NULL;
  24155. WC_RNG rng;
  24156. byte outputHead[FOURK_BUF/2];
  24157. byte outputFoot[FOURK_BUF/2];
  24158. word32 outputHeadSz = (word32)sizeof(outputHead);
  24159. word32 outputFootSz = (word32)sizeof(outputFoot);
  24160. byte data[FOURK_BUF];
  24161. wc_HashAlg hash;
  24162. #ifdef NO_SHA
  24163. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24164. #else
  24165. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24166. #endif
  24167. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24168. word32 hashSz = wc_HashGetDigestSize(hashType);
  24169. #ifndef NO_RSA
  24170. #if defined(USE_CERT_BUFFERS_2048)
  24171. byte key[sizeof(client_key_der_2048)];
  24172. byte cert[sizeof(client_cert_der_2048)];
  24173. word32 keySz = (word32)sizeof(key);
  24174. word32 certSz = (word32)sizeof(cert);
  24175. XMEMSET(key, 0, keySz);
  24176. XMEMSET(cert, 0, certSz);
  24177. XMEMCPY(key, client_key_der_2048, keySz);
  24178. XMEMCPY(cert, client_cert_der_2048, certSz);
  24179. #elif defined(USE_CERT_BUFFERS_1024)
  24180. byte key[sizeof_client_key_der_1024];
  24181. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24182. word32 keySz = (word32)sizeof(key);
  24183. word32 certSz = (word32)sizeof(cert);
  24184. XMEMSET(key, 0, keySz);
  24185. XMEMSET(cert, 0, certSz);
  24186. XMEMCPY(key, client_key_der_1024, keySz);
  24187. XMEMCPY(cert, client_cert_der_1024, certSz);
  24188. #else
  24189. unsigned char cert[ONEK_BUF];
  24190. unsigned char key[ONEK_BUF];
  24191. XFILE fp = XBADFILE;
  24192. int certSz;
  24193. int keySz;
  24194. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24195. XBADFILE);
  24196. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24197. fp), 0);
  24198. if (fp != XBADFILE) {
  24199. XFCLOSE(fp);
  24200. fp = XBADFILE;
  24201. }
  24202. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24203. XBADFILE);
  24204. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24205. 0);
  24206. if (fp != XBADFILE)
  24207. XFCLOSE(fp);
  24208. #endif
  24209. #elif defined(HAVE_ECC)
  24210. #if defined(USE_CERT_BUFFERS_256)
  24211. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24212. unsigned char key[sizeof(ecc_clikey_der_256)];
  24213. int certSz = (int)sizeof(cert);
  24214. int keySz = (int)sizeof(key);
  24215. XMEMSET(cert, 0, certSz);
  24216. XMEMSET(key, 0, keySz);
  24217. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24218. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24219. #else
  24220. unsigned char cert[ONEK_BUF];
  24221. unsigned char key[ONEK_BUF];
  24222. XFILE fp = XBADFILE;
  24223. int certSz;
  24224. int keySz;
  24225. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24226. XBADFILE);
  24227. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24228. fp), 0);
  24229. if (fp != XBADFILE) {
  24230. XFCLOSE(fp);
  24231. fp = XBADFILE;
  24232. }
  24233. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24234. XBADFILE);
  24235. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24236. 0);
  24237. if (fp != XBADFILE)
  24238. XFCLOSE(fp);
  24239. #endif
  24240. #endif
  24241. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24242. /* initialize large data with sequence */
  24243. for (i=0; i<(int)sizeof(data); i++)
  24244. data[i] = i & 0xff;
  24245. XMEMSET(outputHead, 0, outputHeadSz);
  24246. XMEMSET(outputFoot, 0, outputFootSz);
  24247. ExpectIntEQ(wc_InitRng(&rng), 0);
  24248. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24249. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24250. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24251. if (pkcs7 != NULL) {
  24252. pkcs7->content = NULL; /* not used for ex */
  24253. pkcs7->contentSz = (word32)sizeof(data);
  24254. pkcs7->privateKey = key;
  24255. pkcs7->privateKeySz = (word32)sizeof(key);
  24256. pkcs7->encryptOID = RSAk;
  24257. #ifdef NO_SHA
  24258. pkcs7->hashOID = SHA256h;
  24259. #else
  24260. pkcs7->hashOID = SHAh;
  24261. #endif
  24262. pkcs7->rng = &rng;
  24263. }
  24264. /* calculate hash for content */
  24265. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24266. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24267. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24268. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24269. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24270. /* Perform PKCS7 sign using hash directly */
  24271. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24272. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24273. ExpectIntGT(outputHeadSz, 0);
  24274. ExpectIntGT(outputFootSz, 0);
  24275. wc_PKCS7_Free(pkcs7);
  24276. pkcs7 = NULL;
  24277. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24278. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24279. /* required parameter even on verify when using _ex, if using outputHead
  24280. * and outputFoot */
  24281. if (pkcs7 != NULL) {
  24282. pkcs7->contentSz = (word32)sizeof(data);
  24283. }
  24284. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24285. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24286. wc_PKCS7_Free(pkcs7);
  24287. pkcs7 = NULL;
  24288. /* assembly complete PKCS7 sign and use normal verify */
  24289. {
  24290. byte* output = NULL;
  24291. word32 outputSz = 0;
  24292. #ifndef NO_PKCS7_STREAM
  24293. word32 z;
  24294. int ret;
  24295. #endif /* !NO_PKCS7_STREAM */
  24296. ExpectNotNull(output = (byte*)XMALLOC(
  24297. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24298. DYNAMIC_TYPE_TMP_BUFFER));
  24299. if (output != NULL) {
  24300. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24301. outputSz += outputHeadSz;
  24302. XMEMCPY(&output[outputSz], data, sizeof(data));
  24303. outputSz += sizeof(data);
  24304. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24305. outputSz += outputFootSz;
  24306. }
  24307. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24308. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24309. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24310. #ifndef NO_PKCS7_STREAM
  24311. wc_PKCS7_Free(pkcs7);
  24312. pkcs7 = NULL;
  24313. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24314. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24315. /* test for streaming mode */
  24316. ret = -1;
  24317. for (z = 0; z < outputSz && ret != 0; z++) {
  24318. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24319. if (ret < 0){
  24320. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24321. }
  24322. }
  24323. ExpectIntEQ(ret, 0);
  24324. ExpectIntNE(pkcs7->contentSz, 0);
  24325. ExpectNotNull(pkcs7->contentDynamic);
  24326. wc_PKCS7_Free(pkcs7);
  24327. pkcs7 = NULL;
  24328. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24329. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24330. #endif /* !NO_PKCS7_STREAM */
  24331. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24332. }
  24333. /* Pass in bad args. */
  24334. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24335. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24336. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24337. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24338. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24339. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24340. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24341. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24342. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24343. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24344. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24345. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24346. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24347. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24348. if (pkcs7 != NULL) {
  24349. pkcs7->hashOID = 0; /* bad hashOID */
  24350. }
  24351. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24352. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24353. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24354. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24355. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24356. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24357. #ifndef NO_PKCS7_STREAM
  24358. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24359. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24360. #else
  24361. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24362. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24363. #endif
  24364. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24365. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24366. #ifndef NO_PKCS7_STREAM
  24367. /* can pass in 0 buffer length with streaming API */
  24368. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24369. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24370. #else
  24371. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24372. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24373. #endif
  24374. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24375. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24376. #ifndef NO_PKCS7_STREAM
  24377. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24378. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24379. #else
  24380. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24381. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24382. #endif
  24383. wc_PKCS7_Free(pkcs7);
  24384. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24385. #endif
  24386. return EXPECT_RESULT();
  24387. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24388. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24389. /**
  24390. * Loads certs/keys from files or buffers into the argument buffers,
  24391. * helper function called by CreatePKCS7SignedData().
  24392. *
  24393. * Returns 0 on success, negative on error.
  24394. */
  24395. static int LoadPKCS7SignedDataCerts(
  24396. int useIntermediateCertChain, int pkAlgoType,
  24397. byte* intCARoot, word32* intCARootSz,
  24398. byte* intCA1, word32* intCA1Sz,
  24399. byte* intCA2, word32* intCA2Sz,
  24400. byte* cert, word32* certSz,
  24401. byte* key, word32* keySz)
  24402. {
  24403. EXPECT_DECLS;
  24404. int ret = 0;
  24405. XFILE fp = XBADFILE;
  24406. #ifndef NO_RSA
  24407. const char* intCARootRSA = "./certs/ca-cert.der";
  24408. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24409. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24410. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24411. const char* intServKeyRSA = "./certs/server-key.der";
  24412. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24413. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24414. const char* cli1024Key = "./certs/1024/client-key.der";
  24415. #endif
  24416. #endif
  24417. #ifdef HAVE_ECC
  24418. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24419. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24420. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24421. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24422. const char* intServKeyECC = "./certs/ecc-key.der";
  24423. #ifndef USE_CERT_BUFFERS_256
  24424. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24425. const char* cliEccKey = "./certs/client-ecc-key.der";
  24426. #endif
  24427. #endif
  24428. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24429. ((useIntermediateCertChain == 1) &&
  24430. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24431. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24432. return BAD_FUNC_ARG;
  24433. }
  24434. /* Read/load certs and keys to use for signing based on PK type and chain */
  24435. switch (pkAlgoType) {
  24436. #ifndef NO_RSA
  24437. case RSA_TYPE:
  24438. if (useIntermediateCertChain == 1) {
  24439. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24440. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24441. if (fp != XBADFILE) {
  24442. XFCLOSE(fp);
  24443. fp = XBADFILE;
  24444. }
  24445. ExpectIntGT(*intCARootSz, 0);
  24446. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24447. if (fp != XBADFILE) {
  24448. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24449. XFCLOSE(fp);
  24450. fp = XBADFILE;
  24451. }
  24452. ExpectIntGT(*intCA1Sz, 0);
  24453. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24454. if (fp != XBADFILE) {
  24455. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24456. XFCLOSE(fp);
  24457. fp = XBADFILE;
  24458. }
  24459. ExpectIntGT(*intCA2Sz, 0);
  24460. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24461. if (fp != XBADFILE) {
  24462. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24463. XFCLOSE(fp);
  24464. fp = XBADFILE;
  24465. }
  24466. ExpectIntGT(*certSz, 0);
  24467. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24468. if (fp != XBADFILE) {
  24469. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24470. XFCLOSE(fp);
  24471. fp = XBADFILE;
  24472. }
  24473. ExpectIntGT(*keySz, 0);
  24474. }
  24475. else {
  24476. #if defined(USE_CERT_BUFFERS_2048)
  24477. *keySz = sizeof_client_key_der_2048;
  24478. *certSz = sizeof_client_cert_der_2048;
  24479. XMEMCPY(key, client_key_der_2048, *keySz);
  24480. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24481. #elif defined(USE_CERT_BUFFERS_1024)
  24482. *keySz = sizeof_client_key_der_1024;
  24483. *certSz = sizeof_client_cert_der_1024;
  24484. XMEMCPY(key, client_key_der_1024, *keySz);
  24485. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24486. #else
  24487. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24488. if (fp != XBADFILE) {
  24489. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24490. XFCLOSE(fp);
  24491. fp = XBADFILE;
  24492. }
  24493. ExpectIntGT(*keySz, 0);
  24494. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24495. if (fp != XBADFILE) {
  24496. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24497. XFCLOSE(fp);
  24498. fp = XBADFILE;
  24499. }
  24500. ExpectIntGT(*certSz, 0);
  24501. #endif /* USE_CERT_BUFFERS_2048 */
  24502. }
  24503. break;
  24504. #endif /* !NO_RSA */
  24505. #ifdef HAVE_ECC
  24506. case ECC_TYPE:
  24507. if (useIntermediateCertChain == 1) {
  24508. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24509. if (fp != XBADFILE) {
  24510. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24511. fp);
  24512. XFCLOSE(fp);
  24513. fp = XBADFILE;
  24514. }
  24515. ExpectIntGT(*intCARootSz, 0);
  24516. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24517. if (fp != XBADFILE) {
  24518. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24519. XFCLOSE(fp);
  24520. fp = XBADFILE;
  24521. }
  24522. ExpectIntGT(*intCA1Sz, 0);
  24523. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24524. if (fp != XBADFILE) {
  24525. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24526. XFCLOSE(fp);
  24527. fp = XBADFILE;
  24528. }
  24529. ExpectIntGT(*intCA2Sz, 0);
  24530. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24531. if (fp != XBADFILE) {
  24532. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24533. XFCLOSE(fp);
  24534. fp = XBADFILE;
  24535. }
  24536. ExpectIntGT(*certSz, 0);
  24537. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24538. if (fp != XBADFILE) {
  24539. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24540. XFCLOSE(fp);
  24541. fp = XBADFILE;
  24542. }
  24543. ExpectIntGT(*keySz, 0);
  24544. }
  24545. else {
  24546. #if defined(USE_CERT_BUFFERS_256)
  24547. *keySz = sizeof_ecc_clikey_der_256;
  24548. *certSz = sizeof_cliecc_cert_der_256;
  24549. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24550. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24551. #else
  24552. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24553. if (fp != XBADFILE) {
  24554. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24555. XFCLOSE(fp);
  24556. fp = XBADFILE;
  24557. }
  24558. ExpectIntGT(*keySz, 0);
  24559. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24560. if (fp != XBADFILE) {
  24561. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24562. XFCLOSE(fp);
  24563. fp = XBADFILE;
  24564. }
  24565. ExpectIntGT(*certSz, 0);
  24566. #endif /* USE_CERT_BUFFERS_256 */
  24567. }
  24568. break;
  24569. #endif /* HAVE_ECC */
  24570. default:
  24571. WOLFSSL_MSG("Unsupported SignedData PK type");
  24572. ret = BAD_FUNC_ARG;
  24573. break;
  24574. }
  24575. if (EXPECT_FAIL() && (ret == 0)) {
  24576. ret = BAD_FUNC_ARG;
  24577. }
  24578. return ret;
  24579. }
  24580. /**
  24581. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24582. *
  24583. * output output buffer to place SignedData
  24584. * outputSz size of output buffer
  24585. * data data buffer to be signed
  24586. * dataSz size of data buffer
  24587. * withAttribs [1/0] include attributes in SignedData message
  24588. * detachedSig [1/0] create detached signature, no content
  24589. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24590. * root CAs in bundle
  24591. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24592. *
  24593. * Return size of bundle created on success, negative on error */
  24594. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24595. byte* data, word32 dataSz,
  24596. int withAttribs, int detachedSig,
  24597. int useIntermediateCertChain,
  24598. int pkAlgoType)
  24599. {
  24600. EXPECT_DECLS;
  24601. int ret = 0;
  24602. WC_RNG rng;
  24603. PKCS7* pkcs7 = NULL;
  24604. static byte messageTypeOid[] =
  24605. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24606. 0x09, 0x02 };
  24607. static byte messageType[] = { 0x13, 2, '1', '9' };
  24608. PKCS7Attrib attribs[] =
  24609. {
  24610. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24611. sizeof(messageType) }
  24612. };
  24613. byte intCARoot[TWOK_BUF];
  24614. byte intCA1[TWOK_BUF];
  24615. byte intCA2[TWOK_BUF];
  24616. byte cert[TWOK_BUF];
  24617. byte key[TWOK_BUF];
  24618. word32 intCARootSz = sizeof(intCARoot);
  24619. word32 intCA1Sz = sizeof(intCA1);
  24620. word32 intCA2Sz = sizeof(intCA2);
  24621. word32 certSz = sizeof(cert);
  24622. word32 keySz = sizeof(key);
  24623. XMEMSET(intCARoot, 0, intCARootSz);
  24624. XMEMSET(intCA1, 0, intCA1Sz);
  24625. XMEMSET(intCA2, 0, intCA2Sz);
  24626. XMEMSET(cert, 0, certSz);
  24627. XMEMSET(key, 0, keySz);
  24628. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24629. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24630. cert, &certSz, key, &keySz);
  24631. ExpectIntEQ(ret, 0);
  24632. XMEMSET(output, 0, outputSz);
  24633. ExpectIntEQ(wc_InitRng(&rng), 0);
  24634. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24635. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24636. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24637. if (useIntermediateCertChain == 1) {
  24638. /* Add intermediate and root CA certs into SignedData Certs SET */
  24639. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24640. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24641. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24642. }
  24643. if (pkcs7 != NULL) {
  24644. pkcs7->content = data;
  24645. pkcs7->contentSz = dataSz;
  24646. pkcs7->privateKey = key;
  24647. pkcs7->privateKeySz = (word32)sizeof(key);
  24648. if (pkAlgoType == RSA_TYPE) {
  24649. pkcs7->encryptOID = RSAk;
  24650. }
  24651. else {
  24652. pkcs7->encryptOID = ECDSAk;
  24653. }
  24654. #ifdef NO_SHA
  24655. pkcs7->hashOID = SHA256h;
  24656. #else
  24657. pkcs7->hashOID = SHAh;
  24658. #endif
  24659. pkcs7->rng = &rng;
  24660. if (withAttribs) {
  24661. /* include a signed attribute */
  24662. pkcs7->signedAttribs = attribs;
  24663. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24664. }
  24665. }
  24666. if (detachedSig) {
  24667. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24668. }
  24669. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24670. ExpectIntGT(outputSz, 0);
  24671. wc_PKCS7_Free(pkcs7);
  24672. pkcs7 = NULL;
  24673. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24674. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24675. if (detachedSig && (pkcs7 != NULL)) {
  24676. pkcs7->content = data;
  24677. pkcs7->contentSz = dataSz;
  24678. }
  24679. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24680. wc_PKCS7_Free(pkcs7);
  24681. wc_FreeRng(&rng);
  24682. if (EXPECT_FAIL()) {
  24683. outputSz = 0;
  24684. }
  24685. return outputSz;
  24686. }
  24687. #endif
  24688. /*
  24689. * Testing wc_PKCS_VerifySignedData()
  24690. */
  24691. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24692. {
  24693. EXPECT_DECLS;
  24694. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24695. PKCS7* pkcs7 = NULL;
  24696. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24697. word32 outputSz = sizeof(output);
  24698. byte data[] = "Test data to encode.";
  24699. byte badOut[1];
  24700. word32 badOutSz = 0;
  24701. byte badContent[] = "This is different content than was signed";
  24702. wc_HashAlg hash;
  24703. #ifdef NO_SHA
  24704. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24705. #else
  24706. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24707. #endif
  24708. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24709. word32 hashSz = wc_HashGetDigestSize(hashType);
  24710. #ifndef NO_RSA
  24711. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24712. /* contentType OID (1.2.840.113549.1.9.3) */
  24713. static const byte contentTypeOid[] =
  24714. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24715. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24716. static const byte dataType[] =
  24717. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24718. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24719. static const byte messageDigestOid[] =
  24720. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24721. #ifndef NO_ASN_TIME
  24722. /* signingTime OID () */
  24723. static const byte signingTimeOid[] =
  24724. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24725. #endif
  24726. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24727. int dateLength = 0;
  24728. byte dateFormat;
  24729. const byte* datePart = NULL;
  24730. struct tm timearg;
  24731. time_t now;
  24732. struct tm* nowTm = NULL;
  24733. #ifdef NEED_TMP_TIME
  24734. struct tm tmpTimeStorage;
  24735. struct tm* tmpTime = &tmpTimeStorage;
  24736. #endif
  24737. #endif /* !NO_ASN && !NO_ASN_TIME */
  24738. #ifndef NO_PKCS7_STREAM
  24739. word32 z;
  24740. int ret;
  24741. #endif /* !NO_PKCS7_STREAM */
  24742. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24743. /* Success test with RSA certs/key */
  24744. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24745. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24746. /* calculate hash for content, used later */
  24747. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24748. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24749. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24750. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24751. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24752. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24753. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24754. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24755. #ifndef NO_PKCS7_STREAM
  24756. wc_PKCS7_Free(pkcs7);
  24757. pkcs7 = NULL;
  24758. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24759. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24760. /* test for streaming */
  24761. ret = -1;
  24762. for (z = 0; z < outputSz && ret != 0; z++) {
  24763. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24764. if (ret < 0){
  24765. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24766. }
  24767. }
  24768. ExpectIntEQ(ret, 0);
  24769. ExpectIntNE(pkcs7->contentSz, 0);
  24770. ExpectNotNull(pkcs7->contentDynamic);
  24771. #endif /* !NO_PKCS7_STREAM */
  24772. /* Check that decoded signed attributes are correct */
  24773. /* messageDigest should be first */
  24774. if (pkcs7 != NULL) {
  24775. decodedAttrib = pkcs7->decodedAttrib;
  24776. }
  24777. ExpectNotNull(decodedAttrib);
  24778. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24779. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24780. decodedAttrib->oidSz), 0);
  24781. /* + 2 for OCTET STRING and length bytes */
  24782. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24783. ExpectNotNull(decodedAttrib->value);
  24784. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24785. #ifndef NO_ASN_TIME
  24786. /* signingTime should be second */
  24787. if (decodedAttrib != NULL) {
  24788. decodedAttrib = decodedAttrib->next;
  24789. }
  24790. ExpectNotNull(decodedAttrib);
  24791. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24792. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24793. decodedAttrib->oidSz), 0);
  24794. ExpectIntGT(decodedAttrib->valueSz, 0);
  24795. ExpectNotNull(decodedAttrib->value);
  24796. #endif
  24797. /* Verify signingTime if ASN and time are available */
  24798. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24799. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24800. &datePart, &dateFormat, &dateLength), 0);
  24801. ExpectNotNull(datePart);
  24802. ExpectIntGT(dateLength, 0);
  24803. XMEMSET(&timearg, 0, sizeof(timearg));
  24804. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24805. &timearg), 0);
  24806. /* Get current time and compare year/month/day against attribute value */
  24807. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24808. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24809. ExpectNotNull(nowTm);
  24810. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24811. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24812. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24813. #endif /* !NO_ASN && !NO_ASN_TIME */
  24814. /* contentType should be third */
  24815. if (decodedAttrib != NULL) {
  24816. decodedAttrib = decodedAttrib->next;
  24817. }
  24818. ExpectNotNull(decodedAttrib);
  24819. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24820. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24821. decodedAttrib->oidSz), 0);
  24822. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24823. ExpectNotNull(decodedAttrib->value);
  24824. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24825. 0);
  24826. #endif /* !NO_RSA */
  24827. /* Test bad args. */
  24828. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24829. BAD_FUNC_ARG);
  24830. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24831. BAD_FUNC_ARG);
  24832. #ifndef NO_PKCS7_STREAM
  24833. /* can pass in 0 buffer length with streaming API */
  24834. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24835. badOutSz), WC_PKCS7_WANT_READ_E);
  24836. #else
  24837. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24838. badOutSz), BAD_FUNC_ARG);
  24839. #endif
  24840. wc_PKCS7_Free(pkcs7);
  24841. pkcs7 = NULL;
  24842. #ifndef NO_RSA
  24843. /* Try RSA certs/key/sig first */
  24844. outputSz = sizeof(output);
  24845. XMEMSET(output, 0, outputSz);
  24846. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24847. (word32)sizeof(data),
  24848. 1, 1, 0, RSA_TYPE)), 0);
  24849. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24850. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24851. if (pkcs7 != NULL) {
  24852. pkcs7->content = badContent;
  24853. pkcs7->contentSz = sizeof(badContent);
  24854. }
  24855. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24856. SIG_VERIFY_E);
  24857. wc_PKCS7_Free(pkcs7);
  24858. pkcs7 = NULL;
  24859. #ifndef NO_PKCS7_STREAM
  24860. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24861. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24862. if (pkcs7 != NULL) {
  24863. pkcs7->content = badContent;
  24864. pkcs7->contentSz = sizeof(badContent);
  24865. }
  24866. /* test for streaming */
  24867. ret = -1;
  24868. for (z = 0; z < outputSz && ret != 0; z++) {
  24869. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24870. if (ret == WC_PKCS7_WANT_READ_E){
  24871. continue;
  24872. }
  24873. else if (ret < 0) {
  24874. break;
  24875. }
  24876. }
  24877. ExpectIntEQ(ret, SIG_VERIFY_E);
  24878. ExpectIntNE(pkcs7->contentSz, 0);
  24879. ExpectNotNull(pkcs7->contentDynamic);
  24880. wc_PKCS7_Free(pkcs7);
  24881. pkcs7 = NULL;
  24882. #endif /* !NO_PKCS7_STREAM */
  24883. /* Test success case with detached signature and valid content */
  24884. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24885. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24886. if (pkcs7 != NULL) {
  24887. pkcs7->content = data;
  24888. pkcs7->contentSz = sizeof(data);
  24889. }
  24890. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24891. wc_PKCS7_Free(pkcs7);
  24892. pkcs7 = NULL;
  24893. #ifndef NO_PKCS7_STREAM
  24894. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24895. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24896. if (pkcs7 != NULL) {
  24897. pkcs7->content = data;
  24898. pkcs7->contentSz = sizeof(data);
  24899. }
  24900. /* test for streaming */
  24901. ret = -1;
  24902. for (z = 0; z < outputSz && ret != 0; z++) {
  24903. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24904. if (ret < 0){
  24905. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24906. }
  24907. }
  24908. ExpectIntEQ(ret, 0);
  24909. ExpectIntNE(pkcs7->contentSz, 0);
  24910. ExpectNotNull(pkcs7->contentDynamic);
  24911. wc_PKCS7_Free(pkcs7);
  24912. pkcs7 = NULL;
  24913. #endif /* !NO_PKCS7_STREAM */
  24914. /* verify using pre-computed content digest only (no content) */
  24915. {
  24916. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24917. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24918. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24919. output, outputSz, NULL, 0), 0);
  24920. wc_PKCS7_Free(pkcs7);
  24921. pkcs7 = NULL;
  24922. }
  24923. #endif /* !NO_RSA */
  24924. /* Test verify on signedData containing intermediate/root CA certs */
  24925. #ifndef NO_RSA
  24926. outputSz = sizeof(output);
  24927. XMEMSET(output, 0, outputSz);
  24928. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24929. (word32)sizeof(data),
  24930. 0, 0, 1, RSA_TYPE)), 0);
  24931. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24932. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24933. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24934. wc_PKCS7_Free(pkcs7);
  24935. pkcs7 = NULL;
  24936. #ifndef NO_PKCS7_STREAM
  24937. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24938. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24939. /* test for streaming */
  24940. ret = -1;
  24941. for (z = 0; z < outputSz && ret != 0; z++) {
  24942. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24943. if (ret < 0){
  24944. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24945. }
  24946. }
  24947. ExpectIntEQ(ret, 0);
  24948. ExpectIntNE(pkcs7->contentSz, 0);
  24949. ExpectNotNull(pkcs7->contentDynamic);
  24950. wc_PKCS7_Free(pkcs7);
  24951. pkcs7 = NULL;
  24952. #endif /* !NO_PKCS7_STREAM */
  24953. #endif /* !NO_RSA */
  24954. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  24955. !defined(NO_FILESYSTEM)
  24956. {
  24957. XFILE signedBundle = XBADFILE;
  24958. int signedBundleSz = 0;
  24959. int chunkSz = 1;
  24960. int i, rc = 0;
  24961. byte* buf = NULL;
  24962. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  24963. "rb")) != XBADFILE);
  24964. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  24965. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  24966. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  24967. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  24968. DYNAMIC_TYPE_FILE));
  24969. if (buf != NULL) {
  24970. ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
  24971. signedBundleSz);
  24972. }
  24973. if (signedBundle != XBADFILE) {
  24974. XFCLOSE(signedBundle);
  24975. signedBundle = XBADFILE;
  24976. }
  24977. if (buf != NULL) {
  24978. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24979. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24980. for (i = 0; i < signedBundleSz;) {
  24981. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  24982. chunkSz;
  24983. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  24984. if (rc < 0 ) {
  24985. if (rc == WC_PKCS7_WANT_READ_E) {
  24986. i += sz;
  24987. continue;
  24988. }
  24989. break;
  24990. }
  24991. else {
  24992. break;
  24993. }
  24994. }
  24995. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  24996. wc_PKCS7_Free(pkcs7);
  24997. pkcs7 = NULL;
  24998. }
  24999. /* now try with malformed bundle */
  25000. if (buf != NULL) {
  25001. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25002. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25003. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25004. for (i = 0; i < signedBundleSz;) {
  25005. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25006. chunkSz;
  25007. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  25008. if (rc < 0 ) {
  25009. if (rc == WC_PKCS7_WANT_READ_E) {
  25010. i += sz;
  25011. continue;
  25012. }
  25013. break;
  25014. }
  25015. else {
  25016. break;
  25017. }
  25018. }
  25019. ExpectIntEQ(rc, ASN_PARSE_E);
  25020. wc_PKCS7_Free(pkcs7);
  25021. pkcs7 = NULL;
  25022. }
  25023. if (buf != NULL)
  25024. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25025. }
  25026. #endif /* BER and stream */
  25027. #endif
  25028. return EXPECT_RESULT();
  25029. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25030. /*
  25031. * Testing wc_PKCS_VerifySignedData()
  25032. */
  25033. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25034. {
  25035. EXPECT_DECLS;
  25036. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25037. PKCS7* pkcs7 = NULL;
  25038. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25039. word32 outputSz = sizeof(output);
  25040. byte data[] = "Test data to encode.";
  25041. byte badContent[] = "This is different content than was signed";
  25042. wc_HashAlg hash;
  25043. #ifndef NO_PKCS7_STREAM
  25044. word32 z;
  25045. int ret;
  25046. #endif /* !NO_PKCS7_STREAM */
  25047. #ifdef NO_SHA
  25048. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25049. #else
  25050. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25051. #endif
  25052. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25053. word32 hashSz = wc_HashGetDigestSize(hashType);
  25054. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25055. /* Success test with ECC certs/key */
  25056. outputSz = sizeof(output);
  25057. XMEMSET(output, 0, outputSz);
  25058. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25059. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25060. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25061. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25062. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25063. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25064. wc_PKCS7_Free(pkcs7);
  25065. pkcs7 = NULL;
  25066. #ifndef NO_PKCS7_STREAM
  25067. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25068. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25069. /* test for streaming */
  25070. ret = -1;
  25071. for (z = 0; z < outputSz && ret != 0; z++) {
  25072. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25073. if (ret < 0){
  25074. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25075. }
  25076. }
  25077. ExpectIntEQ(ret, 0);
  25078. ExpectIntNE(pkcs7->contentSz, 0);
  25079. ExpectNotNull(pkcs7->contentDynamic);
  25080. wc_PKCS7_Free(pkcs7);
  25081. pkcs7 = NULL;
  25082. #endif /* !NO_PKCS7_STREAM */
  25083. /* Invalid content should error, use detached signature so we can
  25084. * easily change content */
  25085. outputSz = sizeof(output);
  25086. XMEMSET(output, 0, outputSz);
  25087. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25088. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25089. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25090. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25091. if (pkcs7 != NULL) {
  25092. pkcs7->content = badContent;
  25093. pkcs7->contentSz = sizeof(badContent);
  25094. }
  25095. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25096. SIG_VERIFY_E);
  25097. wc_PKCS7_Free(pkcs7);
  25098. pkcs7 = NULL;
  25099. #ifndef NO_PKCS7_STREAM
  25100. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25101. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25102. if (pkcs7 != NULL) {
  25103. pkcs7->content = badContent;
  25104. pkcs7->contentSz = sizeof(badContent);
  25105. }
  25106. /* test for streaming */
  25107. ret = -1;
  25108. for (z = 0; z < outputSz && ret != 0; z++) {
  25109. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25110. if (ret == WC_PKCS7_WANT_READ_E){
  25111. continue;
  25112. }
  25113. else if (ret < 0) {
  25114. break;
  25115. }
  25116. }
  25117. ExpectIntEQ(ret, SIG_VERIFY_E);
  25118. ExpectIntNE(pkcs7->contentSz, 0);
  25119. ExpectNotNull(pkcs7->contentDynamic);
  25120. wc_PKCS7_Free(pkcs7);
  25121. pkcs7 = NULL;
  25122. #endif /* !NO_PKCS7_STREAM */
  25123. /* Test success case with detached signature and valid content */
  25124. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25125. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25126. if (pkcs7 != NULL) {
  25127. pkcs7->content = data;
  25128. pkcs7->contentSz = sizeof(data);
  25129. }
  25130. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25131. wc_PKCS7_Free(pkcs7);
  25132. pkcs7 = NULL;
  25133. #ifndef NO_PKCS7_STREAM
  25134. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25135. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25136. if (pkcs7 != NULL) {
  25137. pkcs7->content = data;
  25138. pkcs7->contentSz = sizeof(data);
  25139. }
  25140. /* test for streaming */
  25141. ret = -1;
  25142. for (z = 0; z < outputSz && ret != 0; z++) {
  25143. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25144. if (ret < 0){
  25145. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25146. }
  25147. }
  25148. ExpectIntEQ(ret, 0);
  25149. ExpectIntNE(pkcs7->contentSz, 0);
  25150. ExpectNotNull(pkcs7->contentDynamic);
  25151. wc_PKCS7_Free(pkcs7);
  25152. pkcs7 = NULL;
  25153. #endif /* !NO_PKCS7_STREAM */
  25154. /* verify using pre-computed content digest only (no content) */
  25155. {
  25156. /* calculate hash for content */
  25157. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25158. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25159. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25160. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25161. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25162. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25163. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25164. output, outputSz, NULL, 0), 0);
  25165. wc_PKCS7_Free(pkcs7);
  25166. pkcs7 = NULL;
  25167. }
  25168. /* Test verify on signedData containing intermediate/root CA certs */
  25169. outputSz = sizeof(output);
  25170. XMEMSET(output, 0, outputSz);
  25171. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25172. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25173. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25174. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25175. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25176. wc_PKCS7_Free(pkcs7);
  25177. pkcs7 = NULL;
  25178. #ifndef NO_PKCS7_STREAM
  25179. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25180. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25181. /* test for streaming */
  25182. ret = -1;
  25183. for (z = 0; z < outputSz && ret != 0; z++) {
  25184. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25185. if (ret < 0){
  25186. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25187. }
  25188. }
  25189. ExpectIntEQ(ret, 0);
  25190. ExpectIntNE(pkcs7->contentSz, 0);
  25191. ExpectNotNull(pkcs7->contentDynamic);
  25192. wc_PKCS7_Free(pkcs7);
  25193. pkcs7 = NULL;
  25194. #endif /* !NO_PKCS7_STREAM */
  25195. #endif
  25196. return EXPECT_RESULT();
  25197. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25198. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25199. !defined(NO_AES_256)
  25200. static const byte defKey[] = {
  25201. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25202. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25203. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25204. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25205. };
  25206. static byte aesHandle[32]; /* simulated hardware key handle */
  25207. /* return 0 on success */
  25208. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25209. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25210. byte* in, int inSz, byte* out, void* usrCtx)
  25211. {
  25212. int ret;
  25213. Aes aes;
  25214. if (usrCtx == NULL) {
  25215. /* no simulated handle passed in */
  25216. return -1;
  25217. }
  25218. switch (encryptOID) {
  25219. case AES256CBCb:
  25220. if (ivSz != AES_BLOCK_SIZE)
  25221. return BAD_FUNC_ARG;
  25222. break;
  25223. default:
  25224. WOLFSSL_MSG("Unsupported content cipher type for test");
  25225. return ALGO_ID_E;
  25226. };
  25227. /* simulate using handle to get key */
  25228. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25229. if (ret == 0) {
  25230. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25231. if (ret == 0)
  25232. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25233. wc_AesFree(&aes);
  25234. }
  25235. (void)aad;
  25236. (void)aadSz;
  25237. (void)authTag;
  25238. (void)authTagSz;
  25239. (void)pkcs7;
  25240. return ret;
  25241. }
  25242. /* returns key size on success */
  25243. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25244. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25245. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25246. {
  25247. int ret = -1;
  25248. (void)cekSz;
  25249. (void)cek;
  25250. (void)outSz;
  25251. (void)keyIdSz;
  25252. (void)direction;
  25253. (void)orginKey; /* used with KAKRI */
  25254. (void)orginKeySz;
  25255. if (out == NULL)
  25256. return BAD_FUNC_ARG;
  25257. if (keyId[0] != 0x00) {
  25258. return -1;
  25259. }
  25260. if (type != (int)PKCS7_KEKRI) {
  25261. return -1;
  25262. }
  25263. switch (keyWrapAlgo) {
  25264. case AES256_WRAP:
  25265. /* simulate setting a handle for later decryption but use key
  25266. * as handle in the test case here */
  25267. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25268. aesHandle, sizeof(aesHandle), NULL);
  25269. if (ret < 0)
  25270. return ret;
  25271. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25272. if (ret < 0)
  25273. return ret;
  25274. /* return key size on success */
  25275. return sizeof(defKey);
  25276. default:
  25277. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25278. return BAD_KEYWRAP_ALG_E;
  25279. };
  25280. }
  25281. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25282. /*
  25283. * Testing wc_PKCS7_EncodeEnvelopedData()
  25284. */
  25285. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25286. {
  25287. EXPECT_DECLS;
  25288. #if defined(HAVE_PKCS7)
  25289. PKCS7* pkcs7 = NULL;
  25290. #ifdef ASN_BER_TO_DER
  25291. int encodedSz = 0;
  25292. #endif
  25293. #ifdef ECC_TIMING_RESISTANT
  25294. WC_RNG rng;
  25295. #endif
  25296. word32 tempWrd32 = 0;
  25297. byte* tmpBytePtr = NULL;
  25298. const char input[] = "Test data to encode.";
  25299. int i;
  25300. int testSz = 0;
  25301. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25302. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25303. byte* rsaCert = NULL;
  25304. byte* rsaPrivKey = NULL;
  25305. word32 rsaCertSz;
  25306. word32 rsaPrivKeySz;
  25307. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25308. !defined(USE_CERT_BUFFERS_2048) )
  25309. static const char* rsaClientCert = "./certs/client-cert.der";
  25310. static const char* rsaClientKey = "./certs/client-key.der";
  25311. rsaCertSz = (word32)sizeof(rsaClientCert);
  25312. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25313. #endif
  25314. #endif
  25315. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25316. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25317. byte* eccCert = NULL;
  25318. byte* eccPrivKey = NULL;
  25319. word32 eccCertSz;
  25320. word32 eccPrivKeySz;
  25321. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25322. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25323. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25324. #endif
  25325. #endif
  25326. /* Generic buffer size. */
  25327. byte output[ONEK_BUF];
  25328. byte decoded[sizeof(input)/sizeof(char)];
  25329. int decodedSz = 0;
  25330. #ifndef NO_FILESYSTEM
  25331. XFILE certFile = XBADFILE;
  25332. XFILE keyFile = XBADFILE;
  25333. #endif
  25334. #ifdef ECC_TIMING_RESISTANT
  25335. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25336. #endif
  25337. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25338. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25339. /* RSA certs and keys. */
  25340. #if defined(USE_CERT_BUFFERS_1024)
  25341. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25342. /* Allocate buffer space. */
  25343. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25344. DYNAMIC_TYPE_TMP_BUFFER));
  25345. /* Init buffer. */
  25346. if (rsaCert != NULL) {
  25347. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25348. }
  25349. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25350. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25351. DYNAMIC_TYPE_TMP_BUFFER));
  25352. if (rsaPrivKey != NULL) {
  25353. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25354. }
  25355. #elif defined(USE_CERT_BUFFERS_2048)
  25356. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25357. /* Allocate buffer */
  25358. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25359. DYNAMIC_TYPE_TMP_BUFFER));
  25360. /* Init buffer. */
  25361. if (rsaCert != NULL) {
  25362. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25363. }
  25364. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25365. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25366. DYNAMIC_TYPE_TMP_BUFFER));
  25367. if (rsaPrivKey != NULL) {
  25368. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25369. }
  25370. #else
  25371. /* File system. */
  25372. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25373. rsaCertSz = (word32)FOURK_BUF;
  25374. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25375. DYNAMIC_TYPE_TMP_BUFFER));
  25376. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25377. certFile)) > 0);
  25378. if (certFile != XBADFILE)
  25379. XFCLOSE(certFile);
  25380. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25381. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25382. DYNAMIC_TYPE_TMP_BUFFER));
  25383. rsaPrivKeySz = (word32)FOURK_BUF;
  25384. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25385. keyFile)) > 0);
  25386. if (keyFile != XBADFILE)
  25387. XFCLOSE(keyFile);
  25388. #endif /* USE_CERT_BUFFERS */
  25389. #endif /* NO_RSA */
  25390. /* ECC */
  25391. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25392. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25393. #ifdef USE_CERT_BUFFERS_256
  25394. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25395. DYNAMIC_TYPE_TMP_BUFFER));
  25396. /* Init buffer. */
  25397. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25398. if (eccCert != NULL) {
  25399. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25400. }
  25401. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25402. DYNAMIC_TYPE_TMP_BUFFER));
  25403. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25404. if (eccPrivKey != NULL) {
  25405. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25406. }
  25407. #else /* File system. */
  25408. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25409. eccCertSz = (word32)FOURK_BUF;
  25410. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25411. DYNAMIC_TYPE_TMP_BUFFER));
  25412. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25413. certFile)) > 0);
  25414. if (certFile != XBADFILE) {
  25415. XFCLOSE(certFile);
  25416. }
  25417. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25418. eccPrivKeySz = (word32)FOURK_BUF;
  25419. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25420. DYNAMIC_TYPE_TMP_BUFFER));
  25421. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25422. keyFile)) > 0);
  25423. if (keyFile != XBADFILE) {
  25424. XFCLOSE(keyFile);
  25425. }
  25426. #endif /* USE_CERT_BUFFERS_256 */
  25427. #endif /* END HAVE_ECC */
  25428. #ifndef NO_FILESYSTEM
  25429. /* Silence. */
  25430. (void)keyFile;
  25431. (void)certFile;
  25432. #endif
  25433. {
  25434. const pkcs7EnvelopedVector testVectors[] = {
  25435. /* DATA is a global variable defined in the makefile. */
  25436. #if !defined(NO_RSA)
  25437. #ifndef NO_DES3
  25438. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25439. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25440. #endif /* NO_DES3 */
  25441. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25442. #ifndef NO_AES_128
  25443. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25444. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25445. #endif
  25446. #ifndef NO_AES_192
  25447. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25448. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25449. #endif
  25450. #ifndef NO_AES_256
  25451. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25452. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25453. #endif
  25454. #endif /* NO_AES && HAVE_AES_CBC */
  25455. #endif /* NO_RSA */
  25456. #if defined(HAVE_ECC)
  25457. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25458. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25459. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25460. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25461. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25462. #endif
  25463. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25464. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25465. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25466. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25467. #endif
  25468. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25469. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25470. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25471. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25472. #endif
  25473. #endif /* NO_AES && HAVE_AES_CBC*/
  25474. #endif /* END HAVE_ECC */
  25475. }; /* END pkcs7EnvelopedVector */
  25476. #ifdef ECC_TIMING_RESISTANT
  25477. ExpectIntEQ(wc_InitRng(&rng), 0);
  25478. #endif
  25479. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25480. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25481. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25482. for (i = 0; i < testSz; i++) {
  25483. #ifdef ASN_BER_TO_DER
  25484. encodeSignedDataStream strm;
  25485. /* test setting stream mode, the first one using IO callbacks */
  25486. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25487. (word32)(testVectors + i)->certSz), 0);
  25488. if (pkcs7 != NULL) {
  25489. #ifdef ECC_TIMING_RESISTANT
  25490. pkcs7->rng = &rng;
  25491. #endif
  25492. if (i != 0)
  25493. pkcs7->content = (byte*)(testVectors + i)->content;
  25494. pkcs7->contentSz = (testVectors + i)->contentSz;
  25495. pkcs7->contentOID = (testVectors + i)->contentOID;
  25496. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25497. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25498. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25499. pkcs7->privateKey = (testVectors + i)->privateKey;
  25500. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25501. }
  25502. if (i == 0) {
  25503. XMEMSET(&strm, 0, sizeof(strm));
  25504. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25505. StreamOutputCB, (void*)&strm), 0);
  25506. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25507. }
  25508. else {
  25509. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25510. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25511. (word32)sizeof(output));
  25512. }
  25513. switch ((testVectors + i)->encryptOID) {
  25514. #ifndef NO_DES3
  25515. case DES3b:
  25516. case DESb:
  25517. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25518. break;
  25519. #endif
  25520. #ifdef HAVE_AESCCM
  25521. #ifdef WOLFSSL_AES_128
  25522. case AES128CCMb:
  25523. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25524. break;
  25525. #endif
  25526. #ifdef WOLFSSL_AES_192
  25527. case AES192CCMb:
  25528. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25529. break;
  25530. #endif
  25531. #ifdef WOLFSSL_AES_256
  25532. case AES256CCMb:
  25533. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25534. break;
  25535. #endif
  25536. #endif
  25537. default:
  25538. ExpectIntGE(encodedSz, 0);
  25539. }
  25540. if (encodedSz > 0) {
  25541. if (i == 0) {
  25542. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25543. strm.out, (word32)encodedSz, decoded,
  25544. (word32)sizeof(decoded));
  25545. }
  25546. else {
  25547. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25548. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25549. }
  25550. ExpectIntGE(decodedSz, 0);
  25551. /* Verify the size of each buffer. */
  25552. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25553. }
  25554. wc_PKCS7_Free(pkcs7);
  25555. pkcs7 = NULL;
  25556. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25557. #endif
  25558. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25559. (word32)(testVectors + i)->certSz), 0);
  25560. if (pkcs7 != NULL) {
  25561. #ifdef ECC_TIMING_RESISTANT
  25562. pkcs7->rng = &rng;
  25563. #endif
  25564. pkcs7->content = (byte*)(testVectors + i)->content;
  25565. pkcs7->contentSz = (testVectors + i)->contentSz;
  25566. pkcs7->contentOID = (testVectors + i)->contentOID;
  25567. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25568. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25569. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25570. pkcs7->privateKey = (testVectors + i)->privateKey;
  25571. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25572. }
  25573. #ifdef ASN_BER_TO_DER
  25574. /* test without setting stream mode */
  25575. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25576. #endif
  25577. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25578. (word32)sizeof(output)), 0);
  25579. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25580. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25581. ExpectIntGE(decodedSz, 0);
  25582. /* Verify the size of each buffer. */
  25583. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25584. /* Don't free the last time through the loop. */
  25585. if (i < testSz - 1) {
  25586. wc_PKCS7_Free(pkcs7);
  25587. pkcs7 = NULL;
  25588. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25589. }
  25590. } /* END test loop. */
  25591. }
  25592. /* Test bad args. */
  25593. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25594. (word32)sizeof(output)), BAD_FUNC_ARG);
  25595. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25596. (word32)sizeof(output)), BAD_FUNC_ARG);
  25597. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25598. /* Decode. */
  25599. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25600. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25601. BAD_FUNC_ARG);
  25602. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25603. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25604. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25605. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25606. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25607. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25608. BAD_FUNC_ARG);
  25609. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25610. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25611. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25612. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25613. /* only a failure for KARI test cases */
  25614. if (pkcs7 != NULL) {
  25615. tempWrd32 = pkcs7->singleCertSz;
  25616. pkcs7->singleCertSz = 0;
  25617. }
  25618. #if defined(WOLFSSL_ASN_TEMPLATE)
  25619. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25620. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25621. BUFFER_E);
  25622. #else
  25623. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25624. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25625. ASN_PARSE_E);
  25626. #endif
  25627. if (pkcs7 != NULL) {
  25628. pkcs7->singleCertSz = tempWrd32;
  25629. tmpBytePtr = pkcs7->singleCert;
  25630. pkcs7->singleCert = NULL;
  25631. }
  25632. #ifndef NO_RSA
  25633. #if defined(NO_PKCS7_STREAM)
  25634. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25635. * is returned from kari parse which gets set to bad func arg */
  25636. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25637. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25638. BAD_FUNC_ARG);
  25639. #else
  25640. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25641. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25642. ASN_PARSE_E);
  25643. #endif
  25644. #endif /* !NO_RSA */
  25645. if (pkcs7 != NULL) {
  25646. pkcs7->singleCert = tmpBytePtr;
  25647. }
  25648. #endif
  25649. if (pkcs7 != NULL) {
  25650. tempWrd32 = pkcs7->privateKeySz;
  25651. pkcs7->privateKeySz = 0;
  25652. }
  25653. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25654. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25655. BAD_FUNC_ARG);
  25656. if (pkcs7 != NULL) {
  25657. pkcs7->privateKeySz = tempWrd32;
  25658. tmpBytePtr = pkcs7->privateKey;
  25659. pkcs7->privateKey = NULL;
  25660. }
  25661. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25662. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25663. BAD_FUNC_ARG);
  25664. if (pkcs7 != NULL) {
  25665. pkcs7->privateKey = tmpBytePtr;
  25666. }
  25667. wc_PKCS7_Free(pkcs7);
  25668. pkcs7 = NULL;
  25669. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25670. /* test of decrypt callback with KEKRI enveloped data */
  25671. {
  25672. int envelopedSz = 0;
  25673. const byte keyId[] = { 0x00 };
  25674. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25675. if (pkcs7 != NULL) {
  25676. pkcs7->content = (byte*)input;
  25677. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25678. pkcs7->contentOID = DATA;
  25679. pkcs7->encryptOID = AES256CBCb;
  25680. }
  25681. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25682. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25683. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25684. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25685. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25686. (word32)sizeof(output))), 0);
  25687. wc_PKCS7_Free(pkcs7);
  25688. pkcs7 = NULL;
  25689. /* decode envelopedData */
  25690. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25691. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25692. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25693. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25694. envelopedSz, decoded, sizeof(decoded))), 0);
  25695. wc_PKCS7_Free(pkcs7);
  25696. pkcs7 = NULL;
  25697. }
  25698. #endif /* !NO_AES && !NO_AES_256 */
  25699. #ifndef NO_RSA
  25700. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25701. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25702. #endif /* NO_RSA */
  25703. #ifdef HAVE_ECC
  25704. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25705. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25706. #endif /* HAVE_ECC */
  25707. #ifdef ECC_TIMING_RESISTANT
  25708. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25709. #endif
  25710. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25711. !defined(NO_RSA) && !defined(NO_SHA)
  25712. {
  25713. byte out[7];
  25714. byte *cms = NULL;
  25715. word32 cmsSz;
  25716. XFILE cmsFile = XBADFILE;
  25717. XMEMSET(out, 0, sizeof(out));
  25718. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25719. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25720. != XBADFILE);
  25721. cmsSz = (word32)FOURK_BUF;
  25722. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25723. DYNAMIC_TYPE_TMP_BUFFER));
  25724. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25725. if (cmsFile != XBADFILE)
  25726. XFCLOSE(cmsFile);
  25727. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25728. sizeof_client_cert_der_2048), 0);
  25729. if (pkcs7 != NULL) {
  25730. pkcs7->privateKey = (byte*)client_key_der_2048;
  25731. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25732. }
  25733. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25734. 2), 0);
  25735. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25736. sizeof(out)), 0);
  25737. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25738. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25739. wc_PKCS7_Free(pkcs7);
  25740. pkcs7 = NULL;
  25741. }
  25742. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25743. #endif /* HAVE_PKCS7 */
  25744. return EXPECT_RESULT();
  25745. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25746. /*
  25747. * Testing wc_PKCS7_EncodeEncryptedData()
  25748. */
  25749. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25750. {
  25751. EXPECT_DECLS;
  25752. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25753. PKCS7* pkcs7 = NULL;
  25754. byte* tmpBytePtr = NULL;
  25755. byte encrypted[TWOK_BUF];
  25756. byte decoded[TWOK_BUF];
  25757. word32 tmpWrd32 = 0;
  25758. int tmpInt = 0;
  25759. int decodedSz = 0;
  25760. int encryptedSz = 0;
  25761. int testSz = 0;
  25762. int i = 0;
  25763. const byte data[] = { /* Hello World */
  25764. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25765. 0x72,0x6c,0x64
  25766. };
  25767. #ifndef NO_DES3
  25768. byte desKey[] = {
  25769. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25770. };
  25771. byte des3Key[] = {
  25772. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25773. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25774. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25775. };
  25776. #endif
  25777. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25778. #ifndef NO_AES_128
  25779. byte aes128Key[] = {
  25780. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25781. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25782. };
  25783. #endif
  25784. #ifndef NO_AES_192
  25785. byte aes192Key[] = {
  25786. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25787. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25788. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25789. };
  25790. #endif
  25791. #ifndef NO_AES_256
  25792. byte aes256Key[] = {
  25793. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25794. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25795. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25796. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25797. };
  25798. #endif
  25799. #endif /* !NO_AES && HAVE_AES_CBC */
  25800. const pkcs7EncryptedVector testVectors[] =
  25801. {
  25802. #ifndef NO_DES3
  25803. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25804. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25805. #endif /* !NO_DES3 */
  25806. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25807. #ifndef NO_AES_128
  25808. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25809. sizeof(aes128Key)},
  25810. #endif
  25811. #ifndef NO_AES_192
  25812. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25813. sizeof(aes192Key)},
  25814. #endif
  25815. #ifndef NO_AES_256
  25816. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25817. sizeof(aes256Key)},
  25818. #endif
  25819. #endif /* !NO_AES && HAVE_AES_CBC */
  25820. };
  25821. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25822. for (i = 0; i < testSz; i++) {
  25823. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25824. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25825. if (pkcs7 != NULL) {
  25826. pkcs7->content = (byte*)testVectors[i].content;
  25827. pkcs7->contentSz = testVectors[i].contentSz;
  25828. pkcs7->contentOID = testVectors[i].contentOID;
  25829. pkcs7->encryptOID = testVectors[i].encryptOID;
  25830. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25831. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25832. pkcs7->heap = HEAP_HINT;
  25833. }
  25834. /* encode encryptedData */
  25835. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25836. sizeof(encrypted)), 0);
  25837. /* Decode encryptedData */
  25838. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25839. encryptedSz, decoded, sizeof(decoded)), 0);
  25840. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25841. /* Keep values for last itr. */
  25842. if (i < testSz - 1) {
  25843. wc_PKCS7_Free(pkcs7);
  25844. pkcs7 = NULL;
  25845. }
  25846. }
  25847. if (pkcs7 == NULL || testSz == 0) {
  25848. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25849. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25850. }
  25851. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25852. sizeof(encrypted)),BAD_FUNC_ARG);
  25853. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25854. sizeof(encrypted)), BAD_FUNC_ARG);
  25855. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25856. 0), BAD_FUNC_ARG);
  25857. /* Testing the struct. */
  25858. if (pkcs7 != NULL) {
  25859. tmpBytePtr = pkcs7->content;
  25860. pkcs7->content = NULL;
  25861. }
  25862. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25863. sizeof(encrypted)), BAD_FUNC_ARG);
  25864. if (pkcs7 != NULL) {
  25865. pkcs7->content = tmpBytePtr;
  25866. tmpWrd32 = pkcs7->contentSz;
  25867. pkcs7->contentSz = 0;
  25868. }
  25869. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25870. sizeof(encrypted)), BAD_FUNC_ARG);
  25871. if (pkcs7 != NULL) {
  25872. pkcs7->contentSz = tmpWrd32;
  25873. tmpInt = pkcs7->encryptOID;
  25874. pkcs7->encryptOID = 0;
  25875. }
  25876. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25877. sizeof(encrypted)), BAD_FUNC_ARG);
  25878. if (pkcs7 != NULL) {
  25879. pkcs7->encryptOID = tmpInt;
  25880. tmpBytePtr = pkcs7->encryptionKey;
  25881. pkcs7->encryptionKey = NULL;
  25882. }
  25883. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25884. sizeof(encrypted)), BAD_FUNC_ARG);
  25885. if (pkcs7 != NULL) {
  25886. pkcs7->encryptionKey = tmpBytePtr;
  25887. tmpWrd32 = pkcs7->encryptionKeySz;
  25888. pkcs7->encryptionKeySz = 0;
  25889. }
  25890. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25891. sizeof(encrypted)), BAD_FUNC_ARG);
  25892. if (pkcs7 != NULL) {
  25893. pkcs7->encryptionKeySz = tmpWrd32;
  25894. }
  25895. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25896. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25897. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25898. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25899. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25900. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25901. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25902. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25903. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25904. decoded, 0), BAD_FUNC_ARG);
  25905. /* Test struct fields */
  25906. if (pkcs7 != NULL) {
  25907. tmpBytePtr = pkcs7->encryptionKey;
  25908. pkcs7->encryptionKey = NULL;
  25909. }
  25910. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25911. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25912. if (pkcs7 != NULL) {
  25913. pkcs7->encryptionKey = tmpBytePtr;
  25914. pkcs7->encryptionKeySz = 0;
  25915. }
  25916. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25917. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25918. wc_PKCS7_Free(pkcs7);
  25919. #endif
  25920. return EXPECT_RESULT();
  25921. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25922. /*
  25923. * Testing wc_PKCS7_Degenerate()
  25924. */
  25925. static int test_wc_PKCS7_Degenerate(void)
  25926. {
  25927. EXPECT_DECLS;
  25928. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25929. PKCS7* pkcs7 = NULL;
  25930. char fName[] = "./certs/test-degenerate.p7b";
  25931. XFILE f = XBADFILE;
  25932. byte der[4096];
  25933. word32 derSz = 0;
  25934. #ifndef NO_PKCS7_STREAM
  25935. word32 z;
  25936. int ret;
  25937. #endif /* !NO_PKCS7_STREAM */
  25938. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25939. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25940. if (f != XBADFILE)
  25941. XFCLOSE(f);
  25942. /* test degenerate success */
  25943. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25944. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25945. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25946. #ifndef NO_RSA
  25947. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25948. #ifndef NO_PKCS7_STREAM
  25949. wc_PKCS7_Free(pkcs7);
  25950. pkcs7 = NULL;
  25951. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25952. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25953. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25954. /* test for streaming */
  25955. ret = -1;
  25956. for (z = 0; z < derSz && ret != 0; z++) {
  25957. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25958. if (ret < 0){
  25959. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25960. }
  25961. }
  25962. ExpectIntEQ(ret, 0);
  25963. #endif /* !NO_PKCS7_STREAM */
  25964. #else
  25965. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25966. #endif /* NO_RSA */
  25967. wc_PKCS7_Free(pkcs7);
  25968. pkcs7 = NULL;
  25969. /* test with turning off degenerate cases */
  25970. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25971. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25972. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25973. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25974. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  25975. PKCS7_NO_SIGNER_E);
  25976. #ifndef NO_PKCS7_STREAM
  25977. wc_PKCS7_Free(pkcs7);
  25978. pkcs7 = NULL;
  25979. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25980. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25981. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25982. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25983. /* test for streaming */
  25984. ret = -1;
  25985. for (z = 0; z < derSz && ret != 0; z++) {
  25986. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25987. if (ret == WC_PKCS7_WANT_READ_E){
  25988. continue;
  25989. }
  25990. else
  25991. break;
  25992. }
  25993. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  25994. #endif /* !NO_PKCS7_STREAM */
  25995. wc_PKCS7_Free(pkcs7);
  25996. #endif
  25997. return EXPECT_RESULT();
  25998. } /* END test_wc_PKCS7_Degenerate() */
  25999. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26000. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26001. static byte berContent[] = {
  26002. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26003. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26004. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26005. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26006. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26007. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26008. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26009. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26010. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26011. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26012. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26013. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26014. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26015. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26016. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26017. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26018. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26019. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26020. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26021. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26022. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26023. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26024. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26025. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26026. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26027. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26028. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26029. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26030. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26031. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26032. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26033. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26034. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26035. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26036. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26037. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26038. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26039. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26040. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26041. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26042. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26043. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26044. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26045. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26046. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26047. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26048. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26049. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26050. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26051. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26052. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26053. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26054. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26055. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26056. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26057. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26058. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26059. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26060. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26061. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26062. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26063. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26064. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26065. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26066. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26067. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26068. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26069. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26070. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26071. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26072. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26073. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26074. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26075. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26076. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26077. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26078. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26079. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26080. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26081. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26082. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26083. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26084. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26085. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26086. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26087. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26088. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26089. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26090. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26091. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26092. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26093. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26094. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26095. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26096. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26097. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26098. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26099. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26100. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26101. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26102. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26103. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26104. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26105. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26106. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26107. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26108. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26109. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26110. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26111. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26112. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26113. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26114. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26115. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26116. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26117. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26118. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26119. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26120. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26121. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26122. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26123. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26124. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26125. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26126. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26127. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26128. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26129. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26130. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26131. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26132. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26133. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26134. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26135. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26136. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26137. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26138. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26139. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26140. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26141. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26142. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26143. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26144. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26145. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26146. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26147. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26148. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26149. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26150. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26151. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26152. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26153. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26154. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26155. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26156. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26157. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26158. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26159. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26160. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26161. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26162. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26163. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26164. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26165. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26166. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26167. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26168. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26169. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26170. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26171. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26172. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26173. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26174. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26175. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26176. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26177. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26178. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26179. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26180. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26181. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26182. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26183. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26184. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26185. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26186. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26187. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26188. 0x00, 0x00, 0x00, 0x00, 0x00
  26189. };
  26190. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26191. * !NO_DES3 && !NO_SHA
  26192. */
  26193. /*
  26194. * Testing wc_PKCS7_BER()
  26195. */
  26196. static int test_wc_PKCS7_BER(void)
  26197. {
  26198. EXPECT_DECLS;
  26199. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26200. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26201. PKCS7* pkcs7 = NULL;
  26202. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26203. XFILE f = XBADFILE;
  26204. byte der[4096];
  26205. #ifndef NO_DES3
  26206. byte decoded[2048];
  26207. #endif
  26208. word32 derSz = 0;
  26209. #ifndef NO_PKCS7_STREAM
  26210. word32 z;
  26211. int ret;
  26212. #endif /* !NO_PKCS7_STREAM */
  26213. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26214. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26215. if (f != XBADFILE) {
  26216. XFCLOSE(f);
  26217. f = XBADFILE;
  26218. }
  26219. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26220. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26221. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26222. #ifndef NO_RSA
  26223. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26224. #ifndef NO_PKCS7_STREAM
  26225. wc_PKCS7_Free(pkcs7);
  26226. pkcs7 = NULL;
  26227. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26228. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26229. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26230. /* test for streaming */
  26231. ret = -1;
  26232. for (z = 0; z < derSz && ret != 0; z++) {
  26233. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26234. if (ret < 0){
  26235. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26236. }
  26237. }
  26238. ExpectIntEQ(ret, 0);
  26239. #endif /* !NO_PKCS7_STREAM */
  26240. #else
  26241. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26242. #endif
  26243. wc_PKCS7_Free(pkcs7);
  26244. pkcs7 = NULL;
  26245. #ifndef NO_DES3
  26246. /* decode BER content */
  26247. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26248. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26249. if (f != XBADFILE) {
  26250. XFCLOSE(f);
  26251. f = XBADFILE;
  26252. }
  26253. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26254. #ifndef NO_RSA
  26255. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26256. #else
  26257. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26258. #endif
  26259. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26260. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26261. if (f != XBADFILE) {
  26262. XFCLOSE(f);
  26263. f = XBADFILE;
  26264. }
  26265. if (pkcs7 != NULL) {
  26266. pkcs7->privateKey = der;
  26267. pkcs7->privateKeySz = derSz;
  26268. }
  26269. #ifndef NO_RSA
  26270. #ifdef WOLFSSL_SP_MATH
  26271. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26272. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26273. #else
  26274. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26275. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26276. #endif
  26277. #else
  26278. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26279. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26280. #endif
  26281. wc_PKCS7_Free(pkcs7);
  26282. #endif /* !NO_DES3 */
  26283. #endif
  26284. return EXPECT_RESULT();
  26285. } /* END test_wc_PKCS7_BER() */
  26286. static int test_wc_PKCS7_signed_enveloped(void)
  26287. {
  26288. EXPECT_DECLS;
  26289. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26290. !defined(NO_FILESYSTEM)
  26291. XFILE f = XBADFILE;
  26292. PKCS7* pkcs7 = NULL;
  26293. #ifdef HAVE_AES_CBC
  26294. PKCS7* inner = NULL;
  26295. #endif
  26296. WC_RNG rng;
  26297. unsigned char key[FOURK_BUF/2];
  26298. unsigned char cert[FOURK_BUF/2];
  26299. unsigned char env[FOURK_BUF];
  26300. int envSz = FOURK_BUF;
  26301. int keySz = 0;
  26302. int certSz = 0;
  26303. unsigned char sig[FOURK_BUF * 2];
  26304. int sigSz = FOURK_BUF * 2;
  26305. #ifdef HAVE_AES_CBC
  26306. unsigned char decoded[FOURK_BUF];
  26307. int decodedSz = FOURK_BUF;
  26308. #endif
  26309. #ifndef NO_PKCS7_STREAM
  26310. int z;
  26311. int ret;
  26312. #endif /* !NO_PKCS7_STREAM */
  26313. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26314. /* load cert */
  26315. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26316. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26317. if (f != XBADFILE) {
  26318. XFCLOSE(f);
  26319. f = XBADFILE;
  26320. }
  26321. /* load key */
  26322. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26323. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26324. if (f != XBADFILE) {
  26325. XFCLOSE(f);
  26326. f = XBADFILE;
  26327. }
  26328. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26329. /* sign cert for envelope */
  26330. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26331. ExpectIntEQ(wc_InitRng(&rng), 0);
  26332. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26333. if (pkcs7 != NULL) {
  26334. pkcs7->content = cert;
  26335. pkcs7->contentSz = certSz;
  26336. pkcs7->contentOID = DATA;
  26337. pkcs7->privateKey = key;
  26338. pkcs7->privateKeySz = keySz;
  26339. pkcs7->encryptOID = RSAk;
  26340. pkcs7->hashOID = SHA256h;
  26341. pkcs7->rng = &rng;
  26342. }
  26343. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26344. wc_PKCS7_Free(pkcs7);
  26345. pkcs7 = NULL;
  26346. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26347. #ifdef HAVE_AES_CBC
  26348. /* create envelope */
  26349. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26350. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26351. if (pkcs7 != NULL) {
  26352. pkcs7->content = sig;
  26353. pkcs7->contentSz = sigSz;
  26354. pkcs7->contentOID = DATA;
  26355. pkcs7->encryptOID = AES256CBCb;
  26356. pkcs7->privateKey = key;
  26357. pkcs7->privateKeySz = keySz;
  26358. }
  26359. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26360. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26361. wc_PKCS7_Free(pkcs7);
  26362. pkcs7 = NULL;
  26363. #endif
  26364. /* create bad signed enveloped data */
  26365. sigSz = FOURK_BUF * 2;
  26366. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26367. ExpectIntEQ(wc_InitRng(&rng), 0);
  26368. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26369. if (pkcs7 != NULL) {
  26370. pkcs7->content = env;
  26371. pkcs7->contentSz = envSz;
  26372. pkcs7->contentOID = DATA;
  26373. pkcs7->privateKey = key;
  26374. pkcs7->privateKeySz = keySz;
  26375. pkcs7->encryptOID = RSAk;
  26376. pkcs7->hashOID = SHA256h;
  26377. pkcs7->rng = &rng;
  26378. }
  26379. /* Set no certs in bundle for this test. */
  26380. if (pkcs7 != NULL) {
  26381. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26382. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26383. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26384. }
  26385. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26386. wc_PKCS7_Free(pkcs7);
  26387. pkcs7 = NULL;
  26388. /* check verify fails */
  26389. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26390. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26391. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26392. PKCS7_SIGNEEDS_CHECK);
  26393. /* try verifying the signature manually */
  26394. {
  26395. RsaKey rKey;
  26396. word32 idx = 0;
  26397. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26398. WC_MAX_DIGEST_SIZE];
  26399. int digestSz = 0;
  26400. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26401. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26402. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26403. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26404. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26405. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26406. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26407. /* verify was success */
  26408. }
  26409. wc_PKCS7_Free(pkcs7);
  26410. pkcs7 = NULL;
  26411. /* initializing the PKCS7 struct with the signing certificate should pass */
  26412. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26413. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26414. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26415. #ifndef NO_PKCS7_STREAM
  26416. wc_PKCS7_Free(pkcs7);
  26417. pkcs7 = NULL;
  26418. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26419. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26420. /* test for streaming */
  26421. ret = -1;
  26422. for (z = 0; z < sigSz && ret != 0; z++) {
  26423. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26424. if (ret < 0){
  26425. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26426. }
  26427. }
  26428. ExpectIntEQ(ret, 0);
  26429. #endif /* !NO_PKCS7_STREAM */
  26430. wc_PKCS7_Free(pkcs7);
  26431. pkcs7 = NULL;
  26432. /* create valid degenerate bundle */
  26433. sigSz = FOURK_BUF * 2;
  26434. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26435. if (pkcs7 != NULL) {
  26436. pkcs7->content = env;
  26437. pkcs7->contentSz = envSz;
  26438. pkcs7->contentOID = DATA;
  26439. pkcs7->privateKey = key;
  26440. pkcs7->privateKeySz = keySz;
  26441. pkcs7->encryptOID = RSAk;
  26442. pkcs7->hashOID = SHA256h;
  26443. pkcs7->rng = &rng;
  26444. }
  26445. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26446. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26447. wc_PKCS7_Free(pkcs7);
  26448. pkcs7 = NULL;
  26449. wc_FreeRng(&rng);
  26450. /* check verify */
  26451. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26452. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26453. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26454. ExpectNotNull(pkcs7->content);
  26455. #ifndef NO_PKCS7_STREAM
  26456. wc_PKCS7_Free(pkcs7);
  26457. pkcs7 = NULL;
  26458. /* create valid degenerate bundle */
  26459. sigSz = FOURK_BUF * 2;
  26460. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26461. if (pkcs7 != NULL) {
  26462. pkcs7->content = env;
  26463. pkcs7->contentSz = envSz;
  26464. pkcs7->contentOID = DATA;
  26465. pkcs7->privateKey = key;
  26466. pkcs7->privateKeySz = keySz;
  26467. pkcs7->encryptOID = RSAk;
  26468. pkcs7->hashOID = SHA256h;
  26469. pkcs7->rng = &rng;
  26470. }
  26471. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26472. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26473. wc_PKCS7_Free(pkcs7);
  26474. pkcs7 = NULL;
  26475. wc_FreeRng(&rng);
  26476. /* check verify */
  26477. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26478. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26479. /* test for streaming */
  26480. ret = -1;
  26481. for (z = 0; z < sigSz && ret != 0; z++) {
  26482. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26483. if (ret < 0){
  26484. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26485. }
  26486. }
  26487. ExpectIntEQ(ret, 0);
  26488. #endif /* !NO_PKCS7_STREAM */
  26489. #ifdef HAVE_AES_CBC
  26490. /* check decode */
  26491. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26492. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26493. if (inner != NULL) {
  26494. inner->privateKey = key;
  26495. inner->privateKeySz = keySz;
  26496. }
  26497. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26498. pkcs7->contentSz, decoded, decodedSz)), 0);
  26499. wc_PKCS7_Free(inner);
  26500. inner = NULL;
  26501. #endif
  26502. wc_PKCS7_Free(pkcs7);
  26503. pkcs7 = NULL;
  26504. #ifdef HAVE_AES_CBC
  26505. /* check cert set */
  26506. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26507. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26508. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26509. ExpectNotNull(pkcs7->singleCert);
  26510. ExpectIntNE(pkcs7->singleCertSz, 0);
  26511. wc_PKCS7_Free(pkcs7);
  26512. pkcs7 = NULL;
  26513. #ifndef NO_PKCS7_STREAM
  26514. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26515. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26516. /* test for streaming */
  26517. ret = -1;
  26518. for (z = 0; z < decodedSz && ret != 0; z++) {
  26519. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26520. if (ret < 0){
  26521. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26522. }
  26523. }
  26524. ExpectIntEQ(ret, 0);
  26525. ExpectNotNull(pkcs7->singleCert);
  26526. ExpectIntNE(pkcs7->singleCertSz, 0);
  26527. wc_PKCS7_Free(pkcs7);
  26528. pkcs7 = NULL;
  26529. #endif /* !NO_PKCS7_STREAM */
  26530. #endif
  26531. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26532. return EXPECT_RESULT();
  26533. }
  26534. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26535. {
  26536. EXPECT_DECLS;
  26537. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26538. && !defined(NO_AES)
  26539. PKCS7* pkcs7 = NULL;
  26540. void* heap = NULL;
  26541. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26542. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26543. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26544. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26545. wc_PKCS7_Free(pkcs7);
  26546. #endif
  26547. return EXPECT_RESULT();
  26548. }
  26549. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26550. {
  26551. EXPECT_DECLS;
  26552. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26553. && !defined(NO_AES)
  26554. PKCS7* pkcs7 = NULL;
  26555. void* heap = NULL;
  26556. WOLFSSL_CTX* ctx = NULL;
  26557. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26558. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26559. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26560. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26561. wc_PKCS7_Free(pkcs7);
  26562. #endif
  26563. return EXPECT_RESULT();
  26564. }
  26565. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26566. {
  26567. EXPECT_DECLS;
  26568. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26569. && !defined(NO_AES)
  26570. PKCS7* pkcs7 = NULL;
  26571. void* heap = NULL;
  26572. WOLFSSL_CTX* ctx = NULL;
  26573. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26574. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26575. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26576. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26577. wc_PKCS7_Free(pkcs7);
  26578. #endif
  26579. return EXPECT_RESULT();
  26580. }
  26581. static int test_wc_PKCS7_DecodeCompressedData(void)
  26582. {
  26583. EXPECT_DECLS;
  26584. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26585. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26586. PKCS7* pkcs7 = NULL;
  26587. void* heap = NULL;
  26588. byte out[4096];
  26589. byte* decompressed = NULL;
  26590. int outSz;
  26591. int decompressedSz;
  26592. const char* cert = "./certs/client-cert.pem";
  26593. byte* cert_buf = NULL;
  26594. size_t cert_sz = 0;
  26595. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26596. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26597. DYNAMIC_TYPE_TMP_BUFFER)));
  26598. decompressedSz = (int)cert_sz;
  26599. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26600. if (pkcs7 != NULL) {
  26601. pkcs7->content = (byte*)cert_buf;
  26602. pkcs7->contentSz = (word32)cert_sz;
  26603. pkcs7->contentOID = DATA;
  26604. }
  26605. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26606. sizeof(out))), 0);
  26607. wc_PKCS7_Free(pkcs7);
  26608. pkcs7 = NULL;
  26609. /* compressed key should be smaller than when started */
  26610. ExpectIntLT(outSz, cert_sz);
  26611. /* test decompression */
  26612. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26613. ExpectIntEQ(pkcs7->contentOID, 0);
  26614. /* fail case with out buffer too small */
  26615. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26616. decompressed, outSz), 0);
  26617. /* success case */
  26618. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26619. decompressed, decompressedSz), cert_sz);
  26620. ExpectIntEQ(pkcs7->contentOID, DATA);
  26621. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26622. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26623. decompressed = NULL;
  26624. /* test decompression function with different 'max' inputs */
  26625. outSz = sizeof(out);
  26626. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26627. 0);
  26628. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26629. out, outSz, 0, heap), 0);
  26630. ExpectNull(decompressed);
  26631. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26632. out, outSz, 0, heap), 0);
  26633. ExpectNotNull(decompressed);
  26634. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26635. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26636. decompressed = NULL;
  26637. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26638. out, outSz, 0, heap), 0);
  26639. ExpectNotNull(decompressed);
  26640. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26641. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26642. if (cert_buf != NULL)
  26643. free(cert_buf);
  26644. wc_PKCS7_Free(pkcs7);
  26645. #endif
  26646. return EXPECT_RESULT();
  26647. }
  26648. static int test_wc_i2d_PKCS12(void)
  26649. {
  26650. EXPECT_DECLS;
  26651. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26652. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26653. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26654. WC_PKCS12* pkcs12 = NULL;
  26655. unsigned char der[FOURK_BUF * 2];
  26656. unsigned char* pt;
  26657. int derSz = 0;
  26658. unsigned char out[FOURK_BUF * 2];
  26659. int outSz = FOURK_BUF * 2;
  26660. const char p12_f[] = "./certs/test-servercert.p12";
  26661. XFILE f = XBADFILE;
  26662. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26663. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26664. if (f != XBADFILE)
  26665. XFCLOSE(f);
  26666. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26667. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26668. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26669. ExpectIntEQ(outSz, derSz);
  26670. outSz = derSz - 1;
  26671. pt = out;
  26672. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26673. outSz = derSz;
  26674. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26675. ExpectIntEQ((pt == out), 0);
  26676. pt = NULL;
  26677. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26678. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26679. wc_PKCS12_free(pkcs12);
  26680. pkcs12 = NULL;
  26681. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26682. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26683. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26684. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26685. ExpectIntEQ(outSz, derSz);
  26686. wc_PKCS12_free(pkcs12);
  26687. pkcs12 = NULL;
  26688. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26689. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26690. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26691. ExpectIntEQ(outSz, derSz);
  26692. wc_PKCS12_free(pkcs12);
  26693. pkcs12 = NULL;
  26694. #endif
  26695. return EXPECT_RESULT();
  26696. }
  26697. /* Testing wc_SignatureGetSize() for signature type ECC */
  26698. static int test_wc_SignatureGetSize_ecc(void)
  26699. {
  26700. EXPECT_DECLS;
  26701. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26702. enum wc_SignatureType sig_type;
  26703. word32 key_len;
  26704. ecc_key ecc;
  26705. const char* qx =
  26706. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26707. const char* qy =
  26708. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26709. const char* d =
  26710. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26711. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26712. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26713. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26714. /* Input for signature type ECC */
  26715. sig_type = WC_SIGNATURE_TYPE_ECC;
  26716. key_len = sizeof(ecc_key);
  26717. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26718. /* Test bad args */
  26719. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26720. sig_type = (enum wc_SignatureType) 100;
  26721. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26722. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26723. sig_type = WC_SIGNATURE_TYPE_ECC;
  26724. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26725. key_len = (word32)0;
  26726. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26727. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26728. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26729. return EXPECT_RESULT();
  26730. } /* END test_wc_SignatureGetSize_ecc() */
  26731. /* Testing wc_SignatureGetSize() for signature type rsa */
  26732. static int test_wc_SignatureGetSize_rsa(void)
  26733. {
  26734. EXPECT_DECLS;
  26735. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26736. enum wc_SignatureType sig_type;
  26737. word32 key_len;
  26738. word32 idx = 0;
  26739. RsaKey rsa_key;
  26740. byte* tmp = NULL;
  26741. size_t bytes;
  26742. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26743. #ifdef USE_CERT_BUFFERS_1024
  26744. bytes = (size_t)sizeof_client_key_der_1024;
  26745. if (bytes < (size_t)sizeof_client_key_der_1024)
  26746. bytes = (size_t)sizeof_client_cert_der_1024;
  26747. #elif defined(USE_CERT_BUFFERS_2048)
  26748. bytes = (size_t)sizeof_client_key_der_2048;
  26749. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26750. bytes = (size_t)sizeof_client_cert_der_2048;
  26751. #else
  26752. bytes = FOURK_BUF;
  26753. #endif
  26754. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26755. DYNAMIC_TYPE_TMP_BUFFER));
  26756. if (tmp != NULL) {
  26757. #ifdef USE_CERT_BUFFERS_1024
  26758. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26759. #elif defined(USE_CERT_BUFFERS_2048)
  26760. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26761. #elif !defined(NO_FILESYSTEM)
  26762. XFILE file = XBADFILE;
  26763. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26764. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26765. if (file != XBADFILE)
  26766. XFCLOSE(file);
  26767. }
  26768. #else
  26769. ExpectFail();
  26770. #endif
  26771. }
  26772. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26773. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26774. /* Input for signature type RSA */
  26775. sig_type = WC_SIGNATURE_TYPE_RSA;
  26776. key_len = sizeof(RsaKey);
  26777. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26778. /* Test bad args */
  26779. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26780. sig_type = (enum wc_SignatureType)100;
  26781. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26782. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26783. sig_type = WC_SIGNATURE_TYPE_RSA;
  26784. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26785. key_len = (word32)0;
  26786. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26787. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26788. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26789. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26790. return EXPECT_RESULT();
  26791. } /* END test_wc_SignatureGetSize_rsa(void) */
  26792. /*----------------------------------------------------------------------------*
  26793. | hash.h Tests
  26794. *----------------------------------------------------------------------------*/
  26795. static int test_wc_HashInit(void)
  26796. {
  26797. EXPECT_DECLS;
  26798. int i; /* 0 indicates tests passed, 1 indicates failure */
  26799. wc_HashAlg hash;
  26800. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26801. enum wc_HashType enumArray[] = {
  26802. #ifndef NO_MD5
  26803. WC_HASH_TYPE_MD5,
  26804. #endif
  26805. #ifndef NO_SHA
  26806. WC_HASH_TYPE_SHA,
  26807. #endif
  26808. #ifdef WOLFSSL_SHA224
  26809. WC_HASH_TYPE_SHA224,
  26810. #endif
  26811. #ifndef NO_SHA256
  26812. WC_HASH_TYPE_SHA256,
  26813. #endif
  26814. #ifdef WOLFSSL_SHA384
  26815. WC_HASH_TYPE_SHA384,
  26816. #endif
  26817. #ifdef WOLFSSL_SHA512
  26818. WC_HASH_TYPE_SHA512,
  26819. #endif
  26820. };
  26821. /* dynamically finds the length */
  26822. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26823. /* For loop to test various arguments... */
  26824. for (i = 0; i < enumlen; i++) {
  26825. /* check for bad args */
  26826. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26827. wc_HashFree(&hash, enumArray[i]);
  26828. /* check for null ptr */
  26829. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26830. } /* end of for loop */
  26831. return EXPECT_RESULT();
  26832. } /* end of test_wc_HashInit */
  26833. /*
  26834. * Unit test function for wc_HashSetFlags()
  26835. */
  26836. static int test_wc_HashSetFlags(void)
  26837. {
  26838. EXPECT_DECLS;
  26839. #ifdef WOLFSSL_HASH_FLAGS
  26840. wc_HashAlg hash;
  26841. word32 flags = 0;
  26842. int i, j;
  26843. int notSupportedLen;
  26844. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26845. enum wc_HashType enumArray[] = {
  26846. #ifndef NO_MD5
  26847. WC_HASH_TYPE_MD5,
  26848. #endif
  26849. #ifndef NO_SHA
  26850. WC_HASH_TYPE_SHA,
  26851. #endif
  26852. #ifdef WOLFSSL_SHA224
  26853. WC_HASH_TYPE_SHA224,
  26854. #endif
  26855. #ifndef NO_SHA256
  26856. WC_HASH_TYPE_SHA256,
  26857. #endif
  26858. #ifdef WOLFSSL_SHA384
  26859. WC_HASH_TYPE_SHA384,
  26860. #endif
  26861. #ifdef WOLFSSL_SHA512
  26862. WC_HASH_TYPE_SHA512,
  26863. #endif
  26864. #ifdef WOLFSSL_SHA3
  26865. WC_HASH_TYPE_SHA3_224,
  26866. #endif
  26867. };
  26868. enum wc_HashType notSupported[] = {
  26869. WC_HASH_TYPE_MD5_SHA,
  26870. WC_HASH_TYPE_MD2,
  26871. WC_HASH_TYPE_MD4,
  26872. WC_HASH_TYPE_BLAKE2B,
  26873. WC_HASH_TYPE_BLAKE2S,
  26874. WC_HASH_TYPE_NONE,
  26875. };
  26876. /* dynamically finds the length */
  26877. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26878. /* For loop to test various arguments... */
  26879. for (i = 0; i < enumlen; i++) {
  26880. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26881. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  26882. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26883. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  26884. wc_HashFree(&hash, enumArray[i]);
  26885. }
  26886. /* For loop to test not supported cases */
  26887. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26888. for (j = 0; j < notSupportedLen; j++) {
  26889. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26890. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  26891. BAD_FUNC_ARG);
  26892. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26893. }
  26894. #endif
  26895. return EXPECT_RESULT();
  26896. } /* END test_wc_HashSetFlags */
  26897. /*
  26898. * Unit test function for wc_HashGetFlags()
  26899. */
  26900. static int test_wc_HashGetFlags(void)
  26901. {
  26902. EXPECT_DECLS;
  26903. #ifdef WOLFSSL_HASH_FLAGS
  26904. wc_HashAlg hash;
  26905. word32 flags = 0;
  26906. int i, j;
  26907. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26908. enum wc_HashType enumArray[] = {
  26909. #ifndef NO_MD5
  26910. WC_HASH_TYPE_MD5,
  26911. #endif
  26912. #ifndef NO_SHA
  26913. WC_HASH_TYPE_SHA,
  26914. #endif
  26915. #ifdef WOLFSSL_SHA224
  26916. WC_HASH_TYPE_SHA224,
  26917. #endif
  26918. #ifndef NO_SHA256
  26919. WC_HASH_TYPE_SHA256,
  26920. #endif
  26921. #ifdef WOLFSSL_SHA384
  26922. WC_HASH_TYPE_SHA384,
  26923. #endif
  26924. #ifdef WOLFSSL_SHA512
  26925. WC_HASH_TYPE_SHA512,
  26926. #endif
  26927. #ifdef WOLFSSL_SHA3
  26928. WC_HASH_TYPE_SHA3_224,
  26929. #endif
  26930. };
  26931. enum wc_HashType notSupported[] = {
  26932. WC_HASH_TYPE_MD5_SHA,
  26933. WC_HASH_TYPE_MD2,
  26934. WC_HASH_TYPE_MD4,
  26935. WC_HASH_TYPE_BLAKE2B,
  26936. WC_HASH_TYPE_BLAKE2S,
  26937. WC_HASH_TYPE_NONE,
  26938. };
  26939. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26940. int notSupportedLen;
  26941. /* For loop to test various arguments... */
  26942. for (i = 0; i < enumlen; i++) {
  26943. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26944. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  26945. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26946. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  26947. wc_HashFree(&hash, enumArray[i]);
  26948. }
  26949. /* For loop to test not supported cases */
  26950. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26951. for (j = 0; j < notSupportedLen; j++) {
  26952. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26953. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  26954. BAD_FUNC_ARG);
  26955. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26956. }
  26957. #endif
  26958. return EXPECT_RESULT();
  26959. } /* END test_wc_HashGetFlags */
  26960. /*----------------------------------------------------------------------------*
  26961. | Compatibility Tests
  26962. *----------------------------------------------------------------------------*/
  26963. /*----------------------------------------------------------------------------*
  26964. | ASN.1 Tests
  26965. *----------------------------------------------------------------------------*/
  26966. static int test_wolfSSL_ASN1_BIT_STRING(void)
  26967. {
  26968. EXPECT_DECLS;
  26969. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  26970. ASN1_BIT_STRING* str = NULL;
  26971. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26972. /* Empty data testing. */
  26973. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  26974. ASN1_BIT_STRING_free(str);
  26975. str = NULL;
  26976. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26977. /* Invalid parameter testing. */
  26978. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  26979. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  26980. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  26981. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  26982. /* No bit string - bit is always 0. */
  26983. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  26984. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  26985. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26986. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  26987. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  26988. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  26989. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  26990. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26991. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  26992. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  26993. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  26994. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  26995. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  26996. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  26997. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  26998. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  26999. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27000. ASN1_BIT_STRING_free(str);
  27001. ASN1_BIT_STRING_free(NULL);
  27002. #endif
  27003. return EXPECT_RESULT();
  27004. }
  27005. static int test_wolfSSL_ASN1_INTEGER(void)
  27006. {
  27007. EXPECT_DECLS;
  27008. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27009. ASN1_INTEGER* a = NULL;
  27010. ASN1_INTEGER* dup = NULL;
  27011. const unsigned char invalidLenDer[] = {
  27012. 0x02, 0x20, 0x00
  27013. };
  27014. const unsigned char longDer[] = {
  27015. 0x02, 0x20,
  27016. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27017. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27018. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27019. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27020. };
  27021. const unsigned char* p;
  27022. /* Invalid parameter testing. */
  27023. ASN1_INTEGER_free(NULL);
  27024. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27025. ExpectNotNull(a = ASN1_INTEGER_new());
  27026. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27027. ASN1_INTEGER_free(dup);
  27028. dup = NULL;
  27029. ASN1_INTEGER_free(a);
  27030. a = NULL;
  27031. p = longDer;
  27032. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27033. p = longDer;
  27034. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27035. ExpectPtrNE(p, longDer);
  27036. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27037. ASN1_INTEGER_free(dup);
  27038. ASN1_INTEGER_free(a);
  27039. #endif
  27040. return EXPECT_RESULT();
  27041. }
  27042. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27043. {
  27044. EXPECT_DECLS;
  27045. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27046. ASN1_INTEGER* a = NULL;
  27047. ASN1_INTEGER* b = NULL;
  27048. ExpectNotNull(a = ASN1_INTEGER_new());
  27049. ExpectNotNull(b = ASN1_INTEGER_new());
  27050. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27051. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27052. /* Invalid parameter testing. */
  27053. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27054. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27055. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27056. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27057. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27058. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27059. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27060. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27061. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27062. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27063. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27064. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27065. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27066. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27067. ASN1_INTEGER_free(b);
  27068. ASN1_INTEGER_free(a);
  27069. #endif
  27070. return EXPECT_RESULT();
  27071. }
  27072. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27073. {
  27074. EXPECT_DECLS;
  27075. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27076. ASN1_INTEGER* ai = NULL;
  27077. ASN1_INTEGER* ai2 = NULL;
  27078. BIGNUM* bn = NULL;
  27079. BIGNUM* bn2 = NULL;
  27080. ExpectNotNull(ai = ASN1_INTEGER_new());
  27081. ExpectNotNull(bn2 = BN_new());
  27082. /* Invalid parameter testing. */
  27083. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27084. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27085. /* at the moment hard setting since no set function */
  27086. if (ai != NULL) {
  27087. ai->data[0] = 0xff; /* No DER encoding. */
  27088. ai->length = 1;
  27089. }
  27090. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27091. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27092. BN_free(bn);
  27093. bn = NULL;
  27094. #else
  27095. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27096. #endif
  27097. if (ai != NULL) {
  27098. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27099. ai->data[1] = 0x04; /* bad length of integer */
  27100. ai->data[2] = 0x03;
  27101. ai->length = 3;
  27102. }
  27103. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27104. /* Interpreted as a number 0x020403. */
  27105. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27106. BN_free(bn);
  27107. bn = NULL;
  27108. #else
  27109. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27110. #endif
  27111. if (ai != NULL) {
  27112. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27113. ai->data[1] = 0x01; /* length of integer */
  27114. ai->data[2] = 0x03;
  27115. ai->length = 3;
  27116. }
  27117. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27118. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27119. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27120. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27121. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27122. if (ai != NULL) {
  27123. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27124. ai->data[1] = 0x02; /* length of integer */
  27125. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27126. ai->data[3] = 0xff;
  27127. ai->length = 4;
  27128. }
  27129. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27130. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27131. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27132. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27133. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27134. if (ai != NULL) {
  27135. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27136. ai->data[1] = 0x01; /* length of integer */
  27137. ai->data[2] = 0x00;
  27138. ai->length = 3;
  27139. }
  27140. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27141. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27142. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27143. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27144. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27145. if (ai != NULL) {
  27146. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27147. ai->data[1] = 0x01; /* length of integer */
  27148. ai->data[2] = 0x01;
  27149. ai->length = 3;
  27150. ai->negative = 1;
  27151. }
  27152. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27153. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27154. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27155. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27156. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27157. BN_free(bn2);
  27158. BN_free(bn);
  27159. ASN1_INTEGER_free(ai2);
  27160. ASN1_INTEGER_free(ai);
  27161. #endif
  27162. return EXPECT_RESULT();
  27163. }
  27164. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27165. {
  27166. EXPECT_DECLS;
  27167. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27168. ASN1_INTEGER *a = NULL;
  27169. long val;
  27170. ExpectNotNull(a = ASN1_INTEGER_new());
  27171. /* Invalid parameter testing. */
  27172. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27173. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27174. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27175. #else
  27176. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27177. #endif
  27178. ASN1_INTEGER_free(a);
  27179. a = NULL;
  27180. ExpectNotNull(a = ASN1_INTEGER_new());
  27181. val = 0;
  27182. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27183. ASN1_INTEGER_free(a);
  27184. a = NULL;
  27185. /* 0 */
  27186. ExpectNotNull(a = ASN1_INTEGER_new());
  27187. val = 0;
  27188. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27189. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27190. ASN1_INTEGER_free(a);
  27191. a = NULL;
  27192. /* 40 */
  27193. ExpectNotNull(a = ASN1_INTEGER_new());
  27194. val = 40;
  27195. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27196. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27197. ASN1_INTEGER_free(a);
  27198. a = NULL;
  27199. /* -40 */
  27200. ExpectNotNull(a = ASN1_INTEGER_new());
  27201. val = -40;
  27202. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27203. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27204. ASN1_INTEGER_free(a);
  27205. a = NULL;
  27206. /* 128 */
  27207. ExpectNotNull(a = ASN1_INTEGER_new());
  27208. val = 128;
  27209. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27210. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27211. ASN1_INTEGER_free(a);
  27212. a = NULL;
  27213. /* -128 */
  27214. ExpectNotNull(a = ASN1_INTEGER_new());
  27215. val = -128;
  27216. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27217. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27218. ASN1_INTEGER_free(a);
  27219. a = NULL;
  27220. /* 200 */
  27221. ExpectNotNull(a = ASN1_INTEGER_new());
  27222. val = 200;
  27223. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27224. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27225. ASN1_INTEGER_free(a);
  27226. a = NULL;
  27227. /* int max (2147483647) */
  27228. ExpectNotNull(a = ASN1_INTEGER_new());
  27229. val = 2147483647;
  27230. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27231. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27232. ASN1_INTEGER_free(a);
  27233. a = NULL;
  27234. /* int min (-2147483648) */
  27235. ExpectNotNull(a = ASN1_INTEGER_new());
  27236. val = -2147483647 - 1;
  27237. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27238. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27239. ASN1_INTEGER_free(a);
  27240. a = NULL;
  27241. /* long max positive */
  27242. ExpectNotNull(a = ASN1_INTEGER_new());
  27243. val = (long)(((unsigned long)-1) >> 1);
  27244. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27245. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27246. ASN1_INTEGER_free(a);
  27247. #endif
  27248. return EXPECT_RESULT();
  27249. }
  27250. #if defined(OPENSSL_EXTRA)
  27251. typedef struct ASN1IntTestVector {
  27252. const byte* der;
  27253. const size_t derSz;
  27254. const long value;
  27255. } ASN1IntTestVector;
  27256. #endif
  27257. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27258. {
  27259. EXPECT_DECLS;
  27260. #if defined(OPENSSL_EXTRA)
  27261. size_t i;
  27262. WOLFSSL_ASN1_INTEGER* a = NULL;
  27263. WOLFSSL_ASN1_INTEGER* b = NULL;
  27264. WOLFSSL_ASN1_INTEGER* c = NULL;
  27265. const byte* p = NULL;
  27266. byte* p2 = NULL;
  27267. byte* reEncoded = NULL;
  27268. int reEncodedSz = 0;
  27269. static const byte zeroDer[] = {
  27270. 0x02, 0x01, 0x00
  27271. };
  27272. static const byte oneDer[] = {
  27273. 0x02, 0x01, 0x01
  27274. };
  27275. static const byte negativeDer[] = {
  27276. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27277. };
  27278. static const byte positiveDer[] = {
  27279. 0x02, 0x03, 0x01, 0x00, 0x01
  27280. };
  27281. static const byte primeDer[] = {
  27282. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27283. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27284. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27285. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27286. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27287. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27288. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27289. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27290. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27291. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27292. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27293. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27294. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27295. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27296. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27297. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27298. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27299. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27300. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27301. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27302. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27303. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27304. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27305. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27306. };
  27307. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27308. static const ASN1IntTestVector testVectors[] = {
  27309. {zeroDer, sizeof(zeroDer), 0},
  27310. {oneDer, sizeof(oneDer), 1},
  27311. {negativeDer, sizeof(negativeDer), -4123123},
  27312. {positiveDer, sizeof(positiveDer), 65537},
  27313. {primeDer, sizeof(primeDer), 0}
  27314. };
  27315. static const size_t NUM_TEST_VECTORS =
  27316. sizeof(testVectors)/sizeof(testVectors[0]);
  27317. /* Check d2i error conditions */
  27318. /* NULL pointer to input. */
  27319. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27320. ExpectNull(b);
  27321. /* NULL input. */
  27322. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27323. ExpectNull(b);
  27324. /* 0 length. */
  27325. p = testVectors[0].der;
  27326. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27327. ExpectNull(b);
  27328. /* Negative length. */
  27329. p = testVectors[0].der;
  27330. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27331. ExpectNull(b);
  27332. /* Garbage DER input. */
  27333. p = garbageDer;
  27334. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27335. ExpectNull(b);
  27336. /* Check i2d error conditions */
  27337. /* NULL input. */
  27338. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27339. /* 0 length input data buffer (a->length == 0). */
  27340. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27341. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27342. if (a != NULL)
  27343. a->data = NULL;
  27344. /* NULL input data buffer. */
  27345. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27346. if (a != NULL) {
  27347. /* Reset a->data. */
  27348. a->data = a->intData;
  27349. }
  27350. /* Set a to valid value. */
  27351. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27352. /* NULL output buffer. */
  27353. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27354. wolfSSL_ASN1_INTEGER_free(a);
  27355. a = NULL;
  27356. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27357. p = testVectors[i].der;
  27358. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27359. testVectors[i].derSz));
  27360. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27361. if (testVectors[i].derSz <= sizeof(long)) {
  27362. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27363. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27364. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27365. wolfSSL_ASN1_INTEGER_free(c);
  27366. c = NULL;
  27367. }
  27368. /* Convert to DER without a pre-allocated output buffer. */
  27369. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27370. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27371. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27372. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27373. * output buffer pointer should be incremented just past the end of the
  27374. * encoded data. */
  27375. p2 = reEncoded;
  27376. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27377. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27378. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27379. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27380. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27381. reEncoded = NULL;
  27382. wolfSSL_ASN1_INTEGER_free(a);
  27383. a = NULL;
  27384. }
  27385. #endif /* OPENSSL_EXTRA */
  27386. return EXPECT_RESULT();
  27387. }
  27388. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27389. {
  27390. EXPECT_DECLS;
  27391. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27392. BIO* bio = NULL;
  27393. BIO* out = NULL;
  27394. BIO* fixed = NULL;
  27395. ASN1_INTEGER* ai = NULL;
  27396. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27397. char tmp[1024];
  27398. int tmpSz;
  27399. const char expected1[] = "123456";
  27400. const char expected2[] = "112345678912345678901234567890";
  27401. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27402. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27403. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27404. ExpectNotNull(ai = ASN1_INTEGER_new());
  27405. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27406. /* Invalid parameter testing. */
  27407. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27408. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27409. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27410. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27411. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27412. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27413. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27414. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27415. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27416. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27417. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27418. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27419. /* No data to read from BIO. */
  27420. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27421. /* read first line */
  27422. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27423. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27424. XMEMSET(tmp, 0, 1024);
  27425. tmpSz = BIO_read(out, tmp, 1024);
  27426. ExpectIntEQ(tmpSz, 6);
  27427. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27428. /* fail on second line (not % 2) */
  27429. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27430. /* read 3rd long line */
  27431. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27432. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27433. XMEMSET(tmp, 0, 1024);
  27434. tmpSz = BIO_read(out, tmp, 1024);
  27435. ExpectIntEQ(tmpSz, 30);
  27436. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27437. /* fail on empty line */
  27438. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27439. BIO_free(bio);
  27440. bio = NULL;
  27441. /* Make long integer, requiring dynamic memory, even longer. */
  27442. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27443. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27444. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27445. XMEMSET(tmp, 0, 1024);
  27446. tmpSz = BIO_read(out, tmp, 1024);
  27447. ExpectIntEQ(tmpSz, 48);
  27448. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27449. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27450. XMEMSET(tmp, 0, 1024);
  27451. tmpSz = BIO_read(out, tmp, 1024);
  27452. ExpectIntEQ(tmpSz, 56);
  27453. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27454. BIO_free(bio);
  27455. BIO_free(out);
  27456. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27457. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27458. /* Ensure there is 0 bytes available to write into. */
  27459. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27460. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27461. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27462. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27463. BIO_free(fixed);
  27464. ASN1_INTEGER_free(ai);
  27465. #endif
  27466. return EXPECT_RESULT();
  27467. }
  27468. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27469. {
  27470. EXPECT_DECLS;
  27471. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27472. ASN1_INTEGER *a = NULL;
  27473. unsigned char *pp = NULL,*tpp = NULL;
  27474. int ret = 0;
  27475. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27476. /* Invalid parameter testing. */
  27477. /* Set pp to an invalid value. */
  27478. pp = NULL;
  27479. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27480. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27481. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27482. /* 40 */
  27483. if (a != NULL) {
  27484. a->intData[0] = ASN_INTEGER;
  27485. a->intData[1] = 1;
  27486. a->intData[2] = 40;
  27487. }
  27488. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27489. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27490. DYNAMIC_TYPE_TMP_BUFFER));
  27491. tpp = pp;
  27492. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27493. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27494. tpp--;
  27495. ExpectIntEQ(*tpp, 40);
  27496. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27497. pp = NULL;
  27498. /* 128 */
  27499. if (a != NULL) {
  27500. a->intData[0] = ASN_INTEGER;
  27501. a->intData[1] = 1;
  27502. a->intData[2] = 128;
  27503. }
  27504. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27505. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27506. DYNAMIC_TYPE_TMP_BUFFER));
  27507. tpp = pp;
  27508. if (tpp != NULL) {
  27509. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27510. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27511. tpp--;
  27512. ExpectIntEQ(*(tpp--), 128);
  27513. ExpectIntEQ(*tpp, 0);
  27514. }
  27515. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27516. pp = NULL;
  27517. /* -40 */
  27518. if (a != NULL) {
  27519. a->intData[0] = ASN_INTEGER;
  27520. a->intData[1] = 1;
  27521. a->intData[2] = 40;
  27522. a->negative = 1;
  27523. }
  27524. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27525. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27526. DYNAMIC_TYPE_TMP_BUFFER));
  27527. tpp = pp;
  27528. if (tpp != NULL) {
  27529. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27530. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27531. tpp--;
  27532. ExpectIntEQ(*tpp, 216);
  27533. }
  27534. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27535. pp = NULL;
  27536. /* -128 */
  27537. if (a != NULL) {
  27538. a->intData[0] = ASN_INTEGER;
  27539. a->intData[1] = 1;
  27540. a->intData[2] = 128;
  27541. a->negative = 1;
  27542. }
  27543. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27544. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27545. DYNAMIC_TYPE_TMP_BUFFER));
  27546. tpp = pp;
  27547. if (tpp != NULL) {
  27548. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27549. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27550. tpp--;
  27551. ExpectIntEQ(*tpp, 128);
  27552. }
  27553. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27554. pp = NULL;
  27555. /* -200 */
  27556. if (a != NULL) {
  27557. a->intData[0] = ASN_INTEGER;
  27558. a->intData[1] = 1;
  27559. a->intData[2] = 200;
  27560. a->negative = 1;
  27561. }
  27562. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27563. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27564. DYNAMIC_TYPE_TMP_BUFFER));
  27565. tpp = pp;
  27566. if (tpp != NULL) {
  27567. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27568. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27569. tpp--;
  27570. ExpectIntEQ(*(tpp--), 56);
  27571. ExpectIntEQ(*tpp, 255);
  27572. }
  27573. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27574. pp = NULL;
  27575. /* Empty */
  27576. if (a != NULL) {
  27577. a->intData[0] = ASN_INTEGER;
  27578. a->intData[1] = 0;
  27579. a->negative = 0;
  27580. }
  27581. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27582. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27583. DYNAMIC_TYPE_TMP_BUFFER));
  27584. tpp = pp;
  27585. if (tpp != NULL) {
  27586. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27587. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27588. tpp--;
  27589. ExpectIntEQ(*tpp, 0);
  27590. }
  27591. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27592. pp = NULL;
  27593. /* 0 */
  27594. if (a != NULL) {
  27595. a->intData[0] = ASN_INTEGER;
  27596. a->intData[1] = 1;
  27597. a->intData[2] = 0;
  27598. a->negative = 1;
  27599. }
  27600. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27601. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27602. DYNAMIC_TYPE_TMP_BUFFER));
  27603. if (tpp != NULL) {
  27604. tpp = pp;
  27605. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27606. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27607. tpp--;
  27608. ExpectIntEQ(*tpp, 0);
  27609. }
  27610. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27611. pp = NULL;
  27612. /* 0x100 */
  27613. if (a != NULL) {
  27614. a->intData[0] = ASN_INTEGER;
  27615. a->intData[1] = 2;
  27616. a->intData[2] = 0x01;
  27617. a->intData[3] = 0x00;
  27618. a->negative = 0;
  27619. }
  27620. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27621. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27622. DYNAMIC_TYPE_TMP_BUFFER));
  27623. if (tpp != NULL) {
  27624. tpp = pp;
  27625. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27626. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27627. tpp -= 2;
  27628. ExpectIntEQ(tpp[0], 0x01);
  27629. ExpectIntEQ(tpp[1], 0x00);
  27630. }
  27631. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27632. pp = NULL;
  27633. /* -0x8000 => 0x8000 */
  27634. if (a != NULL) {
  27635. a->intData[0] = ASN_INTEGER;
  27636. a->intData[1] = 2;
  27637. a->intData[2] = 0x80;
  27638. a->intData[3] = 0x00;
  27639. a->negative = 1;
  27640. }
  27641. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27642. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27643. DYNAMIC_TYPE_TMP_BUFFER));
  27644. tpp = pp;
  27645. if (tpp != NULL) {
  27646. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27647. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27648. tpp -= 2;
  27649. ExpectIntEQ(tpp[0], 0x80);
  27650. ExpectIntEQ(tpp[1], 0x00);
  27651. }
  27652. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27653. pp = NULL;
  27654. /* -0x8001 => 0xFF7FFF */
  27655. if (a != NULL) {
  27656. a->intData[0] = ASN_INTEGER;
  27657. a->intData[1] = 2;
  27658. a->intData[2] = 0x80;
  27659. a->intData[3] = 0x01;
  27660. a->negative = 1;
  27661. }
  27662. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27663. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27664. DYNAMIC_TYPE_TMP_BUFFER));
  27665. tpp = pp;
  27666. if (tpp != NULL) {
  27667. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27668. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27669. tpp -= 3;
  27670. ExpectIntEQ(tpp[0], 0xFF);
  27671. ExpectIntEQ(tpp[1], 0x7F);
  27672. ExpectIntEQ(tpp[2], 0xFF);
  27673. }
  27674. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27675. wolfSSL_ASN1_INTEGER_free(a);
  27676. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27677. return EXPECT_RESULT();
  27678. }
  27679. static int test_wolfSSL_ASN1_OBJECT(void)
  27680. {
  27681. EXPECT_DECLS;
  27682. #if defined(OPENSSL_EXTRA)
  27683. ASN1_OBJECT* a = NULL;
  27684. ASN1_OBJECT s;
  27685. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27686. /* Invalid parameter testing. */
  27687. ASN1_OBJECT_free(NULL);
  27688. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27689. /* Test that a static ASN1_OBJECT can be freed. */
  27690. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27691. ASN1_OBJECT_free(&s);
  27692. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27693. ASN1_OBJECT_free(a);
  27694. a = NULL;
  27695. s.obj = der;
  27696. s.objSz = sizeof(der);
  27697. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27698. ASN1_OBJECT_free(a);
  27699. ASN1_OBJECT_free(&s);
  27700. #endif /* OPENSSL_EXTRA */
  27701. return EXPECT_RESULT();
  27702. }
  27703. static int test_wolfSSL_ASN1_get_object(void)
  27704. {
  27705. EXPECT_DECLS;
  27706. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27707. const unsigned char* derBuf = cliecc_cert_der_256;
  27708. const unsigned char* nullPtr = NULL;
  27709. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27710. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27711. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27712. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27713. const unsigned char* p;
  27714. unsigned char objDer[8];
  27715. unsigned char* der;
  27716. unsigned char* derPtr;
  27717. int len = sizeof_cliecc_cert_der_256;
  27718. long asnLen = 0;
  27719. int tag = 0;
  27720. int cls = 0;
  27721. ASN1_OBJECT* a = NULL;
  27722. ASN1_OBJECT s;
  27723. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27724. /* Invalid encoding at length. */
  27725. p = objDerInvalidLen;
  27726. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27727. 0x80);
  27728. p = objDerBadLen;
  27729. /* Error = 0x80, Constructed = 0x20 */
  27730. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27731. 0x80 | 0x20);
  27732. /* Read a couple TLV triplets and make sure they match the expected values
  27733. */
  27734. /* SEQUENCE */
  27735. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27736. ExpectIntEQ(asnLen, 861);
  27737. ExpectIntEQ(tag, 0x10);
  27738. ExpectIntEQ(cls, 0);
  27739. /* SEQUENCE */
  27740. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27741. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27742. ExpectIntEQ(asnLen, 772);
  27743. ExpectIntEQ(tag, 0x10);
  27744. ExpectIntEQ(cls, 0);
  27745. /* [0] */
  27746. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27747. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27748. ExpectIntEQ(asnLen, 3);
  27749. ExpectIntEQ(tag, 0);
  27750. ExpectIntEQ(cls, 0x80);
  27751. /* INTEGER */
  27752. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27753. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27754. ExpectIntEQ(asnLen, 1);
  27755. ExpectIntEQ(tag, 0x2);
  27756. ExpectIntEQ(cls, 0);
  27757. derBuf += asnLen;
  27758. /* INTEGER */
  27759. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27760. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27761. ExpectIntEQ(asnLen, 20);
  27762. ExpectIntEQ(tag, 0x2);
  27763. ExpectIntEQ(cls, 0);
  27764. derBuf += asnLen;
  27765. /* SEQUENCE */
  27766. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27767. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27768. ExpectIntEQ(asnLen, 10);
  27769. ExpectIntEQ(tag, 0x10);
  27770. ExpectIntEQ(cls, 0);
  27771. /* Found OBJECT_ID. */
  27772. /* Invalid parameter testing. */
  27773. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27774. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27775. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27776. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27777. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27778. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27779. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27780. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27781. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27782. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27783. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27784. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27785. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27786. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27787. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27788. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27789. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27790. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27791. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27792. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27793. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27794. /* Invalid encoding at length. */
  27795. p = objDerInvalidLen;
  27796. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27797. p = objDerBadLen;
  27798. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27799. p = objDerNotObj;
  27800. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27801. p = objDerNoData;
  27802. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27803. /* Create an ASN OBJECT from content */
  27804. p = derBuf + 2;
  27805. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27806. ASN1_OBJECT_free(a);
  27807. a = NULL;
  27808. /* Create an ASN OBJECT from DER */
  27809. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27810. /* Invalid parameter testing. */
  27811. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27812. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27813. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27814. der = NULL;
  27815. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27816. derPtr = objDer;
  27817. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27818. ExpectPtrNE(derPtr, objDer);
  27819. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27820. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27821. ASN1_OBJECT_free(a);
  27822. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27823. return EXPECT_RESULT();
  27824. }
  27825. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27826. {
  27827. EXPECT_DECLS;
  27828. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27829. ASN1_OBJECT* obj = NULL;
  27830. ASN1_OBJECT* a = NULL;
  27831. BIO *bio = NULL;
  27832. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27833. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27834. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27835. const unsigned char* p;
  27836. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27837. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27838. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27839. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27840. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27841. /* No DER encoding in ASN1_OBJECT. */
  27842. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27843. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27844. ASN1_OBJECT_free(a);
  27845. a = NULL;
  27846. /* DER encoding - not OBJECT_ID */
  27847. p = notObjDer;
  27848. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27849. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27850. ASN1_OBJECT_free(a);
  27851. a = NULL;
  27852. /* Bad length encoding. */
  27853. p = badLenDer;
  27854. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27855. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27856. ASN1_OBJECT_free(a);
  27857. a = NULL;
  27858. /* Good encoding - but unknown. */
  27859. p = goodDer;
  27860. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27861. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27862. ASN1_OBJECT_free(a);
  27863. BIO_free(bio);
  27864. ASN1_OBJECT_free(obj);
  27865. #endif
  27866. return EXPECT_RESULT();
  27867. }
  27868. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  27869. {
  27870. EXPECT_DECLS;
  27871. #if defined(OPENSSL_EXTRA) && \
  27872. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  27873. char buf[50] = {0};
  27874. ASN1_OBJECT* obj;
  27875. const char* oid = "2.5.29.19";
  27876. const char* ln = "X509v3 Basic Constraints";
  27877. obj = NULL;
  27878. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  27879. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  27880. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  27881. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  27882. XMEMSET(buf, 0, sizeof(buf));
  27883. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  27884. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  27885. ASN1_OBJECT_free(obj);
  27886. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  27887. return EXPECT_RESULT();
  27888. }
  27889. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  27890. {
  27891. EXPECT_DECLS;
  27892. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  27893. WOLFSSL_STACK* sk = NULL;
  27894. WOLFSSL_ASN1_OBJECT* obj;
  27895. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27896. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27897. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27898. sk = NULL;
  27899. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27900. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27901. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27902. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27903. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27904. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  27905. sk = NULL;
  27906. /* obj freed in pop_free call. */
  27907. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27908. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27909. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27910. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  27911. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27912. wolfSSL_ASN1_OBJECT_free(obj);
  27913. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  27914. return EXPECT_RESULT();
  27915. }
  27916. static int test_wolfSSL_ASN1_STRING(void)
  27917. {
  27918. EXPECT_DECLS;
  27919. #if defined(OPENSSL_EXTRA)
  27920. ASN1_STRING* str = NULL;
  27921. ASN1_STRING* c = NULL;
  27922. const char data[] = "hello wolfSSL";
  27923. const char data2[] = "Same len data";
  27924. const char longData[] =
  27925. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27926. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27927. ASN1_STRING_free(str);
  27928. str = NULL;
  27929. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27930. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  27931. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  27932. /* Check setting to NULL works. */
  27933. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  27934. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  27935. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27936. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  27937. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  27938. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  27939. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  27940. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  27941. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  27942. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  27943. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  27944. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  27945. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  27946. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27947. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  27948. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  27949. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  27950. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  27951. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27952. /* Check setting back to smaller size frees dynamic data. */
  27953. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27954. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  27955. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  27956. ExpectNull(ASN1_STRING_get0_data(NULL));
  27957. ExpectNotNull(ASN1_STRING_get0_data(str));
  27958. ExpectNull(ASN1_STRING_data(NULL));
  27959. ExpectNotNull(ASN1_STRING_data(str));
  27960. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  27961. ExpectIntGT(ASN1_STRING_length(str), 0);
  27962. ASN1_STRING_free(c);
  27963. ASN1_STRING_free(str);
  27964. ASN1_STRING_free(NULL);
  27965. #ifndef NO_WOLFSSL_STUB
  27966. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  27967. #endif
  27968. #endif
  27969. return EXPECT_RESULT();
  27970. }
  27971. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  27972. {
  27973. EXPECT_DECLS;
  27974. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  27975. !defined(NO_FILESYSTEM)
  27976. WOLFSSL_X509* x509 = NULL;
  27977. WOLFSSL_X509_NAME* subject = NULL;
  27978. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  27979. WOLFSSL_ASN1_STRING* a = NULL;
  27980. FILE* file = XBADFILE;
  27981. int idx = 0;
  27982. char targetOutput[16] = "www.wolfssl.com";
  27983. unsigned char* actual_output = NULL;
  27984. int len = 0;
  27985. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27986. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27987. if (file != NULL)
  27988. fclose(file);
  27989. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  27990. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  27991. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  27992. NID_commonName, -1)), 5);
  27993. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  27994. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  27995. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  27996. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  27997. a = NULL;
  27998. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  27999. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28000. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28001. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28002. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28003. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28004. wolfSSL_X509_free(x509);
  28005. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28006. ExpectNotNull(a = ASN1_STRING_new());
  28007. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28008. ASN1_STRING_free(a);
  28009. #endif
  28010. return EXPECT_RESULT();
  28011. }
  28012. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28013. {
  28014. EXPECT_DECLS;
  28015. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28016. WOLFSSL_ASN1_STRING* str = NULL;
  28017. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28018. char* ret = NULL;
  28019. ExpectNotNull(str = ASN1_STRING_new());
  28020. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28021. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28022. ret = NULL;
  28023. /* No data. */
  28024. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28025. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28026. ret = NULL;
  28027. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28028. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28029. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28030. ret = NULL;
  28031. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28032. /* No type. */
  28033. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28034. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28035. ASN1_STRING_free(str);
  28036. #endif
  28037. return EXPECT_RESULT();
  28038. }
  28039. static int test_wolfSSL_ASN1_STRING_canon(void)
  28040. {
  28041. EXPECT_DECLS;
  28042. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28043. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28044. defined(OPENSSL_EXTRA_X509_SMALL))
  28045. WOLFSSL_ASN1_STRING* orig = NULL;
  28046. WOLFSSL_ASN1_STRING* canon = NULL;
  28047. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28048. const char* whitespaceOnly = "\t\r\n";
  28049. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28050. const char* canonData = "\x01 \x02 \xff tt";
  28051. const char longData[] =
  28052. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28053. ExpectNotNull(orig = ASN1_STRING_new());
  28054. ExpectNotNull(canon = ASN1_STRING_new());
  28055. /* Invalid parameter testing. */
  28056. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28057. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28058. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28059. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28060. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28061. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28062. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28063. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28064. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28065. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28066. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28067. ASN1_STRING_free(orig);
  28068. orig = NULL;
  28069. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28070. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28071. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28072. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28073. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28074. ASN1_STRING_free(orig);
  28075. orig = NULL;
  28076. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28077. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28078. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28079. ASN1_STRING_free(orig);
  28080. orig = NULL;
  28081. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28082. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28083. ASN1_STRING_free(orig);
  28084. ASN1_STRING_free(canon);
  28085. #endif
  28086. #endif
  28087. return EXPECT_RESULT();
  28088. }
  28089. static int test_wolfSSL_ASN1_STRING_print(void)
  28090. {
  28091. EXPECT_DECLS;
  28092. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28093. !defined(NO_BIO)
  28094. ASN1_STRING* asnStr = NULL;
  28095. const char HELLO_DATA[]= \
  28096. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28097. #define MAX_UNPRINTABLE_CHAR 32
  28098. #define MAX_BUF 255
  28099. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28100. unsigned char expected[sizeof(unprintableData)+1];
  28101. unsigned char rbuf[MAX_BUF];
  28102. BIO *bio = NULL;
  28103. int p_len;
  28104. int i;
  28105. /* setup */
  28106. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28107. unprintableData[i] = HELLO_DATA[i];
  28108. expected[i] = HELLO_DATA[i];
  28109. }
  28110. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28111. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28112. if (i == (int)'\n' || i == (int)'\r')
  28113. expected[sizeof(HELLO_DATA)+i] = i;
  28114. else
  28115. expected[sizeof(HELLO_DATA)+i] = '.';
  28116. }
  28117. unprintableData[sizeof(unprintableData)-1] = '\0';
  28118. expected[sizeof(expected)-1] = '\0';
  28119. XMEMSET(rbuf, 0, MAX_BUF);
  28120. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28121. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28122. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28123. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28124. (int)sizeof(unprintableData)), 1);
  28125. /* test */
  28126. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28127. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28128. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28129. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28130. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28131. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28132. BIO_free(bio);
  28133. bio = NULL;
  28134. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28135. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28136. /* Ensure there is 0 bytes available to write into. */
  28137. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28138. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28139. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28140. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28141. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28142. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28143. BIO_free(bio);
  28144. ASN1_STRING_free(asnStr);
  28145. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28146. return EXPECT_RESULT();
  28147. }
  28148. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28149. {
  28150. EXPECT_DECLS;
  28151. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28152. ASN1_STRING* asn_str = NULL;
  28153. const char data[] = "Hello wolfSSL!";
  28154. ASN1_STRING* esc_str = NULL;
  28155. const char esc_data[] = "a+;<>";
  28156. ASN1_STRING* neg_int = NULL;
  28157. const char neg_int_data[] = "\xff";
  28158. ASN1_STRING* neg_enum = NULL;
  28159. const char neg_enum_data[] = "\xff";
  28160. BIO *bio = NULL;
  28161. BIO *fixed = NULL;
  28162. unsigned long flags;
  28163. int p_len;
  28164. unsigned char rbuf[255];
  28165. /* setup */
  28166. XMEMSET(rbuf, 0, 255);
  28167. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28168. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28169. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28170. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28171. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28172. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28173. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28174. sizeof(esc_data)), 1);
  28175. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28176. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28177. sizeof(neg_int_data) - 1), 1);
  28178. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28179. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28180. sizeof(neg_enum_data) - 1), 1);
  28181. /* Invalid parameter testing. */
  28182. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28183. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28184. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28185. /* no flags */
  28186. XMEMSET(rbuf, 0, 255);
  28187. flags = 0;
  28188. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28189. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28190. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28191. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28192. /* Ensure there is 0 bytes available to write into. */
  28193. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28194. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28195. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28196. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28197. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28198. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28199. /* RFC2253 Escape */
  28200. XMEMSET(rbuf, 0, 255);
  28201. flags = ASN1_STRFLGS_ESC_2253;
  28202. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28203. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28204. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28205. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28206. /* Ensure there is 0 bytes available to write into. */
  28207. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28208. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28209. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28210. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28211. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28212. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28213. /* Show type */
  28214. XMEMSET(rbuf, 0, 255);
  28215. flags = ASN1_STRFLGS_SHOW_TYPE;
  28216. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28217. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28218. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28219. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28220. /* Ensure there is 0 bytes available to write into. */
  28221. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28222. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28223. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28224. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28225. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28226. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28227. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28228. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28229. /* Dump All */
  28230. XMEMSET(rbuf, 0, 255);
  28231. flags = ASN1_STRFLGS_DUMP_ALL;
  28232. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28233. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28234. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28235. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28236. /* Ensure there is 0 bytes available to write into. */
  28237. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28238. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28239. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28240. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28241. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28242. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28243. /* Dump Der */
  28244. XMEMSET(rbuf, 0, 255);
  28245. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28246. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28247. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28248. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28249. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28250. /* Ensure there is 0 bytes available to write into. */
  28251. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28252. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28253. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28254. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28255. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28256. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28257. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28258. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28259. /* Dump All + Show type */
  28260. XMEMSET(rbuf, 0, 255);
  28261. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28262. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28263. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28264. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28265. /* Dump All + Show type - Negative Integer. */
  28266. XMEMSET(rbuf, 0, 255);
  28267. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28268. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28269. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28270. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28271. /* Dump All + Show type - Negative Enumerated. */
  28272. XMEMSET(rbuf, 0, 255);
  28273. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28274. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28275. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28276. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28277. BIO_free(fixed);
  28278. BIO_free(bio);
  28279. ASN1_STRING_free(asn_str);
  28280. ASN1_STRING_free(esc_str);
  28281. ASN1_STRING_free(neg_int);
  28282. ASN1_STRING_free(neg_enum);
  28283. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28284. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28285. #endif
  28286. return EXPECT_RESULT();
  28287. }
  28288. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28289. {
  28290. EXPECT_DECLS;
  28291. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28292. ASN1_STRING* asn1str_test = NULL;
  28293. ASN1_STRING* asn1str_answer = NULL;
  28294. /* Each character is encoded using 4 bytes */
  28295. char input[] = {
  28296. 0, 0, 0, 'T',
  28297. 0, 0, 0, 'e',
  28298. 0, 0, 0, 's',
  28299. 0, 0, 0, 't',
  28300. };
  28301. char output[] = "Test";
  28302. char badInput[] = {
  28303. 1, 0, 0, 'T',
  28304. 0, 1, 0, 'e',
  28305. 0, 0, 1, 's',
  28306. };
  28307. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28308. /* Test wrong type. */
  28309. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28310. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28311. ASN1_STRING_free(asn1str_test);
  28312. asn1str_test = NULL;
  28313. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28314. /* Test bad length. */
  28315. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28316. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28317. /* Test bad input. */
  28318. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28319. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28320. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28321. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28322. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28323. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28324. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28325. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28326. ExpectNotNull(
  28327. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28328. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28329. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28330. ASN1_STRING_free(asn1str_test);
  28331. ASN1_STRING_free(asn1str_answer);
  28332. #endif /* OPENSSL_ALL && !NO_ASN */
  28333. return EXPECT_RESULT();
  28334. }
  28335. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28336. {
  28337. EXPECT_DECLS;
  28338. #if defined(OPENSSL_EXTRA)
  28339. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28340. unsigned char nullstr[32];
  28341. XMEMSET(nullstr, 0, 32);
  28342. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  28343. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  28344. if (asn1_gtime != NULL) {
  28345. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28346. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28347. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28348. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28349. }
  28350. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28351. #endif /* OPENSSL_EXTRA */
  28352. return EXPECT_RESULT();
  28353. }
  28354. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28355. {
  28356. EXPECT_DECLS;
  28357. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28358. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28359. BIO* bio = NULL;
  28360. unsigned char buf[24];
  28361. int i;
  28362. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28363. BIO_set_write_buf_size(bio, 24);
  28364. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28365. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28366. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28367. /* Type not set. */
  28368. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28369. gtime.type = V_ASN1_GENERALIZEDTIME;
  28370. /* Invalid parameters testing. */
  28371. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28372. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28373. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28374. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28375. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28376. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28377. BIO_free(bio);
  28378. bio = NULL;
  28379. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28380. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28381. /* Ensure there is 0 bytes available to write into. */
  28382. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28383. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28384. for (i = 1; i < 20; i++) {
  28385. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28386. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28387. }
  28388. BIO_free(bio);
  28389. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28390. #endif /* OPENSSL_EXTRA */
  28391. return EXPECT_RESULT();
  28392. }
  28393. static int test_wolfSSL_ASN1_TIME(void)
  28394. {
  28395. EXPECT_DECLS;
  28396. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28397. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28398. unsigned char *data;
  28399. ExpectNotNull(asn_time = ASN1_TIME_new());
  28400. #ifndef NO_WOLFSSL_STUB
  28401. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28402. #endif
  28403. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28404. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28405. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28406. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28407. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28408. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28409. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28410. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28411. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28412. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28413. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28414. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28415. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28416. ASN1_TIME_free(asn_time);
  28417. ASN1_TIME_free(NULL);
  28418. #endif
  28419. return EXPECT_RESULT();
  28420. }
  28421. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28422. {
  28423. EXPECT_DECLS;
  28424. #ifndef NO_ASN_TIME
  28425. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28426. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28427. WOLFSSL_ASN1_TIME* t = NULL;
  28428. char buf[ASN_GENERALIZED_TIME_SIZE];
  28429. ExpectNotNull((t = ASN1_TIME_new()));
  28430. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28431. /* Invalid parameter testing. */
  28432. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28433. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28434. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28435. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28436. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28437. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28438. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28439. /* Buffer needs to be longer than minimum of 5 characters. */
  28440. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28441. ASN1_TIME_free(t);
  28442. #endif
  28443. #endif /* NO_ASN_TIME */
  28444. return EXPECT_RESULT();
  28445. }
  28446. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28447. {
  28448. EXPECT_DECLS;
  28449. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28450. ASN1_TIME* fromTime = NULL;
  28451. ASN1_TIME* closeToTime = NULL;
  28452. ASN1_TIME* toTime = NULL;
  28453. ASN1_TIME* invalidTime = NULL;
  28454. int daysDiff = 0;
  28455. int secsDiff = 0;
  28456. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28457. /* Feb 22, 2003, 21:15:15 */
  28458. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28459. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28460. /* Feb 22, 2003, 21:16:15 */
  28461. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28462. ExpectNotNull((toTime = ASN1_TIME_new()));
  28463. /* Dec 19, 2010, 18:10:11 */
  28464. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28465. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28466. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28467. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28468. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28469. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28470. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28471. /* Error conditions. */
  28472. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28473. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28474. /* If both times are NULL, difference is 0. */
  28475. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28476. ExpectIntEQ(daysDiff, 0);
  28477. ExpectIntEQ(secsDiff, 0);
  28478. /* If one time is NULL, it defaults to the current time. */
  28479. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28480. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28481. /* Normal operation. Both times non-NULL. */
  28482. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28483. ExpectIntEQ(daysDiff, 2856);
  28484. ExpectIntEQ(secsDiff, 75296);
  28485. /* Swapping the times should return negative values. */
  28486. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28487. ExpectIntEQ(daysDiff, -2856);
  28488. ExpectIntEQ(secsDiff, -75296);
  28489. /* Compare with invalid time string. */
  28490. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28491. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28492. /* Compare with days difference of 0. */
  28493. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28494. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28495. /* Days and seconds differences not 0. */
  28496. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28497. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28498. /* Same time. */
  28499. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28500. /* Compare regression test: No seconds difference, just difference in days.
  28501. */
  28502. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28503. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28504. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28505. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28506. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28507. /* Edge case with Unix epoch. */
  28508. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28509. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28510. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28511. ExpectIntEQ(daysDiff, 3652);
  28512. ExpectIntEQ(secsDiff, 0);
  28513. /* Edge case with year > 2038 (year 2038 problem). */
  28514. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28515. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28516. ExpectIntEQ(daysDiff, 2932896);
  28517. ExpectIntEQ(secsDiff, 86399);
  28518. ASN1_TIME_free(fromTime);
  28519. ASN1_TIME_free(closeToTime);
  28520. ASN1_TIME_free(toTime);
  28521. ASN1_TIME_free(invalidTime);
  28522. #endif
  28523. return EXPECT_RESULT();
  28524. }
  28525. static int test_wolfSSL_ASN1_TIME_adj(void)
  28526. {
  28527. EXPECT_DECLS;
  28528. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28529. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28530. const int year = 365*24*60*60;
  28531. const int day = 24*60*60;
  28532. const int hour = 60*60;
  28533. const int mini = 60;
  28534. const byte asn_utc_time = ASN_UTC_TIME;
  28535. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28536. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28537. #endif
  28538. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28539. WOLFSSL_ASN1_TIME* s = NULL;
  28540. int offset_day;
  28541. long offset_sec;
  28542. char date_str[CTC_DATE_SIZE + 1];
  28543. time_t t;
  28544. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28545. /* UTC notation test */
  28546. /* 2000/2/15 20:30:00 */
  28547. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28548. offset_day = 7;
  28549. offset_sec = 45 * mini;
  28550. /* offset_sec = -45 * min;*/
  28551. ExpectNotNull(asn_time =
  28552. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28553. ExpectTrue(asn_time->type == asn_utc_time);
  28554. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28555. CTC_DATE_SIZE));
  28556. date_str[CTC_DATE_SIZE] = '\0';
  28557. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28558. /* negative offset */
  28559. offset_sec = -45 * mini;
  28560. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28561. ExpectNotNull(asn_time);
  28562. ExpectTrue(asn_time->type == asn_utc_time);
  28563. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28564. CTC_DATE_SIZE));
  28565. date_str[CTC_DATE_SIZE] = '\0';
  28566. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28567. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28568. s = NULL;
  28569. XMEMSET(date_str, 0, sizeof(date_str));
  28570. /* Generalized time will overflow time_t if not long */
  28571. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28572. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28573. DYNAMIC_TYPE_OPENSSL);
  28574. /* GeneralizedTime notation test */
  28575. /* 2055/03/01 09:00:00 */
  28576. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28577. offset_day = 12;
  28578. offset_sec = 10 * mini;
  28579. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28580. offset_sec));
  28581. ExpectTrue(asn_time->type == asn_gen_time);
  28582. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28583. CTC_DATE_SIZE));
  28584. date_str[CTC_DATE_SIZE] = '\0';
  28585. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28586. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28587. s = NULL;
  28588. XMEMSET(date_str, 0, sizeof(date_str));
  28589. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28590. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28591. s = NULL;
  28592. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28593. offset_day = 7;
  28594. offset_sec = 45 * mini;
  28595. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28596. offset_sec));
  28597. ExpectTrue(asn_time->type == asn_utc_time);
  28598. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28599. CTC_DATE_SIZE));
  28600. date_str[CTC_DATE_SIZE] = '\0';
  28601. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28602. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28603. asn_time = NULL;
  28604. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28605. offset_sec));
  28606. ExpectTrue(asn_time->type == asn_utc_time);
  28607. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28608. CTC_DATE_SIZE));
  28609. date_str[CTC_DATE_SIZE] = '\0';
  28610. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28611. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28612. #endif
  28613. return EXPECT_RESULT();
  28614. }
  28615. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28616. {
  28617. EXPECT_DECLS;
  28618. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28619. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28620. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28621. ASN1_TIME asnTime;
  28622. struct tm tm;
  28623. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28624. XMEMSET(&tm, 0, sizeof(struct tm));
  28625. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28626. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28627. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28628. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28629. ExpectIntEQ(tm.tm_sec, 15);
  28630. ExpectIntEQ(tm.tm_min, 15);
  28631. ExpectIntEQ(tm.tm_hour, 21);
  28632. ExpectIntEQ(tm.tm_mday, 22);
  28633. ExpectIntEQ(tm.tm_mon, 1);
  28634. ExpectIntEQ(tm.tm_year, 100);
  28635. ExpectIntEQ(tm.tm_isdst, 0);
  28636. #ifdef XMKTIME
  28637. ExpectIntEQ(tm.tm_wday, 2);
  28638. ExpectIntEQ(tm.tm_yday, 52);
  28639. #endif
  28640. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28641. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28642. ExpectIntEQ(tm.tm_year, 50);
  28643. /* Get current time. */
  28644. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28645. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28646. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28647. /* 0 length. */
  28648. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28649. /* No type. */
  28650. asnTime.length = 1;
  28651. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28652. /* Not UTCTIME length. */
  28653. asnTime.type = V_ASN1_UTCTIME;
  28654. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28655. /* Not GENERALIZEDTIME length. */
  28656. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28657. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28658. /* Not Zulu timezone. */
  28659. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28660. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28661. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28662. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28663. #ifdef XMKTIME
  28664. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28665. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28666. ExpectIntEQ(tm.tm_sec, 47);
  28667. ExpectIntEQ(tm.tm_min, 22);
  28668. ExpectIntEQ(tm.tm_hour, 21);
  28669. ExpectIntEQ(tm.tm_mday, 12);
  28670. ExpectIntEQ(tm.tm_mon, 4);
  28671. ExpectIntEQ(tm.tm_year, 123);
  28672. ExpectIntEQ(tm.tm_wday, 5);
  28673. ExpectIntEQ(tm.tm_yday, 131);
  28674. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28675. fields are zeroed out as expected. */
  28676. ExpectIntEQ(tm.tm_isdst, 0);
  28677. #endif
  28678. #endif
  28679. return EXPECT_RESULT();
  28680. }
  28681. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28682. {
  28683. EXPECT_DECLS;
  28684. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28685. WOLFSSL_ASN1_TIME *t = NULL;
  28686. WOLFSSL_ASN1_TIME *out = NULL;
  28687. WOLFSSL_ASN1_TIME *gtime = NULL;
  28688. int tlen = 0;
  28689. unsigned char *data = NULL;
  28690. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28691. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28692. /* type not set. */
  28693. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28694. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28695. t = NULL;
  28696. /* UTC Time test */
  28697. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28698. if (t != NULL) {
  28699. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28700. t->type = ASN_UTC_TIME;
  28701. t->length = ASN_UTC_TIME_SIZE;
  28702. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28703. }
  28704. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28705. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28706. out = NULL;
  28707. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28708. wolfSSL_ASN1_TIME_free(gtime);
  28709. gtime = NULL;
  28710. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28711. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28712. ExpectPtrEq(gtime, out);
  28713. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28714. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28715. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28716. /* Generalized Time test */
  28717. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28718. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28719. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28720. if (t != NULL) {
  28721. t->type = ASN_GENERALIZED_TIME;
  28722. t->length = ASN_GENERALIZED_TIME_SIZE;
  28723. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28724. }
  28725. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28726. ASN_GENERALIZED_TIME_SIZE);
  28727. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28728. "20050727123456Z");
  28729. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28730. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28731. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28732. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28733. /* UTC Time to Generalized Time 1900's test */
  28734. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28735. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28736. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28737. if (t != NULL) {
  28738. t->type = ASN_UTC_TIME;
  28739. t->length = ASN_UTC_TIME_SIZE;
  28740. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28741. }
  28742. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28743. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28744. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28745. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28746. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28747. /* Null parameter test */
  28748. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28749. gtime = NULL;
  28750. out = NULL;
  28751. if (t != NULL) {
  28752. t->type = ASN_UTC_TIME;
  28753. t->length = ASN_UTC_TIME_SIZE;
  28754. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28755. }
  28756. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28757. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28758. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28759. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28760. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28761. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28762. #endif
  28763. return EXPECT_RESULT();
  28764. }
  28765. static int test_wolfSSL_ASN1_TIME_print(void)
  28766. {
  28767. EXPECT_DECLS;
  28768. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28769. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28770. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28771. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28772. !defined(NO_ASN_TIME)
  28773. BIO* bio = NULL;
  28774. BIO* fixed = NULL;
  28775. X509* x509 = NULL;
  28776. const unsigned char* der = client_cert_der_2048;
  28777. ASN1_TIME* notAfter = NULL;
  28778. ASN1_TIME* notBefore = NULL;
  28779. unsigned char buf[25];
  28780. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28781. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28782. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28783. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28784. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28785. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28786. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28787. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28788. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28789. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28790. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28791. /* Test BIO_write fails. */
  28792. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28793. /* Ensure there is 0 bytes available to write into. */
  28794. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28795. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28796. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28797. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28798. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28799. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28800. /* create a bad time and test results */
  28801. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28802. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28803. if (EXPECT_SUCCESS()) {
  28804. notAfter->data[8] = 0;
  28805. notAfter->data[3] = 0;
  28806. }
  28807. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28808. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28809. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28810. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28811. BIO_free(bio);
  28812. BIO_free(fixed);
  28813. X509_free(x509);
  28814. #endif
  28815. return EXPECT_RESULT();
  28816. }
  28817. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28818. {
  28819. EXPECT_DECLS;
  28820. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28821. BIO* bio = NULL;
  28822. ASN1_UTCTIME* utc = NULL;
  28823. unsigned char buf[25];
  28824. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28825. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28826. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28827. /* Valid date */
  28828. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28829. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28830. DYNAMIC_TYPE_ASN1));
  28831. if (utc != NULL) {
  28832. utc->type = ASN_UTC_TIME;
  28833. utc->length = ASN_UTC_TIME_SIZE;
  28834. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28835. }
  28836. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28837. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28838. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28839. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28840. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28841. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28842. XMEMSET(buf, 0, sizeof(buf));
  28843. BIO_free(bio);
  28844. bio = NULL;
  28845. /* Invalid format */
  28846. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28847. if (utc != NULL) {
  28848. utc->type = ASN_UTC_TIME;
  28849. utc->length = ASN_UTC_TIME_SIZE;
  28850. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28851. }
  28852. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28853. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28854. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28855. /* Invalid type */
  28856. if (utc != NULL) {
  28857. utc->type = ASN_GENERALIZED_TIME;
  28858. utc->length = ASN_GENERALIZED_TIME_SIZE;
  28859. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  28860. }
  28861. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28862. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  28863. BIO_free(bio);
  28864. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  28865. return EXPECT_RESULT();
  28866. }
  28867. static int test_wolfSSL_ASN1_TYPE(void)
  28868. {
  28869. EXPECT_DECLS;
  28870. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  28871. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  28872. WOLFSSL_ASN1_TYPE* t = NULL;
  28873. WOLFSSL_ASN1_OBJECT* obj = NULL;
  28874. #ifndef NO_ASN_TIME
  28875. WOLFSSL_ASN1_TIME* time = NULL;
  28876. #endif
  28877. WOLFSSL_ASN1_STRING* str = NULL;
  28878. unsigned char data[] = { 0x00 };
  28879. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  28880. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28881. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  28882. wolfSSL_ASN1_TYPE_free(t);
  28883. t = NULL;
  28884. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28885. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  28886. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  28887. wolfSSL_ASN1_TYPE_free(t);
  28888. t = NULL;
  28889. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28890. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28891. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  28892. wolfSSL_ASN1_TYPE_free(t);
  28893. t = NULL;
  28894. #ifndef NO_ASN_TIME
  28895. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28896. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28897. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28898. wolfSSL_ASN1_TYPE_free(t);
  28899. t = NULL;
  28900. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28901. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28902. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28903. wolfSSL_ASN1_TYPE_free(t);
  28904. t = NULL;
  28905. #endif
  28906. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28907. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28908. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  28909. wolfSSL_ASN1_TYPE_free(t);
  28910. t = NULL;
  28911. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28912. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28913. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  28914. wolfSSL_ASN1_TYPE_free(t);
  28915. t = NULL;
  28916. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28917. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28918. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  28919. wolfSSL_ASN1_TYPE_free(t);
  28920. t = NULL;
  28921. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28922. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28923. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  28924. wolfSSL_ASN1_TYPE_free(t);
  28925. t = NULL;
  28926. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28927. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28928. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  28929. wolfSSL_ASN1_TYPE_free(t);
  28930. t = NULL;
  28931. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28932. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28933. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  28934. wolfSSL_ASN1_TYPE_free(t);
  28935. t = NULL;
  28936. #endif
  28937. return EXPECT_RESULT();
  28938. }
  28939. /* Testing code used in dpp.c in hostap */
  28940. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28941. typedef struct {
  28942. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  28943. * as an OID identifying the curve */
  28944. X509_ALGOR *alg;
  28945. /* Compressed format public key per ANSI X9.63 */
  28946. ASN1_BIT_STRING *pub_key;
  28947. } DPP_BOOTSTRAPPING_KEY;
  28948. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  28949. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  28950. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  28951. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  28952. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  28953. typedef struct {
  28954. ASN1_INTEGER *integer;
  28955. } TEST_ASN1;
  28956. ASN1_SEQUENCE(TEST_ASN1) = {
  28957. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  28958. } ASN1_SEQUENCE_END(TEST_ASN1)
  28959. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  28960. typedef struct {
  28961. ASN1_OCTET_STRING *octet_string;
  28962. } TEST_FAIL_ASN1;
  28963. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  28964. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  28965. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  28966. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  28967. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  28968. #endif
  28969. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  28970. {
  28971. EXPECT_DECLS;
  28972. /* Testing code used in dpp.c in hostap */
  28973. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28974. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28975. EC_KEY *eckey = NULL;
  28976. EVP_PKEY *key = NULL;
  28977. size_t len = 0;
  28978. unsigned char *der = NULL;
  28979. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  28980. const unsigned char *in = ecc_clikey_der_256;
  28981. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  28982. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  28983. const EC_GROUP *group = NULL;
  28984. const EC_POINT *point = NULL;
  28985. int nid;
  28986. TEST_ASN1 *test_asn1 = NULL;
  28987. TEST_FAIL_ASN1 test_fail_asn1;
  28988. const unsigned char badObjDer[] = { 0x06, 0x00 };
  28989. const unsigned char goodObjDer[] = {
  28990. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  28991. };
  28992. WOLFSSL_ASN1_ITEM emptyTemplate;
  28993. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  28994. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28995. der = NULL;
  28996. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  28997. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  28998. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28999. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29000. (long)sizeof_ecc_clikey_der_256));
  29001. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29002. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29003. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29004. nid = EC_GROUP_get_curve_name(group);
  29005. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29006. group_obj = OBJ_nid2obj(nid);
  29007. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29008. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29009. group_obj), 1);
  29010. if (EXPECT_SUCCESS()) {
  29011. ec_obj = NULL;
  29012. group_obj = NULL;
  29013. }
  29014. }
  29015. wolfSSL_ASN1_OBJECT_free(group_obj);
  29016. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29017. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29018. #ifdef HAVE_COMP_KEY
  29019. ExpectIntGT((len = EC_POINT_point2oct(
  29020. group, point, POINT_CONVERSION_COMPRESSED,
  29021. NULL, 0, NULL)), 0);
  29022. #else
  29023. ExpectIntGT((len = EC_POINT_point2oct(
  29024. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29025. NULL, 0, NULL)), 0);
  29026. #endif
  29027. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29028. #ifdef HAVE_COMP_KEY
  29029. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29030. der, len-1, NULL), 0);
  29031. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29032. der, len, NULL), len);
  29033. #else
  29034. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29035. der, len-1, NULL), 0);
  29036. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29037. der, len, NULL), len);
  29038. #endif
  29039. if (EXPECT_SUCCESS()) {
  29040. bootstrap->pub_key->data = der;
  29041. bootstrap->pub_key->length = (int)len;
  29042. /* Not actually used */
  29043. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29044. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29045. }
  29046. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29047. der = NULL;
  29048. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29049. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29050. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29051. EVP_PKEY_free(key);
  29052. EC_KEY_free(eckey);
  29053. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29054. bootstrap = NULL;
  29055. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29056. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29057. * a NULL and an empty BIT_STRING. */
  29058. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29059. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29060. if (EXPECT_SUCCESS()) {
  29061. bootstrap->alg->algorithm->obj = badObjDer;
  29062. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29063. }
  29064. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29065. if (EXPECT_SUCCESS()) {
  29066. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29067. bootstrap->alg->parameter->value.ptr = NULL;
  29068. bootstrap->pub_key->data = NULL;
  29069. bootstrap->pub_key->length = 0;
  29070. /* Not actually used */
  29071. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29072. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29073. }
  29074. /* Encode with bad OBJECT_ID. */
  29075. der = NULL;
  29076. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29077. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29078. if (EXPECT_SUCCESS()) {
  29079. bootstrap->alg->algorithm->obj = goodObjDer;
  29080. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29081. bootstrap->alg->algorithm->grp = 2;
  29082. }
  29083. der = NULL;
  29084. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29085. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29086. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29087. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29088. /* Test integer */
  29089. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29090. der = NULL;
  29091. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29092. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29093. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29094. TEST_ASN1_free(test_asn1);
  29095. /* Test integer cases. */
  29096. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29097. TEST_ASN1_free(NULL);
  29098. /* Test error cases. */
  29099. ExpectNull(TEST_FAIL_ASN1_new());
  29100. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29101. TEST_FAIL_ASN1_free(NULL);
  29102. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29103. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29104. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29105. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29106. return EXPECT_RESULT();
  29107. }
  29108. static int test_wolfSSL_lhash(void)
  29109. {
  29110. EXPECT_DECLS;
  29111. #ifdef OPENSSL_ALL
  29112. const char testStr[] = "Like a true nature's child\n"
  29113. "We were born\n"
  29114. "Born to be wild";
  29115. #ifdef NO_SHA
  29116. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29117. #else
  29118. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29119. #endif
  29120. #endif
  29121. return EXPECT_RESULT();
  29122. }
  29123. static int test_wolfSSL_X509_NAME(void)
  29124. {
  29125. EXPECT_DECLS;
  29126. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29127. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29128. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29129. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29130. defined(OPENSSL_EXTRA))
  29131. X509* x509 = NULL;
  29132. const unsigned char* c = NULL;
  29133. unsigned char buf[4096];
  29134. int bytes = 0;
  29135. XFILE f = XBADFILE;
  29136. const X509_NAME* a = NULL;
  29137. const X509_NAME* b = NULL;
  29138. X509_NAME* d2i_name = NULL;
  29139. int sz = 0;
  29140. unsigned char* tmp = NULL;
  29141. char file[] = "./certs/ca-cert.der";
  29142. #ifndef OPENSSL_EXTRA_X509_SMALL
  29143. byte empty[] = { /* CN=empty emailAddress= */
  29144. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29145. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29146. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29147. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29148. 0x01, 0x16, 0x00
  29149. };
  29150. #endif
  29151. #ifndef OPENSSL_EXTRA_X509_SMALL
  29152. /* test compile of deprecated function, returns 0 */
  29153. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29154. #endif
  29155. ExpectNotNull(a = X509_NAME_new());
  29156. X509_NAME_free((X509_NAME*)a);
  29157. a = NULL;
  29158. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29159. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29160. if (f != XBADFILE)
  29161. XFCLOSE(f);
  29162. c = buf;
  29163. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29164. /* test cmp function */
  29165. ExpectNotNull(a = X509_get_issuer_name(x509));
  29166. ExpectNotNull(b = X509_get_subject_name(x509));
  29167. #ifndef OPENSSL_EXTRA_X509_SMALL
  29168. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29169. #endif
  29170. tmp = buf;
  29171. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29172. if (sz > 0 && tmp == buf) {
  29173. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29174. __LINE__);
  29175. fprintf(stderr, " Expected pointer to be incremented\n");
  29176. abort();
  29177. }
  29178. #ifndef OPENSSL_EXTRA_X509_SMALL
  29179. tmp = buf;
  29180. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29181. #endif
  29182. /* if output parameter is NULL, should still return required size. */
  29183. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29184. /* retry but with the function creating a buffer */
  29185. tmp = NULL;
  29186. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29187. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29188. tmp = NULL;
  29189. #ifdef WOLFSSL_CERT_NAME_ALL
  29190. /* test for givenName and name */
  29191. {
  29192. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29193. const byte gName[] = "test-given-name";
  29194. const byte name[] = "test-name";
  29195. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29196. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29197. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29198. 1);
  29199. wolfSSL_X509_NAME_ENTRY_free(entry);
  29200. entry = NULL;
  29201. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29202. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29203. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29204. 1);
  29205. wolfSSL_X509_NAME_ENTRY_free(entry);
  29206. tmp = NULL;
  29207. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29208. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29209. }
  29210. #endif
  29211. b = NULL;
  29212. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29213. #ifndef OPENSSL_EXTRA_X509_SMALL
  29214. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29215. #endif
  29216. X509_NAME_free((X509_NAME*)b);
  29217. X509_NAME_free(d2i_name);
  29218. d2i_name = NULL;
  29219. X509_free(x509);
  29220. #ifndef OPENSSL_EXTRA_X509_SMALL
  29221. /* test with an empty domain component */
  29222. tmp = empty;
  29223. sz = sizeof(empty);
  29224. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29225. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29226. /* size of empty emailAddress will be 0 */
  29227. tmp = buf;
  29228. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29229. (char*)tmp, sizeof(buf)), 0);
  29230. /* should contain no organization name */
  29231. tmp = buf;
  29232. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29233. (char*)tmp, sizeof(buf)), -1);
  29234. X509_NAME_free(d2i_name);
  29235. #endif
  29236. #endif
  29237. return EXPECT_RESULT();
  29238. }
  29239. static int test_wolfSSL_X509_NAME_hash(void)
  29240. {
  29241. EXPECT_DECLS;
  29242. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29243. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29244. BIO* bio = NULL;
  29245. X509* x509 = NULL;
  29246. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29247. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29248. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29249. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29250. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29251. X509_free(x509);
  29252. BIO_free(bio);
  29253. #endif
  29254. return EXPECT_RESULT();
  29255. }
  29256. static int test_wolfSSL_X509_NAME_print_ex(void)
  29257. {
  29258. EXPECT_DECLS;
  29259. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29260. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29261. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29262. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29263. !defined(NO_BIO) && !defined(NO_RSA)
  29264. int memSz = 0;
  29265. byte* mem = NULL;
  29266. BIO* bio = NULL;
  29267. BIO* membio = NULL;
  29268. X509* x509 = NULL;
  29269. X509_NAME* name = NULL;
  29270. const char* expNormal = "C=US, CN=wolfssl.com";
  29271. const char* expReverse = "CN=wolfssl.com, C=US";
  29272. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29273. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29274. const char* expRFC5523 =
  29275. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29276. /* Test with real cert (svrCertFile) first */
  29277. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29278. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29279. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29280. ExpectNotNull(name = X509_get_subject_name(x509));
  29281. /* Test without flags */
  29282. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29283. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29284. BIO_free(membio);
  29285. membio = NULL;
  29286. /* Test flag: XN_FLAG_RFC2253 */
  29287. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29288. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29289. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29290. BIO_free(membio);
  29291. membio = NULL;
  29292. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29293. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29294. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29295. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29296. BIO_free(membio);
  29297. membio = NULL;
  29298. X509_free(x509);
  29299. BIO_free(bio);
  29300. name = NULL;
  29301. /* Test normal case without escaped characters */
  29302. {
  29303. /* Create name: "/C=US/CN=wolfssl.com" */
  29304. ExpectNotNull(name = X509_NAME_new());
  29305. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29306. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29307. WOLFSSL_SUCCESS);
  29308. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29309. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29310. WOLFSSL_SUCCESS);
  29311. /* Test without flags */
  29312. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29313. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29314. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29315. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29316. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29317. BIO_free(membio);
  29318. membio = NULL;
  29319. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29320. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29321. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29322. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29323. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29324. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29325. BIO_free(membio);
  29326. membio = NULL;
  29327. /* Test flags: XN_FLAG_DN_REV - reversed */
  29328. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29329. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29330. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29331. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29332. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29333. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29334. BIO_free(membio);
  29335. membio = NULL;
  29336. X509_NAME_free(name);
  29337. name = NULL;
  29338. }
  29339. /* Test RFC2253 characters are escaped with backslashes */
  29340. {
  29341. ExpectNotNull(name = X509_NAME_new());
  29342. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29343. /* space at beginning and end, and: ,+"\ */
  29344. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29345. WOLFSSL_SUCCESS);
  29346. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29347. /* # at beginning, and: <>;*/
  29348. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29349. WOLFSSL_SUCCESS);
  29350. /* Test without flags */
  29351. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29352. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29353. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29354. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29355. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29356. XSTRLEN(expNotEscaped)), 0);
  29357. BIO_free(membio);
  29358. membio = NULL;
  29359. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29360. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29361. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29362. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29363. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29364. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29365. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29366. BIO_free(membio);
  29367. membio = NULL;
  29368. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29369. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29370. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29371. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29372. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29373. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29374. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29375. XSTRLEN(expNotEscapedRev)), 0);
  29376. BIO_free(membio);
  29377. X509_NAME_free(name);
  29378. }
  29379. #endif
  29380. return EXPECT_RESULT();
  29381. }
  29382. #ifndef NO_BIO
  29383. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29384. {
  29385. EXPECT_DECLS;
  29386. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29387. STACK_OF(X509_INFO) *info_stack = NULL;
  29388. X509_INFO *info = NULL;
  29389. int len;
  29390. int i;
  29391. const char* files[] = {
  29392. cliCertFile,
  29393. cliKeyFile,
  29394. /* This needs to be the order as svrCertFile contains the
  29395. * intermediate cert as well. */
  29396. svrKeyFile,
  29397. svrCertFile,
  29398. NULL,
  29399. };
  29400. const char** curFile;
  29401. BIO *fileBIO = NULL;
  29402. BIO *concatBIO = NULL;
  29403. byte tmp[FOURK_BUF];
  29404. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29405. * to group objects together. */
  29406. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29407. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29408. int fileLen = 0;
  29409. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29410. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29411. if (EXPECT_SUCCESS()) {
  29412. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29413. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29414. fileLen -= len;
  29415. if (EXPECT_FAIL())
  29416. break;
  29417. }
  29418. /* Make sure we read the entire file */
  29419. ExpectIntEQ(fileLen, 0);
  29420. }
  29421. BIO_free(fileBIO);
  29422. fileBIO = NULL;
  29423. }
  29424. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29425. NULL));
  29426. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29427. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29428. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29429. ExpectNotNull(info->x509);
  29430. ExpectNull(info->crl);
  29431. if (i != 0) {
  29432. ExpectNotNull(info->x_pkey);
  29433. ExpectIntEQ(X509_check_private_key(info->x509,
  29434. info->x_pkey->dec_pkey), 1);
  29435. }
  29436. else {
  29437. ExpectNull(info->x_pkey);
  29438. }
  29439. }
  29440. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29441. BIO_free(concatBIO);
  29442. #endif
  29443. return EXPECT_RESULT();
  29444. }
  29445. #endif
  29446. #ifndef NO_BIO
  29447. static int test_wolfSSL_X509_INFO(void)
  29448. {
  29449. EXPECT_DECLS;
  29450. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29451. STACK_OF(X509_INFO) *info_stack = NULL;
  29452. X509_INFO *info = NULL;
  29453. BIO *cert = NULL;
  29454. int i;
  29455. /* PEM in hex format to avoid null terminator */
  29456. byte data[] = {
  29457. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29458. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29459. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29460. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29461. 0x2d, 0x2d, 0x2d
  29462. };
  29463. /* PEM in hex format to avoid null terminator */
  29464. byte data2[] = {
  29465. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29466. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29467. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29468. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29469. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29470. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29471. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29472. };
  29473. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29474. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29475. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29476. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29477. ExpectNotNull(info->x509);
  29478. ExpectNull(info->crl);
  29479. ExpectNull(info->x_pkey);
  29480. }
  29481. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29482. info_stack = NULL;
  29483. BIO_free(cert);
  29484. cert = NULL;
  29485. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29486. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29487. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29488. info_stack = NULL;
  29489. BIO_free(cert);
  29490. cert = NULL;
  29491. /* This case should fail due to invalid input. */
  29492. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29493. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29494. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29495. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29496. info_stack = NULL;
  29497. BIO_free(cert);
  29498. cert = NULL;
  29499. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29500. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29501. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29502. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29503. BIO_free(cert);
  29504. #endif
  29505. return EXPECT_RESULT();
  29506. }
  29507. #endif
  29508. static int test_wolfSSL_X509_subject_name_hash(void)
  29509. {
  29510. EXPECT_DECLS;
  29511. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29512. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29513. X509* x509 = NULL;
  29514. X509_NAME* subjectName = NULL;
  29515. unsigned long ret1 = 0;
  29516. unsigned long ret2 = 0;
  29517. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29518. SSL_FILETYPE_PEM));
  29519. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29520. /* These two
  29521. * - X509_subject_name_hash(x509)
  29522. * - X509_NAME_hash(X509_get_subject_name(x509))
  29523. * should give the same hash, if !defined(NO_SHA) is true. */
  29524. ret1 = X509_subject_name_hash(x509);
  29525. ExpectIntNE(ret1, 0);
  29526. #if !defined(NO_SHA)
  29527. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29528. ExpectIntNE(ret2, 0);
  29529. ExpectIntEQ(ret1, ret2);
  29530. #else
  29531. (void) ret2;
  29532. #endif
  29533. X509_free(x509);
  29534. #endif
  29535. return EXPECT_RESULT();
  29536. }
  29537. static int test_wolfSSL_X509_issuer_name_hash(void)
  29538. {
  29539. EXPECT_DECLS;
  29540. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29541. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29542. X509* x509 = NULL;
  29543. X509_NAME* issuertName = NULL;
  29544. unsigned long ret1 = 0;
  29545. unsigned long ret2 = 0;
  29546. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29547. SSL_FILETYPE_PEM));
  29548. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29549. /* These two
  29550. * - X509_issuer_name_hash(x509)
  29551. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29552. * should give the same hash, if !defined(NO_SHA) is true. */
  29553. ret1 = X509_issuer_name_hash(x509);
  29554. ExpectIntNE(ret1, 0);
  29555. #if !defined(NO_SHA)
  29556. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29557. ExpectIntNE(ret2, 0);
  29558. ExpectIntEQ(ret1, ret2);
  29559. #else
  29560. (void) ret2;
  29561. #endif
  29562. X509_free(x509);
  29563. #endif
  29564. return EXPECT_RESULT();
  29565. }
  29566. static int test_wolfSSL_X509_check_host(void)
  29567. {
  29568. EXPECT_DECLS;
  29569. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29570. && !defined(NO_SHA) && !defined(NO_RSA)
  29571. X509* x509 = NULL;
  29572. const char altName[] = "example.com";
  29573. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29574. SSL_FILETYPE_PEM));
  29575. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29576. WOLFSSL_SUCCESS);
  29577. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29578. WOLFSSL_FAILURE);
  29579. X509_free(x509);
  29580. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29581. WOLFSSL_FAILURE);
  29582. #endif
  29583. return EXPECT_RESULT();
  29584. }
  29585. static int test_wolfSSL_X509_check_email(void)
  29586. {
  29587. EXPECT_DECLS;
  29588. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29589. X509* x509 = NULL;
  29590. const char goodEmail[] = "info@wolfssl.com";
  29591. const char badEmail[] = "disinfo@wolfssl.com";
  29592. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29593. SSL_FILETYPE_PEM));
  29594. /* Should fail on non-matching email address */
  29595. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29596. WOLFSSL_FAILURE);
  29597. /* Should succeed on matching email address */
  29598. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29599. WOLFSSL_SUCCESS);
  29600. /* Should compute length internally when not provided */
  29601. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29602. WOLFSSL_SUCCESS);
  29603. /* Should fail when email address is NULL */
  29604. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29605. WOLFSSL_FAILURE);
  29606. X509_free(x509);
  29607. /* Should fail when x509 is NULL */
  29608. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29609. WOLFSSL_FAILURE);
  29610. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29611. return EXPECT_RESULT();
  29612. }
  29613. static int test_wc_PemToDer(void)
  29614. {
  29615. EXPECT_DECLS;
  29616. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29617. int ret;
  29618. DerBuffer* pDer = NULL;
  29619. const char* ca_cert = "./certs/server-cert.pem";
  29620. byte* cert_buf = NULL;
  29621. size_t cert_sz = 0;
  29622. int eccKey = 0;
  29623. EncryptedInfo info;
  29624. XMEMSET(&info, 0, sizeof(info));
  29625. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29626. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  29627. &info, &eccKey), 0);
  29628. wc_FreeDer(&pDer);
  29629. pDer = NULL;
  29630. if (cert_buf != NULL) {
  29631. free(cert_buf);
  29632. cert_buf = NULL;
  29633. }
  29634. #ifdef HAVE_ECC
  29635. {
  29636. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29637. byte key_buf[256] = {0};
  29638. /* Test fail of loading a key with cert type */
  29639. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29640. key_buf[0] = '\n';
  29641. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29642. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29643. &pDer, NULL, &info, &eccKey)), 0);
  29644. #ifdef OPENSSL_EXTRA
  29645. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29646. &pDer, NULL, &info, &eccKey)), 0);
  29647. #endif
  29648. wc_FreeDer(&pDer);
  29649. if (cert_buf != NULL)
  29650. free(cert_buf);
  29651. }
  29652. #endif
  29653. #endif
  29654. return EXPECT_RESULT();
  29655. }
  29656. static int test_wc_AllocDer(void)
  29657. {
  29658. EXPECT_DECLS;
  29659. #if !defined(NO_CERTS)
  29660. DerBuffer* pDer = NULL;
  29661. word32 testSize = 1024;
  29662. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29663. BAD_FUNC_ARG);
  29664. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29665. ExpectNotNull(pDer);
  29666. wc_FreeDer(&pDer);
  29667. #endif
  29668. return EXPECT_RESULT();
  29669. }
  29670. static int test_wc_CertPemToDer(void)
  29671. {
  29672. EXPECT_DECLS;
  29673. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29674. const char* ca_cert = "./certs/ca-cert.pem";
  29675. byte* cert_buf = NULL;
  29676. size_t cert_sz = 0;
  29677. size_t cert_dersz = 0;
  29678. byte* cert_der = NULL;
  29679. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29680. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29681. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29682. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29683. (int)cert_dersz, CERT_TYPE), 0);
  29684. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29685. BAD_FUNC_ARG);
  29686. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29687. BAD_FUNC_ARG);
  29688. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29689. BAD_FUNC_ARG);
  29690. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29691. CERT_TYPE), BAD_FUNC_ARG);
  29692. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29693. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29694. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29695. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29696. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29697. CERT_TYPE), BAD_FUNC_ARG);
  29698. if (cert_der != NULL)
  29699. free(cert_der);
  29700. if (cert_buf != NULL)
  29701. free(cert_buf);
  29702. #endif
  29703. return EXPECT_RESULT();
  29704. }
  29705. static int test_wc_KeyPemToDer(void)
  29706. {
  29707. EXPECT_DECLS;
  29708. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29709. int ret = 0;
  29710. const byte cert_buf[] = \
  29711. "-----BEGIN PRIVATE KEY-----\n"
  29712. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29713. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29714. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29715. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29716. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29717. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29718. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29719. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29720. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29721. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29722. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29723. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29724. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29725. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29726. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29727. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29728. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29729. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29730. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29731. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29732. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29733. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29734. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29735. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29736. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29737. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29738. "-----END PRIVATE KEY-----\n";
  29739. const int cert_sz = sizeof(cert_buf);
  29740. const char cert_pw[] = "password";
  29741. int cert_dersz = 0;
  29742. byte* cert_der = NULL;
  29743. /* Bad arg: Cert buffer is NULL */
  29744. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29745. BAD_FUNC_ARG);
  29746. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29747. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29748. BAD_FUNC_ARG);
  29749. /* Test normal operation */
  29750. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29751. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29752. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29753. cert_pw), 0);
  29754. ExpectIntLE(ret, cert_sz);
  29755. if (cert_der != NULL) {
  29756. free(cert_der);
  29757. cert_der = NULL;
  29758. }
  29759. /* Test NULL for DER buffer to return needed DER buffer size */
  29760. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29761. ExpectIntLE(ret, cert_sz);
  29762. if (EXPECT_SUCCESS())
  29763. cert_dersz = ret;
  29764. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29765. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29766. cert_pw), 0);
  29767. ExpectIntLE(ret, cert_sz);
  29768. if (cert_der != NULL)
  29769. free(cert_der);
  29770. #endif
  29771. return EXPECT_RESULT();
  29772. }
  29773. static int test_wc_PubKeyPemToDer(void)
  29774. {
  29775. EXPECT_DECLS;
  29776. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29777. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29778. int ret = 0;
  29779. const char* key = "./certs/ecc-client-keyPub.pem";
  29780. byte* cert_buf = NULL;
  29781. size_t cert_sz = 0, cert_dersz = 0;
  29782. byte* cert_der = NULL;
  29783. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29784. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29785. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29786. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29787. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29788. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29789. (int)cert_dersz), 0);
  29790. if (cert_der != NULL) {
  29791. free(cert_der);
  29792. cert_der = NULL;
  29793. }
  29794. /* Test NULL for DER buffer to return needed DER buffer size */
  29795. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29796. ExpectIntLE(ret, cert_sz);
  29797. cert_dersz = ret;
  29798. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29799. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29800. (int)cert_dersz), 0);
  29801. if (cert_der != NULL) {
  29802. free(cert_der);
  29803. }
  29804. if (cert_buf != NULL) {
  29805. free(cert_buf);
  29806. }
  29807. #endif
  29808. return EXPECT_RESULT();
  29809. }
  29810. static int test_wc_PemPubKeyToDer(void)
  29811. {
  29812. EXPECT_DECLS;
  29813. #if !defined(NO_FILESYSTEM) && \
  29814. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29815. const char* key = "./certs/ecc-client-keyPub.pem";
  29816. size_t cert_dersz = 1024;
  29817. byte* cert_der = NULL;
  29818. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29819. BAD_FUNC_ARG);
  29820. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29821. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29822. if (cert_der != NULL) {
  29823. free(cert_der);
  29824. }
  29825. #endif
  29826. return EXPECT_RESULT();
  29827. }
  29828. static int test_wc_GetPubKeyDerFromCert(void)
  29829. {
  29830. EXPECT_DECLS;
  29831. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29832. int ret;
  29833. word32 idx = 0;
  29834. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29835. word32 keyDerSz = (word32)sizeof(keyDer);
  29836. DecodedCert decoded;
  29837. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29838. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29839. word32 certBufSz = sizeof(certBuf);
  29840. #endif
  29841. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29842. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29843. XFILE fp = XBADFILE;
  29844. #endif
  29845. #ifndef NO_RSA
  29846. RsaKey rsaKey;
  29847. #if defined(USE_CERT_BUFFERS_2048)
  29848. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29849. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29850. #elif defined(USE_CERT_BUFFERS_1024)
  29851. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29852. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29853. #else
  29854. unsigned char rsaCertDer[TWOK_BUF];
  29855. word32 rsaCertDerSz;
  29856. #endif
  29857. #endif
  29858. #ifdef HAVE_ECC
  29859. ecc_key eccKey;
  29860. #if defined(USE_CERT_BUFFERS_256)
  29861. byte* eccCert = (byte*)cliecc_cert_der_256;
  29862. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  29863. #else
  29864. unsigned char eccCert[ONEK_BUF];
  29865. word32 eccCertSz;
  29866. XFILE fp2 = XBADFILE;
  29867. #endif
  29868. #endif
  29869. #ifndef NO_RSA
  29870. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  29871. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  29872. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  29873. fp), 0);
  29874. if (fp != XBADFILE) {
  29875. XFCLOSE(fp);
  29876. fp = XBADFILE;
  29877. }
  29878. #endif
  29879. /* good test case - RSA DER cert */
  29880. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  29881. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29882. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29883. ExpectIntGT(keyDerSz, 0);
  29884. /* sanity check, verify we can import DER public key */
  29885. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29886. ExpectIntEQ(ret, 0);
  29887. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29888. if (ret == 0) {
  29889. wc_FreeRsaKey(&rsaKey);
  29890. }
  29891. /* test LENGTH_ONLY_E case */
  29892. keyDerSz = 0;
  29893. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29894. LENGTH_ONLY_E);
  29895. ExpectIntGT(keyDerSz, 0);
  29896. /* bad args: DecodedCert NULL */
  29897. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29898. /* bad args: output key buff size */
  29899. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29900. /* bad args: zero size output key buffer */
  29901. keyDerSz = 0;
  29902. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29903. BAD_FUNC_ARG);
  29904. wc_FreeDecodedCert(&decoded);
  29905. /* Certificate Request Tests */
  29906. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29907. {
  29908. XMEMSET(certBuf, 0, sizeof(certBuf));
  29909. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  29910. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  29911. if (fp != XBADFILE) {
  29912. XFCLOSE(fp);
  29913. }
  29914. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  29915. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  29916. /* good test case - RSA DER certificate request */
  29917. keyDerSz = sizeof(keyDer);
  29918. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29919. 0);
  29920. ExpectIntGT(keyDerSz, 0);
  29921. /* sanity check, verify we can import DER public key */
  29922. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29923. ExpectIntEQ(ret, 0);
  29924. idx = 0;
  29925. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29926. if (ret == 0) {
  29927. wc_FreeRsaKey(&rsaKey);
  29928. }
  29929. wc_FreeDecodedCert(&decoded);
  29930. }
  29931. #endif /* WOLFSSL_CERT_REQ */
  29932. #endif /* NO_RSA */
  29933. #ifdef HAVE_ECC
  29934. #ifndef USE_CERT_BUFFERS_256
  29935. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  29936. XBADFILE);
  29937. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  29938. if (fp2 != XBADFILE) {
  29939. XFCLOSE(fp2);
  29940. }
  29941. #endif
  29942. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  29943. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29944. /* good test case - ECC */
  29945. XMEMSET(keyDer, 0, sizeof(keyDer));
  29946. keyDerSz = sizeof(keyDer);
  29947. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29948. ExpectIntGT(keyDerSz, 0);
  29949. /* sanity check, verify we can import DER public key */
  29950. ret = wc_ecc_init(&eccKey);
  29951. ExpectIntEQ(ret, 0);
  29952. idx = 0; /* reset idx to 0, used above in RSA case */
  29953. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  29954. if (ret == 0) {
  29955. wc_ecc_free(&eccKey);
  29956. }
  29957. /* test LENGTH_ONLY_E case */
  29958. keyDerSz = 0;
  29959. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29960. LENGTH_ONLY_E);
  29961. ExpectIntGT(keyDerSz, 0);
  29962. wc_FreeDecodedCert(&decoded);
  29963. #endif
  29964. #endif /* !NO_RSA || HAVE_ECC */
  29965. return EXPECT_RESULT();
  29966. }
  29967. static int test_wc_CheckCertSigPubKey(void)
  29968. {
  29969. EXPECT_DECLS;
  29970. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29971. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  29972. int ret = 0;
  29973. const char* ca_cert = "./certs/ca-cert.pem";
  29974. byte* cert_buf = NULL;
  29975. size_t cert_sz = 0;
  29976. byte* cert_der = NULL;
  29977. word32 cert_dersz = 0;
  29978. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29979. word32 keyDerSz = (word32)sizeof(keyDer);
  29980. DecodedCert decoded;
  29981. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29982. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  29983. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29984. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29985. (int)cert_dersz, CERT_TYPE), 0);
  29986. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  29987. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29988. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29989. ExpectIntGT(keyDerSz, 0);
  29990. /* Good test case. */
  29991. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29992. keyDerSz, RSAk), 0);
  29993. /* No certificate. */
  29994. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  29995. ECDSAk), BAD_FUNC_ARG);
  29996. /* Bad cert size. */
  29997. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  29998. RSAk), 0);
  29999. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30000. /* No public key. */
  30001. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30002. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30003. /* Bad public key size. */
  30004. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30005. RSAk), BAD_FUNC_ARG);
  30006. /* Wrong aglo. */
  30007. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30008. keyDerSz, ECDSAk), ASN_PARSE_E);
  30009. wc_FreeDecodedCert(&decoded);
  30010. if (cert_der != NULL)
  30011. free(cert_der);
  30012. if (cert_buf != NULL)
  30013. free(cert_buf);
  30014. #endif
  30015. return EXPECT_RESULT();
  30016. }
  30017. static int test_wolfSSL_certs(void)
  30018. {
  30019. EXPECT_DECLS;
  30020. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30021. !defined(NO_RSA)
  30022. X509* x509ext = NULL;
  30023. #ifdef OPENSSL_ALL
  30024. X509* x509 = NULL;
  30025. WOLFSSL_X509_EXTENSION* ext = NULL;
  30026. ASN1_OBJECT* obj = NULL;
  30027. #endif
  30028. WOLFSSL* ssl = NULL;
  30029. WOLFSSL_CTX* ctx = NULL;
  30030. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30031. ASN1_STRING* asn1_str = NULL;
  30032. AUTHORITY_KEYID* akey = NULL;
  30033. BASIC_CONSTRAINTS* bc = NULL;
  30034. int crit = 0;
  30035. #ifndef NO_WOLFSSL_SERVER
  30036. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30037. #else
  30038. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30039. #endif
  30040. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30041. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30042. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30043. #if !defined(NO_CHECK_PRIVATE_KEY)
  30044. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30045. #endif
  30046. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30047. #if !defined(NO_CHECK_PRIVATE_KEY)
  30048. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30049. #endif
  30050. ExpectNotNull(ssl = SSL_new(ctx));
  30051. #if !defined(NO_CHECK_PRIVATE_KEY)
  30052. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30053. #endif
  30054. #ifdef HAVE_PK_CALLBACKS
  30055. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30056. #endif /* HAVE_PK_CALLBACKS */
  30057. /* create and use x509 */
  30058. #ifdef OPENSSL_ALL
  30059. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30060. WOLFSSL_FILETYPE_PEM));
  30061. #endif
  30062. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30063. WOLFSSL_FILETYPE_PEM));
  30064. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30065. #if !defined(NO_CHECK_PRIVATE_KEY)
  30066. /* with loading in a new cert the check on private key should now fail */
  30067. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30068. #endif
  30069. #if defined(USE_CERT_BUFFERS_2048)
  30070. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30071. (unsigned char*)server_cert_der_2048,
  30072. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30073. #endif
  30074. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30075. /************* Get Digest of Certificate ******************/
  30076. {
  30077. byte digest[64]; /* max digest size */
  30078. word32 digestSz;
  30079. XMEMSET(digest, 0, sizeof(digest));
  30080. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30081. WOLFSSL_SUCCESS);
  30082. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30083. WOLFSSL_SUCCESS);
  30084. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30085. WOLFSSL_FAILURE);
  30086. }
  30087. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30088. /* test and checkout X509 extensions */
  30089. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30090. NID_basic_constraints, &crit, NULL));
  30091. ExpectIntEQ(crit, 0);
  30092. #ifdef OPENSSL_ALL
  30093. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30094. X509_EXTENSION_free(ext);
  30095. ext = NULL;
  30096. ExpectNotNull(ext = X509_EXTENSION_new());
  30097. X509_EXTENSION_set_critical(ext, 1);
  30098. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30099. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30100. ASN1_OBJECT_free(obj);
  30101. obj = NULL;
  30102. X509_EXTENSION_free(ext);
  30103. ext = NULL;
  30104. ExpectNotNull(ext = X509_EXTENSION_new());
  30105. X509_EXTENSION_set_critical(ext, 0);
  30106. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30107. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30108. NULL);
  30109. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30110. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30111. * and X509_get_ext_d2i has created new */
  30112. asn1_str = NULL;
  30113. X509_EXTENSION_free(ext);
  30114. ext = NULL;
  30115. #endif
  30116. BASIC_CONSTRAINTS_free(bc);
  30117. bc = NULL;
  30118. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30119. NID_key_usage, &crit, NULL));
  30120. ExpectIntEQ(crit, 1);
  30121. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30122. #ifdef OPENSSL_ALL
  30123. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30124. X509_EXTENSION_free(ext);
  30125. ext = NULL;
  30126. #endif
  30127. ASN1_STRING_free(asn1_str);
  30128. asn1_str = NULL;
  30129. #ifdef OPENSSL_ALL
  30130. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30131. NID_ext_key_usage, &crit, NULL));
  30132. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30133. X509_EXTENSION_free(ext);
  30134. ext = NULL;
  30135. EXTENDED_KEY_USAGE_free(sk);
  30136. sk = NULL;
  30137. #else
  30138. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30139. &crit, NULL);
  30140. ExpectNull(sk);
  30141. #endif
  30142. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30143. NID_authority_key_identifier, &crit, NULL));
  30144. #ifdef OPENSSL_ALL
  30145. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30146. akey));
  30147. X509_EXTENSION_free(ext);
  30148. ext = NULL;
  30149. #endif
  30150. wolfSSL_AUTHORITY_KEYID_free(akey);
  30151. akey = NULL;
  30152. /* NID not yet supported */
  30153. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30154. NID_private_key_usage_period, &crit, NULL));
  30155. ExpectIntEQ(crit, -1);
  30156. sk_ASN1_OBJECT_free(sk);
  30157. sk = NULL;
  30158. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30159. NID_subject_alt_name, &crit, NULL));
  30160. {
  30161. int i;
  30162. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30163. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30164. ExpectIntEQ(gen->type, GEN_DNS);
  30165. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30166. }
  30167. }
  30168. sk_GENERAL_NAME_free(sk);
  30169. sk = NULL;
  30170. /* NID not yet supported */
  30171. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30172. NID_issuer_alt_name, &crit, NULL));
  30173. ExpectIntEQ(crit, -1);
  30174. sk_ASN1_OBJECT_free(sk);
  30175. sk = NULL;
  30176. /* NID not yet supported */
  30177. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30178. NID_info_access, &crit, NULL));
  30179. sk_ASN1_OBJECT_free(sk);
  30180. sk = NULL;
  30181. /* NID not yet supported */
  30182. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30183. NID_sinfo_access, &crit, NULL));
  30184. ExpectIntEQ(crit, -1);
  30185. sk_ASN1_OBJECT_free(sk);
  30186. sk = NULL;
  30187. /* NID not yet supported */
  30188. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30189. NID_name_constraints, &crit, NULL));
  30190. ExpectIntEQ(crit, -1);
  30191. sk_ASN1_OBJECT_free(sk);
  30192. sk = NULL;
  30193. /* no cert policy set */
  30194. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30195. NID_certificate_policies, &crit, NULL));
  30196. sk_ASN1_OBJECT_free(sk);
  30197. sk = NULL;
  30198. /* NID not yet supported */
  30199. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30200. NID_policy_mappings, &crit, NULL));
  30201. ExpectIntEQ(crit, -1);
  30202. sk_ASN1_OBJECT_free(sk);
  30203. sk = NULL;
  30204. /* NID not yet supported */
  30205. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30206. NID_policy_constraints, &crit, NULL));
  30207. ExpectIntEQ(crit, -1);
  30208. sk_ASN1_OBJECT_free(sk);
  30209. sk = NULL;
  30210. /* NID not yet supported */
  30211. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30212. NID_inhibit_any_policy, &crit, NULL));
  30213. ExpectIntEQ(crit, -1);
  30214. sk_ASN1_OBJECT_free(sk);
  30215. sk = NULL;
  30216. /* NID not yet supported */
  30217. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30218. NID_tlsfeature, &crit, NULL));
  30219. ExpectIntEQ(crit, -1);
  30220. sk_ASN1_OBJECT_free(sk);
  30221. sk = NULL;
  30222. /* test invalid cases */
  30223. crit = 0;
  30224. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30225. NULL));
  30226. ExpectIntEQ(crit, -1);
  30227. /* NULL passed for criticality. */
  30228. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30229. NID_tlsfeature, NULL, NULL));
  30230. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30231. #ifdef OPENSSL_ALL
  30232. X509_free(x509);
  30233. #endif
  30234. X509_free(x509ext);
  30235. SSL_free(ssl);
  30236. SSL_CTX_free(ctx);
  30237. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30238. return EXPECT_RESULT();
  30239. }
  30240. static int test_wolfSSL_X509_check_private_key(void)
  30241. {
  30242. EXPECT_DECLS;
  30243. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30244. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30245. X509* x509 = NULL;
  30246. EVP_PKEY* pkey = NULL;
  30247. const byte* key;
  30248. /* Check with correct key */
  30249. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30250. SSL_FILETYPE_PEM)));
  30251. key = client_key_der_2048;
  30252. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30253. (long)sizeof_client_key_der_2048));
  30254. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30255. EVP_PKEY_free(pkey);
  30256. pkey = NULL;
  30257. /* Check with wrong key */
  30258. key = server_key_der_2048;
  30259. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30260. (long)sizeof_server_key_der_2048));
  30261. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30262. /* test for incorrect parameter */
  30263. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30264. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30265. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30266. EVP_PKEY_free(pkey);
  30267. X509_free(x509);
  30268. #endif
  30269. return EXPECT_RESULT();
  30270. }
  30271. static int test_wolfSSL_private_keys(void)
  30272. {
  30273. EXPECT_DECLS;
  30274. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30275. !defined(NO_FILESYSTEM)
  30276. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30277. WOLFSSL* ssl = NULL;
  30278. WOLFSSL_CTX* ctx = NULL;
  30279. EVP_PKEY* pkey = NULL;
  30280. OpenSSL_add_all_digests();
  30281. OpenSSL_add_all_algorithms();
  30282. #ifndef NO_RSA
  30283. #ifndef NO_WOLFSSL_SERVER
  30284. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30285. #else
  30286. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30287. #endif
  30288. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30289. /* Have to load a cert before you can check the private key against that
  30290. * certificates public key! */
  30291. #if !defined(NO_CHECK_PRIVATE_KEY)
  30292. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30293. #endif
  30294. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30295. #if !defined(NO_CHECK_PRIVATE_KEY)
  30296. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30297. #endif
  30298. ExpectNotNull(ssl = SSL_new(ctx));
  30299. #if !defined(NO_CHECK_PRIVATE_KEY)
  30300. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30301. #endif
  30302. #ifdef USE_CERT_BUFFERS_2048
  30303. {
  30304. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30305. unsigned char buf[FOURK_BUF];
  30306. word32 bufSz;
  30307. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30308. (unsigned char*)client_key_der_2048,
  30309. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30310. #if !defined(NO_CHECK_PRIVATE_KEY)
  30311. /* Should mismatch now that a different private key loaded */
  30312. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30313. #endif
  30314. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30315. (unsigned char*)server_key,
  30316. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30317. #if !defined(NO_CHECK_PRIVATE_KEY)
  30318. /* After loading back in DER format of original key, should match */
  30319. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30320. #endif
  30321. /* test loading private key to the WOLFSSL_CTX */
  30322. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30323. (unsigned char*)client_key_der_2048,
  30324. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30325. #if !defined(NO_CHECK_PRIVATE_KEY)
  30326. /* Should mismatch now that a different private key loaded */
  30327. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30328. #endif
  30329. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30330. (unsigned char*)server_key,
  30331. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30332. #if !defined(NO_CHECK_PRIVATE_KEY)
  30333. /* After loading back in DER format of original key, should match */
  30334. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30335. #endif
  30336. /* pkey not set yet, expecting to fail */
  30337. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30338. /* set PKEY and test again */
  30339. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30340. &server_key, (long)sizeof_server_key_der_2048));
  30341. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30342. /* reuse PKEY structure and test
  30343. * this should be checked with a memory management sanity checker */
  30344. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30345. server_key = (const unsigned char*)server_key_der_2048;
  30346. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30347. &server_key, (long)sizeof_server_key_der_2048));
  30348. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30349. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30350. bufSz = FOURK_BUF;
  30351. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30352. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30353. RSAk, NULL, 0)), 0);
  30354. server_key = (const unsigned char*)buf;
  30355. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30356. (long)bufSz));
  30357. }
  30358. #endif
  30359. EVP_PKEY_free(pkey);
  30360. pkey = NULL;
  30361. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30362. ssl = NULL;
  30363. SSL_CTX_free(ctx);
  30364. ctx = NULL;
  30365. #endif /* end of RSA private key match tests */
  30366. #ifdef HAVE_ECC
  30367. #ifndef NO_WOLFSSL_SERVER
  30368. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30369. #else
  30370. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30371. #endif
  30372. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30373. WOLFSSL_FILETYPE_PEM));
  30374. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30375. WOLFSSL_FILETYPE_PEM));
  30376. ExpectNotNull(ssl = SSL_new(ctx));
  30377. #if !defined(NO_CHECK_PRIVATE_KEY)
  30378. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30379. #endif
  30380. SSL_free(ssl);
  30381. ssl = NULL;
  30382. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30383. WOLFSSL_FILETYPE_PEM));
  30384. ExpectNotNull(ssl = SSL_new(ctx));
  30385. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30386. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30387. #endif
  30388. SSL_free(ssl);
  30389. ssl = NULL;
  30390. SSL_CTX_free(ctx);
  30391. ctx = NULL;
  30392. #endif /* end of ECC private key match tests */
  30393. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30394. #ifndef NO_WOLFSSL_SERVER
  30395. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30396. #else
  30397. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30398. #endif
  30399. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30400. WOLFSSL_FILETYPE_PEM));
  30401. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30402. WOLFSSL_FILETYPE_PEM));
  30403. ExpectNotNull(ssl = SSL_new(ctx));
  30404. #if !defined(NO_CHECK_PRIVATE_KEY)
  30405. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30406. #endif
  30407. SSL_free(ssl);
  30408. ssl = NULL;
  30409. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30410. WOLFSSL_FILETYPE_PEM));
  30411. ExpectNotNull(ssl = SSL_new(ctx));
  30412. #if !defined(NO_CHECK_PRIVATE_KEY)
  30413. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30414. #endif
  30415. SSL_free(ssl);
  30416. ssl = NULL;
  30417. SSL_CTX_free(ctx);
  30418. ctx = NULL;
  30419. #endif /* end of Ed25519 private key match tests */
  30420. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30421. #ifndef NO_WOLFSSL_SERVER
  30422. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30423. #else
  30424. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30425. #endif
  30426. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30427. WOLFSSL_FILETYPE_PEM));
  30428. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30429. WOLFSSL_FILETYPE_PEM));
  30430. ExpectNotNull(ssl = SSL_new(ctx));
  30431. #if !defined(NO_CHECK_PRIVATE_KEY)
  30432. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30433. #endif
  30434. SSL_free(ssl);
  30435. ssl = NULL;
  30436. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30437. WOLFSSL_FILETYPE_PEM));
  30438. ExpectNotNull(ssl = SSL_new(ctx));
  30439. #if !defined(NO_CHECK_PRIVATE_KEY)
  30440. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30441. #endif
  30442. SSL_free(ssl);
  30443. ssl = NULL;
  30444. SSL_CTX_free(ctx);
  30445. ctx = NULL;
  30446. #endif /* end of Ed448 private key match tests */
  30447. EVP_cleanup();
  30448. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30449. CONF_modules_free();
  30450. ENGINE_cleanup();
  30451. CONF_modules_unload();
  30452. (void)ssl;
  30453. (void)ctx;
  30454. (void)pkey;
  30455. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30456. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30457. return EXPECT_RESULT();
  30458. }
  30459. static int test_wolfSSL_PEM_def_callback(void)
  30460. {
  30461. EXPECT_DECLS;
  30462. #ifdef OPENSSL_EXTRA
  30463. char buf[10];
  30464. const char* defpwd = "DEF PWD";
  30465. int defpwdLen = (int)XSTRLEN(defpwd);
  30466. int smallLen = 1;
  30467. /* Bad parameters. */
  30468. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30469. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30470. 0);
  30471. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30472. XMEMSET(buf, 0, sizeof(buf));
  30473. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30474. defpwdLen);
  30475. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30476. ExpectIntEQ(buf[defpwdLen], 0);
  30477. /* Size of buffer is smaller than default password. */
  30478. XMEMSET(buf, 0, sizeof(buf));
  30479. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30480. smallLen);
  30481. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30482. ExpectIntEQ(buf[smallLen], 0);
  30483. #endif /* OPENSSL_EXTRA */
  30484. return EXPECT_RESULT();
  30485. }
  30486. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30487. {
  30488. EXPECT_DECLS;
  30489. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30490. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30491. XFILE file = XBADFILE;
  30492. #if !defined(NO_RSA)
  30493. const char* fname_rsa = "./certs/server-key.pem";
  30494. RSA* rsa = NULL;
  30495. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30496. unsigned char* sig = NULL;
  30497. size_t sigLen = 0;
  30498. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30499. size_t tbsLen = sizeof(tbs);
  30500. #endif
  30501. #if !defined(NO_DSA)
  30502. const char* fname_dsa = "./certs/dsa2048.pem";
  30503. #endif
  30504. #if defined(HAVE_ECC)
  30505. const char* fname_ec = "./certs/ecc-key.pem";
  30506. #endif
  30507. #if !defined(NO_DH)
  30508. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30509. #endif
  30510. EVP_PKEY* pkey = NULL;
  30511. /* Check error case. */
  30512. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30513. /* not a PEM key. */
  30514. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30515. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30516. if (file != XBADFILE)
  30517. XFCLOSE(file);
  30518. file = XBADFILE;
  30519. #ifndef NO_RSA
  30520. /* Read in an RSA key. */
  30521. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30522. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30523. if (file != XBADFILE)
  30524. XFCLOSE(file);
  30525. file = XBADFILE;
  30526. /* Make sure the key is usable by signing some data with it. */
  30527. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30528. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30529. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30530. DYNAMIC_TYPE_TMP_BUFFER));
  30531. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30532. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30533. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30534. WOLFSSL_SUCCESS);
  30535. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30536. EVP_PKEY_CTX_free(ctx);
  30537. EVP_PKEY_free(pkey);
  30538. pkey = NULL;
  30539. #endif
  30540. #ifndef NO_DSA
  30541. /* Read in a DSA key. */
  30542. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30543. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30544. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30545. EVP_PKEY_free(pkey);
  30546. pkey = NULL;
  30547. #else
  30548. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30549. #endif
  30550. if (file != XBADFILE)
  30551. XFCLOSE(file);
  30552. file = XBADFILE;
  30553. #endif
  30554. #ifdef HAVE_ECC
  30555. /* Read in an EC key. */
  30556. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30557. ExpectNotNull(pkey = EVP_PKEY_new());
  30558. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30559. if (file != XBADFILE)
  30560. XFCLOSE(file);
  30561. file = XBADFILE;
  30562. EVP_PKEY_free(pkey);
  30563. pkey = NULL;
  30564. #endif
  30565. #ifndef NO_DH
  30566. /* Read in a DH key. */
  30567. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30568. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30569. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30570. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30571. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30572. EVP_PKEY_free(pkey);
  30573. pkey = NULL;
  30574. #else
  30575. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30576. #endif
  30577. if (file != XBADFILE)
  30578. XFCLOSE(file);
  30579. file = XBADFILE;
  30580. #endif
  30581. #endif
  30582. return EXPECT_RESULT();
  30583. }
  30584. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30585. {
  30586. EXPECT_DECLS;
  30587. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30588. && !defined(NO_FILESYSTEM)
  30589. XFILE file = XBADFILE;
  30590. const char* fname = "./certs/client-keyPub.pem";
  30591. EVP_PKEY* pkey = NULL;
  30592. /* Check error case. */
  30593. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30594. /* Read in an RSA key. */
  30595. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30596. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30597. EVP_PKEY_free(pkey);
  30598. pkey = NULL;
  30599. if (file != XBADFILE)
  30600. XFCLOSE(file);
  30601. file = XBADFILE;
  30602. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30603. ExpectNotNull(pkey = EVP_PKEY_new());
  30604. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30605. EVP_PKEY_free(pkey);
  30606. if (file != XBADFILE)
  30607. XFCLOSE(file);
  30608. #endif
  30609. return EXPECT_RESULT();
  30610. }
  30611. /* test loading RSA key using BIO */
  30612. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30613. {
  30614. EXPECT_DECLS;
  30615. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30616. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30617. !defined(NO_BIO)
  30618. BIO* bio = NULL;
  30619. XFILE file = XBADFILE;
  30620. const char* fname = "./certs/server-key.pem";
  30621. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30622. EVP_PKEY* pkey = NULL;
  30623. size_t sz = 0;
  30624. byte* buf = NULL;
  30625. EVP_PKEY* pkey2 = NULL;
  30626. EVP_PKEY* pkey3 = NULL;
  30627. RSA* rsa_key = NULL;
  30628. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30629. unsigned char extra[10];
  30630. int i;
  30631. BIO* pub_bio = NULL;
  30632. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30633. #endif
  30634. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30635. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30636. ExpectIntGT(sz = XFTELL(file), 0);
  30637. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30638. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30639. if (buf != NULL) {
  30640. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30641. }
  30642. if (file != XBADFILE) {
  30643. XFCLOSE(file);
  30644. file = XBADFILE;
  30645. }
  30646. /* Test using BIO new mem and loading PEM private key */
  30647. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30648. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30649. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30650. buf = NULL;
  30651. BIO_free(bio);
  30652. bio = NULL;
  30653. /* New empty EVP_PKEY */
  30654. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30655. if (pkey2 != NULL) {
  30656. pkey2->type = EVP_PKEY_RSA;
  30657. }
  30658. /* Test parameter copy */
  30659. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30660. EVP_PKEY_free(pkey2);
  30661. EVP_PKEY_free(pkey);
  30662. pkey = NULL;
  30663. /* Qt unit test case : rsa pkcs8 key */
  30664. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30665. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30666. ExpectIntGT(sz = XFTELL(file), 0);
  30667. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30668. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30669. if (buf) {
  30670. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30671. }
  30672. if (file != XBADFILE) {
  30673. XFCLOSE(file);
  30674. file = XBADFILE;
  30675. }
  30676. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30677. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30678. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30679. buf = NULL;
  30680. BIO_free(bio);
  30681. bio = NULL;
  30682. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30683. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30684. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30685. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30686. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30687. #else
  30688. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30689. #endif
  30690. RSA_free(rsa_key);
  30691. EVP_PKEY_free(pkey3);
  30692. EVP_PKEY_free(pkey);
  30693. pkey = NULL;
  30694. pkey2 = NULL;
  30695. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30696. #define BIO_PEM_TEST_CHAR 'a'
  30697. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30698. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30699. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30700. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30701. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30702. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30703. (long)sizeof_server_key_der_2048));
  30704. ExpectNull(pkey);
  30705. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30706. (long)sizeof_server_key_der_2048));
  30707. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30708. WOLFSSL_FAILURE);
  30709. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30710. WOLFSSL_FAILURE);
  30711. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30712. WOLFSSL_SUCCESS);
  30713. ExpectIntGT(BIO_pending(bio), 0);
  30714. ExpectIntEQ(BIO_pending(bio), 1679);
  30715. /* Check if the pubkey API writes only the public key */
  30716. #ifdef WOLFSSL_KEY_GEN
  30717. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30718. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30719. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30720. ExpectIntGT(BIO_pending(pub_bio), 0);
  30721. /* Previously both the private key and the pubkey calls would write
  30722. * out the private key and the PEM header was the only difference.
  30723. * The public PEM should be significantly shorter than the
  30724. * private key versison. */
  30725. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30726. #else
  30727. /* Not supported. */
  30728. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30729. #endif
  30730. /* test creating new EVP_PKEY with good args */
  30731. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30732. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30733. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30734. pkey->pkey_sz), 0);
  30735. }
  30736. /* test of reuse of EVP_PKEY */
  30737. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30738. ExpectIntEQ(BIO_pending(bio), 0);
  30739. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30740. SSL_SUCCESS);
  30741. /* add 10 extra bytes after PEM */
  30742. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30743. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30744. ExpectNotNull(pkey);
  30745. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30746. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30747. pkey->pkey_sz), 0);
  30748. }
  30749. /* check 10 extra bytes still there */
  30750. ExpectIntEQ(BIO_pending(bio), 10);
  30751. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30752. for (i = 0; i < 10; i++) {
  30753. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30754. }
  30755. BIO_free(pub_bio);
  30756. BIO_free(bio);
  30757. bio = NULL;
  30758. EVP_PKEY_free(pkey);
  30759. pkey = NULL;
  30760. EVP_PKEY_free(pkey2);
  30761. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  30762. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  30763. * !NO_FILESYSTEM && !NO_BIO */
  30764. return EXPECT_RESULT();
  30765. }
  30766. /* test loading ECC key using BIO */
  30767. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  30768. {
  30769. EXPECT_DECLS;
  30770. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  30771. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30772. BIO* bio = NULL;
  30773. EVP_PKEY* pkey = NULL;
  30774. XFILE file = XBADFILE;
  30775. const char* fname = "./certs/ecc-key.pem";
  30776. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30777. size_t sz = 0;
  30778. byte* buf = NULL;
  30779. EVP_PKEY* pkey2 = NULL;
  30780. EVP_PKEY* pkey3 = NULL;
  30781. EC_KEY* ec_key = NULL;
  30782. int nid = 0;
  30783. BIO* pub_bio = NULL;
  30784. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30785. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30786. ExpectIntGT(sz = XFTELL(file), 0);
  30787. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30788. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30789. if (buf) {
  30790. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30791. }
  30792. if (file != XBADFILE) {
  30793. XFCLOSE(file);
  30794. file = XBADFILE;
  30795. }
  30796. /* Test using BIO new mem and loading PEM private key */
  30797. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30798. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30799. BIO_free(bio);
  30800. bio = NULL;
  30801. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30802. buf = NULL;
  30803. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30804. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30805. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30806. WOLFSSL_SUCCESS);
  30807. ExpectIntGT(BIO_pending(bio), 0);
  30808. /* No parmeters. */
  30809. ExpectIntEQ(BIO_pending(bio), 227);
  30810. /* Check if the pubkey API writes only the public key */
  30811. #ifdef WOLFSSL_KEY_GEN
  30812. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30813. ExpectIntGT(BIO_pending(pub_bio), 0);
  30814. /* Previously both the private key and the pubkey calls would write
  30815. * out the private key and the PEM header was the only difference.
  30816. * The public PEM should be significantly shorter than the
  30817. * private key versison. */
  30818. ExpectIntEQ(BIO_pending(pub_bio), 178);
  30819. #endif
  30820. BIO_free(pub_bio);
  30821. BIO_free(bio);
  30822. bio = NULL;
  30823. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30824. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30825. if (pkey2 != NULL) {
  30826. pkey2->type = EVP_PKEY_EC;
  30827. }
  30828. /* Test parameter copy */
  30829. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30830. /* Qt unit test case 1*/
  30831. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30832. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30833. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30834. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30835. #else
  30836. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30837. #endif
  30838. /* Test default digest */
  30839. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30840. ExpectIntEQ(nid, NID_sha256);
  30841. EC_KEY_free(ec_key);
  30842. ec_key = NULL;
  30843. EVP_PKEY_free(pkey3);
  30844. pkey3 = NULL;
  30845. EVP_PKEY_free(pkey2);
  30846. pkey2 = NULL;
  30847. EVP_PKEY_free(pkey);
  30848. pkey = NULL;
  30849. /* Qt unit test case ec pkcs8 key */
  30850. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30851. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30852. ExpectIntGT(sz = XFTELL(file), 0);
  30853. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30854. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30855. if (buf) {
  30856. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30857. }
  30858. if (file != XBADFILE) {
  30859. XFCLOSE(file);
  30860. file = XBADFILE;
  30861. }
  30862. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30863. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30864. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30865. buf = NULL;
  30866. BIO_free(bio);
  30867. bio = NULL;
  30868. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30869. /* Qt unit test case */
  30870. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30871. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30872. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30873. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30874. #else
  30875. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30876. #endif
  30877. EC_KEY_free(ec_key);
  30878. EVP_PKEY_free(pkey3);
  30879. EVP_PKEY_free(pkey);
  30880. pkey = NULL;
  30881. #endif
  30882. return EXPECT_RESULT();
  30883. }
  30884. /* test loading DSA key using BIO */
  30885. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  30886. {
  30887. EXPECT_DECLS;
  30888. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  30889. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30890. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  30891. BIO* bio = NULL;
  30892. EVP_PKEY* pkey = NULL;
  30893. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  30894. /* Private DSA EVP_PKEY */
  30895. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30896. NULL));
  30897. BIO_free(bio);
  30898. bio = NULL;
  30899. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30900. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30901. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30902. NULL), 0);
  30903. #endif
  30904. #ifdef WOLFSSL_KEY_GEN
  30905. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  30906. ExpectIntEQ(BIO_pending(bio), 1178);
  30907. BIO_reset(bio);
  30908. #endif
  30909. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30910. 1);
  30911. ExpectIntEQ(BIO_pending(bio), 1196);
  30912. BIO_free(bio);
  30913. bio = NULL;
  30914. EVP_PKEY_free(pkey);
  30915. pkey = NULL;
  30916. #endif
  30917. #endif
  30918. return EXPECT_RESULT();
  30919. }
  30920. /* test loading DH key using BIO */
  30921. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  30922. {
  30923. EXPECT_DECLS;
  30924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  30925. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30926. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30927. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30928. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30929. BIO* bio = NULL;
  30930. EVP_PKEY* pkey = NULL;
  30931. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  30932. /* Private DH EVP_PKEY */
  30933. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30934. NULL));
  30935. BIO_free(bio);
  30936. bio = NULL;
  30937. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30938. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30939. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30940. NULL), 0);
  30941. #endif
  30942. #ifdef WOLFSSL_KEY_GEN
  30943. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  30944. #endif
  30945. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30946. 1);
  30947. ExpectIntEQ(BIO_pending(bio), 806);
  30948. BIO_free(bio);
  30949. bio = NULL;
  30950. EVP_PKEY_free(pkey);
  30951. pkey = NULL;
  30952. #endif
  30953. #endif
  30954. return EXPECT_RESULT();
  30955. }
  30956. static int test_wolfSSL_PEM_PrivateKey(void)
  30957. {
  30958. EXPECT_DECLS;
  30959. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30960. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  30961. #ifndef NO_BIO
  30962. BIO* bio = NULL;
  30963. #endif
  30964. EVP_PKEY* pkey = NULL;
  30965. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30966. #ifndef NO_BIO
  30967. /* test creating new EVP_PKEY with bad arg */
  30968. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  30969. /* Test bad EVP_PKEY type. */
  30970. /* New HMAC EVP_PKEY */
  30971. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  30972. ExpectNotNull(pkey = EVP_PKEY_new());
  30973. if (pkey != NULL) {
  30974. pkey->type = EVP_PKEY_HMAC;
  30975. }
  30976. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30977. 0);
  30978. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30979. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30980. NULL), 0);
  30981. #endif
  30982. #ifdef WOLFSSL_KEY_GEN
  30983. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  30984. #endif
  30985. EVP_PKEY_free(pkey);
  30986. pkey = NULL;
  30987. BIO_free(bio);
  30988. bio = NULL;
  30989. /* key is DES encrypted */
  30990. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  30991. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  30992. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30993. {
  30994. XFILE f = XBADFILE;
  30995. wc_pem_password_cb* passwd_cb = NULL;
  30996. void* passwd_cb_userdata;
  30997. SSL_CTX* ctx = NULL;
  30998. char passwd[] = "bad password";
  30999. #ifndef WOLFSSL_NO_TLS12
  31000. #ifndef NO_WOLFSSL_SERVER
  31001. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31002. #else
  31003. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31004. #endif
  31005. #else
  31006. #ifndef NO_WOLFSSL_SERVER
  31007. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31008. #else
  31009. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31010. #endif
  31011. #endif
  31012. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31013. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31014. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31015. ExpectNull(passwd_cb_userdata =
  31016. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31017. /* fail case with password call back */
  31018. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31019. (void*)passwd));
  31020. BIO_free(bio);
  31021. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31022. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31023. (void*)passwd));
  31024. BIO_free(bio);
  31025. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31026. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31027. if ((bio == NULL) && (f != XBADFILE)) {
  31028. XFCLOSE(f);
  31029. }
  31030. /* use callback that works */
  31031. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31032. (void*)"yassl123"));
  31033. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31034. EVP_PKEY_free(pkey);
  31035. pkey = NULL;
  31036. BIO_free(bio);
  31037. bio = NULL;
  31038. SSL_CTX_free(ctx);
  31039. }
  31040. #endif /* !defined(NO_DES3) */
  31041. #endif /* !NO_BIO */
  31042. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31043. {
  31044. unsigned char buf[2048];
  31045. size_t bytes = 0;
  31046. XFILE f = XBADFILE;
  31047. SSL_CTX* ctx = NULL;
  31048. #ifndef WOLFSSL_NO_TLS12
  31049. #ifndef NO_WOLFSSL_SERVER
  31050. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31051. #else
  31052. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31053. #endif
  31054. #else
  31055. #ifndef NO_WOLFSSL_SERVER
  31056. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31057. #else
  31058. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31059. #endif
  31060. #endif
  31061. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31062. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31063. if (f != XBADFILE)
  31064. XFCLOSE(f);
  31065. server_key = buf;
  31066. pkey = NULL;
  31067. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  31068. ExpectNull(pkey);
  31069. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  31070. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31071. EVP_PKEY_free(pkey);
  31072. pkey = NULL;
  31073. SSL_CTX_free(ctx);
  31074. server_key = NULL;
  31075. }
  31076. #endif
  31077. #ifndef NO_BIO
  31078. (void)bio;
  31079. #endif
  31080. (void)pkey;
  31081. (void)server_key;
  31082. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31083. return EXPECT_RESULT();
  31084. }
  31085. static int test_wolfSSL_PEM_file_RSAKey(void)
  31086. {
  31087. EXPECT_DECLS;
  31088. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31089. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31090. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31091. RSA* rsa = NULL;
  31092. XFILE fp = XBADFILE;
  31093. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31094. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31095. if (fp != XBADFILE)
  31096. XFCLOSE(fp);
  31097. ExpectIntEQ(RSA_size(rsa), 256);
  31098. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31099. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31100. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31101. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31102. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31103. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31104. RSA_free(rsa);
  31105. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31106. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31107. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31108. return EXPECT_RESULT();
  31109. }
  31110. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31111. {
  31112. EXPECT_DECLS;
  31113. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31114. !defined(NO_FILESYSTEM) && \
  31115. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31116. RSA* rsa = NULL;
  31117. XFILE f = NULL;
  31118. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31119. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31120. ExpectIntEQ(RSA_size(rsa), 256);
  31121. if (f != XBADFILE) {
  31122. XFCLOSE(f);
  31123. f = XBADFILE;
  31124. }
  31125. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31126. NULL), WOLFSSL_FAILURE);
  31127. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31128. NULL), WOLFSSL_FAILURE);
  31129. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31130. WOLFSSL_SUCCESS);
  31131. RSA_free(rsa);
  31132. #ifdef HAVE_ECC
  31133. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31134. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31135. if (f != XBADFILE)
  31136. XFCLOSE(f);
  31137. #endif /* HAVE_ECC */
  31138. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31139. return EXPECT_RESULT();
  31140. }
  31141. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31142. {
  31143. EXPECT_DECLS;
  31144. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31145. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31146. XFILE file = XBADFILE;
  31147. const char* fname = "./certs/client-keyPub.pem";
  31148. RSA *rsa = NULL;
  31149. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31150. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31151. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31152. ExpectIntEQ(RSA_size(rsa), 256);
  31153. RSA_free(rsa);
  31154. if (file != XBADFILE)
  31155. XFCLOSE(file);
  31156. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31157. return EXPECT_RESULT();
  31158. }
  31159. #ifndef NO_BIO
  31160. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31161. {
  31162. EXPECT_DECLS;
  31163. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31164. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31165. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31166. RSA* rsa = NULL;
  31167. BIO* bio = NULL;
  31168. /* PrivateKey */
  31169. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31170. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31171. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31172. ExpectNotNull(rsa);
  31173. ExpectIntEQ(RSA_size(rsa), 256);
  31174. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31175. NULL), WOLFSSL_FAILURE);
  31176. BIO_free(bio);
  31177. bio = NULL;
  31178. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31179. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31180. NULL), WOLFSSL_SUCCESS);
  31181. BIO_free(bio);
  31182. bio = NULL;
  31183. RSA_free(rsa);
  31184. rsa = NULL;
  31185. /* PUBKEY */
  31186. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31187. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31188. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31189. ExpectIntEQ(RSA_size(rsa), 256);
  31190. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31191. BIO_free(bio);
  31192. bio = NULL;
  31193. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31194. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31195. BIO_free(bio);
  31196. bio = NULL;
  31197. RSA_free(rsa);
  31198. rsa = NULL;
  31199. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31200. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31201. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31202. BIO_free(bio);
  31203. bio = NULL;
  31204. RSA_free(rsa);
  31205. rsa = NULL;
  31206. #ifdef HAVE_ECC
  31207. /* ensure that non-rsa keys do not work */
  31208. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31209. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31210. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31211. BIO_free(bio);
  31212. bio = NULL;
  31213. RSA_free(rsa);
  31214. rsa = NULL;
  31215. #endif /* HAVE_ECC */
  31216. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31217. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31218. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31219. return EXPECT_RESULT();
  31220. }
  31221. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31222. {
  31223. EXPECT_DECLS;
  31224. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31225. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31226. RSA* rsa = NULL;
  31227. RSA* rsa_dup = NULL;
  31228. BIO* bio = NULL;
  31229. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31230. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31231. ExpectIntEQ(RSA_size(rsa), 256);
  31232. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31233. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31234. /* Test duplicating empty key. */
  31235. ExpectNotNull(rsa_dup = RSA_new());
  31236. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31237. RSA_free(rsa_dup);
  31238. rsa_dup = NULL;
  31239. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31240. ExpectPtrNE(rsa_dup, rsa);
  31241. #endif
  31242. /* test if valgrind complains about unreleased memory */
  31243. RSA_up_ref(rsa);
  31244. RSA_free(rsa);
  31245. BIO_free(bio);
  31246. bio = NULL;
  31247. RSA_free(rsa);
  31248. rsa = NULL;
  31249. RSA_free(rsa_dup);
  31250. rsa_dup = NULL;
  31251. #ifdef HAVE_ECC
  31252. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31253. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31254. BIO_free(bio);
  31255. #endif /* HAVE_ECC */
  31256. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31257. return EXPECT_RESULT();
  31258. }
  31259. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31260. {
  31261. EXPECT_DECLS;
  31262. #ifndef HAVE_SELFTEST
  31263. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31264. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31265. DSA* dsa = NULL;
  31266. BIO* bio = NULL;
  31267. /* PrivateKey */
  31268. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31269. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31270. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31271. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31272. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31273. NULL), WOLFSSL_FAILURE);
  31274. BIO_free(bio);
  31275. bio = NULL;
  31276. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31277. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31278. NULL), WOLFSSL_SUCCESS);
  31279. BIO_free(bio);
  31280. bio = NULL;
  31281. DSA_free(dsa);
  31282. dsa = NULL;
  31283. /* PUBKEY */
  31284. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31285. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31286. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31287. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31288. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31289. BIO_free(bio);
  31290. bio = NULL;
  31291. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31292. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31293. BIO_free(bio);
  31294. bio = NULL;
  31295. DSA_free(dsa);
  31296. dsa = NULL;
  31297. #ifdef HAVE_ECC
  31298. /* ensure that non-dsa keys do not work */
  31299. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31300. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31301. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31302. BIO_free(bio);
  31303. bio = NULL;
  31304. DSA_free(dsa);
  31305. dsa = NULL;
  31306. #endif /* HAVE_ECC */
  31307. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31308. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31309. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31310. #endif /* HAVE_SELFTEST */
  31311. return EXPECT_RESULT();
  31312. }
  31313. static int test_wolfSSL_PEM_bio_ECKey(void)
  31314. {
  31315. EXPECT_DECLS;
  31316. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31317. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31318. EC_KEY* ec = NULL;
  31319. EC_KEY* ec2;
  31320. BIO* bio = NULL;
  31321. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31322. unsigned char* pem = NULL;
  31323. int pLen;
  31324. #endif
  31325. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31326. "MAA=\n"
  31327. "-----END PUBLIC KEY-----";
  31328. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31329. "MAA=\n"
  31330. "-----END EC PRIVATE KEY-----";
  31331. /* PrivateKey */
  31332. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31333. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31334. ec2 = NULL;
  31335. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31336. ExpectIntEQ(ec == ec2, 1);
  31337. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31338. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31339. NULL), WOLFSSL_FAILURE);
  31340. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31341. NULL), WOLFSSL_FAILURE);
  31342. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31343. WOLFSSL_FAILURE);
  31344. BIO_free(bio);
  31345. bio = NULL;
  31346. /* Public key data - fail. */
  31347. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31348. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31349. BIO_free(bio);
  31350. bio = NULL;
  31351. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31352. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31353. NULL), WOLFSSL_SUCCESS);
  31354. BIO_free(bio);
  31355. bio = NULL;
  31356. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31357. NULL),WOLFSSL_FAILURE);
  31358. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31359. WOLFSSL_FAILURE);
  31360. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31361. WOLFSSL_FAILURE);
  31362. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31363. WOLFSSL_SUCCESS);
  31364. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31365. NULL), 0);
  31366. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31367. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31368. NULL), 0);
  31369. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31370. NULL), 0);
  31371. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31372. &pLen), 0);
  31373. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31374. &pLen), 0);
  31375. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31376. &pLen), 0);
  31377. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31378. NULL), 0);
  31379. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31380. &pLen), 1);
  31381. ExpectIntGT(pLen, 0);
  31382. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31383. #endif
  31384. EC_KEY_free(ec);
  31385. ec = NULL;
  31386. /* PUBKEY */
  31387. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31388. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31389. ec2 = NULL;
  31390. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31391. ExpectIntEQ(ec == ec2, 1);
  31392. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31393. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31394. BIO_free(bio);
  31395. bio = NULL;
  31396. /* Test 0x30, 0x00 fails. */
  31397. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31398. sizeof(ec_key_bad_1)));
  31399. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31400. BIO_free(bio);
  31401. bio = NULL;
  31402. /* Private key data - fail. */
  31403. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31404. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31405. BIO_free(bio);
  31406. bio = NULL;
  31407. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31408. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31409. BIO_free(bio);
  31410. bio = NULL;
  31411. /* Same test as above, but with a file pointer rather than a BIO. */
  31412. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31413. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31414. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31415. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31416. EC_KEY_free(ec);
  31417. ec = NULL;
  31418. #ifndef NO_RSA
  31419. /* ensure that non-ec keys do not work */
  31420. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31421. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31422. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31423. BIO_free(bio);
  31424. bio = NULL;
  31425. EC_KEY_free(ec);
  31426. ec = NULL;
  31427. #endif /* !NO_RSA */
  31428. /* Test 0x30, 0x00 fails. */
  31429. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31430. sizeof(ec_priv_key_bad_1)));
  31431. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31432. BIO_free(bio);
  31433. bio = NULL;
  31434. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31435. return EXPECT_RESULT();
  31436. }
  31437. static int test_wolfSSL_PEM_PUBKEY(void)
  31438. {
  31439. EXPECT_DECLS;
  31440. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31441. BIO* bio = NULL;
  31442. EVP_PKEY* pkey = NULL;
  31443. /* test creating new EVP_PKEY with bad arg */
  31444. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31445. /* test loading ECC key using BIO */
  31446. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31447. {
  31448. XFILE file = XBADFILE;
  31449. const char* fname = "./certs/ecc-client-keyPub.pem";
  31450. size_t sz = 0;
  31451. byte* buf = NULL;
  31452. EVP_PKEY* pkey2 = NULL;
  31453. EC_KEY* ec_key = NULL;
  31454. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31455. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31456. ExpectIntGT(sz = XFTELL(file), 0);
  31457. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31458. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31459. if (buf != NULL) {
  31460. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31461. }
  31462. if (file != XBADFILE) {
  31463. XFCLOSE(file);
  31464. }
  31465. /* Test using BIO new mem and loading PEM private key */
  31466. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31467. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31468. BIO_free(bio);
  31469. bio = NULL;
  31470. EVP_PKEY_free(pkey);
  31471. pkey = NULL;
  31472. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31473. ExpectNotNull(pkey = EVP_PKEY_new());
  31474. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31475. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31476. BIO_free(bio);
  31477. bio = NULL;
  31478. /* Qt unit test case*/
  31479. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31480. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31481. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31482. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31483. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31484. #else
  31485. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31486. #endif
  31487. EC_KEY_free(ec_key);
  31488. EVP_PKEY_free(pkey2);
  31489. EVP_PKEY_free(pkey);
  31490. pkey = NULL;
  31491. }
  31492. #endif
  31493. (void)bio;
  31494. (void)pkey;
  31495. #endif
  31496. return EXPECT_RESULT();
  31497. }
  31498. #endif /* !NO_BIO */
  31499. static int test_DSA_do_sign_verify(void)
  31500. {
  31501. EXPECT_DECLS;
  31502. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31503. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31504. !defined(NO_DSA)
  31505. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31506. DSA_SIG* sig = NULL;
  31507. DSA* dsa = NULL;
  31508. word32 bytes;
  31509. byte sigBin[DSA_SIG_SIZE];
  31510. int dsacheck;
  31511. #ifdef USE_CERT_BUFFERS_1024
  31512. byte tmp[ONEK_BUF];
  31513. XMEMSET(tmp, 0, sizeof(tmp));
  31514. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31515. bytes = sizeof_dsa_key_der_1024;
  31516. #elif defined(USE_CERT_BUFFERS_2048)
  31517. byte tmp[TWOK_BUF];
  31518. XMEMSET(tmp, 0, sizeof(tmp));
  31519. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31520. bytes = sizeof_dsa_key_der_2048;
  31521. #else
  31522. byte tmp[TWOK_BUF];
  31523. XFILE fp = XBADFILE;
  31524. XMEMSET(tmp, 0, sizeof(tmp));
  31525. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31526. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31527. if (fp != XBADFILE)
  31528. XFCLOSE(fp);
  31529. #endif /* END USE_CERT_BUFFERS_1024 */
  31530. XMEMSET(digest, 202, sizeof(digest));
  31531. ExpectNotNull(dsa = DSA_new());
  31532. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  31533. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31534. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31535. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31536. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31537. DSA_SIG_free(sig);
  31538. DSA_free(dsa);
  31539. #endif
  31540. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31541. return EXPECT_RESULT();
  31542. }
  31543. static int test_wolfSSL_tmp_dh(void)
  31544. {
  31545. EXPECT_DECLS;
  31546. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31547. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31548. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31549. byte buff[6000];
  31550. char file[] = "./certs/dsaparams.pem";
  31551. XFILE f = XBADFILE;
  31552. int bytes = 0;
  31553. DSA* dsa = NULL;
  31554. DH* dh = NULL;
  31555. #if defined(WOLFSSL_DH_EXTRA) && \
  31556. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31557. DH* dh2 = NULL;
  31558. #endif
  31559. BIO* bio = NULL;
  31560. SSL* ssl = NULL;
  31561. SSL_CTX* ctx = NULL;
  31562. #ifndef NO_WOLFSSL_SERVER
  31563. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31564. #else
  31565. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31566. #endif
  31567. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31568. WOLFSSL_FILETYPE_PEM));
  31569. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31570. WOLFSSL_FILETYPE_PEM));
  31571. ExpectNotNull(ssl = SSL_new(ctx));
  31572. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31573. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31574. if (f != XBADFILE)
  31575. XFCLOSE(f);
  31576. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31577. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31578. ExpectNotNull(dsa);
  31579. dh = wolfSSL_DSA_dup_DH(dsa);
  31580. ExpectNotNull(dh);
  31581. #if defined(WOLFSSL_DH_EXTRA) && \
  31582. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31583. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31584. #endif
  31585. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31586. #ifndef NO_WOLFSSL_SERVER
  31587. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31588. #else
  31589. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31590. #endif
  31591. BIO_free(bio);
  31592. DSA_free(dsa);
  31593. DH_free(dh);
  31594. dh = NULL;
  31595. #if defined(WOLFSSL_DH_EXTRA) && \
  31596. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31597. DH_free(dh2);
  31598. dh2 = NULL;
  31599. #endif
  31600. SSL_free(ssl);
  31601. SSL_CTX_free(ctx);
  31602. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31603. #endif
  31604. return EXPECT_RESULT();
  31605. }
  31606. static int test_wolfSSL_ctrl(void)
  31607. {
  31608. EXPECT_DECLS;
  31609. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31610. byte buff[6000];
  31611. BIO* bio = NULL;
  31612. int bytes;
  31613. BUF_MEM* ptr = NULL;
  31614. XMEMSET(buff, 0, sizeof(buff));
  31615. bytes = sizeof(buff);
  31616. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31617. ExpectNotNull(BIO_s_socket());
  31618. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31619. /* needs tested after stubs filled out @TODO
  31620. SSL_ctrl
  31621. SSL_CTX_ctrl
  31622. */
  31623. BIO_free(bio);
  31624. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31625. return EXPECT_RESULT();
  31626. }
  31627. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31628. {
  31629. EXPECT_DECLS;
  31630. #ifdef OPENSSL_EXTRA
  31631. static const unsigned char pw[] = "password";
  31632. static const int pwSz = sizeof(pw) - 1;
  31633. size_t checkPwSz = 0;
  31634. const unsigned char* checkPw = NULL;
  31635. WOLFSSL_EVP_PKEY* key = NULL;
  31636. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31637. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31638. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31639. pwSz));
  31640. if (key != NULL) {
  31641. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31642. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31643. ExpectIntEQ(key->pkey_sz, pwSz);
  31644. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31645. }
  31646. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31647. ExpectIntEQ((int)checkPwSz, pwSz);
  31648. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31649. wolfSSL_EVP_PKEY_free(key);
  31650. key = NULL;
  31651. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31652. 0));
  31653. ExpectIntEQ(key->pkey_sz, 0);
  31654. if (EXPECT_SUCCESS()) {
  31655. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31656. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31657. }
  31658. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31659. ExpectIntEQ((int)checkPwSz, 0);
  31660. wolfSSL_EVP_PKEY_free(key);
  31661. key = NULL;
  31662. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31663. 0));
  31664. ExpectIntEQ(key->pkey_sz, 0);
  31665. if (EXPECT_SUCCESS()) {
  31666. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31667. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31668. }
  31669. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31670. ExpectIntEQ((int)checkPwSz, 0);
  31671. wolfSSL_EVP_PKEY_free(key);
  31672. key = NULL;
  31673. #endif /* OPENSSL_EXTRA */
  31674. return EXPECT_RESULT();
  31675. }
  31676. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31677. {
  31678. EXPECT_DECLS;
  31679. #ifdef OPENSSL_EXTRA
  31680. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31681. const char *priv = "ABCDEFGHIJKLMNOP";
  31682. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31683. WOLFSSL_EVP_PKEY* key = NULL;
  31684. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31685. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31686. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31687. NULL, (const unsigned char *)priv, 0, cipher));
  31688. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31689. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31690. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31691. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31692. wolfSSL_EVP_PKEY_free(key);
  31693. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31694. #endif /* OPENSSL_EXTRA */
  31695. return EXPECT_RESULT();
  31696. }
  31697. static int test_wolfSSL_EVP_Digest(void)
  31698. {
  31699. EXPECT_DECLS;
  31700. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31701. const char* in = "abc";
  31702. int inLen = (int)XSTRLEN(in);
  31703. byte out[WC_SHA256_DIGEST_SIZE];
  31704. unsigned int outLen;
  31705. const char* expOut =
  31706. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31707. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31708. "\x15\xAD";
  31709. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31710. "SHA256", NULL), 1);
  31711. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31712. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31713. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31714. return EXPECT_RESULT();
  31715. }
  31716. static int test_wolfSSL_EVP_Digest_all(void)
  31717. {
  31718. EXPECT_DECLS;
  31719. #ifdef OPENSSL_EXTRA
  31720. const char* digests[] = {
  31721. #ifndef NO_MD5
  31722. "MD5",
  31723. #endif
  31724. #ifndef NO_SHA
  31725. "SHA",
  31726. #endif
  31727. #ifdef WOLFSSL_SHA224
  31728. "SHA224",
  31729. #endif
  31730. #ifndef NO_SHA256
  31731. "SHA256",
  31732. #endif
  31733. #ifdef WOLFSSL_SHA384
  31734. "SHA384",
  31735. #endif
  31736. #ifdef WOLFSSL_SHA512
  31737. "SHA512",
  31738. #endif
  31739. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31740. "SHA512_224",
  31741. #endif
  31742. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31743. "SHA512_256",
  31744. #endif
  31745. #ifdef WOLFSSL_SHA3
  31746. #ifndef WOLFSSL_NOSHA3_224
  31747. "SHA3_224",
  31748. #endif
  31749. #ifndef WOLFSSL_NOSHA3_256
  31750. "SHA3_256",
  31751. #endif
  31752. "SHA3_384",
  31753. #ifndef WOLFSSL_NOSHA3_512
  31754. "SHA3_512",
  31755. #endif
  31756. #endif /* WOLFSSL_SHA3 */
  31757. NULL
  31758. };
  31759. const char** d;
  31760. const unsigned char in[] = "abc";
  31761. int inLen = XSTR_SIZEOF(in);
  31762. byte out[WC_MAX_DIGEST_SIZE];
  31763. unsigned int outLen;
  31764. for (d = digests; *d != NULL; d++) {
  31765. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31766. ExpectIntGT(outLen, 0);
  31767. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31768. }
  31769. #endif
  31770. return EXPECT_RESULT();
  31771. }
  31772. static int test_wolfSSL_EVP_MD_size(void)
  31773. {
  31774. EXPECT_DECLS;
  31775. #ifdef OPENSSL_EXTRA
  31776. WOLFSSL_EVP_MD_CTX mdCtx;
  31777. #ifdef WOLFSSL_SHA3
  31778. #ifndef WOLFSSL_NOSHA3_224
  31779. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31780. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31781. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31782. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31783. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31784. #endif
  31785. #ifndef WOLFSSL_NOSHA3_256
  31786. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31787. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31788. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31789. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31790. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31791. #endif
  31792. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31793. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31794. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31795. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31796. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31797. #ifndef WOLFSSL_NOSHA3_512
  31798. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31799. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31800. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31801. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31802. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31803. #endif
  31804. #endif /* WOLFSSL_SHA3 */
  31805. #ifndef NO_SHA256
  31806. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31807. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31808. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31809. WC_SHA256_DIGEST_SIZE);
  31810. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31811. WC_SHA256_BLOCK_SIZE);
  31812. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31813. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31814. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31815. #endif
  31816. #ifndef NO_MD5
  31817. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31818. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31819. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31820. WC_MD5_DIGEST_SIZE);
  31821. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31822. WC_MD5_BLOCK_SIZE);
  31823. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31824. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31825. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31826. #endif
  31827. #ifdef WOLFSSL_SHA224
  31828. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31829. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31830. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31831. WC_SHA224_DIGEST_SIZE);
  31832. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31833. WC_SHA224_BLOCK_SIZE);
  31834. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31835. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31836. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31837. #endif
  31838. #ifdef WOLFSSL_SHA384
  31839. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31840. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31841. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31842. WC_SHA384_DIGEST_SIZE);
  31843. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31844. WC_SHA384_BLOCK_SIZE);
  31845. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31846. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31847. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31848. #endif
  31849. #ifdef WOLFSSL_SHA512
  31850. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31851. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31852. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31853. WC_SHA512_DIGEST_SIZE);
  31854. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31855. WC_SHA512_BLOCK_SIZE);
  31856. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31857. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31858. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31859. #endif
  31860. #ifndef NO_SHA
  31861. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31862. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31863. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31864. WC_SHA_DIGEST_SIZE);
  31865. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31866. WC_SHA_BLOCK_SIZE);
  31867. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31868. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31869. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31870. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31871. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31872. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31873. WC_SHA_DIGEST_SIZE);
  31874. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31875. WC_SHA_BLOCK_SIZE);
  31876. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31877. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31878. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31879. #endif
  31880. /* error case */
  31881. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31882. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31883. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31884. BAD_FUNC_ARG);
  31885. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31886. /* Cleanup is valid on uninit'ed struct */
  31887. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31888. #endif /* OPENSSL_EXTRA */
  31889. return EXPECT_RESULT();
  31890. }
  31891. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31892. {
  31893. EXPECT_DECLS;
  31894. #ifdef OPENSSL_EXTRA
  31895. const WOLFSSL_EVP_MD* md;
  31896. #ifndef NO_MD5
  31897. ExpectNotNull(md = EVP_md5());
  31898. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31899. #endif
  31900. #ifndef NO_SHA
  31901. ExpectNotNull(md = EVP_sha1());
  31902. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31903. #endif
  31904. #ifdef WOLFSSL_SHA224
  31905. ExpectNotNull(md = EVP_sha224());
  31906. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31907. #endif
  31908. ExpectNotNull(md = EVP_sha256());
  31909. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31910. #ifdef WOLFSSL_SHA384
  31911. ExpectNotNull(md = EVP_sha384());
  31912. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31913. #endif
  31914. #ifdef WOLFSSL_SHA512
  31915. ExpectNotNull(md = EVP_sha512());
  31916. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31917. #endif
  31918. #endif
  31919. return EXPECT_RESULT();
  31920. }
  31921. #ifdef OPENSSL_EXTRA
  31922. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  31923. size_t testKeySz, const char* testData, size_t testDataSz,
  31924. const byte* testResult, size_t testResultSz)
  31925. {
  31926. EXPECT_DECLS;
  31927. unsigned char check[WC_MAX_DIGEST_SIZE];
  31928. size_t checkSz = -1;
  31929. WOLFSSL_EVP_PKEY* key = NULL;
  31930. WOLFSSL_EVP_MD_CTX mdCtx;
  31931. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31932. testKey, (int)testKeySz));
  31933. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31934. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31935. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31936. (unsigned int)testDataSz), 1);
  31937. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31938. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31939. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31940. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31941. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31942. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31943. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31944. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31945. (unsigned int)testDataSz), 1);
  31946. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31947. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31948. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31949. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31950. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31951. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31952. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31953. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31954. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31955. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31956. (unsigned int)testDataSz - 4), 1);
  31957. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31958. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31959. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31960. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31961. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31962. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31963. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31964. (unsigned int)testDataSz - 4), 1);
  31965. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31966. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31967. wolfSSL_EVP_PKEY_free(key);
  31968. return EXPECT_RESULT();
  31969. }
  31970. #endif
  31971. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  31972. {
  31973. EXPECT_DECLS;
  31974. #ifdef OPENSSL_EXTRA
  31975. static const unsigned char testKey[] =
  31976. {
  31977. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31978. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31979. 0x0b, 0x0b, 0x0b, 0x0b
  31980. };
  31981. static const char testData[] = "Hi There";
  31982. #ifdef WOLFSSL_SHA224
  31983. static const unsigned char testResultSha224[] =
  31984. {
  31985. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  31986. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  31987. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  31988. 0x53, 0x68, 0x4b, 0x22
  31989. };
  31990. #endif
  31991. #ifndef NO_SHA256
  31992. static const unsigned char testResultSha256[] =
  31993. {
  31994. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  31995. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  31996. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  31997. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  31998. };
  31999. #endif
  32000. #ifdef WOLFSSL_SHA384
  32001. static const unsigned char testResultSha384[] =
  32002. {
  32003. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32004. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32005. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32006. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32007. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32008. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32009. };
  32010. #endif
  32011. #ifdef WOLFSSL_SHA512
  32012. static const unsigned char testResultSha512[] =
  32013. {
  32014. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32015. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32016. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32017. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32018. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32019. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32020. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32021. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32022. };
  32023. #endif
  32024. #ifdef WOLFSSL_SHA3
  32025. #ifndef WOLFSSL_NOSHA3_224
  32026. static const unsigned char testResultSha3_224[] =
  32027. {
  32028. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32029. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32030. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32031. 0xf3, 0xc8, 0x60, 0xf7
  32032. };
  32033. #endif
  32034. #ifndef WOLFSSL_NOSHA3_256
  32035. static const unsigned char testResultSha3_256[] =
  32036. {
  32037. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32038. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32039. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32040. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32041. };
  32042. #endif
  32043. #ifndef WOLFSSL_NOSHA3_384
  32044. static const unsigned char testResultSha3_384[] =
  32045. {
  32046. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32047. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32048. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32049. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32050. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32051. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32052. };
  32053. #endif
  32054. #ifndef WOLFSSL_NOSHA3_512
  32055. static const unsigned char testResultSha3_512[] =
  32056. {
  32057. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32058. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32059. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32060. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32061. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32062. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32063. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32064. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32065. };
  32066. #endif
  32067. #endif
  32068. #ifndef NO_SHA256
  32069. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32070. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32071. sizeof(testResultSha256)), TEST_SUCCESS);
  32072. #endif
  32073. #ifdef WOLFSSL_SHA224
  32074. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32075. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32076. sizeof(testResultSha224)), TEST_SUCCESS);
  32077. #endif
  32078. #ifdef WOLFSSL_SHA384
  32079. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32080. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32081. sizeof(testResultSha384)), TEST_SUCCESS);
  32082. #endif
  32083. #ifdef WOLFSSL_SHA512
  32084. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32085. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32086. sizeof(testResultSha512)), TEST_SUCCESS);
  32087. #endif
  32088. #ifdef WOLFSSL_SHA3
  32089. #ifndef WOLFSSL_NOSHA3_224
  32090. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32091. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32092. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32093. #endif
  32094. #ifndef WOLFSSL_NOSHA3_256
  32095. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32096. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32097. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32098. #endif
  32099. #ifndef WOLFSSL_NOSHA3_384
  32100. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32101. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32102. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32103. #endif
  32104. #ifndef WOLFSSL_NOSHA3_512
  32105. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32106. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32107. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32108. #endif
  32109. #endif
  32110. #endif /* OPENSSL_EXTRA */
  32111. return EXPECT_RESULT();
  32112. }
  32113. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32114. {
  32115. EXPECT_DECLS;
  32116. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32117. WOLFSSL_EVP_PKEY* privKey = NULL;
  32118. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32119. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32120. const char testData[] = "Hi There";
  32121. WOLFSSL_EVP_MD_CTX mdCtx;
  32122. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32123. int ret;
  32124. size_t checkSz = -1;
  32125. int sz = 2048 / 8;
  32126. const unsigned char* cp;
  32127. const unsigned char* p;
  32128. unsigned char check[2048/8];
  32129. size_t i;
  32130. int paddings[] = {
  32131. RSA_PKCS1_PADDING,
  32132. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32133. RSA_PKCS1_PSS_PADDING,
  32134. #endif
  32135. };
  32136. cp = client_key_der_2048;
  32137. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32138. sizeof_client_key_der_2048)));
  32139. p = client_keypub_der_2048;
  32140. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32141. sizeof_client_keypub_der_2048)));
  32142. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32143. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32144. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32145. NULL, privKey), 1);
  32146. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32147. (unsigned int)XSTRLEN(testData)), 1);
  32148. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32149. ExpectIntEQ((int)checkSz, sz);
  32150. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32151. ExpectIntEQ((int)checkSz,sz);
  32152. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32153. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32154. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32155. ExpectIntEQ(ret, 1);
  32156. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32157. ExpectIntEQ(ret, 1);
  32158. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32159. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32160. NULL, pubKey), 1);
  32161. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32162. (unsigned int)XSTRLEN(testData)),
  32163. 1);
  32164. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32165. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32166. ExpectIntEQ(ret, 1);
  32167. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32168. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32169. NULL, privKey), 1);
  32170. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32171. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32172. ExpectIntEQ((int)checkSz, sz);
  32173. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32174. ExpectIntEQ((int)checkSz, sz);
  32175. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32176. (unsigned int)XSTRLEN(testData) - 4), 1);
  32177. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32178. ExpectIntEQ((int)checkSz, sz);
  32179. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32180. ExpectIntEQ(ret, 1);
  32181. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32182. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32183. NULL, pubKey), 1);
  32184. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32185. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32186. (unsigned int)XSTRLEN(testData) - 4),
  32187. 1);
  32188. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32189. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32190. ExpectIntEQ(ret, 1);
  32191. /* Check all signing padding types */
  32192. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32193. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32194. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32195. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32196. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32197. paddings[i]), 1);
  32198. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32199. (unsigned int)XSTRLEN(testData)), 1);
  32200. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32201. ExpectIntEQ((int)checkSz, sz);
  32202. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32203. ExpectIntEQ((int)checkSz,sz);
  32204. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32205. ExpectIntEQ(ret, 1);
  32206. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32207. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32208. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32209. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32210. paddings[i]), 1);
  32211. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32212. (unsigned int)XSTRLEN(testData)), 1);
  32213. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32214. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32215. ExpectIntEQ(ret, 1);
  32216. }
  32217. wolfSSL_EVP_PKEY_free(pubKey);
  32218. wolfSSL_EVP_PKEY_free(privKey);
  32219. #endif
  32220. return EXPECT_RESULT();
  32221. }
  32222. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32223. {
  32224. EXPECT_DECLS;
  32225. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32226. WOLFSSL_EVP_PKEY* privKey = NULL;
  32227. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32228. const char testData[] = "Hi There";
  32229. WOLFSSL_EVP_MD_CTX mdCtx;
  32230. int ret;
  32231. size_t checkSz = -1;
  32232. const unsigned char* cp;
  32233. const unsigned char* p;
  32234. unsigned char check[2048/8];
  32235. cp = ecc_clikey_der_256;
  32236. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32237. sizeof_ecc_clikey_der_256));
  32238. p = ecc_clikeypub_der_256;
  32239. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32240. sizeof_ecc_clikeypub_der_256)));
  32241. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32242. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32243. NULL, privKey), 1);
  32244. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32245. (unsigned int)XSTRLEN(testData)), 1);
  32246. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32247. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32248. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32249. ExpectIntEQ(ret, 1);
  32250. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32251. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32252. NULL, pubKey), 1);
  32253. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32254. (unsigned int)XSTRLEN(testData)),
  32255. 1);
  32256. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32257. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32258. ExpectIntEQ(ret, 1);
  32259. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32260. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32261. NULL, privKey), 1);
  32262. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32263. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32264. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32265. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32266. (unsigned int)XSTRLEN(testData) - 4), 1);
  32267. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32268. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32269. ExpectIntEQ(ret, 1);
  32270. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32271. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32272. NULL, pubKey), 1);
  32273. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32274. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32275. (unsigned int)XSTRLEN(testData) - 4),
  32276. 1);
  32277. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32278. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32279. ExpectIntEQ(ret, 1);
  32280. wolfSSL_EVP_PKEY_free(pubKey);
  32281. wolfSSL_EVP_PKEY_free(privKey);
  32282. #endif
  32283. return EXPECT_RESULT();
  32284. }
  32285. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32286. {
  32287. EXPECT_DECLS;
  32288. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32289. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32290. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32291. char caFile[] = "./certs/client-ca.pem";
  32292. char clientFile[] = "./certs/client-cert.pem";
  32293. SSL_CTX* ctx = NULL;
  32294. X509* x509 = NULL;
  32295. BIO *bio = NULL;
  32296. X509 *cert = NULL;
  32297. X509 *ca = NULL;
  32298. STACK_OF(X509) *chain = NULL;
  32299. STACK_OF(X509) *chain2 = NULL;
  32300. #ifndef NO_WOLFSSL_SERVER
  32301. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32302. #else
  32303. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32304. #endif
  32305. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32306. WOLFSSL_FILETYPE_PEM));
  32307. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32308. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32309. WOLFSSL_FILETYPE_PEM));
  32310. /* additional test of getting EVP_PKEY key size from X509
  32311. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32312. * allowed with user RSA */
  32313. {
  32314. EVP_PKEY* pkey = NULL;
  32315. #if defined(HAVE_ECC)
  32316. X509* ecX509 = NULL;
  32317. #endif /* HAVE_ECC */
  32318. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32319. /* current RSA key is 2048 bit (256 bytes) */
  32320. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32321. EVP_PKEY_free(pkey);
  32322. pkey = NULL;
  32323. #if defined(HAVE_ECC)
  32324. #if defined(USE_CERT_BUFFERS_256)
  32325. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32326. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32327. SSL_FILETYPE_ASN1));
  32328. #else
  32329. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32330. cliEccCertFile, SSL_FILETYPE_PEM));
  32331. #endif
  32332. pkey = X509_get_pubkey(ecX509);
  32333. ExpectNotNull(pkey);
  32334. /* current ECC key is 256 bit (32 bytes) */
  32335. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32336. X509_free(ecX509);
  32337. ecX509 = NULL;
  32338. EVP_PKEY_free(pkey);
  32339. pkey = NULL;
  32340. #endif /* HAVE_ECC */
  32341. }
  32342. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32343. if (EXPECT_SUCCESS()) {
  32344. x509 = NULL;
  32345. }
  32346. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32347. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32348. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32349. #endif
  32350. SSL_CTX_free(ctx);
  32351. ctx = NULL;
  32352. #ifndef NO_WOLFSSL_SERVER
  32353. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32354. #else
  32355. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32356. #endif
  32357. /* Test haproxy use case */
  32358. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32359. /* Read Certificate */
  32360. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32361. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32362. ExpectNotNull(chain = sk_X509_new_null());
  32363. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32364. if (EXPECT_SUCCESS()) {
  32365. ca = NULL;
  32366. }
  32367. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32368. ExpectNotNull(ca = sk_X509_shift(chain2));
  32369. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32370. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32371. if (EXPECT_SUCCESS()) {
  32372. ca = NULL;
  32373. }
  32374. BIO_free(bio);
  32375. X509_free(cert);
  32376. X509_free(ca);
  32377. X509_free(x509);
  32378. sk_X509_pop_free(chain, X509_free);
  32379. sk_X509_pop_free(chain2, X509_free);
  32380. SSL_CTX_free(ctx);
  32381. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32382. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32383. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32384. return EXPECT_RESULT();
  32385. }
  32386. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32387. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32388. {
  32389. EXPECT_DECLS;
  32390. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32391. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32392. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32393. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32394. callback_functions client_cb;
  32395. callback_functions server_cb;
  32396. int line = 0;
  32397. int flag = ERR_TXT_STRING;
  32398. const char* file = NULL;
  32399. const char* data = NULL;
  32400. /* create a failed connection and inspect the error */
  32401. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32402. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32403. client_cb.method = wolfTLSv1_1_client_method;
  32404. server_cb.method = wolfTLSv1_2_server_method;
  32405. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32406. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32407. ExpectNotNull(data);
  32408. /* check clearing error state */
  32409. ERR_remove_state(0);
  32410. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32411. ERR_peek_last_error_line(NULL, &line);
  32412. ExpectIntEQ(line, 0);
  32413. ERR_peek_last_error_line(&file, NULL);
  32414. ExpectNull(file);
  32415. /* retry connection to fill error queue */
  32416. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32417. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32418. client_cb.method = wolfTLSv1_1_client_method;
  32419. server_cb.method = wolfTLSv1_2_server_method;
  32420. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32421. /* check that error code was stored */
  32422. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32423. ERR_peek_last_error_line(NULL, &line);
  32424. ExpectIntNE(line, 0);
  32425. ERR_peek_last_error_line(&file, NULL);
  32426. ExpectNotNull(file);
  32427. fprintf(stderr, "\nTesting error print out\n");
  32428. ERR_print_errors_fp(stderr);
  32429. fprintf(stderr, "Done testing print out\n\n");
  32430. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32431. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32432. return EXPECT_RESULT();
  32433. }
  32434. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32435. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32436. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32437. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32438. {
  32439. (void) ok;
  32440. (void) ctx;
  32441. fprintf(stderr, "ENTER verify_cb\n");
  32442. return SSL_SUCCESS;
  32443. }
  32444. #endif
  32445. static int test_wolfSSL_X509_Name_canon(void)
  32446. {
  32447. EXPECT_DECLS;
  32448. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32449. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32450. defined(WOLFSSL_CERT_GEN) && \
  32451. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32452. const long ex_hash1 = 0x0fdb2da4;
  32453. const long ex_hash2 = 0x9f3e8c9e;
  32454. X509_NAME *name = NULL;
  32455. X509 *x509 = NULL;
  32456. XFILE file = XBADFILE;
  32457. unsigned long hash = 0;
  32458. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32459. byte *pbuf = NULL;
  32460. word32 len = 0;
  32461. (void) ex_hash2;
  32462. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32463. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32464. ExpectNotNull(name = X509_get_issuer_name(x509));
  32465. /* When output buffer is NULL, should return necessary output buffer
  32466. * length.*/
  32467. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32468. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32469. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32470. hash = (((unsigned long)digest[3] << 24) |
  32471. ((unsigned long)digest[2] << 16) |
  32472. ((unsigned long)digest[1] << 8) |
  32473. ((unsigned long)digest[0]));
  32474. ExpectIntEQ(hash, ex_hash1);
  32475. if (file != XBADFILE) {
  32476. XFCLOSE(file);
  32477. file = XBADFILE;
  32478. }
  32479. X509_free(x509);
  32480. x509 = NULL;
  32481. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32482. pbuf = NULL;
  32483. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32484. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32485. ExpectNotNull(name = X509_get_issuer_name(x509));
  32486. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32487. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32488. hash = (((unsigned long)digest[3] << 24) |
  32489. ((unsigned long)digest[2] << 16) |
  32490. ((unsigned long)digest[1] << 8) |
  32491. ((unsigned long)digest[0]));
  32492. ExpectIntEQ(hash, ex_hash2);
  32493. if (file != XBADFILE)
  32494. XFCLOSE(file);
  32495. X509_free(x509);
  32496. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32497. #endif
  32498. return EXPECT_RESULT();
  32499. }
  32500. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32501. {
  32502. EXPECT_DECLS;
  32503. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32504. const int MAX_DIR = 4;
  32505. const char paths[][32] = {
  32506. "./certs/ed25519",
  32507. "./certs/ecc",
  32508. "./certs/crl",
  32509. "./certs/",
  32510. };
  32511. char CertCrl_path[MAX_FILENAME_SZ];
  32512. char *p;
  32513. X509_STORE* str = NULL;
  32514. X509_LOOKUP* lookup = NULL;
  32515. WOLFSSL_STACK* sk = NULL;
  32516. int len, total_len, i;
  32517. (void)sk;
  32518. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32519. /* illegal string */
  32520. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32521. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32522. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32523. SSL_FILETYPE_PEM,NULL), 0);
  32524. /* free store */
  32525. X509_STORE_free(str);
  32526. str = NULL;
  32527. /* short folder string */
  32528. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32529. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32530. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32531. SSL_FILETYPE_PEM,NULL), 1);
  32532. #if defined(WOLFSSL_INT_H)
  32533. /* only available when including internal.h */
  32534. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32535. #endif
  32536. /* free store */
  32537. X509_STORE_free(str);
  32538. str = NULL;
  32539. /* typical function check */
  32540. p = &CertCrl_path[0];
  32541. total_len = 0;
  32542. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32543. len = (int)XSTRLEN((const char*)&paths[i]);
  32544. total_len += len;
  32545. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32546. p += len;
  32547. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32548. }
  32549. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32550. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32551. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32552. SSL_FILETYPE_PEM,NULL), 1);
  32553. #if defined(WOLFSSL_INT_H)
  32554. /* only available when including internal.h */
  32555. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32556. #endif
  32557. X509_STORE_free(str);
  32558. #endif
  32559. return EXPECT_RESULT();
  32560. }
  32561. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32562. {
  32563. EXPECT_DECLS;
  32564. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32565. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32566. defined(WOLFSSL_SIGNER_DER_CERT)
  32567. X509_STORE_CTX* ctx = NULL;
  32568. X509_STORE* str = NULL;
  32569. X509_LOOKUP* lookup = NULL;
  32570. X509* cert1 = NULL;
  32571. X509* x509Ca = NULL;
  32572. X509* x509Svr = NULL;
  32573. X509* issuer = NULL;
  32574. WOLFSSL_STACK* sk = NULL;
  32575. X509_NAME* caName = NULL;
  32576. X509_NAME* issuerName = NULL;
  32577. XFILE file1 = XBADFILE;
  32578. int i;
  32579. int cert_count = 0;
  32580. int cmp;
  32581. char der[] = "certs/ca-cert.der";
  32582. #ifdef HAVE_CRL
  32583. char pem[][100] = {
  32584. "./certs/crl/crl.pem",
  32585. "./certs/crl/crl2.pem",
  32586. "./certs/crl/caEccCrl.pem",
  32587. "./certs/crl/eccCliCRL.pem",
  32588. "./certs/crl/eccSrvCRL.pem",
  32589. ""
  32590. };
  32591. #endif
  32592. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32593. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32594. if (file1 != XBADFILE)
  32595. XFCLOSE(file1);
  32596. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32597. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32598. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32599. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32600. SSL_FILETYPE_PEM,NULL), 1);
  32601. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32602. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32603. /* check if CA cert is loaded into the store */
  32604. for (i = 0; i < cert_count; i++) {
  32605. x509Ca = sk_X509_value(sk, i);
  32606. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32607. }
  32608. ExpectNotNull((x509Svr =
  32609. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32610. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32611. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32612. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32613. ExpectNotNull(issuer);
  32614. caName = X509_get_subject_name(x509Ca);
  32615. ExpectNotNull(caName);
  32616. issuerName = X509_get_subject_name(issuer);
  32617. ExpectNotNull(issuerName);
  32618. cmp = X509_NAME_cmp(caName, issuerName);
  32619. ExpectIntEQ(cmp, 0);
  32620. /* load der format */
  32621. X509_free(issuer);
  32622. issuer = NULL;
  32623. X509_STORE_CTX_free(ctx);
  32624. ctx = NULL;
  32625. X509_STORE_free(str);
  32626. str = NULL;
  32627. sk_X509_pop_free(sk, NULL);
  32628. sk = NULL;
  32629. X509_free(x509Svr);
  32630. x509Svr = NULL;
  32631. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32632. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32633. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32634. SSL_FILETYPE_ASN1,NULL), 1);
  32635. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32636. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32637. /* check if CA cert is loaded into the store */
  32638. for (i = 0; i < cert_count; i++) {
  32639. x509Ca = sk_X509_value(sk, i);
  32640. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32641. }
  32642. X509_STORE_free(str);
  32643. str = NULL;
  32644. sk_X509_pop_free(sk, NULL);
  32645. sk = NULL;
  32646. X509_free(cert1);
  32647. cert1 = NULL;
  32648. #ifdef HAVE_CRL
  32649. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32650. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32651. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32652. SSL_FILETYPE_PEM,NULL), 1);
  32653. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32654. "certs/server-revoked-cert.pem",
  32655. SSL_FILETYPE_PEM,NULL), 1);
  32656. if (str) {
  32657. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32658. WOLFSSL_FILETYPE_PEM), 1);
  32659. /* since store hasn't yet known the revoked cert*/
  32660. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32661. "certs/server-revoked-cert.pem",
  32662. WOLFSSL_FILETYPE_PEM), 1);
  32663. }
  32664. for (i = 0; pem[i][0] != '\0'; i++)
  32665. {
  32666. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32667. SSL_FILETYPE_PEM, NULL), 1);
  32668. }
  32669. if (str) {
  32670. /* since store knows crl list */
  32671. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32672. "certs/server-revoked-cert.pem",
  32673. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32674. }
  32675. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32676. X509_STORE_free(str);
  32677. #endif
  32678. #endif
  32679. return EXPECT_RESULT();
  32680. }
  32681. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32682. {
  32683. int res = TEST_SKIPPED;
  32684. #if defined(OPENSSL_EXTRA)
  32685. X509_STORE_CTX_cleanup(NULL);
  32686. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32687. res = TEST_SUCCESS;
  32688. #endif
  32689. return res;
  32690. }
  32691. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32692. {
  32693. EXPECT_DECLS;
  32694. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32695. X509_STORE_CTX* ctx = NULL;
  32696. X509_STORE* str = NULL;
  32697. X509* x509Ca = NULL;
  32698. X509* x509Svr = NULL;
  32699. X509* issuer = NULL;
  32700. X509_NAME* caName = NULL;
  32701. X509_NAME* issuerName = NULL;
  32702. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32703. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32704. ExpectNotNull((x509Ca =
  32705. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32706. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32707. ExpectNotNull((x509Svr =
  32708. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32709. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32710. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32711. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32712. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32713. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32714. #ifdef WOLFSSL_SIGNER_DER_CERT
  32715. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32716. #endif
  32717. X509_free(issuer);
  32718. X509_STORE_CTX_free(ctx);
  32719. X509_free(x509Svr);
  32720. X509_STORE_free(str);
  32721. X509_free(x509Ca);
  32722. #endif
  32723. return EXPECT_RESULT();
  32724. }
  32725. static int test_wolfSSL_PKCS7_certs(void)
  32726. {
  32727. EXPECT_DECLS;
  32728. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32729. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32730. STACK_OF(X509)* sk = NULL;
  32731. STACK_OF(X509_INFO)* info_sk = NULL;
  32732. PKCS7 *p7 = NULL;
  32733. BIO* bio = NULL;
  32734. const byte* p = NULL;
  32735. int buflen = 0;
  32736. int i;
  32737. /* Test twice. Once with d2i and once without to test
  32738. * that everything is free'd correctly. */
  32739. for (i = 0; i < 2; i++) {
  32740. ExpectNotNull(p7 = PKCS7_new());
  32741. if (p7 != NULL) {
  32742. p7->version = 1;
  32743. #ifdef NO_SHA
  32744. p7->hashOID = SHA256h;
  32745. #else
  32746. p7->hashOID = SHAh;
  32747. #endif
  32748. }
  32749. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32750. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32751. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32752. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32753. ExpectNotNull(sk = sk_X509_new_null());
  32754. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32755. X509_INFO* info = NULL;
  32756. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32757. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32758. if (EXPECT_SUCCESS() && (info != NULL)) {
  32759. info->x509 = NULL;
  32760. }
  32761. X509_INFO_free(info);
  32762. }
  32763. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32764. info_sk = NULL;
  32765. BIO_free(bio);
  32766. bio = NULL;
  32767. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32768. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32769. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32770. sk_X509_pop_free(sk, X509_free);
  32771. }
  32772. sk = NULL;
  32773. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32774. if (i == 0) {
  32775. PKCS7_free(p7);
  32776. p7 = NULL;
  32777. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32778. if (p7 != NULL) {
  32779. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32780. * them */
  32781. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32782. }
  32783. /* PKCS7_free free's the certs */
  32784. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32785. }
  32786. BIO_free(bio);
  32787. bio = NULL;
  32788. PKCS7_free(p7);
  32789. p7 = NULL;
  32790. }
  32791. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32792. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32793. return EXPECT_RESULT();
  32794. }
  32795. static int test_wolfSSL_X509_STORE_CTX(void)
  32796. {
  32797. EXPECT_DECLS;
  32798. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32799. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32800. X509_STORE_CTX* ctx = NULL;
  32801. X509_STORE* str = NULL;
  32802. X509* x509 = NULL;
  32803. #ifdef OPENSSL_ALL
  32804. X509* x5092 = NULL;
  32805. STACK_OF(X509) *sk = NULL;
  32806. STACK_OF(X509) *sk2 = NULL;
  32807. STACK_OF(X509) *sk3 = NULL;
  32808. #endif
  32809. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32810. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32811. ExpectNotNull((x509 =
  32812. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32813. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32814. #ifdef OPENSSL_ALL
  32815. /* sk_X509_new only in OPENSSL_ALL */
  32816. sk = sk_X509_new_null();
  32817. ExpectNotNull(sk);
  32818. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32819. #else
  32820. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32821. #endif
  32822. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32823. X509_STORE_CTX_set_error(ctx, -5);
  32824. X509_STORE_CTX_set_error(NULL, -5);
  32825. X509_STORE_CTX_free(ctx);
  32826. ctx = NULL;
  32827. #ifdef OPENSSL_ALL
  32828. sk_X509_pop_free(sk, NULL);
  32829. sk = NULL;
  32830. #endif
  32831. X509_STORE_free(str);
  32832. str = NULL;
  32833. X509_free(x509);
  32834. x509 = NULL;
  32835. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32836. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32837. X509_STORE_CTX_free(ctx);
  32838. ctx = NULL;
  32839. #ifdef OPENSSL_ALL
  32840. /* test X509_STORE_CTX_get(1)_chain */
  32841. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32842. SSL_FILETYPE_PEM)));
  32843. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32844. SSL_FILETYPE_PEM)));
  32845. ExpectNotNull((sk = sk_X509_new_null()));
  32846. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32847. if (EXPECT_FAIL()) {
  32848. X509_free(x509);
  32849. x509 = NULL;
  32850. }
  32851. ExpectNotNull((str = X509_STORE_new()));
  32852. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32853. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32854. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32855. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32856. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32857. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32858. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32859. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32860. X509_STORE_CTX_free(ctx);
  32861. ctx = NULL;
  32862. X509_STORE_free(str);
  32863. str = NULL;
  32864. /* CTX certs not freed yet */
  32865. X509_free(x5092);
  32866. x5092 = NULL;
  32867. sk_X509_pop_free(sk, NULL);
  32868. sk = NULL;
  32869. /* sk3 is dup so free here */
  32870. sk_X509_pop_free(sk3, NULL);
  32871. sk3 = NULL;
  32872. #endif
  32873. /* test X509_STORE_CTX_get/set_ex_data */
  32874. {
  32875. int i = 0, tmpData = 5;
  32876. void* tmpDataRet;
  32877. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32878. #ifdef HAVE_EX_DATA
  32879. for (i = 0; i < MAX_EX_DATA; i++) {
  32880. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32881. WOLFSSL_SUCCESS);
  32882. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32883. ExpectNotNull(tmpDataRet);
  32884. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32885. }
  32886. #else
  32887. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32888. WOLFSSL_FAILURE);
  32889. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32890. ExpectNull(tmpDataRet);
  32891. #endif
  32892. X509_STORE_CTX_free(ctx);
  32893. ctx = NULL;
  32894. }
  32895. /* test X509_STORE_get/set_ex_data */
  32896. {
  32897. int i = 0, tmpData = 99;
  32898. void* tmpDataRet;
  32899. ExpectNotNull(str = X509_STORE_new());
  32900. #ifdef HAVE_EX_DATA
  32901. for (i = 0; i < MAX_EX_DATA; i++) {
  32902. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32903. WOLFSSL_SUCCESS);
  32904. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32905. ExpectNotNull(tmpDataRet);
  32906. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32907. }
  32908. #else
  32909. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32910. WOLFSSL_FAILURE);
  32911. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32912. ExpectNull(tmpDataRet);
  32913. #endif
  32914. X509_STORE_free(str);
  32915. str = NULL;
  32916. }
  32917. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32918. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32919. return EXPECT_RESULT();
  32920. }
  32921. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32922. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  32923. STACK_OF(X509)* chain)
  32924. {
  32925. EXPECT_DECLS;
  32926. XFILE fp = XBADFILE;
  32927. X509* cert = NULL;
  32928. ExpectTrue((fp = XFOPEN(filename, "rb"))
  32929. != XBADFILE);
  32930. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32931. if (fp != XBADFILE) {
  32932. XFCLOSE(fp);
  32933. fp = XBADFILE;
  32934. }
  32935. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  32936. if (EXPECT_FAIL())
  32937. X509_free(cert);
  32938. return EXPECT_RESULT();
  32939. }
  32940. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  32941. int err, int loadCA)
  32942. {
  32943. EXPECT_DECLS;
  32944. X509_STORE_CTX* ctx = NULL;
  32945. X509_STORE* str = NULL;
  32946. XFILE fp = XBADFILE;
  32947. X509* cert = NULL;
  32948. STACK_OF(X509)* untrusted = NULL;
  32949. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  32950. != XBADFILE);
  32951. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32952. if (fp != XBADFILE) {
  32953. XFCLOSE(fp);
  32954. fp = XBADFILE;
  32955. }
  32956. ExpectNotNull(str = X509_STORE_new());
  32957. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32958. ExpectNotNull(untrusted = sk_X509_new_null());
  32959. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  32960. if (loadCA) {
  32961. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  32962. 1);
  32963. }
  32964. for (; *filenames; filenames++) {
  32965. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  32966. untrusted), TEST_SUCCESS);
  32967. }
  32968. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  32969. ExpectIntEQ(X509_verify_cert(ctx), ret);
  32970. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  32971. X509_free(cert);
  32972. X509_STORE_free(str);
  32973. X509_STORE_CTX_free(ctx);
  32974. sk_X509_pop_free(untrusted, NULL);
  32975. return EXPECT_RESULT();
  32976. }
  32977. #endif
  32978. static int test_X509_STORE_untrusted(void)
  32979. {
  32980. EXPECT_DECLS;
  32981. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32982. const char* untrusted1[] = {
  32983. "./certs/intermediate/ca-int2-cert.pem",
  32984. NULL
  32985. };
  32986. const char* untrusted2[] = {
  32987. "./certs/intermediate/ca-int-cert.pem",
  32988. "./certs/intermediate/ca-int2-cert.pem",
  32989. NULL
  32990. };
  32991. const char* untrusted3[] = {
  32992. "./certs/intermediate/ca-int-cert.pem",
  32993. "./certs/intermediate/ca-int2-cert.pem",
  32994. "./certs/ca-cert.pem",
  32995. NULL
  32996. };
  32997. /* Adding unrelated certs that should be ignored */
  32998. const char* untrusted4[] = {
  32999. "./certs/client-ca.pem",
  33000. "./certs/intermediate/ca-int-cert.pem",
  33001. "./certs/server-cert.pem",
  33002. "./certs/intermediate/ca-int2-cert.pem",
  33003. NULL
  33004. };
  33005. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33006. * to loaded CA. */
  33007. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33008. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33009. /* Succeeds because path to loaded CA is available. */
  33010. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33011. TEST_SUCCESS);
  33012. /* Fails because root CA is in the untrusted stack */
  33013. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33014. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33015. /* Succeeds because path to loaded CA is available. */
  33016. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33017. TEST_SUCCESS);
  33018. #endif
  33019. return EXPECT_RESULT();
  33020. }
  33021. static int test_wolfSSL_X509_STORE_set_flags(void)
  33022. {
  33023. EXPECT_DECLS;
  33024. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33025. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33026. X509_STORE* store = NULL;
  33027. X509* x509 = NULL;
  33028. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33029. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33030. WOLFSSL_FILETYPE_PEM)));
  33031. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33032. #ifdef HAVE_CRL
  33033. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33034. WOLFSSL_SUCCESS);
  33035. #else
  33036. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33037. NOT_COMPILED_IN);
  33038. #endif
  33039. wolfSSL_X509_free(x509);
  33040. wolfSSL_X509_STORE_free(store);
  33041. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33042. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33043. return EXPECT_RESULT();
  33044. }
  33045. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33046. {
  33047. EXPECT_DECLS;
  33048. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33049. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33050. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33051. WOLFSSL_X509_STORE* store = NULL;
  33052. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33053. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33054. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33055. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33056. X509_FILETYPE_PEM), 1);
  33057. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33058. X509_FILETYPE_PEM), 1);
  33059. if (store != NULL) {
  33060. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33061. WOLFSSL_FILETYPE_PEM), 1);
  33062. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33063. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33064. }
  33065. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33066. X509_FILETYPE_PEM), 1);
  33067. if (store != NULL) {
  33068. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33069. WOLFSSL_FILETYPE_PEM), 1);
  33070. }
  33071. wolfSSL_X509_STORE_free(store);
  33072. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33073. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33074. return EXPECT_RESULT();
  33075. }
  33076. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33077. {
  33078. EXPECT_DECLS;
  33079. #if defined(OPENSSL_EXTRA)
  33080. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33081. time_t c_time;
  33082. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33083. c_time = 365*24*60*60;
  33084. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33085. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33086. WOLFSSL_USE_CHECK_TIME);
  33087. ExpectTrue(ctx->param->check_time == c_time);
  33088. wolfSSL_X509_STORE_CTX_free(ctx);
  33089. #endif /* OPENSSL_EXTRA */
  33090. return EXPECT_RESULT();
  33091. }
  33092. static int test_wolfSSL_CTX_get0_set1_param(void)
  33093. {
  33094. EXPECT_DECLS;
  33095. #if defined(OPENSSL_EXTRA)
  33096. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33097. SSL_CTX* ctx = NULL;
  33098. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33099. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33100. char testIPv4[] = "127.0.0.1";
  33101. char testhostName[] = "foo.hoge.com";
  33102. #ifndef NO_WOLFSSL_SERVER
  33103. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33104. #else
  33105. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33106. #endif
  33107. ExpectNull(SSL_CTX_get0_param(NULL));
  33108. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33109. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33110. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33111. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33112. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33113. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33114. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33115. WOLFSSL_SUCCESS);
  33116. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33117. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33118. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33119. (int)XSTRLEN(testhostName)));
  33120. ExpectIntEQ(0x01, pParam->hostFlags);
  33121. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33122. /* test for incorrect parameter */
  33123. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33124. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33125. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33126. SSL_CTX_free(ctx);
  33127. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33128. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33129. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33130. return EXPECT_RESULT();
  33131. }
  33132. static int test_wolfSSL_get0_param(void)
  33133. {
  33134. EXPECT_DECLS;
  33135. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33136. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33137. SSL_CTX* ctx = NULL;
  33138. SSL* ssl = NULL;
  33139. #ifndef NO_WOLFSSL_SERVER
  33140. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33141. #else
  33142. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33143. #endif
  33144. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33145. SSL_FILETYPE_PEM));
  33146. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33147. ExpectNotNull(ssl = SSL_new(ctx));
  33148. ExpectNotNull(SSL_get0_param(ssl));
  33149. SSL_free(ssl);
  33150. SSL_CTX_free(ctx);
  33151. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33152. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33153. return EXPECT_RESULT();
  33154. }
  33155. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33156. {
  33157. EXPECT_DECLS;
  33158. #if defined(OPENSSL_EXTRA)
  33159. const char host[] = "www.example.com";
  33160. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33161. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33162. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33163. if (pParam != NULL) {
  33164. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33165. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33166. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33167. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33168. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33169. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33170. }
  33171. #endif /* OPENSSL_EXTRA */
  33172. return EXPECT_RESULT();
  33173. }
  33174. static int test_wolfSSL_set1_host(void)
  33175. {
  33176. EXPECT_DECLS;
  33177. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33178. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33179. const char host[] = "www.test_wolfSSL_set1_host.com";
  33180. const char emptyStr[] = "";
  33181. SSL_CTX* ctx = NULL;
  33182. SSL* ssl = NULL;
  33183. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33184. #ifndef NO_WOLFSSL_SERVER
  33185. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33186. #else
  33187. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33188. #endif
  33189. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33190. SSL_FILETYPE_PEM));
  33191. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33192. ExpectNotNull(ssl = SSL_new(ctx));
  33193. pParam = SSL_get0_param(ssl);
  33194. /* we should get back host string */
  33195. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33196. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33197. /* we should get back empty string */
  33198. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33199. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33200. /* we should get back host string */
  33201. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33202. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33203. /* we should get back empty string */
  33204. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33205. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33206. SSL_free(ssl);
  33207. SSL_CTX_free(ctx);
  33208. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33209. #endif /* OPENSSL_EXTRA */
  33210. return EXPECT_RESULT();
  33211. }
  33212. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33213. {
  33214. EXPECT_DECLS;
  33215. #if defined(OPENSSL_EXTRA)
  33216. unsigned char buf[16] = {0};
  33217. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33218. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33219. /* test 127.0.0.1 */
  33220. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33221. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33222. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33223. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33224. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33225. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33226. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33227. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33228. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33229. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33230. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33231. /* test 2001:db8:: */
  33232. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33233. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33234. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33235. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33236. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33237. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33238. /* test ::1234:5678 */
  33239. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33240. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33241. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33242. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33243. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33244. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33245. /* test 2001:db8::1234:5678 */
  33246. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33247. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33248. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33249. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33250. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33251. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33252. sizeof(param->ipasc)), 0);
  33253. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33254. /* 2001:db8:1::ab9:c0a8:102 */
  33255. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33256. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33257. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33258. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33259. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33260. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33261. sizeof(param->ipasc)), 0);
  33262. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33263. #endif /* OPENSSL_EXTRA */
  33264. return EXPECT_RESULT();
  33265. }
  33266. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33267. {
  33268. EXPECT_DECLS;
  33269. #if defined(OPENSSL_EXTRA)
  33270. X509_STORE* store = NULL;
  33271. X509_STORE_CTX* ctx = NULL;
  33272. X509_STORE_CTX* ctx_no_init = NULL;
  33273. ExpectNotNull((store = X509_STORE_new()));
  33274. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33275. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33276. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33277. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33278. /* should return NULL if ctx has not bee initialized */
  33279. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33280. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33281. wolfSSL_X509_STORE_CTX_free(ctx);
  33282. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33283. X509_STORE_free(store);
  33284. #endif /* OPENSSL_EXTRA */
  33285. return EXPECT_RESULT();
  33286. }
  33287. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33288. {
  33289. EXPECT_DECLS;
  33290. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33291. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33292. WOLFSSL_CTX* ctx = NULL;
  33293. WOLFSSL* ssl = NULL;
  33294. X509_NAME* name = NULL;
  33295. STACK_OF(X509_NAME)* names = NULL;
  33296. STACK_OF(X509_NAME)* ca_list = NULL;
  33297. int names_len = 0;
  33298. int i;
  33299. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33300. /* Send two X501 names in cert request */
  33301. names = SSL_load_client_CA_file(cliCertFile);
  33302. ExpectNotNull(names);
  33303. ca_list = SSL_load_client_CA_file(caCertFile);
  33304. ExpectNotNull(ca_list);
  33305. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33306. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33307. if (EXPECT_FAIL()) {
  33308. wolfSSL_X509_NAME_free(name);
  33309. name = NULL;
  33310. }
  33311. SSL_CTX_set_client_CA_list(ctx, names);
  33312. /* This should only free the stack structure */
  33313. sk_X509_NAME_free(ca_list);
  33314. ca_list = NULL;
  33315. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33316. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33317. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33318. for (i = 0; i < names_len; i++) {
  33319. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33320. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33321. }
  33322. /* Needed to be able to create ssl object */
  33323. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33324. SSL_FILETYPE_PEM));
  33325. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33326. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33327. /* load again as old names are responsibility of ctx to free*/
  33328. names = SSL_load_client_CA_file(cliCertFile);
  33329. ExpectNotNull(names);
  33330. SSL_set_client_CA_list(ssl, names);
  33331. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33332. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33333. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33334. for (i = 0; i < names_len; i++) {
  33335. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33336. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33337. }
  33338. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33339. {
  33340. tcp_ready ready;
  33341. func_args server_args;
  33342. callback_functions server_cb;
  33343. THREAD_TYPE serverThread;
  33344. WOLFSSL* ssl_client = NULL;
  33345. WOLFSSL_CTX* ctx_client = NULL;
  33346. SOCKET_T sockfd = 0;
  33347. /* wolfSSL_get_client_CA_list() with handshake */
  33348. StartTCP();
  33349. InitTcpReady(&ready);
  33350. XMEMSET(&server_args, 0, sizeof(func_args));
  33351. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33352. server_args.signal = &ready;
  33353. server_args.callbacks = &server_cb;
  33354. /* we are responsible for free'ing WOLFSSL_CTX */
  33355. server_cb.ctx = ctx;
  33356. server_cb.isSharedCtx = 1;
  33357. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33358. cliCertFile, 0));
  33359. start_thread(test_server_nofail, &server_args, &serverThread);
  33360. wait_tcp_ready(&server_args);
  33361. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33362. ExpectNotNull(ctx_client =
  33363. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33364. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33365. ctx_client, caCertFile, 0));
  33366. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33367. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33368. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33369. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33370. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33371. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33372. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33373. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33374. /* We are expecting two cert names to be sent */
  33375. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33376. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33377. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33378. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33379. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33380. }
  33381. wolfSSL_shutdown(ssl_client);
  33382. wolfSSL_free(ssl_client);
  33383. wolfSSL_CTX_free(ctx_client);
  33384. CloseSocket(sockfd);
  33385. join_thread(serverThread);
  33386. FreeTcpReady(&ready);
  33387. }
  33388. #endif
  33389. wolfSSL_free(ssl);
  33390. wolfSSL_CTX_free(ctx);
  33391. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33392. * !NO_BIO */
  33393. return EXPECT_RESULT();
  33394. }
  33395. static int test_wolfSSL_CTX_add_client_CA(void)
  33396. {
  33397. EXPECT_DECLS;
  33398. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33399. !defined(NO_WOLFSSL_CLIENT)
  33400. WOLFSSL_CTX* ctx = NULL;
  33401. WOLFSSL_X509* x509 = NULL;
  33402. WOLFSSL_X509* x509_a = NULL;
  33403. STACK_OF(X509_NAME)* ca_list = NULL;
  33404. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33405. /* Add client cert */
  33406. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33407. SSL_FILETYPE_PEM));
  33408. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33409. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33410. /* Add another client cert */
  33411. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33412. SSL_FILETYPE_PEM));
  33413. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33414. /* test for incorrect parameter */
  33415. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33416. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33417. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33418. X509_free(x509);
  33419. X509_free(x509_a);
  33420. SSL_CTX_free(ctx);
  33421. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33422. return EXPECT_RESULT();
  33423. }
  33424. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33425. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33426. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33427. {
  33428. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33429. WOLFSSL_CTX* ctx = callbacks->ctx;
  33430. WOLFSSL* ssl = NULL;
  33431. SOCKET_T sfd = 0;
  33432. SOCKET_T cfd = 0;
  33433. word16 port;
  33434. char input[1024];
  33435. int idx;
  33436. int ret, err = 0;
  33437. const char* privateName = "ech-private-name.com";
  33438. int privateNameLen = (int)XSTRLEN(privateName);
  33439. ((func_args*)args)->return_code = TEST_FAIL;
  33440. port = ((func_args*)args)->signal->port;
  33441. AssertIntEQ(WOLFSSL_SUCCESS,
  33442. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33443. AssertIntEQ(WOLFSSL_SUCCESS,
  33444. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33445. WOLFSSL_FILETYPE_PEM));
  33446. AssertIntEQ(WOLFSSL_SUCCESS,
  33447. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33448. WOLFSSL_FILETYPE_PEM));
  33449. if (callbacks->ctx_ready)
  33450. callbacks->ctx_ready(ctx);
  33451. ssl = wolfSSL_new(ctx);
  33452. /* set the sni for the server */
  33453. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33454. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33455. CloseSocket(sfd);
  33456. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33457. if (callbacks->ssl_ready)
  33458. callbacks->ssl_ready(ssl);
  33459. do {
  33460. err = 0; /* Reset error */
  33461. ret = wolfSSL_accept(ssl);
  33462. if (ret != WOLFSSL_SUCCESS) {
  33463. err = wolfSSL_get_error(ssl, 0);
  33464. }
  33465. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33466. if (ret != WOLFSSL_SUCCESS) {
  33467. char buff[WOLFSSL_MAX_ERROR_SZ];
  33468. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33469. }
  33470. else {
  33471. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33472. input[idx] = 0;
  33473. fprintf(stderr, "Client message: %s\n", input);
  33474. }
  33475. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33476. privateNameLen));
  33477. ((func_args*)args)->return_code = TEST_SUCCESS;
  33478. }
  33479. if (callbacks->on_result)
  33480. callbacks->on_result(ssl);
  33481. wolfSSL_shutdown(ssl);
  33482. wolfSSL_free(ssl);
  33483. wolfSSL_CTX_free(ctx);
  33484. CloseSocket(cfd);
  33485. #ifdef FP_ECC
  33486. wc_ecc_fp_free();
  33487. #endif
  33488. WOLFSSL_RETURN_FROM_THREAD(0);
  33489. }
  33490. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33491. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33492. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33493. {
  33494. AssertNotNull(ssl);
  33495. AssertNotNull(line);
  33496. XFILE fp;
  33497. const byte lf = '\n';
  33498. fp = XFOPEN("./MyKeyLog.txt", "a");
  33499. XFWRITE( line, 1, strlen(line),fp);
  33500. XFWRITE( (void*)&lf,1,1,fp);
  33501. XFFLUSH(fp);
  33502. XFCLOSE(fp);
  33503. }
  33504. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33505. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33506. {
  33507. EXPECT_DECLS;
  33508. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33509. !defined(NO_WOLFSSL_CLIENT)
  33510. SSL_CTX* ctx = NULL;
  33511. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33512. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33513. SSL_CTX_free(ctx);
  33514. SSL_CTX_set_keylog_callback(NULL, NULL);
  33515. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33516. return EXPECT_RESULT();
  33517. }
  33518. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33519. {
  33520. EXPECT_DECLS;
  33521. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33522. !defined(NO_WOLFSSL_CLIENT)
  33523. SSL_CTX* ctx = NULL;
  33524. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33525. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33526. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33527. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33528. SSL_CTX_set_keylog_callback(ctx, NULL );
  33529. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33530. SSL_CTX_free(ctx);
  33531. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33532. return EXPECT_RESULT();
  33533. }
  33534. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33535. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33536. {
  33537. /* set keylog callback */
  33538. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33539. return TEST_SUCCESS;
  33540. }
  33541. #endif
  33542. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33543. {
  33544. EXPECT_DECLS;
  33545. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33546. /* This test is intended for checking whether keylog callback is called
  33547. * in client during TLS handshake between the client and a server.
  33548. */
  33549. test_ssl_cbf server_cbf;
  33550. test_ssl_cbf client_cbf;
  33551. XFILE fp = XBADFILE;
  33552. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33553. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33554. server_cbf.method = wolfTLSv1_2_server_method;
  33555. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33556. /* clean up keylog file */
  33557. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33558. if (fp != XBADFILE) {
  33559. XFFLUSH(fp);
  33560. XFCLOSE(fp);
  33561. fp = XBADFILE;
  33562. }
  33563. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33564. &server_cbf, NULL), TEST_SUCCESS);
  33565. XSLEEP_MS(100);
  33566. /* check if the keylog file exists */
  33567. char buff[300] = {0};
  33568. int found = 0;
  33569. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33570. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33571. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33572. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33573. found = 1;
  33574. break;
  33575. }
  33576. }
  33577. if (fp != XBADFILE) {
  33578. XFCLOSE(fp);
  33579. }
  33580. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33581. ExpectIntEQ(found, 1);
  33582. /* clean up */
  33583. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33584. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33585. return EXPECT_RESULT();
  33586. }
  33587. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33588. defined(HAVE_SECRET_CALLBACK)
  33589. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33590. {
  33591. /* set keylog callback */
  33592. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33593. return TEST_SUCCESS;
  33594. }
  33595. #endif
  33596. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33597. {
  33598. EXPECT_DECLS;
  33599. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33600. defined(HAVE_SECRET_CALLBACK)
  33601. /* This test is intended for checking whether keylog callback is called
  33602. * in client during TLS handshake between the client and a server.
  33603. */
  33604. test_ssl_cbf server_cbf;
  33605. test_ssl_cbf client_cbf;
  33606. XFILE fp = XBADFILE;
  33607. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33608. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33609. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33610. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33611. /* clean up keylog file */
  33612. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33613. if (fp != XBADFILE) {
  33614. XFCLOSE(fp);
  33615. fp = XBADFILE;
  33616. }
  33617. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33618. &server_cbf, NULL), TEST_SUCCESS);
  33619. /* check if the keylog file exists */
  33620. {
  33621. char buff[300] = {0};
  33622. int found[4] = {0};
  33623. int numfnd = 0;
  33624. int i;
  33625. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33626. while (EXPECT_SUCCESS() &&
  33627. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33628. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33629. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33630. found[0] = 1;
  33631. continue;
  33632. }
  33633. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33634. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33635. found[1] = 1;
  33636. continue;
  33637. }
  33638. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33639. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33640. found[2] = 1;
  33641. continue;
  33642. }
  33643. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33644. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33645. found[3] = 1;
  33646. continue;
  33647. }
  33648. }
  33649. if (fp != XBADFILE)
  33650. XFCLOSE(fp);
  33651. for (i = 0; i < 4; i++) {
  33652. if (found[i] != 0)
  33653. numfnd++;
  33654. }
  33655. ExpectIntEQ(numfnd, 4);
  33656. }
  33657. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33658. return EXPECT_RESULT();
  33659. }
  33660. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33661. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33662. static int test_wolfSSL_Tls13_ECH_params(void)
  33663. {
  33664. EXPECT_DECLS;
  33665. #if !defined(NO_WOLFSSL_CLIENT)
  33666. word32 outputLen = 0;
  33667. byte testBuf[72];
  33668. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33669. WOLFSSL *ssl = wolfSSL_new(ctx);
  33670. ExpectNotNull(ctx);
  33671. ExpectNotNull(ssl);
  33672. /* invalid ctx */
  33673. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33674. "ech-public-name.com", 0, 0, 0));
  33675. /* invalid public name */
  33676. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33677. 0, 0));
  33678. /* invalid algorithms */
  33679. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33680. "ech-public-name.com", 1000, 1000, 1000));
  33681. /* invalid ctx */
  33682. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33683. &outputLen));
  33684. /* invalid output len */
  33685. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33686. /* invalid ssl */
  33687. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33688. (char*)testBuf, sizeof(testBuf)));
  33689. /* invalid configs64 */
  33690. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33691. sizeof(testBuf)));
  33692. /* invalid size */
  33693. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33694. (char*)testBuf, 0));
  33695. /* invalid ssl */
  33696. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33697. sizeof(testBuf)));
  33698. /* invalid configs */
  33699. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33700. sizeof(testBuf)));
  33701. /* invalid size */
  33702. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33703. /* invalid ssl */
  33704. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33705. /* invalid size */
  33706. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33707. wolfSSL_free(ssl);
  33708. wolfSSL_CTX_free(ctx);
  33709. #endif /* !NO_WOLFSSL_CLIENT */
  33710. return EXPECT_RESULT();
  33711. }
  33712. static int test_wolfSSL_Tls13_ECH(void)
  33713. {
  33714. EXPECT_DECLS;
  33715. tcp_ready ready;
  33716. func_args client_args;
  33717. func_args server_args;
  33718. THREAD_TYPE serverThread;
  33719. callback_functions server_cbf;
  33720. callback_functions client_cbf;
  33721. SOCKET_T sockfd = 0;
  33722. WOLFSSL_CTX* ctx = NULL;
  33723. WOLFSSL* ssl = NULL;
  33724. const char* publicName = "ech-public-name.com";
  33725. const char* privateName = "ech-private-name.com";
  33726. int privateNameLen = 20;
  33727. char reply[1024];
  33728. int replyLen = 0;
  33729. byte rawEchConfig[128];
  33730. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33731. InitTcpReady(&ready);
  33732. ready.port = 22222;
  33733. XMEMSET(&client_args, 0, sizeof(func_args));
  33734. XMEMSET(&server_args, 0, sizeof(func_args));
  33735. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33736. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33737. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33738. /* create the server context here so we can get the ech config */
  33739. ExpectNotNull(server_cbf.ctx =
  33740. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33741. /* generate ech config */
  33742. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33743. publicName, 0, 0, 0));
  33744. /* get the config for the client to use */
  33745. ExpectIntEQ(WOLFSSL_SUCCESS,
  33746. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33747. &rawEchConfigLen));
  33748. server_args.callbacks = &server_cbf;
  33749. server_args.signal = &ready;
  33750. /* start server task */
  33751. start_thread(server_task_ech, &server_args, &serverThread);
  33752. wait_tcp_ready(&server_args);
  33753. /* run as a TLS1.3 client */
  33754. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33755. ExpectIntEQ(WOLFSSL_SUCCESS,
  33756. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33757. ExpectIntEQ(WOLFSSL_SUCCESS,
  33758. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33759. ExpectIntEQ(WOLFSSL_SUCCESS,
  33760. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33761. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33762. /* get connected the server task */
  33763. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33764. /* set the ech configs for the client */
  33765. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33766. rawEchConfigLen));
  33767. /* set the sni for the client */
  33768. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33769. privateName, privateNameLen));
  33770. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33771. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33772. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33773. privateNameLen);
  33774. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33775. /* add th null terminator for string compare */
  33776. reply[replyLen] = 0;
  33777. /* check that the server replied with the private name */
  33778. ExpectStrEQ(privateName, reply);
  33779. wolfSSL_free(ssl);
  33780. wolfSSL_CTX_free(ctx);
  33781. CloseSocket(sockfd);
  33782. join_thread(serverThread);
  33783. FreeTcpReady(&ready);
  33784. return EXPECT_RESULT();
  33785. }
  33786. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33787. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33788. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33789. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33790. static int post_auth_version_cb(WOLFSSL* ssl)
  33791. {
  33792. EXPECT_DECLS;
  33793. /* do handshake and then test version error */
  33794. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33795. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33796. return EXPECT_RESULT();
  33797. }
  33798. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33799. {
  33800. EXPECT_DECLS;
  33801. /* do handshake and then test version error */
  33802. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33803. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33804. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33805. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33806. /* check was added to error queue */
  33807. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33808. /* check the string matches expected string */
  33809. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33810. "WRONG_SSL_VERSION");
  33811. #endif
  33812. return EXPECT_RESULT();
  33813. }
  33814. static int post_auth_cb(WOLFSSL* ssl)
  33815. {
  33816. EXPECT_DECLS;
  33817. WOLFSSL_X509* x509 = NULL;
  33818. /* do handshake and then test version error */
  33819. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33820. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33821. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33822. wolfSSL_X509_free(x509);
  33823. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33824. return EXPECT_RESULT();
  33825. }
  33826. static int set_post_auth_cb(WOLFSSL* ssl)
  33827. {
  33828. if (!wolfSSL_is_server(ssl)) {
  33829. EXPECT_DECLS;
  33830. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33831. return EXPECT_RESULT();
  33832. }
  33833. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33834. return TEST_SUCCESS;
  33835. }
  33836. #endif
  33837. static int test_wolfSSL_Tls13_postauth(void)
  33838. {
  33839. EXPECT_DECLS;
  33840. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33841. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33842. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33843. test_ssl_cbf server_cbf;
  33844. test_ssl_cbf client_cbf;
  33845. /* test version failure doing post auth with TLS 1.2 connection */
  33846. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33847. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33848. server_cbf.method = wolfTLSv1_2_server_method;
  33849. server_cbf.ssl_ready = set_post_auth_cb;
  33850. server_cbf.on_result = post_auth_version_cb;
  33851. client_cbf.ssl_ready = set_post_auth_cb;
  33852. client_cbf.on_result = post_auth_version_client_cb;
  33853. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33854. &server_cbf, NULL), TEST_SUCCESS);
  33855. /* tests on post auth with TLS 1.3 */
  33856. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33857. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33858. server_cbf.method = wolfTLSv1_3_server_method;
  33859. server_cbf.ssl_ready = set_post_auth_cb;
  33860. client_cbf.ssl_ready = set_post_auth_cb;
  33861. server_cbf.on_result = post_auth_cb;
  33862. client_cbf.on_result = NULL;
  33863. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33864. &server_cbf, NULL), TEST_SUCCESS);
  33865. #endif
  33866. return EXPECT_RESULT();
  33867. }
  33868. static int test_wolfSSL_X509_NID(void)
  33869. {
  33870. EXPECT_DECLS;
  33871. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33872. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33873. int sigType;
  33874. int nameSz;
  33875. X509* cert = NULL;
  33876. EVP_PKEY* pubKeyTmp = NULL;
  33877. X509_NAME* name = NULL;
  33878. char commonName[80];
  33879. char countryName[80];
  33880. char localityName[80];
  33881. char stateName[80];
  33882. char orgName[80];
  33883. char orgUnit[80];
  33884. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33885. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33886. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  33887. sizeof_client_cert_der_2048, HEAP_HINT));
  33888. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33889. /* extract PUBLIC KEY from cert */
  33890. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33891. /* extract signatureType */
  33892. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33893. /* extract subjectName info */
  33894. ExpectNotNull(name = X509_get_subject_name(cert));
  33895. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33896. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33897. NULL, 0)), 0);
  33898. ExpectIntEQ(nameSz, 15);
  33899. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33900. commonName, sizeof(commonName))), 0);
  33901. ExpectIntEQ(nameSz, 15);
  33902. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33903. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33904. commonName, 9)), 0);
  33905. ExpectIntEQ(nameSz, 8);
  33906. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33907. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33908. countryName, sizeof(countryName))), 0);
  33909. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33910. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33911. localityName, sizeof(localityName))), 0);
  33912. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33913. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33914. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  33915. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33916. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33917. orgName, sizeof(orgName))), 0);
  33918. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33919. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33920. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  33921. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  33922. EVP_PKEY_free(pubKeyTmp);
  33923. X509_free(cert);
  33924. #endif
  33925. return EXPECT_RESULT();
  33926. }
  33927. static int test_wolfSSL_CTX_set_srp_username(void)
  33928. {
  33929. EXPECT_DECLS;
  33930. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  33931. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33932. WOLFSSL_CTX* ctx = NULL;
  33933. WOLFSSL* ssl = NULL;
  33934. const char *username = "TESTUSER";
  33935. const char *password = "TESTPASSWORD";
  33936. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33937. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33938. SSL_SUCCESS);
  33939. wolfSSL_CTX_free(ctx);
  33940. ctx = NULL;
  33941. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33942. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33943. SSL_SUCCESS);
  33944. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33945. SSL_SUCCESS);
  33946. ExpectNotNull(ssl = SSL_new(ctx));
  33947. ExpectNotNull(SSL_get_srp_username(ssl));
  33948. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  33949. wolfSSL_free(ssl);
  33950. wolfSSL_CTX_free(ctx);
  33951. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33952. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33953. return EXPECT_RESULT();
  33954. }
  33955. static int test_wolfSSL_CTX_set_srp_password(void)
  33956. {
  33957. EXPECT_DECLS;
  33958. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  33959. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33960. WOLFSSL_CTX* ctx = NULL;
  33961. const char *username = "TESTUSER";
  33962. const char *password = "TESTPASSWORD";
  33963. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33964. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33965. SSL_SUCCESS);
  33966. wolfSSL_CTX_free(ctx);
  33967. ctx = NULL;
  33968. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33969. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33970. SSL_SUCCESS);
  33971. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33972. SSL_SUCCESS);
  33973. wolfSSL_CTX_free(ctx);
  33974. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33975. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33976. return EXPECT_RESULT();
  33977. }
  33978. static int test_wolfSSL_X509_STORE(void)
  33979. {
  33980. EXPECT_DECLS;
  33981. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33982. X509_STORE *store = NULL;
  33983. #ifdef HAVE_CRL
  33984. X509_STORE_CTX *storeCtx = NULL;
  33985. X509_CRL *crl = NULL;
  33986. X509 *ca = NULL;
  33987. X509 *cert = NULL;
  33988. const char crlPem[] = "./certs/crl/crl.revoked";
  33989. const char srvCert[] = "./certs/server-revoked-cert.pem";
  33990. const char caCert[] = "./certs/ca-cert.pem";
  33991. XFILE fp = XBADFILE;
  33992. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33993. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  33994. SSL_FILETYPE_PEM)));
  33995. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  33996. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  33997. SSL_FILETYPE_PEM)));
  33998. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  33999. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34000. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34001. X509_STORE_free(store);
  34002. store = NULL;
  34003. X509_STORE_CTX_free(storeCtx);
  34004. storeCtx = NULL;
  34005. X509_free(cert);
  34006. cert = NULL;
  34007. X509_free(ca);
  34008. ca = NULL;
  34009. /* should fail to verify now after adding in CRL */
  34010. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34011. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34012. SSL_FILETYPE_PEM)));
  34013. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34014. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34015. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34016. NULL, NULL));
  34017. if (fp != XBADFILE)
  34018. XFCLOSE(fp);
  34019. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34020. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34021. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34022. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34023. SSL_FILETYPE_PEM)));
  34024. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34025. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34026. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34027. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34028. X509_CRL_free(crl);
  34029. crl = NULL;
  34030. X509_STORE_free(store);
  34031. store = NULL;
  34032. X509_STORE_CTX_free(storeCtx);
  34033. storeCtx = NULL;
  34034. X509_free(cert);
  34035. cert = NULL;
  34036. X509_free(ca);
  34037. ca = NULL;
  34038. #endif /* HAVE_CRL */
  34039. #ifndef WOLFCRYPT_ONLY
  34040. {
  34041. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34042. SSL_CTX* ctx = NULL;
  34043. SSL* ssl = NULL;
  34044. int i;
  34045. for (i = 0; i < 2; i++) {
  34046. #ifndef NO_WOLFSSL_SERVER
  34047. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34048. #else
  34049. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34050. #endif
  34051. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34052. SSL_CTX_set_cert_store(ctx, store);
  34053. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34054. SSL_CTX_set_cert_store(ctx, store);
  34055. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34056. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34057. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34058. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34059. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34060. ExpectNotNull(ssl = SSL_new(ctx));
  34061. if (i == 0) {
  34062. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34063. SSL_SUCCESS);
  34064. }
  34065. else {
  34066. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34067. #ifdef OPENSSL_ALL
  34068. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34069. #endif
  34070. }
  34071. if (EXPECT_FAIL() || (i == 1)) {
  34072. X509_STORE_free(store);
  34073. store = NULL;
  34074. }
  34075. SSL_free(ssl);
  34076. ssl = NULL;
  34077. SSL_CTX_free(ctx);
  34078. ctx = NULL;
  34079. }
  34080. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34081. }
  34082. #endif
  34083. #endif
  34084. return EXPECT_RESULT();
  34085. }
  34086. static int test_wolfSSL_X509_STORE_load_locations(void)
  34087. {
  34088. EXPECT_DECLS;
  34089. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34090. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34091. SSL_CTX *ctx = NULL;
  34092. X509_STORE *store = NULL;
  34093. const char ca_file[] = "./certs/ca-cert.pem";
  34094. const char client_pem_file[] = "./certs/client-cert.pem";
  34095. const char client_der_file[] = "./certs/client-cert.der";
  34096. const char ecc_file[] = "./certs/ecc-key.pem";
  34097. const char certs_path[] = "./certs/";
  34098. const char bad_path[] = "./bad-path/";
  34099. #ifdef HAVE_CRL
  34100. const char crl_path[] = "./certs/crl/";
  34101. const char crl_file[] = "./certs/crl/crl.pem";
  34102. #endif
  34103. #ifndef NO_WOLFSSL_SERVER
  34104. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34105. #else
  34106. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34107. #endif
  34108. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34109. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34110. WOLFSSL_SUCCESS);
  34111. /* Test bad arguments */
  34112. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34113. WOLFSSL_FAILURE);
  34114. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34115. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34116. WOLFSSL_FAILURE);
  34117. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34118. WOLFSSL_FAILURE);
  34119. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34120. WOLFSSL_FAILURE);
  34121. #ifdef HAVE_CRL
  34122. /* Test with CRL */
  34123. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34124. WOLFSSL_SUCCESS);
  34125. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34126. WOLFSSL_SUCCESS);
  34127. #endif
  34128. /* Test with CA */
  34129. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34130. WOLFSSL_SUCCESS);
  34131. /* Test with client_cert and certs path */
  34132. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34133. WOLFSSL_SUCCESS);
  34134. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34135. WOLFSSL_SUCCESS);
  34136. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34137. /* Clear nodes */
  34138. ERR_clear_error();
  34139. #endif
  34140. SSL_CTX_free(ctx);
  34141. #endif
  34142. return EXPECT_RESULT();
  34143. }
  34144. static int test_X509_STORE_get0_objects(void)
  34145. {
  34146. EXPECT_DECLS;
  34147. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34148. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34149. X509_STORE *store = NULL;
  34150. X509_STORE *store_cpy = NULL;
  34151. SSL_CTX *ctx = NULL;
  34152. X509_OBJECT *obj = NULL;
  34153. STACK_OF(X509_OBJECT) *objs = NULL;
  34154. int i;
  34155. /* Setup store */
  34156. #ifndef NO_WOLFSSL_SERVER
  34157. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34158. #else
  34159. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34160. #endif
  34161. ExpectNotNull(store_cpy = X509_STORE_new());
  34162. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34163. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34164. WOLFSSL_SUCCESS);
  34165. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34166. WOLFSSL_SUCCESS);
  34167. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34168. WOLFSSL_SUCCESS);
  34169. #ifdef HAVE_CRL
  34170. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34171. WOLFSSL_SUCCESS);
  34172. #endif
  34173. /* Store ready */
  34174. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34175. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34176. #ifdef HAVE_CRL
  34177. #ifdef WOLFSSL_SIGNER_DER_CERT
  34178. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34179. #else
  34180. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34181. #endif
  34182. #else
  34183. #ifdef WOLFSSL_SIGNER_DER_CERT
  34184. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34185. #else
  34186. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34187. #endif
  34188. #endif
  34189. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34190. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34191. switch (X509_OBJECT_get_type(obj)) {
  34192. case X509_LU_X509:
  34193. {
  34194. WOLFSSL_X509* x509;
  34195. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34196. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34197. break;
  34198. }
  34199. case X509_LU_CRL:
  34200. #ifdef HAVE_CRL
  34201. {
  34202. WOLFSSL_CRL* crl = NULL;
  34203. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34204. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34205. break;
  34206. }
  34207. #endif
  34208. case X509_LU_NONE:
  34209. default:
  34210. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34211. "(when built with crl support)"),
  34212. ("Unrecognized X509_OBJECT type or none"));
  34213. }
  34214. }
  34215. X509_STORE_free(store_cpy);
  34216. SSL_CTX_free(ctx);
  34217. #endif
  34218. return EXPECT_RESULT();
  34219. }
  34220. static int test_wolfSSL_BN_CTX(void)
  34221. {
  34222. EXPECT_DECLS;
  34223. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34224. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34225. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34226. WOLFSSL_BIGNUM* t = NULL;
  34227. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34228. /* No implementation. */
  34229. BN_CTX_init(NULL);
  34230. ExpectNotNull(t = BN_CTX_get(NULL));
  34231. BN_free(t);
  34232. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34233. BN_free(t);
  34234. #ifndef NO_WOLFSSL_STUB
  34235. /* No implementation. */
  34236. BN_CTX_start(NULL);
  34237. BN_CTX_start(bn_ctx);
  34238. #endif
  34239. BN_CTX_free(NULL);
  34240. BN_CTX_free(bn_ctx);
  34241. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34242. return EXPECT_RESULT();
  34243. }
  34244. static int test_wolfSSL_BN(void)
  34245. {
  34246. EXPECT_DECLS;
  34247. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34248. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34249. BIGNUM* a = NULL;
  34250. BIGNUM* b = NULL;
  34251. BIGNUM* c = NULL;
  34252. BIGNUM* d = NULL;
  34253. BIGNUM emptyBN;
  34254. /* Setup */
  34255. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34256. /* internal not set emptyBN. */
  34257. ExpectNotNull(a = BN_new());
  34258. ExpectNotNull(b = BN_new());
  34259. ExpectNotNull(c = BN_dup(b));
  34260. ExpectNotNull(d = BN_new());
  34261. /* Invalid parameter testing. */
  34262. BN_free(NULL);
  34263. ExpectNull(BN_dup(NULL));
  34264. ExpectNull(BN_dup(&emptyBN));
  34265. ExpectNull(BN_copy(NULL, NULL));
  34266. ExpectNull(BN_copy(b, NULL));
  34267. ExpectNull(BN_copy(NULL, c));
  34268. ExpectNull(BN_copy(b, &emptyBN));
  34269. ExpectNull(BN_copy(&emptyBN, c));
  34270. BN_clear(NULL);
  34271. BN_clear(&emptyBN);
  34272. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34273. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34274. ExpectIntEQ(BN_num_bits(NULL), 0);
  34275. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34276. ExpectIntEQ(BN_is_negative(NULL), 0);
  34277. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34278. /* END Invalid Parameters */
  34279. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34280. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34281. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34282. ExpectIntEQ(BN_num_bits(a), 2);
  34283. ExpectIntEQ(BN_num_bytes(a), 1);
  34284. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34285. defined(WOLFSSL_SP_INT_NEGATIVE))
  34286. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34287. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34288. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34289. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34290. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34291. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34292. {
  34293. /* Do additional tests on negative BN conversions. */
  34294. char* ret = NULL;
  34295. ASN1_INTEGER* asn1 = NULL;
  34296. BIGNUM* tmp = NULL;
  34297. /* Sanity check we have a negative BN. */
  34298. ExpectIntEQ(BN_is_negative(c), 1);
  34299. ExpectNotNull(ret = BN_bn2dec(c));
  34300. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34301. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34302. ret = NULL;
  34303. /* Convert to ASN1_INTEGER and back to BN. */
  34304. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34305. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34306. /* After converting back BN should be negative and correct. */
  34307. ExpectIntEQ(BN_is_negative(tmp), 1);
  34308. ExpectNotNull(ret = BN_bn2dec(tmp));
  34309. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34310. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34311. ASN1_INTEGER_free(asn1);
  34312. BN_free(tmp);
  34313. }
  34314. #endif
  34315. ExpectIntEQ(BN_get_word(c), 4);
  34316. #endif
  34317. ExpectIntEQ(BN_set_word(a, 3), 1);
  34318. ExpectIntEQ(BN_set_word(b, 3), 1);
  34319. ExpectIntEQ(BN_set_word(c, 4), 1);
  34320. /* NULL == NULL, NULL < num, num > NULL */
  34321. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34322. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34323. ExpectIntLT(BN_cmp(NULL, b), 0);
  34324. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34325. ExpectIntGT(BN_cmp(a, NULL), 0);
  34326. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34327. ExpectIntEQ(BN_cmp(a, b), 0);
  34328. ExpectIntLT(BN_cmp(a, c), 0);
  34329. ExpectIntGT(BN_cmp(c, b), 0);
  34330. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34331. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34332. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34333. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34334. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34335. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34336. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34337. #endif
  34338. BN_clear(a);
  34339. BN_free(a);
  34340. BN_free(b);
  34341. BN_free(c);
  34342. BN_clear_free(d);
  34343. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34344. return EXPECT_RESULT();
  34345. }
  34346. static int test_wolfSSL_BN_init(void)
  34347. {
  34348. EXPECT_DECLS;
  34349. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34350. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34351. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34352. BIGNUM* ap = NULL;
  34353. BIGNUM bv;
  34354. BIGNUM cv;
  34355. BIGNUM dv;
  34356. ExpectNotNull(ap = BN_new());
  34357. BN_init(NULL);
  34358. XMEMSET(&bv, 0, sizeof(bv));
  34359. ExpectNull(BN_dup(&bv));
  34360. BN_init(&bv);
  34361. BN_init(&cv);
  34362. BN_init(&dv);
  34363. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34364. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34365. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34366. /* a^b mod c = */
  34367. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34368. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34369. /* check result 3^2 mod 5 */
  34370. ExpectIntEQ(BN_get_word(&dv), 4);
  34371. /* a*b mod c = */
  34372. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34373. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34374. /* check result 3*2 mod 5 */
  34375. ExpectIntEQ(BN_get_word(&dv), 1);
  34376. BN_free(ap);
  34377. #endif
  34378. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34379. return EXPECT_RESULT();
  34380. }
  34381. static int test_wolfSSL_BN_enc_dec(void)
  34382. {
  34383. EXPECT_DECLS;
  34384. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34385. BIGNUM* a = NULL;
  34386. BIGNUM* b = NULL;
  34387. BIGNUM* c = NULL;
  34388. BIGNUM emptyBN;
  34389. char* str = NULL;
  34390. const char* emptyStr = "";
  34391. const char* numberStr = "12345";
  34392. const char* badStr = "g12345";
  34393. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34394. const char* twoStr = "2";
  34395. #endif
  34396. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34397. unsigned char outNum[5];
  34398. /* Setup */
  34399. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34400. ExpectNotNull(a = BN_new());
  34401. ExpectNotNull(b = BN_new());
  34402. ExpectIntEQ(BN_set_word(a, 2), 1);
  34403. /* Invalid parameters */
  34404. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34405. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34406. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34407. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34408. ExpectNull(BN_bn2hex(NULL));
  34409. ExpectNull(BN_bn2hex(&emptyBN));
  34410. ExpectNull(BN_bn2dec(NULL));
  34411. ExpectNull(BN_bn2dec(&emptyBN));
  34412. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34413. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34414. ExpectNull(BN_bin2bn(binNum, -1, a));
  34415. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34416. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34417. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34418. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34419. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34420. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34421. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34422. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34423. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34424. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34425. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34426. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34427. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34428. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34429. ExpectIntEQ(BN_set_word(a, 2), 1);
  34430. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34431. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34432. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34433. ExpectIntEQ(BN_cmp(a, b), 0);
  34434. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34435. ExpectIntEQ(BN_cmp(a, b), -1);
  34436. ExpectNotNull(str = BN_bn2hex(a));
  34437. ExpectNotNull(BN_hex2bn(&b, str));
  34438. ExpectIntEQ(BN_cmp(a, b), 0);
  34439. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34440. ExpectIntEQ(BN_cmp(a, b), -1);
  34441. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34442. str = NULL;
  34443. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34444. ExpectNotNull(str = BN_bn2dec(a));
  34445. ExpectStrEQ(str, twoStr);
  34446. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34447. str = NULL;
  34448. #ifndef NO_RSA
  34449. ExpectNotNull(str = BN_bn2dec(a));
  34450. ExpectNotNull(BN_dec2bn(&b, str));
  34451. ExpectIntEQ(BN_cmp(a, b), 0);
  34452. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34453. ExpectIntEQ(BN_cmp(a, b), -1);
  34454. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34455. str = NULL;
  34456. #else
  34457. /* No implementation - fail with good parameters. */
  34458. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34459. #endif
  34460. #endif
  34461. BN_free(b);
  34462. BN_free(a);
  34463. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34464. return EXPECT_RESULT();
  34465. }
  34466. static int test_wolfSSL_BN_word(void)
  34467. {
  34468. EXPECT_DECLS;
  34469. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34470. BIGNUM* a = NULL;
  34471. BIGNUM* b = NULL;
  34472. BIGNUM* c = NULL;
  34473. BIGNUM av;
  34474. ExpectNotNull(a = BN_new());
  34475. ExpectNotNull(b = BN_new());
  34476. ExpectNotNull(c = BN_new());
  34477. XMEMSET(&av, 0, sizeof(av));
  34478. /* Invalid parameter. */
  34479. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34480. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34481. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34482. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34483. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34484. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34485. ExpectIntEQ(BN_get_word(NULL), 0);
  34486. ExpectIntEQ(BN_get_word(&av), 0);
  34487. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34488. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34489. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34490. !defined(NO_DSA))
  34491. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34492. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34493. #endif
  34494. ExpectIntEQ(BN_one(NULL), 0);
  34495. ExpectIntEQ(BN_one(&av), 0);
  34496. BN_zero(NULL);
  34497. BN_zero(&av);
  34498. ExpectIntEQ(BN_is_one(NULL), 0);
  34499. ExpectIntEQ(BN_is_one(&av), 0);
  34500. ExpectIntEQ(BN_is_zero(NULL), 0);
  34501. ExpectIntEQ(BN_is_zero(&av), 0);
  34502. ExpectIntEQ(BN_set_word(a, 3), 1);
  34503. ExpectIntEQ(BN_set_word(b, 2), 1);
  34504. ExpectIntEQ(BN_set_word(c, 5), 1);
  34505. /* a + 3 = */
  34506. ExpectIntEQ(BN_add_word(a, 3), 1);
  34507. /* check result 3 + 3*/
  34508. ExpectIntEQ(BN_get_word(a), 6);
  34509. ExpectIntEQ(BN_is_word(a, 6), 1);
  34510. ExpectIntEQ(BN_is_word(a, 5), 0);
  34511. /* set a back to 3 */
  34512. ExpectIntEQ(BN_set_word(a, 3), 1);
  34513. /* a - 3 = */
  34514. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34515. /* check result 3 - 3*/
  34516. ExpectIntEQ(BN_get_word(a), 0);
  34517. ExpectIntEQ(BN_one(a), 1);
  34518. ExpectIntEQ(BN_is_word(a, 1), 1);
  34519. ExpectIntEQ(BN_is_word(a, 0), 0);
  34520. ExpectIntEQ(BN_is_one(a), 1);
  34521. ExpectIntEQ(BN_is_zero(a), 0);
  34522. BN_zero(a);
  34523. ExpectIntEQ(BN_is_word(a, 0), 1);
  34524. ExpectIntEQ(BN_is_word(a, 1), 0);
  34525. ExpectIntEQ(BN_is_zero(a), 1);
  34526. ExpectIntEQ(BN_is_one(a), 0);
  34527. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34528. !defined(NO_DSA))
  34529. ExpectIntEQ(BN_set_word(a, 5), 1);
  34530. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34531. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34532. #endif
  34533. BN_free(c);
  34534. BN_free(b);
  34535. BN_free(a);
  34536. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34537. return EXPECT_RESULT();
  34538. }
  34539. static int test_wolfSSL_BN_bits(void)
  34540. {
  34541. EXPECT_DECLS;
  34542. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34543. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34544. BIGNUM* a = NULL;
  34545. BIGNUM emptyBN;
  34546. /* Setup */
  34547. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34548. ExpectNotNull(a = BN_new());
  34549. /* Invalid parameters. */
  34550. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34551. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34552. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34553. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34554. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34555. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34556. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34557. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34558. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34559. ExpectIntEQ(BN_is_odd(NULL), 0);
  34560. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34561. ExpectIntEQ(BN_set_word(a, 0), 1);
  34562. ExpectIntEQ(BN_is_zero(a), 1);
  34563. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34564. ExpectIntEQ(BN_is_zero(a), 0);
  34565. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34566. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34567. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34568. ExpectIntEQ(BN_is_zero(a), 1);
  34569. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34570. ExpectIntEQ(BN_is_odd(a), 1);
  34571. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34572. ExpectIntEQ(BN_is_odd(a), 0);
  34573. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34574. ExpectIntEQ(BN_is_odd(a), 0);
  34575. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34576. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34577. #ifndef NO_WOLFSSL_STUB
  34578. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34579. #endif
  34580. BN_free(a);
  34581. #endif
  34582. return EXPECT_RESULT();
  34583. }
  34584. static int test_wolfSSL_BN_shift(void)
  34585. {
  34586. EXPECT_DECLS;
  34587. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34588. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34589. BIGNUM* a = NULL;
  34590. BIGNUM* b = NULL;
  34591. BIGNUM emptyBN;
  34592. /* Setup */
  34593. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34594. ExpectNotNull(a = BN_new());
  34595. ExpectNotNull(b = BN_new());
  34596. /* Invalid parameters. */
  34597. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34598. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34599. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34600. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34601. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34602. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34603. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34604. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34605. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34606. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34607. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34608. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34609. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34610. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34611. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34612. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34613. ExpectIntEQ(BN_set_word(a, 1), 1);
  34614. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34615. ExpectIntEQ(BN_is_word(b, 2), 1);
  34616. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34617. ExpectIntEQ(BN_is_word(a, 2), 1);
  34618. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34619. ExpectIntEQ(BN_is_word(b, 1), 1);
  34620. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34621. ExpectIntEQ(BN_is_word(a, 1), 1);
  34622. BN_free(b);
  34623. BN_free(a);
  34624. #endif
  34625. return EXPECT_RESULT();
  34626. }
  34627. static int test_wolfSSL_BN_math(void)
  34628. {
  34629. EXPECT_DECLS;
  34630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34631. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34632. BIGNUM* a = NULL;
  34633. BIGNUM* b = NULL;
  34634. BIGNUM* r = NULL;
  34635. BIGNUM* rem = NULL;
  34636. BIGNUM emptyBN;
  34637. BN_ULONG val1;
  34638. BN_ULONG val2;
  34639. /* Setup */
  34640. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34641. ExpectNotNull(a = BN_new());
  34642. ExpectNotNull(b = BN_new());
  34643. ExpectNotNull(r = BN_new());
  34644. ExpectNotNull(rem = BN_new());
  34645. /* Invalid parameters. */
  34646. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34647. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34648. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34649. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34650. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34651. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34652. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34653. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34654. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34655. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34656. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34657. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34658. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34659. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34660. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34661. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34662. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34663. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34664. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34665. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34666. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34667. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34668. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34669. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34670. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34671. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34672. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34673. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34674. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34675. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34676. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34677. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34678. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34679. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34680. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34681. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34682. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34683. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34684. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34685. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34686. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34687. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34688. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34689. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34690. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34691. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34692. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34693. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34694. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34695. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34696. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34697. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34698. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34699. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34700. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34701. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34702. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34703. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34704. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34705. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34706. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34707. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34708. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34709. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34710. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34711. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34712. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34713. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34714. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34715. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34716. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34717. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34718. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34719. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34720. /* END Invalid parameters. */
  34721. val1 = 8;
  34722. val2 = 3;
  34723. ExpectIntEQ(BN_set_word(a, val1), 1);
  34724. ExpectIntEQ(BN_set_word(b, val2), 1);
  34725. ExpectIntEQ(BN_add(r, a, b), 1);
  34726. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34727. ExpectIntEQ(BN_sub(r, a, b), 1);
  34728. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34729. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34730. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34731. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34732. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34733. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34734. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34735. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34736. BN_free(rem);
  34737. BN_free(r);
  34738. BN_free(b);
  34739. BN_free(a);
  34740. #endif
  34741. return EXPECT_RESULT();
  34742. }
  34743. static int test_wolfSSL_BN_math_mod(void)
  34744. {
  34745. EXPECT_DECLS;
  34746. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34747. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34748. BIGNUM* a = NULL;
  34749. BIGNUM* b = NULL;
  34750. BIGNUM* m = NULL;
  34751. BIGNUM* r = NULL;
  34752. BIGNUM* t = NULL;
  34753. BIGNUM emptyBN;
  34754. BN_ULONG val1;
  34755. BN_ULONG val2;
  34756. BN_ULONG val3;
  34757. /* Setup */
  34758. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34759. ExpectNotNull(a = BN_new());
  34760. ExpectNotNull(b = BN_new());
  34761. ExpectNotNull(m = BN_new());
  34762. ExpectNotNull(r = BN_new());
  34763. /* Invalid parameters. */
  34764. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34765. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34766. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34767. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34768. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34769. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34770. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34771. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34772. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34773. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34774. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34775. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34776. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34777. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34778. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34779. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34780. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34781. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34782. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34783. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34784. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34785. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34786. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34787. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34788. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34789. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34790. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34791. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34792. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34793. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34794. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34795. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34796. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34797. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34798. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34799. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34800. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34801. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34802. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34803. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34804. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34805. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34806. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34807. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34808. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34809. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34810. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34811. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34812. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34813. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34814. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34815. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34816. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34817. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34818. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34819. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34820. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34821. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34822. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34823. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34824. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34825. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34826. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34827. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34828. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34829. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34830. /* END Invalid parameters. */
  34831. val1 = 9;
  34832. val2 = 13;
  34833. val3 = 5;
  34834. ExpectIntEQ(BN_set_word(a, val1), 1);
  34835. ExpectIntEQ(BN_set_word(b, val2), 1);
  34836. ExpectIntEQ(BN_set_word(m, val3), 1);
  34837. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34838. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34839. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34840. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34841. ExpectIntEQ(BN_set_word(a, 2), 1);
  34842. ExpectIntEQ(BN_set_word(b, 3), 1);
  34843. ExpectIntEQ(BN_set_word(m, 5), 1);
  34844. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34845. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34846. ExpectIntEQ(BN_is_word(r, 3), 1);
  34847. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34848. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34849. ExpectIntEQ(BN_is_word(r, 3), 1);
  34850. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34851. ExpectIntEQ(BN_is_word(t, 3), 1);
  34852. BN_free(t);
  34853. /* No inverse case. No inverse when a divides b. */
  34854. ExpectIntEQ(BN_set_word(a, 3), 1);
  34855. ExpectIntEQ(BN_set_word(m, 9), 1);
  34856. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  34857. BN_free(r);
  34858. BN_free(m);
  34859. BN_free(b);
  34860. BN_free(a);
  34861. #endif
  34862. return EXPECT_RESULT();
  34863. }
  34864. static int test_wolfSSL_BN_math_other(void)
  34865. {
  34866. EXPECT_DECLS;
  34867. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34868. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34869. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34870. BIGNUM* a = NULL;
  34871. BIGNUM* b = NULL;
  34872. BIGNUM* r = NULL;
  34873. BIGNUM emptyBN;
  34874. /* Setup */
  34875. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34876. ExpectNotNull(a = BN_new());
  34877. ExpectNotNull(b = BN_new());
  34878. ExpectNotNull(r = BN_new());
  34879. /* Invalid parameters. */
  34880. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34881. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34882. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34883. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34884. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34885. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34886. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34887. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34888. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34889. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34890. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34891. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34892. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34893. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34894. /* END Invalid parameters. */
  34895. /* No common factors between 2 and 3. */
  34896. ExpectIntEQ(BN_set_word(a, 2), 1);
  34897. ExpectIntEQ(BN_set_word(b, 3), 1);
  34898. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34899. ExpectIntEQ(BN_is_word(r, 1), 1);
  34900. /* 3 is largest value that divides both 6 and 9. */
  34901. ExpectIntEQ(BN_set_word(a, 6), 1);
  34902. ExpectIntEQ(BN_set_word(b, 9), 1);
  34903. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34904. ExpectIntEQ(BN_is_word(r, 3), 1);
  34905. /* GCD of 0 and 0 is undefined. */
  34906. ExpectIntEQ(BN_set_word(a, 0), 1);
  34907. ExpectIntEQ(BN_set_word(b, 0), 1);
  34908. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  34909. /* Teardown */
  34910. BN_free(r);
  34911. BN_free(b);
  34912. BN_free(a);
  34913. #endif
  34914. #endif
  34915. return EXPECT_RESULT();
  34916. }
  34917. static int test_wolfSSL_BN_rand(void)
  34918. {
  34919. EXPECT_DECLS;
  34920. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  34921. BIGNUM* bn = NULL;
  34922. BIGNUM* range = NULL;
  34923. BIGNUM emptyBN;
  34924. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34925. ExpectNotNull(bn = BN_new());
  34926. ExpectNotNull(range = BN_new());
  34927. /* Invalid parameters. */
  34928. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  34929. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34930. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  34931. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  34932. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34933. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  34934. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  34935. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34936. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  34937. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  34938. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34939. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  34940. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  34941. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  34942. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  34943. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  34944. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  34945. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  34946. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  34947. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34948. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34949. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34950. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34951. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34952. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34953. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34954. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34955. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34956. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34957. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34958. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34959. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34960. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34961. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34962. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34963. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34964. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34965. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34966. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34967. /* 1 bit random value must have no more than one top bit set. */
  34968. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34969. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34970. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34971. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34972. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34973. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34974. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34975. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34976. /* END Invalid parameters. */
  34977. /* 0 bit random: 0. */
  34978. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34979. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34980. ExpectIntEQ(BN_is_zero(bn), 1);
  34981. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  34982. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34983. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34984. ExpectIntEQ(BN_is_zero(bn), 1);
  34985. /* 1 bit random: 0 or 1. */
  34986. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34987. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34988. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34989. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34990. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34991. ExpectIntEQ(BN_get_word(bn), 1);
  34992. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34993. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34994. ExpectIntEQ(BN_get_word(bn), 1);
  34995. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34996. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34997. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34998. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34999. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35000. ExpectIntEQ(BN_get_word(bn), 1);
  35001. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35002. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35003. ExpectIntEQ(BN_get_word(bn), 1);
  35004. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35005. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35006. ExpectIntEQ(BN_num_bits(bn), 8);
  35007. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35008. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35009. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35010. ExpectIntEQ(BN_num_bits(bn), 8);
  35011. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35012. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35013. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35014. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35015. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35016. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35017. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35018. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35019. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35020. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35021. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35022. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35023. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35024. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35025. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35026. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35027. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35028. * case, requesting a 13-bit random number would actually return a 16-bit
  35029. * random number. */
  35030. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35031. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35032. ExpectIntEQ(BN_num_bits(bn), 13);
  35033. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35034. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35035. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35036. ExpectIntEQ(BN_set_word(range, 0), 1);
  35037. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35038. ExpectIntEQ(BN_set_word(range, 1), 1);
  35039. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35040. BN_free(bn);
  35041. BN_free(range);
  35042. #endif
  35043. return EXPECT_RESULT();
  35044. }
  35045. static int test_wolfSSL_BN_prime(void)
  35046. {
  35047. EXPECT_DECLS;
  35048. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35049. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35050. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35051. BIGNUM* a = NULL;
  35052. BIGNUM* add = NULL;
  35053. BIGNUM* rem = NULL;
  35054. BIGNUM emptyBN;
  35055. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35056. ExpectNotNull(a = BN_new());
  35057. ExpectNotNull(add = BN_new());
  35058. ExpectNotNull(rem = BN_new());
  35059. /* Invalid parameters. */
  35060. /* BN_generate_prime_ex()
  35061. * prime - must have valid BIGNUM
  35062. * bits - Greater then 0
  35063. * safe - not supported, must be 0
  35064. * add - not supported, must be NULL
  35065. * rem - not supported, must be NULL
  35066. * cb - anything
  35067. */
  35068. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35069. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35070. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35071. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35072. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35073. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35074. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35075. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35076. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35077. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35078. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35079. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35080. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35081. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35082. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35083. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35084. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35085. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35086. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35087. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35088. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35089. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35090. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35091. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35092. /* END Invalid parameters. */
  35093. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35094. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35095. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35096. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35097. BN_free(rem);
  35098. BN_free(add);
  35099. BN_free(a);
  35100. #endif
  35101. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35102. return EXPECT_RESULT();
  35103. }
  35104. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35105. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35106. #define TEST_ARG 0x1234
  35107. static void msg_cb(int write_p, int version, int content_type,
  35108. const void *buf, size_t len, SSL *ssl, void *arg)
  35109. {
  35110. (void)write_p;
  35111. (void)version;
  35112. (void)content_type;
  35113. (void)buf;
  35114. (void)len;
  35115. (void)ssl;
  35116. AssertTrue(arg == (void*)TEST_ARG);
  35117. }
  35118. #endif
  35119. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35120. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35121. #if defined(SESSION_CERTS)
  35122. #include "wolfssl/internal.h"
  35123. #endif
  35124. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35125. {
  35126. EXPECT_DECLS;
  35127. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35128. STACK_OF(X509)* sk = NULL;
  35129. X509* x509 = NULL;
  35130. int i, num;
  35131. BIO* bio = NULL;
  35132. #endif
  35133. ExpectNotNull(ctx);
  35134. ExpectNotNull(ssl);
  35135. fprintf(stderr, "\n===== msgcb called ====\n");
  35136. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35137. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35138. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35139. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35140. #endif
  35141. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35142. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35143. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35144. if (sk == NULL) {
  35145. BIO_free(bio);
  35146. return TEST_FAIL;
  35147. }
  35148. num = sk_X509_num(sk);
  35149. ExpectTrue(num > 0);
  35150. for (i = 0; i < num; i++) {
  35151. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35152. if (x509 == NULL)
  35153. break;
  35154. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35155. X509_print(bio,x509);
  35156. fprintf(stderr, "\n\n");
  35157. }
  35158. BIO_free(bio);
  35159. #endif
  35160. return EXPECT_RESULT();
  35161. }
  35162. #endif
  35163. static int test_wolfSSL_msgCb(void)
  35164. {
  35165. EXPECT_DECLS;
  35166. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35167. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35168. test_ssl_cbf client_cb;
  35169. test_ssl_cbf server_cb;
  35170. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35171. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35172. #ifndef WOLFSSL_NO_TLS12
  35173. client_cb.method = wolfTLSv1_2_client_method;
  35174. server_cb.method = wolfTLSv1_2_server_method;
  35175. #else
  35176. client_cb.method = wolfTLSv1_3_client_method;
  35177. server_cb.method = wolfTLSv1_3_server_method;
  35178. #endif
  35179. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35180. &server_cb, msgCb), TEST_SUCCESS);
  35181. #endif
  35182. return EXPECT_RESULT();
  35183. }
  35184. static int test_wolfSSL_either_side(void)
  35185. {
  35186. EXPECT_DECLS;
  35187. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35188. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35189. test_ssl_cbf client_cb;
  35190. test_ssl_cbf server_cb;
  35191. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35192. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35193. /* Use different CTX for client and server */
  35194. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35195. ExpectNotNull(client_cb.ctx);
  35196. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35197. ExpectNotNull(server_cb.ctx);
  35198. /* we are responsible for free'ing WOLFSSL_CTX */
  35199. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35200. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35201. &server_cb, NULL), TEST_SUCCESS);
  35202. wolfSSL_CTX_free(client_cb.ctx);
  35203. wolfSSL_CTX_free(server_cb.ctx);
  35204. #endif
  35205. return EXPECT_RESULT();
  35206. }
  35207. static int test_wolfSSL_DTLS_either_side(void)
  35208. {
  35209. EXPECT_DECLS;
  35210. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35211. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35212. test_ssl_cbf client_cb;
  35213. test_ssl_cbf server_cb;
  35214. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35215. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35216. /* Use different CTX for client and server */
  35217. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35218. ExpectNotNull(client_cb.ctx);
  35219. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35220. ExpectNotNull(server_cb.ctx);
  35221. /* we are responsible for free'ing WOLFSSL_CTX */
  35222. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35223. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35224. &server_cb, NULL), TEST_SUCCESS);
  35225. wolfSSL_CTX_free(client_cb.ctx);
  35226. wolfSSL_CTX_free(server_cb.ctx);
  35227. #endif
  35228. return EXPECT_RESULT();
  35229. }
  35230. static int test_generate_cookie(void)
  35231. {
  35232. EXPECT_DECLS;
  35233. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35234. SSL_CTX* ctx = NULL;
  35235. SSL* ssl = NULL;
  35236. byte buf[FOURK_BUF] = {0};
  35237. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35238. ExpectNotNull(ssl = SSL_new(ctx));
  35239. /* Test unconnected */
  35240. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35241. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35242. wolfSSL_SetCookieCtx(ssl, ctx);
  35243. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35244. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35245. SSL_free(ssl);
  35246. SSL_CTX_free(ctx);
  35247. #endif
  35248. return EXPECT_RESULT();
  35249. }
  35250. static int test_wolfSSL_set_options(void)
  35251. {
  35252. EXPECT_DECLS;
  35253. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35254. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35255. WOLFSSL* ssl = NULL;
  35256. WOLFSSL_CTX* ctx = NULL;
  35257. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35258. char appData[] = "extra msg";
  35259. #endif
  35260. #ifdef OPENSSL_EXTRA
  35261. unsigned char protos[] = {
  35262. 7, 't', 'l', 's', '/', '1', '.', '2',
  35263. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35264. };
  35265. unsigned int len = sizeof(protos);
  35266. void *arg = (void *)TEST_ARG;
  35267. #endif
  35268. #ifndef NO_WOLFSSL_SERVER
  35269. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35270. #else
  35271. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35272. #endif
  35273. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35274. WOLFSSL_FILETYPE_PEM));
  35275. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35276. WOLFSSL_FILETYPE_PEM));
  35277. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35278. == WOLFSSL_OP_NO_TLSv1);
  35279. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35280. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35281. WOLFSSL_OP_NO_SSLv2)), 0);
  35282. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35283. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35284. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35285. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35286. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35287. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35288. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35289. WOLFSSL_OP_NO_COMPRESSION));
  35290. wolfSSL_CTX_free(ctx);
  35291. ctx = NULL;
  35292. #ifndef NO_WOLFSSL_SERVER
  35293. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35294. #else
  35295. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35296. #endif
  35297. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35298. WOLFSSL_FILETYPE_PEM));
  35299. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35300. WOLFSSL_FILETYPE_PEM));
  35301. #ifdef OPENSSL_EXTRA
  35302. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35303. #endif
  35304. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35305. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35306. #ifdef HAVE_EX_DATA
  35307. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35308. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35309. if (ssl != NULL) {
  35310. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35311. appData, sizeof(appData)), 0);
  35312. }
  35313. #else
  35314. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35315. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35316. #endif
  35317. #endif
  35318. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35319. WOLFSSL_OP_NO_TLSv1);
  35320. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35321. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35322. WOLFSSL_OP_NO_SSLv2)), 0);
  35323. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35324. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35325. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35326. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35327. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35328. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35329. #ifdef OPENSSL_EXTRA
  35330. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35331. WOLFSSL_OP_NO_COMPRESSION));
  35332. #endif
  35333. #ifdef OPENSSL_EXTRA
  35334. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35335. wolfSSL_set_msg_callback_arg(ssl, arg);
  35336. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35337. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35338. #else
  35339. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35340. #endif
  35341. #endif
  35342. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35343. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35344. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35345. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35346. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35347. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35348. #else
  35349. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35350. #endif
  35351. #endif /* HAVE_ALPN && !NO_BIO */
  35352. #endif
  35353. wolfSSL_free(ssl);
  35354. wolfSSL_CTX_free(ctx);
  35355. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35356. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35357. return EXPECT_RESULT();
  35358. }
  35359. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35360. {
  35361. EXPECT_DECLS;
  35362. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35363. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35364. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35365. SSL* ssl = NULL;
  35366. SSL_CTX* ctx = NULL;
  35367. STACK_OF(SSL_CIPHER) *sk = NULL;
  35368. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35369. #ifndef NO_WOLFSSL_SERVER
  35370. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35371. #else
  35372. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35373. #endif
  35374. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35375. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35376. ExpectNotNull(ssl = SSL_new(ctx));
  35377. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35378. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35379. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35380. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35381. /* error case because connection has not been established yet */
  35382. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35383. sk_SSL_CIPHER_free(dupSk);
  35384. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35385. SSL_free(ssl);
  35386. SSL_CTX_free(ctx);
  35387. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35388. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35389. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35390. return EXPECT_RESULT();
  35391. }
  35392. static int test_wolfSSL_set1_curves_list(void)
  35393. {
  35394. EXPECT_DECLS;
  35395. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35396. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35397. SSL* ssl = NULL;
  35398. SSL_CTX* ctx = NULL;
  35399. #ifndef NO_WOLFSSL_SERVER
  35400. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35401. #else
  35402. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35403. #endif
  35404. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35405. SSL_FILETYPE_PEM));
  35406. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35407. ExpectNotNull(ssl = SSL_new(ctx));
  35408. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35409. #ifdef HAVE_ECC
  35410. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35411. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35412. #endif
  35413. #ifdef HAVE_CURVE25519
  35414. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35415. #else
  35416. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35417. #endif
  35418. #ifdef HAVE_CURVE448
  35419. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35420. #else
  35421. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35422. #endif
  35423. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35424. #ifdef HAVE_ECC
  35425. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35426. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35427. #endif
  35428. #ifdef HAVE_CURVE25519
  35429. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35430. #else
  35431. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35432. #endif
  35433. #ifdef HAVE_CURVE448
  35434. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35435. #else
  35436. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35437. #endif
  35438. SSL_free(ssl);
  35439. SSL_CTX_free(ctx);
  35440. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35441. #endif
  35442. return EXPECT_RESULT();
  35443. }
  35444. static int test_wolfSSL_set1_sigalgs_list(void)
  35445. {
  35446. EXPECT_DECLS;
  35447. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35448. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35449. SSL* ssl = NULL;
  35450. SSL_CTX* ctx = NULL;
  35451. #ifndef NO_WOLFSSL_SERVER
  35452. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35453. #else
  35454. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35455. #endif
  35456. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35457. SSL_FILETYPE_PEM));
  35458. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35459. ExpectNotNull(ssl = SSL_new(ctx));
  35460. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35461. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35462. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35463. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35464. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35465. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35466. #ifndef NO_RSA
  35467. #ifndef NO_SHA256
  35468. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35469. WOLFSSL_FAILURE);
  35470. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35471. WOLFSSL_FAILURE);
  35472. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35473. WOLFSSL_SUCCESS);
  35474. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35475. WOLFSSL_SUCCESS);
  35476. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35477. WOLFSSL_FAILURE);
  35478. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35479. WOLFSSL_FAILURE);
  35480. #ifdef WC_RSA_PSS
  35481. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35482. WOLFSSL_SUCCESS);
  35483. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35484. WOLFSSL_SUCCESS);
  35485. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35486. WOLFSSL_SUCCESS);
  35487. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35488. WOLFSSL_SUCCESS);
  35489. #endif
  35490. #ifdef WOLFSSL_SHA512
  35491. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35492. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35493. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35494. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35495. #elif defined(WOLFSSL_SHA384)
  35496. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35497. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35498. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35499. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35500. #endif
  35501. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35502. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35503. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35504. WOLFSSL_FAILURE);
  35505. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35506. WOLFSSL_FAILURE);
  35507. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35508. WOLFSSL_FAILURE);
  35509. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35510. WOLFSSL_FAILURE);
  35511. #endif
  35512. #endif
  35513. #ifdef HAVE_ECC
  35514. #ifndef NO_SHA256
  35515. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35516. WOLFSSL_SUCCESS);
  35517. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35518. WOLFSSL_SUCCESS);
  35519. #ifdef WOLFSSL_SHA512
  35520. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35521. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35522. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35523. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35524. #elif defined(WOLFSSL_SHA384)
  35525. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35526. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35527. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35528. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35529. #endif
  35530. #endif
  35531. #endif
  35532. #ifdef HAVE_ED25519
  35533. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35534. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35535. #endif
  35536. #ifdef HAVE_ED448
  35537. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35538. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35539. #endif
  35540. #ifndef NO_DSA
  35541. #ifndef NO_SHA256
  35542. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35543. WOLFSSL_SUCCESS);
  35544. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35545. WOLFSSL_SUCCESS);
  35546. #endif
  35547. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35548. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35549. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35550. WOLFSSL_SUCCESS);
  35551. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35552. WOLFSSL_SUCCESS);
  35553. #endif
  35554. #endif
  35555. SSL_free(ssl);
  35556. SSL_CTX_free(ctx);
  35557. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35558. #endif
  35559. return EXPECT_RESULT();
  35560. }
  35561. /* Testing wolfSSL_set_tlsext_status_type function.
  35562. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35563. */
  35564. static int test_wolfSSL_set_tlsext_status_type(void)
  35565. {
  35566. EXPECT_DECLS;
  35567. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35568. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35569. SSL* ssl = NULL;
  35570. SSL_CTX* ctx = NULL;
  35571. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35572. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35573. SSL_FILETYPE_PEM));
  35574. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35575. ExpectNotNull(ssl = SSL_new(ctx));
  35576. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35577. SSL_SUCCESS);
  35578. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35579. SSL_free(ssl);
  35580. SSL_CTX_free(ctx);
  35581. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35582. return EXPECT_RESULT();
  35583. }
  35584. #ifndef NO_BIO
  35585. static int test_wolfSSL_PEM_read_bio(void)
  35586. {
  35587. EXPECT_DECLS;
  35588. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35589. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35590. byte buff[6000];
  35591. XFILE f = XBADFILE;
  35592. int bytes;
  35593. X509* x509 = NULL;
  35594. BIO* bio = NULL;
  35595. BUF_MEM* buf = NULL;
  35596. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35597. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35598. if (f != XBADFILE)
  35599. XFCLOSE(f);
  35600. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35601. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35602. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35603. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35604. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35605. /* BIO should return the set EOF value */
  35606. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35607. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35608. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35609. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35610. BIO_free(bio);
  35611. BUF_MEM_free(buf);
  35612. X509_free(x509);
  35613. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35614. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35615. return EXPECT_RESULT();
  35616. }
  35617. #if defined(OPENSSL_EXTRA)
  35618. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35619. long argl, long ret)
  35620. {
  35621. (void)bio;
  35622. (void)cmd;
  35623. (void)argp;
  35624. (void)argi;
  35625. (void)argl;
  35626. return ret;
  35627. }
  35628. #endif
  35629. static int test_wolfSSL_BIO(void)
  35630. {
  35631. EXPECT_DECLS;
  35632. #if defined(OPENSSL_EXTRA)
  35633. const unsigned char* p = NULL;
  35634. byte buff[20];
  35635. BIO* bio1 = NULL;
  35636. BIO* bio2 = NULL;
  35637. BIO* bio3 = NULL;
  35638. char* bufPt = NULL;
  35639. int i;
  35640. for (i = 0; i < 20; i++) {
  35641. buff[i] = i;
  35642. }
  35643. /* test BIO_free with NULL */
  35644. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35645. /* Creating and testing type BIO_s_bio */
  35646. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35647. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35648. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35649. /* read/write before set up */
  35650. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35651. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35652. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35653. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35654. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35655. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35656. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35657. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35658. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35659. /* write buffer full */
  35660. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35661. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35662. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35663. /* write the other direction with pair */
  35664. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35665. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35666. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35667. /* try read */
  35668. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35669. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35670. /* try read using ctrl function */
  35671. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35672. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35673. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35674. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35675. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35676. for (i = 0; i < 20; i++) {
  35677. ExpectIntEQ((int)bufPt[i], i);
  35678. }
  35679. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35680. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35681. for (i = 0; i < 8; i++) {
  35682. ExpectIntEQ((int)bufPt[i], i);
  35683. }
  35684. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35685. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35686. /* new pair */
  35687. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35688. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35689. bio2 = NULL;
  35690. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35691. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35692. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35693. /* test wrap around... */
  35694. ExpectIntEQ(BIO_reset(bio1), 0);
  35695. ExpectIntEQ(BIO_reset(bio3), 0);
  35696. /* fill write buffer, read only small amount then write again */
  35697. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35698. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35699. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35700. for (i = 0; i < 4; i++) {
  35701. ExpectIntEQ(bufPt[i], i);
  35702. }
  35703. /* try writing over read index */
  35704. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35705. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35706. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35707. /* read and write 0 bytes */
  35708. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35709. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35710. /* should read only to end of write buffer then need to read again */
  35711. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35712. for (i = 0; i < 16; i++) {
  35713. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35714. }
  35715. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35716. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35717. for (i = 0; i < 4; i++) {
  35718. ExpectIntEQ(bufPt[i], 0);
  35719. }
  35720. /* read index should not have advanced with nread0 */
  35721. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35722. for (i = 0; i < 4; i++) {
  35723. ExpectIntEQ(bufPt[i], 0);
  35724. }
  35725. /* write and fill up buffer checking reset of index state */
  35726. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35727. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35728. /* test reset on data in bio1 write buffer */
  35729. ExpectIntEQ(BIO_reset(bio1), 0);
  35730. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35731. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35732. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35733. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35734. ExpectNotNull(p);
  35735. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35736. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35737. for (i = 0; i < 6; i++) {
  35738. ExpectIntEQ(bufPt[i], i);
  35739. }
  35740. /* test case of writing twice with offset read index */
  35741. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35742. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35743. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35744. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35745. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35746. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35747. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35748. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35749. BIO_free(bio1);
  35750. bio1 = NULL;
  35751. BIO_free(bio3);
  35752. bio3 = NULL;
  35753. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35754. {
  35755. BIO* bioA = NULL;
  35756. BIO* bioB = NULL;
  35757. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35758. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35759. BIO_free(bioA);
  35760. bioA = NULL;
  35761. BIO_free(bioB);
  35762. bioB = NULL;
  35763. }
  35764. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35765. /* BIOs with file pointers */
  35766. #if !defined(NO_FILESYSTEM)
  35767. {
  35768. XFILE f1 = XBADFILE;
  35769. XFILE f2 = XBADFILE;
  35770. BIO* f_bio1 = NULL;
  35771. BIO* f_bio2 = NULL;
  35772. unsigned char cert[300];
  35773. char testFile[] = "tests/bio_write_test.txt";
  35774. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35775. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35776. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35777. /* Failure due to wrong BIO type */
  35778. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35779. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35780. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35781. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35782. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35783. WOLFSSL_SUCCESS);
  35784. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35785. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35786. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35787. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35788. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35789. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35790. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35791. ExpectIntEQ(BIO_reset(f_bio2), 0);
  35792. ExpectIntEQ(BIO_tell(NULL),-1);
  35793. ExpectIntEQ(BIO_tell(f_bio2),0);
  35794. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  35795. ExpectIntEQ(BIO_tell(f_bio2),4);
  35796. BIO_free(f_bio1);
  35797. f_bio1 = NULL;
  35798. BIO_free(f_bio2);
  35799. f_bio2 = NULL;
  35800. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35801. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35802. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35803. BIO_free(f_bio1);
  35804. f_bio1 = NULL;
  35805. }
  35806. #endif /* !defined(NO_FILESYSTEM) */
  35807. /* BIO info callback */
  35808. {
  35809. const char* testArg = "test";
  35810. BIO* cb_bio = NULL;
  35811. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35812. BIO_set_callback(cb_bio, bioCallback);
  35813. ExpectNotNull(BIO_get_callback(cb_bio));
  35814. BIO_set_callback(cb_bio, NULL);
  35815. ExpectNull(BIO_get_callback(cb_bio));
  35816. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35817. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35818. ExpectNull(BIO_get_callback_arg(NULL));
  35819. BIO_free(cb_bio);
  35820. cb_bio = NULL;
  35821. }
  35822. /* BIO_vfree */
  35823. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35824. BIO_vfree(NULL);
  35825. BIO_vfree(bio1);
  35826. #endif
  35827. return EXPECT_RESULT();
  35828. }
  35829. static int test_wolfSSL_BIO_BIO_ring_read(void)
  35830. {
  35831. EXPECT_DECLS;
  35832. #if defined(OPENSSL_ALL)
  35833. BIO* bio1 = NULL;
  35834. BIO* bio2 = NULL;
  35835. byte data[50];
  35836. byte tmp[50];
  35837. XMEMSET(data, 42, sizeof(data));
  35838. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  35839. SSL_SUCCESS);
  35840. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  35841. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  35842. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  35843. ExpectBufEQ(tmp, data, 20);
  35844. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  35845. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  35846. ExpectBufEQ(tmp, data, 40);
  35847. BIO_free(bio1);
  35848. BIO_free(bio2);
  35849. #endif
  35850. return EXPECT_RESULT();
  35851. }
  35852. #endif /* !NO_BIO */
  35853. static int test_wolfSSL_a2i_IPADDRESS(void)
  35854. {
  35855. EXPECT_DECLS;
  35856. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35857. const unsigned char* data = NULL;
  35858. int dataSz = 0;
  35859. ASN1_OCTET_STRING *st = NULL;
  35860. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35861. const unsigned char ipv6_exp[] = {
  35862. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35863. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35864. };
  35865. const unsigned char ipv6_home[] = {
  35866. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35867. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35868. };
  35869. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35870. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35871. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35872. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  35873. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35874. ASN1_STRING_free(st);
  35875. st = NULL;
  35876. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  35877. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35878. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35879. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  35880. ASN1_STRING_free(st);
  35881. st = NULL;
  35882. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  35883. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35884. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35885. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  35886. ASN1_STRING_free(st);
  35887. #endif
  35888. return EXPECT_RESULT();
  35889. }
  35890. static int test_wolfSSL_X509_cmp_time(void)
  35891. {
  35892. EXPECT_DECLS;
  35893. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  35894. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  35895. WOLFSSL_ASN1_TIME asn_time;
  35896. time_t t;
  35897. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  35898. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  35899. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  35900. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  35901. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  35902. #endif
  35903. return EXPECT_RESULT();
  35904. }
  35905. static int test_wolfSSL_X509_time_adj(void)
  35906. {
  35907. EXPECT_DECLS;
  35908. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  35909. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  35910. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  35911. !defined(NO_ASN_TIME)
  35912. X509* x509 = NULL;
  35913. time_t t;
  35914. time_t not_before;
  35915. time_t not_after;
  35916. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  35917. client_cert_der_2048, sizeof_client_cert_der_2048,
  35918. WOLFSSL_FILETYPE_ASN1));
  35919. t = 0;
  35920. not_before = wc_Time(0);
  35921. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  35922. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  35923. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  35924. /* Check X509_gmtime_adj, too. */
  35925. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  35926. X509_free(x509);
  35927. #endif
  35928. return EXPECT_RESULT();
  35929. }
  35930. static int test_wolfSSL_X509(void)
  35931. {
  35932. EXPECT_DECLS;
  35933. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35934. !defined(NO_RSA)
  35935. X509* x509 = NULL;
  35936. #ifndef NO_BIO
  35937. BIO* bio = NULL;
  35938. X509_STORE_CTX* ctx = NULL;
  35939. X509_STORE* store = NULL;
  35940. #endif
  35941. char der[] = "certs/ca-cert.der";
  35942. XFILE fp = XBADFILE;
  35943. ExpectNotNull(x509 = X509_new());
  35944. X509_free(x509);
  35945. x509 = NULL;
  35946. #ifndef NO_BIO
  35947. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35948. SSL_FILETYPE_PEM));
  35949. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35950. #ifdef WOLFSSL_CERT_GEN
  35951. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  35952. #endif
  35953. ExpectNotNull(ctx = X509_STORE_CTX_new());
  35954. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  35955. ExpectNotNull(store = X509_STORE_new());
  35956. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  35957. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  35958. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  35959. X509_STORE_CTX_free(ctx);
  35960. X509_STORE_free(store);
  35961. X509_free(x509);
  35962. x509 = NULL;
  35963. BIO_free(bio);
  35964. #endif
  35965. /** d2i_X509_fp test **/
  35966. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35967. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  35968. ExpectNotNull(x509);
  35969. X509_free(x509);
  35970. x509 = NULL;
  35971. if (fp != XBADFILE) {
  35972. XFCLOSE(fp);
  35973. fp = XBADFILE;
  35974. }
  35975. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35976. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  35977. ExpectNotNull(x509);
  35978. X509_free(x509);
  35979. if (fp != XBADFILE)
  35980. XFCLOSE(fp);
  35981. /* X509_up_ref test */
  35982. ExpectIntEQ(X509_up_ref(NULL), 0);
  35983. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  35984. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  35985. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  35986. X509_free(x509); /* refCount = 2 */
  35987. X509_free(x509); /* refCount = 1 */
  35988. X509_free(x509); /* refCount = 0, free */
  35989. #endif
  35990. return EXPECT_RESULT();
  35991. }
  35992. static int test_wolfSSL_X509_get_ext_count(void)
  35993. {
  35994. EXPECT_DECLS;
  35995. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35996. !defined(NO_RSA)
  35997. int ret = 0;
  35998. WOLFSSL_X509* x509 = NULL;
  35999. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36000. XFILE f = XBADFILE;
  36001. /* NULL parameter check */
  36002. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36003. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36004. SSL_FILETYPE_PEM));
  36005. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36006. wolfSSL_X509_free(x509);
  36007. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36008. SSL_FILETYPE_PEM));
  36009. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36010. wolfSSL_X509_free(x509);
  36011. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36012. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36013. if (f != XBADFILE)
  36014. XFCLOSE(f);
  36015. /* wolfSSL_X509_get_ext_count() valid input */
  36016. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36017. /* wolfSSL_X509_get_ext_count() NULL argument */
  36018. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36019. wolfSSL_X509_free(x509);
  36020. #endif
  36021. return EXPECT_RESULT();
  36022. }
  36023. static int test_wolfSSL_X509_sign2(void)
  36024. {
  36025. EXPECT_DECLS;
  36026. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36027. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36028. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36029. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36030. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36031. defined(WOLFSSL_IP_ALT_NAME))
  36032. WOLFSSL_X509 *x509 = NULL;
  36033. WOLFSSL_X509 *ca = NULL;
  36034. const unsigned char *der = NULL;
  36035. const unsigned char *pt = NULL;
  36036. WOLFSSL_EVP_PKEY *priv = NULL;
  36037. WOLFSSL_X509_NAME *name = NULL;
  36038. int derSz;
  36039. #ifndef NO_ASN_TIME
  36040. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36041. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36042. const int year = 365*24*60*60;
  36043. const int day = 24*60*60;
  36044. const int hour = 60*60;
  36045. const int mini = 60;
  36046. time_t t;
  36047. #endif
  36048. const unsigned char expected[] = {
  36049. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36050. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36051. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36052. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36053. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36054. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36055. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36056. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36057. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36058. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36059. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36060. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36061. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36062. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36063. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36064. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36065. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36066. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36067. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36068. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36069. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36070. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36071. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36072. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36073. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36074. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36075. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36076. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36077. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36078. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36079. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36080. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36081. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36082. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36083. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36084. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36085. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36086. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36087. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36088. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36089. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36090. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36091. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36092. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36093. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36094. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36095. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36096. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36097. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36098. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36099. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36100. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36101. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36102. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36103. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36104. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36105. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36106. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36107. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36108. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36109. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36110. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36111. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36112. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36113. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36114. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36115. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36116. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36117. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36118. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36119. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36120. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36121. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36122. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36123. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36124. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36125. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36126. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36127. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36128. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36129. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36130. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36131. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36132. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36133. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36134. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36135. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36136. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36137. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36138. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36139. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36140. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36141. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36142. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36143. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36144. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36145. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36146. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36147. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36148. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36149. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36150. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36151. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36152. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36153. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36154. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36155. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36156. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36157. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36158. };
  36159. pt = ca_key_der_2048;
  36160. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36161. sizeof_ca_key_der_2048));
  36162. pt = client_cert_der_2048;
  36163. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36164. sizeof_client_cert_der_2048));
  36165. pt = ca_cert_der_2048;
  36166. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36167. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36168. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36169. #ifndef NO_ASN_TIME
  36170. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36171. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36172. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36173. ExpectIntEQ(notAfter->length, 13);
  36174. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36175. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36176. #endif
  36177. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36178. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36179. ExpectIntEQ(derSz, sizeof(expected));
  36180. #ifndef NO_ASN_TIME
  36181. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36182. #endif
  36183. wolfSSL_X509_free(ca);
  36184. wolfSSL_X509_free(x509);
  36185. wolfSSL_EVP_PKEY_free(priv);
  36186. #ifndef NO_ASN_TIME
  36187. wolfSSL_ASN1_TIME_free(notBefore);
  36188. wolfSSL_ASN1_TIME_free(notAfter);
  36189. #endif
  36190. #endif
  36191. return EXPECT_RESULT();
  36192. }
  36193. static int test_wolfSSL_X509_sign(void)
  36194. {
  36195. EXPECT_DECLS;
  36196. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36197. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36198. int ret = 0;
  36199. char *cn = NULL;
  36200. word32 cnSz = 0;
  36201. X509_NAME *name = NULL;
  36202. X509 *x509 = NULL;
  36203. X509 *ca = NULL;
  36204. DecodedCert dCert;
  36205. EVP_PKEY *pub = NULL;
  36206. EVP_PKEY *priv = NULL;
  36207. EVP_MD_CTX *mctx = NULL;
  36208. #if defined(USE_CERT_BUFFERS_1024)
  36209. const unsigned char* rsaPriv = client_key_der_1024;
  36210. const unsigned char* rsaPub = client_keypub_der_1024;
  36211. const unsigned char* certIssuer = client_cert_der_1024;
  36212. long clientKeySz = (long)sizeof_client_key_der_1024;
  36213. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36214. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36215. #elif defined(USE_CERT_BUFFERS_2048)
  36216. const unsigned char* rsaPriv = client_key_der_2048;
  36217. const unsigned char* rsaPub = client_keypub_der_2048;
  36218. const unsigned char* certIssuer = client_cert_der_2048;
  36219. long clientKeySz = (long)sizeof_client_key_der_2048;
  36220. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36221. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36222. #endif
  36223. byte sn[16];
  36224. int snSz = sizeof(sn);
  36225. /* Set X509_NAME fields */
  36226. ExpectNotNull(name = X509_NAME_new());
  36227. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36228. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36229. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36230. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36231. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36232. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36233. /* Get private and public keys */
  36234. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36235. clientKeySz));
  36236. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36237. ExpectNotNull(x509 = X509_new());
  36238. /* Set version 3 */
  36239. ExpectIntNE(X509_set_version(x509, 2L), 0);
  36240. /* Set subject name, add pubkey, and sign certificate */
  36241. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36242. X509_NAME_free(name);
  36243. name = NULL;
  36244. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36245. #ifdef WOLFSSL_ALT_NAMES
  36246. /* Add some subject alt names */
  36247. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  36248. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36249. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36250. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36251. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36252. "sphygmomanometer",
  36253. ASN_DNS_TYPE), SSL_SUCCESS);
  36254. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36255. "supercalifragilisticexpialidocious",
  36256. ASN_DNS_TYPE), SSL_SUCCESS);
  36257. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36258. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36259. ASN_DNS_TYPE), SSL_SUCCESS);
  36260. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36261. {
  36262. unsigned char ip4_type[] = {127,128,0,255};
  36263. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36264. 0xff, 0xee, 0x99, 0x88,
  36265. 0x77, 0x66, 0x55, 0x44,
  36266. 0x00, 0x33, 0x22, 0x11};
  36267. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36268. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36269. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36270. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36271. }
  36272. #endif
  36273. #endif /* WOLFSSL_ALT_NAMES */
  36274. /* test valid sign case */
  36275. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36276. /* test valid X509_sign_ctx case */
  36277. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36278. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36279. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  36280. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36281. ExpectIntEQ(X509_get_ext_count(x509), 1);
  36282. #endif
  36283. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36284. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36285. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36286. #endif
  36287. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36288. WOLFSSL_SUCCESS);
  36289. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36290. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36291. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36292. * with the MSB set. See GenerateInteger in asn.c */
  36293. #ifndef USE_CERT_BUFFERS_1024
  36294. #ifndef WOLFSSL_ALT_NAMES
  36295. /* Valid case - size should be 781-786 with 16 byte serial number */
  36296. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36297. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36298. /* Valid case - size should be 955-960 with 16 byte serial number */
  36299. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36300. #else
  36301. /* Valid case - size should be 926-931 with 16 byte serial number */
  36302. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36303. #endif
  36304. #else
  36305. #ifndef WOLFSSL_ALT_NAMES
  36306. /* Valid case - size should be 537-542 with 16 byte serial number */
  36307. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36308. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36309. /* Valid case - size should be 695-670 with 16 byte serial number */
  36310. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36311. #else
  36312. /* Valid case - size should be 666-671 with 16 byte serial number */
  36313. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36314. #endif
  36315. #endif
  36316. /* check that issuer name is as expected after signature */
  36317. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36318. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36319. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36320. ExpectNotNull(name = X509_get_subject_name(ca));
  36321. cnSz = X509_NAME_get_sz(name);
  36322. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36323. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36324. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36325. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36326. cn = NULL;
  36327. #ifdef WOLFSSL_MULTI_ATTRIB
  36328. /* test adding multiple OU's to the signer */
  36329. ExpectNotNull(name = X509_get_subject_name(ca));
  36330. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36331. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36332. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36333. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36334. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36335. #endif
  36336. ExpectNotNull(name = X509_get_subject_name(ca));
  36337. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36338. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36339. ExpectNotNull(name = X509_get_issuer_name(x509));
  36340. cnSz = X509_NAME_get_sz(name);
  36341. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36342. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36343. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36344. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36345. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36346. cn = NULL;
  36347. FreeDecodedCert(&dCert);
  36348. /* Test invalid parameters */
  36349. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36350. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36351. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  36352. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36353. EVP_MD_CTX_free(mctx);
  36354. mctx = NULL;
  36355. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36356. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  36357. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  36358. /* test invalid version number */
  36359. #if defined(OPENSSL_ALL)
  36360. ExpectIntNE(X509_set_version(x509, 6L), 0);
  36361. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36362. /* uses ParseCert which fails on bad version number */
  36363. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36364. #endif
  36365. EVP_MD_CTX_free(mctx);
  36366. EVP_PKEY_free(priv);
  36367. EVP_PKEY_free(pub);
  36368. X509_free(x509);
  36369. X509_free(ca);
  36370. #endif
  36371. return EXPECT_RESULT();
  36372. }
  36373. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36374. {
  36375. EXPECT_DECLS;
  36376. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36377. X509* x509 = NULL;
  36378. const X509_ALGOR* alg;
  36379. ExpectNotNull(x509 = X509_new());
  36380. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  36381. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36382. X509_free(x509);
  36383. #endif
  36384. return EXPECT_RESULT();
  36385. }
  36386. static int test_wolfSSL_X509_ALGOR_get0(void)
  36387. {
  36388. EXPECT_DECLS;
  36389. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36390. !defined(NO_SHA256) && !defined(NO_RSA)
  36391. X509* x509 = NULL;
  36392. const ASN1_OBJECT* obj = NULL;
  36393. const X509_ALGOR* alg = NULL;
  36394. int pptype = 0;
  36395. const void *ppval = NULL;
  36396. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36397. SSL_FILETYPE_PEM));
  36398. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36399. /* Invalid case */
  36400. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36401. ExpectNull(obj);
  36402. /* Valid case */
  36403. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36404. ExpectNotNull(obj);
  36405. ExpectNull(ppval);
  36406. ExpectIntNE(pptype, 0);
  36407. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36408. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36409. X509_free(x509);
  36410. #endif
  36411. return EXPECT_RESULT();
  36412. }
  36413. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36414. {
  36415. EXPECT_DECLS;
  36416. #if defined(OPENSSL_EXTRA)
  36417. X509_VERIFY_PARAM *paramTo = NULL;
  36418. X509_VERIFY_PARAM *paramFrom = NULL;
  36419. char testIPv4[] = "127.0.0.1";
  36420. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36421. char testhostName1[] = "foo.hoge.com";
  36422. char testhostName2[] = "foobar.hoge.com";
  36423. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  36424. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  36425. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  36426. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  36427. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36428. (int)XSTRLEN(testhostName1)), 1);
  36429. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36430. (int)XSTRLEN(testhostName1)));
  36431. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36432. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36433. ExpectIntEQ(0x01, paramFrom->hostFlags);
  36434. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  36435. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  36436. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36437. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  36438. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  36439. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36440. /* null pointer */
  36441. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  36442. /* in the case of "from" null, returns success */
  36443. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  36444. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  36445. /* inherit flags test : VPARAM_DEFAULT */
  36446. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36447. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36448. (int)XSTRLEN(testhostName1)));
  36449. ExpectIntEQ(0x01, paramTo->hostFlags);
  36450. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36451. /* inherit flags test : VPARAM OVERWRITE */
  36452. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36453. (int)XSTRLEN(testhostName2)), 1);
  36454. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36455. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36456. if (paramTo != NULL) {
  36457. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36458. }
  36459. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36460. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36461. (int)XSTRLEN(testhostName1)));
  36462. ExpectIntEQ(0x01, paramTo->hostFlags);
  36463. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36464. /* inherit flags test : VPARAM_RESET_FLAGS */
  36465. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36466. (int)XSTRLEN(testhostName2)), 1);
  36467. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36468. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36469. if (paramTo != NULL) {
  36470. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36471. }
  36472. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36473. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36474. (int)XSTRLEN(testhostName1)));
  36475. ExpectIntEQ(0x01, paramTo->hostFlags);
  36476. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36477. /* inherit flags test : VPARAM_LOCKED */
  36478. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36479. (int)XSTRLEN(testhostName2)), 1);
  36480. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36481. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36482. if (paramTo != NULL) {
  36483. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36484. }
  36485. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36486. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36487. (int)XSTRLEN(testhostName2)));
  36488. ExpectIntEQ(0x00, paramTo->hostFlags);
  36489. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36490. /* test for incorrect parameters */
  36491. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  36492. 0);
  36493. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  36494. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36495. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  36496. 1);
  36497. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  36498. X509_V_FLAG_CRL_CHECK_ALL);
  36499. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  36500. X509_V_FLAG_CRL_CHECK_ALL), 1);
  36501. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  36502. X509_VERIFY_PARAM_free(paramTo);
  36503. X509_VERIFY_PARAM_free(paramFrom);
  36504. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36505. #endif
  36506. return EXPECT_RESULT();
  36507. }
  36508. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36509. static int test_wolfSSL_check_domain_verify_count = 0;
  36510. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36511. WOLFSSL_X509_STORE_CTX* store)
  36512. {
  36513. EXPECT_DECLS;
  36514. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  36515. ExpectIntEQ(preverify, 1);
  36516. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  36517. return EXPECT_SUCCESS();
  36518. }
  36519. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36520. {
  36521. EXPECT_DECLS;
  36522. X509_VERIFY_PARAM *param = NULL;
  36523. ExpectNotNull(param = SSL_get0_param(ssl));
  36524. /* Domain check should only be done on the leaf cert */
  36525. X509_VERIFY_PARAM_set_hostflags(param,
  36526. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36527. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36528. "wolfSSL Server Chain", 0), 1);
  36529. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36530. test_wolfSSL_check_domain_verify_cb);
  36531. return EXPECT_RESULT();
  36532. }
  36533. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36534. {
  36535. EXPECT_DECLS;
  36536. /* Use a cert with different domains in chain */
  36537. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36538. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36539. return EXPECT_RESULT();
  36540. }
  36541. static int test_wolfSSL_check_domain(void)
  36542. {
  36543. EXPECT_DECLS;
  36544. test_ssl_cbf func_cb_client;
  36545. test_ssl_cbf func_cb_server;
  36546. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  36547. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  36548. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36549. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36550. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  36551. &func_cb_server, NULL), TEST_SUCCESS);
  36552. /* Should have been called once for each cert in sent chain */
  36553. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36554. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36555. #else
  36556. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36557. #endif
  36558. return EXPECT_RESULT();
  36559. }
  36560. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  36561. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36562. {
  36563. EXPECT_DECLS;
  36564. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36565. X509* x509 = NULL;
  36566. X509_PUBKEY* pubKey;
  36567. ExpectNotNull(x509 = X509_new());
  36568. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36569. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36570. X509_free(x509);
  36571. #endif
  36572. return EXPECT_RESULT();
  36573. }
  36574. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36575. {
  36576. EXPECT_DECLS;
  36577. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36578. !defined(NO_SHA256) && !defined(NO_RSA)
  36579. X509* x509 = NULL;
  36580. ASN1_OBJECT* obj = NULL;
  36581. const ASN1_OBJECT* pa_oid = NULL;
  36582. X509_PUBKEY* pubKey = NULL;
  36583. X509_PUBKEY* pubKey2 = NULL;
  36584. EVP_PKEY* evpKey = NULL;
  36585. const unsigned char *pk = NULL;
  36586. int ppklen;
  36587. int pptype;
  36588. X509_ALGOR *pa = NULL;
  36589. const void *pval;
  36590. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36591. SSL_FILETYPE_PEM));
  36592. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36593. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36594. ExpectNotNull(pk);
  36595. ExpectNotNull(pa);
  36596. ExpectNotNull(pubKey);
  36597. ExpectIntGT(ppklen, 0);
  36598. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36599. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36600. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36601. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36602. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36603. ExpectNotNull(pk);
  36604. ExpectNotNull(pa);
  36605. ExpectIntGT(ppklen, 0);
  36606. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36607. ExpectNotNull(pa_oid);
  36608. ExpectNull(pval);
  36609. ExpectIntEQ(pptype, V_ASN1_NULL);
  36610. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36611. X509_PUBKEY_free(pubKey2);
  36612. X509_free(x509);
  36613. EVP_PKEY_free(evpKey);
  36614. #endif
  36615. return EXPECT_RESULT();
  36616. }
  36617. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36618. {
  36619. EXPECT_DECLS;
  36620. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36621. X509* x509 = NULL;
  36622. ASN1_OBJECT* obj = NULL;
  36623. ASN1_OBJECT* poid = NULL;
  36624. const ASN1_OBJECT* pa_oid = NULL;
  36625. X509_PUBKEY* pubKey = NULL;
  36626. X509_PUBKEY* pubKey2 = NULL;
  36627. EVP_PKEY* evpKey = NULL;
  36628. const unsigned char *pk = NULL;
  36629. int ppklen;
  36630. int pptype;
  36631. X509_ALGOR *pa = NULL;
  36632. const void *pval;
  36633. char buf[50];
  36634. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36635. SSL_FILETYPE_PEM));
  36636. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36637. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36638. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36639. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36640. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36641. ExpectNotNull(pk);
  36642. ExpectNotNull(pa);
  36643. ExpectIntGT(ppklen, 0);
  36644. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36645. ExpectNotNull(pa_oid);
  36646. ExpectNotNull(pval);
  36647. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36648. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36649. poid = (ASN1_OBJECT *)pval;
  36650. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36651. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36652. X509_PUBKEY_free(pubKey2);
  36653. X509_free(x509);
  36654. EVP_PKEY_free(evpKey);
  36655. #endif
  36656. return EXPECT_RESULT();
  36657. }
  36658. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36659. {
  36660. EXPECT_DECLS;
  36661. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36662. word32 bytes;
  36663. #ifdef USE_CERT_BUFFERS_1024
  36664. byte tmp[ONEK_BUF];
  36665. #elif defined(USE_CERT_BUFFERS_2048)
  36666. byte tmp[TWOK_BUF];
  36667. #else
  36668. byte tmp[TWOK_BUF];
  36669. #endif /* END USE_CERT_BUFFERS_1024 */
  36670. const unsigned char* dsaKeyDer = tmp;
  36671. ASN1_OBJECT* obj = NULL;
  36672. ASN1_STRING* str;
  36673. const ASN1_OBJECT* pa_oid = NULL;
  36674. X509_PUBKEY* pubKey = NULL;
  36675. EVP_PKEY* evpKey = NULL;
  36676. const unsigned char *pk = NULL;
  36677. int ppklen, pptype;
  36678. X509_ALGOR *pa = NULL;
  36679. const void *pval;
  36680. #ifdef USE_CERT_BUFFERS_1024
  36681. XMEMSET(tmp, 0, sizeof(tmp));
  36682. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36683. bytes = sizeof_dsa_key_der_1024;
  36684. #elif defined(USE_CERT_BUFFERS_2048)
  36685. XMEMSET(tmp, 0, sizeof(tmp));
  36686. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36687. bytes = sizeof_dsa_key_der_2048;
  36688. #else
  36689. {
  36690. XFILE fp = XBADFILE;
  36691. XMEMSET(tmp, 0, sizeof(tmp));
  36692. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  36693. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  36694. if (fp != XBADFILE)
  36695. XFCLOSE(fp);
  36696. }
  36697. #endif
  36698. /* Initialize pkey with der format dsa key */
  36699. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36700. ExpectNotNull(pubKey = X509_PUBKEY_new());
  36701. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36702. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36703. ExpectNotNull(pk);
  36704. ExpectNotNull(pa);
  36705. ExpectIntGT(ppklen, 0);
  36706. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36707. ExpectNotNull(pa_oid);
  36708. ExpectNotNull(pval);
  36709. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  36710. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36711. str = (ASN1_STRING *)pval;
  36712. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36713. #ifdef USE_CERT_BUFFERS_1024
  36714. ExpectIntEQ(ASN1_STRING_length(str), 291);
  36715. #else
  36716. ExpectIntEQ(ASN1_STRING_length(str), 549);
  36717. #endif /* END USE_CERT_BUFFERS_1024 */
  36718. X509_PUBKEY_free(pubKey);
  36719. EVP_PKEY_free(evpKey);
  36720. #endif
  36721. return EXPECT_RESULT();
  36722. }
  36723. static int test_wolfSSL_BUF(void)
  36724. {
  36725. EXPECT_DECLS;
  36726. #if defined(OPENSSL_EXTRA)
  36727. BUF_MEM* buf = NULL;
  36728. ExpectNotNull(buf = BUF_MEM_new());
  36729. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  36730. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  36731. BUF_MEM_free(buf);
  36732. #endif
  36733. return EXPECT_RESULT();
  36734. }
  36735. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36736. static int stub_rand_seed(const void *buf, int num)
  36737. {
  36738. (void)buf;
  36739. (void)num;
  36740. return 123;
  36741. }
  36742. static int stub_rand_bytes(unsigned char *buf, int num)
  36743. {
  36744. (void)buf;
  36745. (void)num;
  36746. return 456;
  36747. }
  36748. static byte* was_stub_rand_cleanup_called(void)
  36749. {
  36750. static byte was_called = 0;
  36751. return &was_called;
  36752. }
  36753. static void stub_rand_cleanup(void)
  36754. {
  36755. byte* was_called = was_stub_rand_cleanup_called();
  36756. *was_called = 1;
  36757. return;
  36758. }
  36759. static byte* was_stub_rand_add_called(void)
  36760. {
  36761. static byte was_called = 0;
  36762. return &was_called;
  36763. }
  36764. static int stub_rand_add(const void *buf, int num, double entropy)
  36765. {
  36766. byte* was_called = was_stub_rand_add_called();
  36767. (void)buf;
  36768. (void)num;
  36769. (void)entropy;
  36770. *was_called = 1;
  36771. return 0;
  36772. }
  36773. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36774. {
  36775. (void)buf;
  36776. (void)num;
  36777. return 9876;
  36778. }
  36779. static int stub_rand_status(void)
  36780. {
  36781. return 5432;
  36782. }
  36783. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36784. static int test_wolfSSL_RAND_set_rand_method(void)
  36785. {
  36786. EXPECT_DECLS;
  36787. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36788. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36789. unsigned char* buf = NULL;
  36790. int num = 0;
  36791. double entropy = 0;
  36792. int ret;
  36793. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36794. byte* was_add_called = was_stub_rand_add_called();
  36795. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36796. DYNAMIC_TYPE_TMP_BUFFER));
  36797. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  36798. ExpectIntEQ(*was_cleanup_called, 0);
  36799. RAND_cleanup();
  36800. ExpectIntEQ(*was_cleanup_called, 0);
  36801. rand_methods.seed = &stub_rand_seed;
  36802. rand_methods.bytes = &stub_rand_bytes;
  36803. rand_methods.cleanup = &stub_rand_cleanup;
  36804. rand_methods.add = &stub_rand_add;
  36805. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  36806. rand_methods.status = &stub_rand_status;
  36807. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  36808. ExpectIntEQ(RAND_seed(buf, num), 123);
  36809. ExpectIntEQ(RAND_bytes(buf, num), 456);
  36810. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  36811. ExpectIntEQ(RAND_status(), 5432);
  36812. ExpectIntEQ(*was_add_called, 0);
  36813. /* The function pointer for RAND_add returns int, but RAND_add itself
  36814. * returns void. */
  36815. RAND_add(buf, num, entropy);
  36816. ExpectIntEQ(*was_add_called, 1);
  36817. was_add_called = 0;
  36818. ExpectIntEQ(*was_cleanup_called, 0);
  36819. RAND_cleanup();
  36820. ExpectIntEQ(*was_cleanup_called, 1);
  36821. *was_cleanup_called = 0;
  36822. ret = RAND_set_rand_method(NULL);
  36823. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36824. ExpectIntNE(RAND_status(), 5432);
  36825. ExpectIntEQ(*was_cleanup_called, 0);
  36826. RAND_cleanup();
  36827. ExpectIntEQ(*was_cleanup_called, 0);
  36828. RAND_set_rand_method(NULL);
  36829. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36830. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36831. return EXPECT_RESULT();
  36832. }
  36833. static int test_wolfSSL_RAND_bytes(void)
  36834. {
  36835. EXPECT_DECLS;
  36836. #if defined(OPENSSL_EXTRA)
  36837. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  36838. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  36839. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  36840. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  36841. int max_bufsize;
  36842. byte *my_buf = NULL;
  36843. /* sanity check */
  36844. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  36845. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  36846. max_bufsize = size4;
  36847. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  36848. DYNAMIC_TYPE_TMP_BUFFER));
  36849. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  36850. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  36851. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  36852. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  36853. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  36854. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  36855. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  36856. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36857. #endif
  36858. return EXPECT_RESULT();
  36859. }
  36860. static int test_wolfSSL_RAND(void)
  36861. {
  36862. EXPECT_DECLS;
  36863. #if defined(OPENSSL_EXTRA)
  36864. byte seed[16];
  36865. XMEMSET(seed, 0, sizeof(seed));
  36866. /* No global methods set. */
  36867. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  36868. ExpectIntEQ(RAND_poll(), 1);
  36869. RAND_cleanup();
  36870. ExpectIntEQ(RAND_egd(NULL), -1);
  36871. #ifndef NO_FILESYSTEM
  36872. {
  36873. char fname[100];
  36874. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36875. ExpectIntEQ(RAND_write_file(NULL), 0);
  36876. }
  36877. #endif
  36878. #endif
  36879. return EXPECT_RESULT();
  36880. }
  36881. static int test_wolfSSL_PKCS8_Compat(void)
  36882. {
  36883. EXPECT_DECLS;
  36884. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  36885. !defined(NO_BIO)
  36886. PKCS8_PRIV_KEY_INFO* pt = NULL;
  36887. BIO* bio = NULL;
  36888. XFILE f = XBADFILE;
  36889. int bytes;
  36890. char pkcs8_buffer[512];
  36891. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36892. EVP_PKEY *pkey = NULL;
  36893. #endif
  36894. /* file from wolfssl/certs/ directory */
  36895. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  36896. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  36897. 0);
  36898. if (f != XBADFILE)
  36899. XFCLOSE(f);
  36900. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36901. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  36902. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36903. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  36904. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  36905. /* gets PKCS8 pointer to pkey */
  36906. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  36907. EVP_PKEY_free(pkey);
  36908. #endif
  36909. BIO_free(bio);
  36910. PKCS8_PRIV_KEY_INFO_free(pt);
  36911. #endif
  36912. return EXPECT_RESULT();
  36913. }
  36914. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  36915. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  36916. {
  36917. (void)passwd;
  36918. (void)sz;
  36919. (void)rw;
  36920. (void)userdata;
  36921. return -1;
  36922. }
  36923. #endif
  36924. static int test_wolfSSL_PKCS8_d2i(void)
  36925. {
  36926. EXPECT_DECLS;
  36927. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  36928. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  36929. * requires a 12 byte password in FIPS mode. This test ends up
  36930. * trying to use an 8 byte password. */
  36931. #ifndef NO_FILESYSTEM
  36932. unsigned char pkcs8_buffer[2048];
  36933. const unsigned char* p = NULL;
  36934. int bytes = 0;
  36935. XFILE file = XBADFILE;
  36936. WOLFSSL_EVP_PKEY* pkey = NULL;
  36937. #ifndef NO_BIO
  36938. BIO* bio = NULL;
  36939. #if defined(OPENSSL_ALL) && \
  36940. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  36941. defined(HAVE_ECC)) && \
  36942. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36943. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  36944. #endif
  36945. #endif
  36946. #ifndef NO_RSA
  36947. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  36948. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  36949. #ifndef NO_DES3
  36950. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  36951. #endif
  36952. #endif /* NO_RSA */
  36953. #ifdef HAVE_ECC
  36954. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  36955. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  36956. #ifndef NO_DES3
  36957. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  36958. #endif
  36959. #endif /* HAVE_ECC */
  36960. #endif /* !NO_FILESYSTEM */
  36961. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  36962. #ifndef NO_RSA
  36963. #ifdef USE_CERT_BUFFERS_1024
  36964. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  36965. int rsaSz = sizeof_server_key_der_1024;
  36966. #else
  36967. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  36968. int rsaSz = sizeof_server_key_der_2048;
  36969. #endif
  36970. #endif
  36971. #ifdef HAVE_ECC
  36972. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  36973. int ecSz = sizeof_ecc_key_der_256;
  36974. #endif
  36975. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  36976. #ifndef NO_FILESYSTEM
  36977. (void)pkcs8_buffer;
  36978. (void)p;
  36979. (void)bytes;
  36980. (void)file;
  36981. #ifndef NO_BIO
  36982. (void)bio;
  36983. #endif
  36984. #endif
  36985. #ifdef OPENSSL_ALL
  36986. #ifndef NO_RSA
  36987. /* Try to auto-detect normal RSA private key */
  36988. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  36989. EVP_PKEY_free(pkey);
  36990. pkey = NULL;
  36991. #endif
  36992. #ifdef HAVE_ECC
  36993. /* Try to auto-detect normal EC private key */
  36994. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  36995. EVP_PKEY_free(pkey);
  36996. pkey = NULL;
  36997. #endif
  36998. #endif /* OPENSSL_ALL */
  36999. #ifndef NO_FILESYSTEM
  37000. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37001. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37002. NULL), 0);
  37003. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37004. NULL), 0);
  37005. #endif
  37006. #ifndef NO_RSA
  37007. /* Get DER encoded RSA PKCS#8 data. */
  37008. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37009. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37010. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37011. file)), 0);
  37012. if (file != XBADFILE) {
  37013. XFCLOSE(file);
  37014. file = XBADFILE;
  37015. }
  37016. p = pkcs8_buffer;
  37017. #ifdef OPENSSL_ALL
  37018. /* Try to decode - auto-detect key type. */
  37019. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37020. #else
  37021. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37022. #endif
  37023. /* Get PEM encoded RSA PKCS#8 data. */
  37024. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37025. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37026. file)), 0);
  37027. if (file != XBADFILE) {
  37028. XFCLOSE(file);
  37029. file = XBADFILE;
  37030. }
  37031. #if defined(OPENSSL_ALL) && \
  37032. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37033. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37034. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37035. NULL), 0);
  37036. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37037. NULL), 0);
  37038. /* Write PKCS#8 PEM to BIO. */
  37039. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37040. NULL), bytes);
  37041. /* Write PKCS#8 PEM to stderr. */
  37042. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37043. NULL), bytes);
  37044. /* Compare file and written data */
  37045. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37046. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37047. BIO_free(bio);
  37048. bio = NULL;
  37049. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37050. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37051. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37052. #endif
  37053. #if !defined(NO_DES3) && !defined(NO_SHA)
  37054. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37055. /* Write Encrypted PKCS#8 PEM to BIO. */
  37056. bytes = 1834;
  37057. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37058. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37059. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37060. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37061. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37062. (void*)"yassl123"));
  37063. EVP_PKEY_free(evpPkey);
  37064. evpPkey = NULL;
  37065. BIO_free(bio);
  37066. bio = NULL;
  37067. #endif /* !NO_DES3 && !NO_SHA */
  37068. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37069. EVP_PKEY_free(pkey);
  37070. pkey = NULL;
  37071. /* PKCS#8 encrypted RSA key */
  37072. #ifndef NO_DES3
  37073. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37074. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37075. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37076. file)), 0);
  37077. if (file != XBADFILE) {
  37078. XFCLOSE(file);
  37079. file = XBADFILE;
  37080. }
  37081. #if defined(OPENSSL_ALL) && \
  37082. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37083. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37084. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37085. (void*)"yassl123"));
  37086. EVP_PKEY_free(pkey);
  37087. pkey = NULL;
  37088. BIO_free(bio);
  37089. bio = NULL;
  37090. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37091. #endif /* !NO_DES3 */
  37092. #endif /* NO_RSA */
  37093. #ifdef HAVE_ECC
  37094. /* PKCS#8 encode EC key */
  37095. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37096. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37097. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37098. file)), 0);
  37099. if (file != XBADFILE) {
  37100. XFCLOSE(file);
  37101. file = XBADFILE;
  37102. }
  37103. p = pkcs8_buffer;
  37104. #ifdef OPENSSL_ALL
  37105. /* Try to decode - auto-detect key type. */
  37106. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37107. #else
  37108. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37109. #endif
  37110. /* Get PEM encoded RSA PKCS#8 data. */
  37111. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37112. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37113. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37114. file)), 0);
  37115. if (file != XBADFILE) {
  37116. XFCLOSE(file);
  37117. file = XBADFILE;
  37118. }
  37119. #if defined(OPENSSL_ALL) && \
  37120. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37121. defined(HAVE_AES_CBC)
  37122. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37123. /* Write PKCS#8 PEM to BIO. */
  37124. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37125. NULL), bytes);
  37126. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37127. NULL), bytes);
  37128. /* Compare file and written data */
  37129. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37130. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37131. BIO_free(bio);
  37132. bio = NULL;
  37133. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37134. /* Write Encrypted PKCS#8 PEM to BIO. */
  37135. bytes = 379;
  37136. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37137. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37138. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37139. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37140. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37141. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37142. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37143. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37144. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37145. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37146. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37147. (void*)"yassl123"));
  37148. EVP_PKEY_free(evpPkey);
  37149. evpPkey = NULL;
  37150. BIO_free(bio);
  37151. bio = NULL;
  37152. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37153. EVP_PKEY_free(pkey);
  37154. pkey = NULL;
  37155. /* PKCS#8 encrypted EC key */
  37156. #ifndef NO_DES3
  37157. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37158. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37159. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37160. file)), 0);
  37161. if (file != XBADFILE) {
  37162. XFCLOSE(file);
  37163. file = XBADFILE;
  37164. }
  37165. #if defined(OPENSSL_ALL) && \
  37166. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37167. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37168. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37169. (void*)"yassl123"));
  37170. EVP_PKEY_free(pkey);
  37171. pkey = NULL;
  37172. BIO_free(bio);
  37173. bio = NULL;
  37174. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37175. #endif /* !NO_DES3 */
  37176. #endif /* HAVE_ECC */
  37177. #endif /* !NO_FILESYSTEM */
  37178. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37179. return EXPECT_RESULT();
  37180. }
  37181. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37182. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37183. #define LOGGING_THREADS 5
  37184. #define ERROR_COUNT 10
  37185. /* copied from logging.c since this is not exposed otherwise */
  37186. #ifndef ERROR_QUEUE_MAX
  37187. #ifdef ERROR_QUEUE_PER_THREAD
  37188. #define ERROR_QUEUE_MAX 16
  37189. #else
  37190. /* this breaks from compat of unlimited error queue size */
  37191. #define ERROR_QUEUE_MAX 100
  37192. #endif
  37193. #endif
  37194. static volatile int loggingThreadsReady;
  37195. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37196. {
  37197. const char* file;
  37198. int line;
  37199. unsigned long err;
  37200. int errorCount = 0;
  37201. int i;
  37202. (void)args;
  37203. while (!loggingThreadsReady);
  37204. for (i = 0; i < ERROR_COUNT; i++)
  37205. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37206. while ((err = ERR_get_error_line(&file, &line))) {
  37207. AssertIntEQ(err, 990 + errorCount);
  37208. errorCount++;
  37209. }
  37210. AssertIntEQ(errorCount, ERROR_COUNT);
  37211. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37212. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37213. errorCount = 0;
  37214. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  37215. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37216. while ((err = ERR_get_error_line(&file, &line))) {
  37217. AssertIntEQ(err, 990 + errorCount);
  37218. errorCount++;
  37219. }
  37220. /* test that the 3 errors over the max were dropped */
  37221. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37222. WOLFSSL_RETURN_FROM_THREAD(0);
  37223. }
  37224. #endif
  37225. static int test_error_queue_per_thread(void)
  37226. {
  37227. int res = TEST_SKIPPED;
  37228. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37229. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37230. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37231. int i;
  37232. ERR_clear_error(); /* clear out any error nodes */
  37233. loggingThreadsReady = 0;
  37234. for (i = 0; i < LOGGING_THREADS; i++)
  37235. start_thread(test_logging, NULL, &loggingThreads[i]);
  37236. loggingThreadsReady = 1;
  37237. for (i = 0; i < LOGGING_THREADS; i++)
  37238. join_thread(loggingThreads[i]);
  37239. res = TEST_SUCCESS;
  37240. #endif
  37241. return res;
  37242. }
  37243. static int test_wolfSSL_ERR_put_error(void)
  37244. {
  37245. EXPECT_DECLS;
  37246. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37247. defined(DEBUG_WOLFSSL)
  37248. const char* file;
  37249. int line;
  37250. ERR_clear_error(); /* clear out any error nodes */
  37251. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37252. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37253. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37254. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  37255. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37256. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  37257. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37258. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  37259. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37260. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  37261. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37262. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  37263. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37264. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  37265. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37266. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  37267. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37268. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  37269. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37270. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  37271. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37272. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  37273. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37274. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  37275. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37276. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  37277. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37278. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  37279. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37280. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  37281. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37282. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  37283. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37284. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37285. "this file", 100);
  37286. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37287. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37288. ExpectIntEQ(line, 100);
  37289. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  37290. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37291. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37292. #endif
  37293. /* try reading past end of error queue */
  37294. file = NULL;
  37295. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37296. ExpectNull(file);
  37297. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37298. PEMerr(4,4);
  37299. ExpectIntEQ(ERR_get_error(), 4);
  37300. /* Empty and free up all error nodes */
  37301. ERR_clear_error();
  37302. /* Verify all nodes are cleared */
  37303. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37304. ERR_clear_error();
  37305. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37306. #endif
  37307. return EXPECT_RESULT();
  37308. }
  37309. /*
  37310. * This is a regression test for a bug where the peek/get error functions were
  37311. * drawing from the end of the queue rather than the front.
  37312. */
  37313. static int test_wolfSSL_ERR_get_error_order(void)
  37314. {
  37315. EXPECT_DECLS;
  37316. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  37317. /* Empty the queue. */
  37318. wolfSSL_ERR_clear_error();
  37319. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37320. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37321. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37322. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37323. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37324. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37325. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  37326. return EXPECT_RESULT();
  37327. }
  37328. #ifndef NO_BIO
  37329. static int test_wolfSSL_ERR_print_errors(void)
  37330. {
  37331. EXPECT_DECLS;
  37332. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37333. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37334. BIO* bio = NULL;
  37335. char buf[1024];
  37336. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37337. ERR_clear_error(); /* clear out any error nodes */
  37338. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37339. /* Choosing -600 as an unused errno. */
  37340. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  37341. ERR_print_errors(bio);
  37342. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37343. ExpectIntEQ(XSTRNCMP(
  37344. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37345. buf, 55), 0);
  37346. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37347. ExpectIntEQ(XSTRNCMP(
  37348. "error:600:wolfSSL library:unknown error number:asn.c:100",
  37349. buf, 56), 0);
  37350. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37351. ExpectIntEQ(buf[0], '\0');
  37352. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37353. BIO_free(bio);
  37354. #endif
  37355. return EXPECT_RESULT();
  37356. }
  37357. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37358. defined(DEBUG_WOLFSSL)
  37359. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37360. {
  37361. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37362. return 0;
  37363. }
  37364. #endif
  37365. static int test_wolfSSL_ERR_print_errors_cb(void)
  37366. {
  37367. EXPECT_DECLS;
  37368. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37369. defined(DEBUG_WOLFSSL)
  37370. BIO* bio = NULL;
  37371. char buf[1024];
  37372. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37373. ERR_clear_error(); /* clear out any error nodes */
  37374. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37375. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37376. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37377. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37378. ExpectIntEQ(XSTRNCMP(
  37379. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37380. buf, 53), 0);
  37381. ExpectIntEQ(XSTRNCMP(
  37382. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37383. buf + 53, 55), 0);
  37384. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37385. BIO_free(bio);
  37386. #endif
  37387. return EXPECT_RESULT();
  37388. }
  37389. /*
  37390. * Testing WOLFSSL_ERROR_MSG
  37391. */
  37392. static int test_WOLFSSL_ERROR_MSG(void)
  37393. {
  37394. int res = TEST_SKIPPED;
  37395. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37396. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37397. const char* msg = TEST_STRING;
  37398. WOLFSSL_ERROR_MSG(msg);
  37399. res = TEST_SUCCESS;
  37400. #endif
  37401. return res;
  37402. } /* End test_WOLFSSL_ERROR_MSG */
  37403. /*
  37404. * Testing wc_ERR_remove_state
  37405. */
  37406. static int test_wc_ERR_remove_state(void)
  37407. {
  37408. int res = TEST_SKIPPED;
  37409. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37410. wc_ERR_remove_state();
  37411. res = TEST_SUCCESS;
  37412. #endif
  37413. return res;
  37414. } /* End test_wc_ERR_remove_state */
  37415. /*
  37416. * Testing wc_ERR_print_errors_fp
  37417. */
  37418. static int test_wc_ERR_print_errors_fp(void)
  37419. {
  37420. EXPECT_DECLS;
  37421. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37422. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37423. long sz;
  37424. XFILE fp = XBADFILE;
  37425. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37426. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  37427. XBADFILE);
  37428. wc_ERR_print_errors_fp(fp);
  37429. #if defined(DEBUG_WOLFSSL)
  37430. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37431. #ifdef NO_ERROR_QUEUE
  37432. ExpectIntEQ(sz = XFTELL(fp), 0);
  37433. #else
  37434. ExpectIntNE(sz = XFTELL(fp), 0);
  37435. #endif
  37436. #endif
  37437. if (fp != XBADFILE)
  37438. XFCLOSE(fp);
  37439. (void)sz;
  37440. #endif
  37441. return EXPECT_RESULT();
  37442. } /* End test_wc_ERR_print_errors_fp */
  37443. #ifdef DEBUG_WOLFSSL
  37444. static void Logging_cb(const int logLevel, const char *const logMessage)
  37445. {
  37446. (void)logLevel;
  37447. (void)logMessage;
  37448. }
  37449. #endif
  37450. /*
  37451. * Testing wolfSSL_GetLoggingCb
  37452. */
  37453. static int test_wolfSSL_GetLoggingCb(void)
  37454. {
  37455. EXPECT_DECLS;
  37456. #ifdef DEBUG_WOLFSSL
  37457. /* Testing without wolfSSL_SetLoggingCb() */
  37458. ExpectNull(wolfSSL_GetLoggingCb());
  37459. /* Testing with wolfSSL_SetLoggingCb() */
  37460. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  37461. ExpectNotNull(wolfSSL_GetLoggingCb());
  37462. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  37463. #endif
  37464. ExpectNull(wolfSSL_GetLoggingCb());
  37465. return EXPECT_RESULT();
  37466. } /* End test_wolfSSL_GetLoggingCb */
  37467. #endif /* !NO_BIO */
  37468. static int test_wolfSSL_MD4(void)
  37469. {
  37470. EXPECT_DECLS;
  37471. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  37472. MD4_CTX md4;
  37473. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  37474. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  37475. "789012345678901234567890";
  37476. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  37477. "\xcc\x05\x36";
  37478. int msgSz = (int)XSTRLEN(msg);
  37479. XMEMSET(out, 0, sizeof(out));
  37480. MD4_Init(&md4);
  37481. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  37482. MD4_Final(out, &md4);
  37483. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  37484. #endif
  37485. return EXPECT_RESULT();
  37486. }
  37487. static int test_wolfSSL_MD5(void)
  37488. {
  37489. EXPECT_DECLS;
  37490. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37491. byte input1[] = "";
  37492. byte input2[] = "message digest";
  37493. byte hash[WC_MD5_DIGEST_SIZE];
  37494. unsigned char output1[] =
  37495. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37496. unsigned char output2[] =
  37497. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37498. WOLFSSL_MD5_CTX md5;
  37499. XMEMSET(&md5, 0, sizeof(md5));
  37500. /* Test cases for illegal parameters */
  37501. ExpectIntEQ(MD5_Init(NULL), 0);
  37502. ExpectIntEQ(MD5_Init(&md5), 1);
  37503. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37504. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37505. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37506. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37507. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37508. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37509. /* Init MD5 CTX */
  37510. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37511. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37512. 1);
  37513. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37514. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37515. /* Init MD5 CTX */
  37516. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37517. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37518. (int)XSTRLEN((const char*)input2)), 1);
  37519. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37520. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37521. #if !defined(NO_OLD_NAMES) && \
  37522. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37523. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37524. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37525. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37526. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37527. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37528. &hash);
  37529. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37530. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37531. &hash);
  37532. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37533. {
  37534. byte data[] = "Data to be hashed.";
  37535. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37536. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37537. ExpectNotNull(MD5(data, sizeof(data), hash));
  37538. ExpectNotNull(MD5(NULL, 0, hash));
  37539. ExpectNull(MD5(NULL, sizeof(data), hash));
  37540. }
  37541. #endif
  37542. #endif
  37543. return EXPECT_RESULT();
  37544. }
  37545. static int test_wolfSSL_MD5_Transform(void)
  37546. {
  37547. EXPECT_DECLS;
  37548. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37549. byte input1[] = "";
  37550. byte input2[] = "abc";
  37551. byte local[WC_MD5_BLOCK_SIZE];
  37552. word32 sLen = 0;
  37553. #ifdef BIG_ENDIAN_ORDER
  37554. unsigned char output1[] =
  37555. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37556. unsigned char output2[] =
  37557. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37558. #else
  37559. unsigned char output1[] =
  37560. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37561. unsigned char output2[] =
  37562. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37563. #endif
  37564. union {
  37565. wc_Md5 native;
  37566. MD5_CTX compat;
  37567. } md5;
  37568. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37569. XMEMSET(&local, 0, sizeof(local));
  37570. /* sanity check */
  37571. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37572. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37573. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37574. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37575. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37576. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37577. /* Init MD5 CTX */
  37578. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37579. /* Do Transform*/
  37580. sLen = (word32)XSTRLEN((char*)input1);
  37581. XMEMCPY(local, input1, sLen);
  37582. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37583. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37584. /* Init MD5 CTX */
  37585. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37586. sLen = (word32)XSTRLEN((char*)input2);
  37587. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37588. XMEMCPY(local, input2, sLen);
  37589. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37590. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37591. #endif
  37592. return EXPECT_RESULT();
  37593. }
  37594. static int test_wolfSSL_SHA(void)
  37595. {
  37596. EXPECT_DECLS;
  37597. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37598. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37599. (!defined(HAVE_FIPS) || \
  37600. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37601. {
  37602. const unsigned char in[] = "abc";
  37603. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37604. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37605. unsigned char out[WC_SHA_DIGEST_SIZE];
  37606. unsigned char* p = NULL;
  37607. WOLFSSL_SHA_CTX sha;
  37608. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37609. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37610. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37611. /* SHA interface test */
  37612. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37613. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37614. ExpectNotNull(SHA(in, 0, out));
  37615. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37616. ExpectNotNull(SHA(NULL, 0, out));
  37617. ExpectNotNull(SHA(NULL, 0, NULL));
  37618. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37619. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37620. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  37621. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  37622. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  37623. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37624. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  37625. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37626. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  37627. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37628. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  37629. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37630. }
  37631. #endif
  37632. #if !defined(NO_SHA256)
  37633. {
  37634. const unsigned char in[] = "abc";
  37635. unsigned char expected[] =
  37636. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37637. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37638. "\x15\xAD";
  37639. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37640. unsigned char* p = NULL;
  37641. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37642. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37643. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37644. #else
  37645. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37646. #endif
  37647. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37648. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37649. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  37650. #else
  37651. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  37652. #endif
  37653. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  37654. }
  37655. #endif
  37656. #if defined(WOLFSSL_SHA384)
  37657. {
  37658. const unsigned char in[] = "abc";
  37659. unsigned char expected[] =
  37660. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37661. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37662. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37663. "\xc8\x25\xa7";
  37664. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37665. unsigned char* p = NULL;
  37666. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37667. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37668. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37669. #else
  37670. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37671. #endif
  37672. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37673. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37674. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37675. #else
  37676. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37677. #endif
  37678. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37679. }
  37680. #endif
  37681. #if defined(WOLFSSL_SHA512)
  37682. {
  37683. const unsigned char in[] = "abc";
  37684. unsigned char expected[] =
  37685. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37686. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37687. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37688. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37689. "\xa5\x4c\xa4\x9f";
  37690. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37691. unsigned char* p = NULL;
  37692. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37693. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37694. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37695. #else
  37696. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37697. #endif
  37698. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37699. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37700. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  37701. #else
  37702. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  37703. #endif
  37704. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  37705. }
  37706. #endif
  37707. #endif
  37708. return EXPECT_RESULT();
  37709. }
  37710. static int test_wolfSSL_SHA_Transform(void)
  37711. {
  37712. EXPECT_DECLS;
  37713. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37714. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37715. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37716. byte input1[] = "";
  37717. byte input2[] = "abc";
  37718. byte local[WC_SHA_BLOCK_SIZE];
  37719. word32 sLen = 0;
  37720. #ifdef BIG_ENDIAN_ORDER
  37721. unsigned char output1[] =
  37722. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37723. "\x09\xf7\x3a\x93";
  37724. unsigned char output2[] =
  37725. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37726. "\x7c\x6e\x08\xb8";
  37727. #else
  37728. unsigned char output1[] =
  37729. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37730. "\x93\x3a\xf7\x09";
  37731. unsigned char output2[] =
  37732. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37733. "\xb8\x08\x6e\x7c";
  37734. #endif
  37735. union {
  37736. wc_Sha native;
  37737. SHA_CTX compat;
  37738. } sha;
  37739. union {
  37740. wc_Sha native;
  37741. SHA_CTX compat;
  37742. } sha1;
  37743. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37744. XMEMSET(&local, 0, sizeof(local));
  37745. /* sanity check */
  37746. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37747. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37748. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37749. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37750. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37751. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37752. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37753. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37754. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37755. /* Init SHA CTX */
  37756. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37757. /* Do Transform*/
  37758. sLen = (word32)XSTRLEN((char*)input1);
  37759. XMEMCPY(local, input1, sLen);
  37760. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37761. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37762. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37763. /* Init SHA CTX */
  37764. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37765. sLen = (word32)XSTRLEN((char*)input2);
  37766. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37767. XMEMCPY(local, input2, sLen);
  37768. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37769. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37770. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37771. /* SHA1 */
  37772. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37773. /* Init SHA CTX */
  37774. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37775. /* Do Transform*/
  37776. sLen = (word32)XSTRLEN((char*)input1);
  37777. XMEMCPY(local, input1, sLen);
  37778. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37779. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37780. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  37781. /* Init SHA CTX */
  37782. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37783. sLen = (word32)XSTRLEN((char*)input2);
  37784. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37785. XMEMCPY(local, input2, sLen);
  37786. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37787. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37788. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37789. #endif
  37790. #endif
  37791. return EXPECT_RESULT();
  37792. }
  37793. static int test_wolfSSL_SHA224(void)
  37794. {
  37795. EXPECT_DECLS;
  37796. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37797. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37798. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37799. unsigned char input[] =
  37800. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37801. unsigned char output[] =
  37802. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37803. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37804. size_t inLen;
  37805. byte hash[WC_SHA224_DIGEST_SIZE];
  37806. unsigned char* p;
  37807. inLen = XSTRLEN((char*)input);
  37808. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37809. ExpectNull(SHA224(NULL, inLen, hash));
  37810. ExpectNotNull(SHA224(input, 0, hash));
  37811. ExpectNotNull(SHA224(input, inLen, NULL));
  37812. ExpectNotNull(SHA224(NULL, 0, hash));
  37813. ExpectNotNull(SHA224(NULL, 0, NULL));
  37814. ExpectNotNull(SHA224(input, inLen, hash));
  37815. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37816. ExpectNotNull(p = SHA224(input, inLen, NULL));
  37817. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  37818. #endif
  37819. return EXPECT_RESULT();
  37820. }
  37821. static int test_wolfSSL_SHA256(void)
  37822. {
  37823. EXPECT_DECLS;
  37824. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37825. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37826. unsigned char input[] =
  37827. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37828. unsigned char output[] =
  37829. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37830. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37831. "\x06\xC1";
  37832. size_t inLen;
  37833. byte hash[WC_SHA256_DIGEST_SIZE];
  37834. inLen = XSTRLEN((char*)input);
  37835. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37836. ExpectNotNull(SHA256(input, inLen, hash));
  37837. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37838. #endif
  37839. return EXPECT_RESULT();
  37840. }
  37841. static int test_wolfSSL_SHA256_Transform(void)
  37842. {
  37843. EXPECT_DECLS;
  37844. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37845. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37846. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37847. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37848. !defined(WOLFSSL_KCAPI_HASH)
  37849. byte input1[] = "";
  37850. byte input2[] = "abc";
  37851. byte local[WC_SHA256_BLOCK_SIZE];
  37852. word32 sLen = 0;
  37853. #ifdef BIG_ENDIAN_ORDER
  37854. unsigned char output1[] =
  37855. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37856. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37857. unsigned char output2[] =
  37858. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37859. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37860. #else
  37861. unsigned char output1[] =
  37862. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37863. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37864. unsigned char output2[] =
  37865. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37866. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37867. #endif
  37868. union {
  37869. wc_Sha256 native;
  37870. SHA256_CTX compat;
  37871. } sha256;
  37872. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37873. XMEMSET(&local, 0, sizeof(local));
  37874. /* sanity check */
  37875. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37876. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37877. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37878. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37879. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37880. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37881. /* Init SHA256 CTX */
  37882. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37883. /* Do Transform*/
  37884. sLen = (word32)XSTRLEN((char*)input1);
  37885. XMEMCPY(local, input1, sLen);
  37886. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37887. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37888. 0);
  37889. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37890. /* Init SHA256 CTX */
  37891. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37892. sLen = (word32)XSTRLEN((char*)input2);
  37893. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37894. XMEMCPY(local, input2, sLen);
  37895. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37896. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37897. 0);
  37898. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37899. #endif
  37900. #endif
  37901. return EXPECT_RESULT();
  37902. }
  37903. static int test_wolfSSL_SHA512_Transform(void)
  37904. {
  37905. EXPECT_DECLS;
  37906. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37907. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37908. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37909. !defined(WOLFSSL_KCAPI_HASH)
  37910. byte input1[] = "";
  37911. byte input2[] = "abc";
  37912. byte local[WC_SHA512_BLOCK_SIZE];
  37913. word32 sLen = 0;
  37914. #ifdef BIG_ENDIAN_ORDER
  37915. unsigned char output1[] =
  37916. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37917. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37918. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37919. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37920. unsigned char output2[] =
  37921. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37922. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37923. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37924. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37925. #else
  37926. unsigned char output1[] =
  37927. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37928. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37929. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37930. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37931. unsigned char output2[] =
  37932. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37933. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37934. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37935. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37936. #endif
  37937. union {
  37938. wc_Sha512 native;
  37939. SHA512_CTX compat;
  37940. } sha512;
  37941. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37942. XMEMSET(&local, 0, sizeof(local));
  37943. /* sanity check */
  37944. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  37945. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37946. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37947. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37948. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37949. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37950. /* Init SHA512 CTX */
  37951. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37952. /* Do Transform*/
  37953. sLen = (word32)XSTRLEN((char*)input1);
  37954. XMEMCPY(local, input1, sLen);
  37955. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37956. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37957. WC_SHA512_DIGEST_SIZE), 0);
  37958. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37959. /* Init SHA512 CTX */
  37960. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  37961. sLen = (word32)XSTRLEN((char*)input2);
  37962. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37963. XMEMCPY(local, input2, sLen);
  37964. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37965. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37966. WC_SHA512_DIGEST_SIZE), 0);
  37967. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37968. (void)input1;
  37969. #endif
  37970. #endif
  37971. return EXPECT_RESULT();
  37972. }
  37973. static int test_wolfSSL_SHA512_224_Transform(void)
  37974. {
  37975. EXPECT_DECLS;
  37976. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  37977. !defined(WOLFSSL_NOSHA512_224)
  37978. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37979. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37980. !defined(WOLFSSL_KCAPI_HASH)
  37981. byte input1[] = "";
  37982. byte input2[] = "abc";
  37983. byte local[WC_SHA512_BLOCK_SIZE];
  37984. word32 sLen = 0;
  37985. unsigned char output1[] =
  37986. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  37987. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  37988. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  37989. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  37990. unsigned char output2[] =
  37991. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  37992. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  37993. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  37994. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  37995. union {
  37996. wc_Sha512 native;
  37997. SHA512_CTX compat;
  37998. } sha512;
  37999. #ifdef BIG_ENDIAN_ORDER
  38000. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38001. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38002. #endif
  38003. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38004. XMEMSET(&local, 0, sizeof(local));
  38005. /* sanity check */
  38006. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38007. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38008. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38009. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38010. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38011. BAD_FUNC_ARG);
  38012. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38013. /* Init SHA512 CTX */
  38014. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38015. /* Do Transform*/
  38016. sLen = (word32)XSTRLEN((char*)input1);
  38017. XMEMCPY(local, input1, sLen);
  38018. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38019. 1);
  38020. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38021. WC_SHA512_DIGEST_SIZE), 0);
  38022. /* frees resources */
  38023. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38024. /* Init SHA512 CTX */
  38025. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38026. sLen = (word32)XSTRLEN((char*)input2);
  38027. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38028. XMEMCPY(local, input2, sLen);
  38029. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38030. 1);
  38031. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38032. WC_SHA512_DIGEST_SIZE), 0);
  38033. /* frees resources */
  38034. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38035. #endif
  38036. #endif
  38037. return EXPECT_RESULT();
  38038. }
  38039. static int test_wolfSSL_SHA512_256_Transform(void)
  38040. {
  38041. EXPECT_DECLS;
  38042. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38043. !defined(WOLFSSL_NOSHA512_256)
  38044. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38045. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38046. !defined(WOLFSSL_KCAPI_HASH)
  38047. byte input1[] = "";
  38048. byte input2[] = "abc";
  38049. byte local[WC_SHA512_BLOCK_SIZE];
  38050. word32 sLen = 0;
  38051. unsigned char output1[] =
  38052. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38053. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38054. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38055. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38056. unsigned char output2[] =
  38057. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38058. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38059. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38060. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38061. union {
  38062. wc_Sha512 native;
  38063. SHA512_CTX compat;
  38064. } sha512;
  38065. #ifdef BIG_ENDIAN_ORDER
  38066. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38067. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38068. #endif
  38069. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38070. XMEMSET(&local, 0, sizeof(local));
  38071. /* sanity check */
  38072. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38073. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38074. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38075. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38076. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38077. BAD_FUNC_ARG);
  38078. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38079. /* Init SHA512 CTX */
  38080. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38081. /* Do Transform*/
  38082. sLen = (word32)XSTRLEN((char*)input1);
  38083. XMEMCPY(local, input1, sLen);
  38084. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38085. 1);
  38086. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38087. WC_SHA512_DIGEST_SIZE), 0);
  38088. /* frees resources */
  38089. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38090. /* Init SHA512 CTX */
  38091. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38092. sLen = (word32)XSTRLEN((char*)input2);
  38093. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38094. XMEMCPY(local, input2, sLen);
  38095. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38096. 1);
  38097. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38098. WC_SHA512_DIGEST_SIZE), 0);
  38099. /* frees resources */
  38100. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38101. #endif
  38102. #endif
  38103. return EXPECT_RESULT();
  38104. }
  38105. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38106. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38107. * buffer of 64 bytes.
  38108. *
  38109. * returns the size of the digest buffer on success and a negative value on
  38110. * failure.
  38111. */
  38112. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38113. int* sz)
  38114. {
  38115. EXPECT_DECLS;
  38116. HMAC_CTX ctx1;
  38117. HMAC_CTX ctx2;
  38118. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38119. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38120. unsigned char long_key[] =
  38121. "0123456789012345678901234567890123456789"
  38122. "0123456789012345678901234567890123456789"
  38123. "0123456789012345678901234567890123456789"
  38124. "0123456789012345678901234567890123456789";
  38125. unsigned char msg[] = "message to hash";
  38126. unsigned int digestSz = 64;
  38127. int keySz = sizeof(key);
  38128. int long_keySz = sizeof(long_key);
  38129. int msgSz = sizeof(msg);
  38130. unsigned char digest2[64];
  38131. unsigned int digestSz2 = 64;
  38132. HMAC_CTX_init(&ctx1);
  38133. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38134. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38135. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38136. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38137. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38138. HMAC_CTX_cleanup(&ctx1);
  38139. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38140. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38141. HMAC_CTX_cleanup(&ctx2);
  38142. ExpectIntEQ(digestSz, digestSz2);
  38143. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38144. /* test HMAC_Init with NULL key */
  38145. /* init after copy */
  38146. HMAC_CTX_init(&ctx1);
  38147. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38148. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38149. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38150. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38151. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38152. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38153. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38154. HMAC_CTX_cleanup(&ctx1);
  38155. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38156. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38157. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38158. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38159. HMAC_CTX_cleanup(&ctx2);
  38160. ExpectIntEQ(digestSz, digestSz2);
  38161. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38162. /* long key */
  38163. HMAC_CTX_init(&ctx1);
  38164. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38165. SSL_SUCCESS);
  38166. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38167. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38168. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38169. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38170. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38171. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38172. HMAC_CTX_cleanup(&ctx1);
  38173. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38174. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38175. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38176. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38177. HMAC_CTX_cleanup(&ctx2);
  38178. ExpectIntEQ(digestSz, digestSz2);
  38179. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38180. /* init before copy */
  38181. HMAC_CTX_init(&ctx1);
  38182. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38183. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38184. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38185. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38186. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38187. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38188. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38189. HMAC_CTX_cleanup(&ctx1);
  38190. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38191. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38192. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38193. HMAC_CTX_cleanup(&ctx2);
  38194. ExpectIntEQ(digestSz, digestSz2);
  38195. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38196. *sz = digestSz;
  38197. return EXPECT_RESULT();
  38198. }
  38199. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  38200. static int test_wolfSSL_HMAC_CTX(void)
  38201. {
  38202. EXPECT_DECLS;
  38203. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38204. unsigned char digest[64];
  38205. int digestSz;
  38206. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  38207. WOLFSSL_HMAC_CTX ctx1;
  38208. WOLFSSL_HMAC_CTX ctx2;
  38209. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  38210. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  38211. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  38212. wolfSSL_HMAC_CTX_free(NULL);
  38213. wolfSSL_HMAC_CTX_free(hmac_ctx);
  38214. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  38215. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  38216. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  38217. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  38218. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  38219. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  38220. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38221. ((! defined(HAVE_FIPS_VERSION)) || \
  38222. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38223. /* Copy object that hasn't had a digest set - MD5. */
  38224. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  38225. #else
  38226. /* Copy object that hasn't had a digest set. */
  38227. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  38228. #endif
  38229. HMAC_CTX_cleanup(NULL);
  38230. HMAC_CTX_cleanup(&ctx2);
  38231. ExpectNull(HMAC_CTX_get_md(NULL));
  38232. #ifndef NO_SHA
  38233. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  38234. TEST_SUCCESS);
  38235. ExpectIntEQ(digestSz, 20);
  38236. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  38237. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  38238. #endif /* !NO_SHA */
  38239. #ifdef WOLFSSL_SHA224
  38240. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  38241. TEST_SUCCESS);
  38242. ExpectIntEQ(digestSz, 28);
  38243. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  38244. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  38245. "\x02\x0E", digest, digestSz), 0);
  38246. #endif /* WOLFSSL_SHA224 */
  38247. #ifndef NO_SHA256
  38248. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  38249. TEST_SUCCESS);
  38250. ExpectIntEQ(digestSz, 32);
  38251. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  38252. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  38253. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  38254. #endif /* !NO_SHA256 */
  38255. #ifdef WOLFSSL_SHA384
  38256. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  38257. TEST_SUCCESS);
  38258. ExpectIntEQ(digestSz, 48);
  38259. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  38260. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  38261. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  38262. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  38263. digest, digestSz), 0);
  38264. #endif /* WOLFSSL_SHA384 */
  38265. #ifdef WOLFSSL_SHA512
  38266. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  38267. TEST_SUCCESS);
  38268. ExpectIntEQ(digestSz, 64);
  38269. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  38270. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  38271. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  38272. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  38273. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  38274. digest, digestSz), 0);
  38275. #endif /* WOLFSSL_SHA512 */
  38276. #ifdef WOLFSSL_SHA3
  38277. #ifndef WOLFSSL_NOSHA3_224
  38278. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  38279. TEST_SUCCESS);
  38280. ExpectIntEQ(digestSz, 28);
  38281. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  38282. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  38283. "\x29\x0f", digest, digestSz), 0);
  38284. #endif
  38285. #ifndef WOLFSSL_NOSHA3_256
  38286. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  38287. TEST_SUCCESS);
  38288. ExpectIntEQ(digestSz, 32);
  38289. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  38290. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  38291. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  38292. #endif
  38293. #ifndef WOLFSSL_NOSHA3_384
  38294. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  38295. TEST_SUCCESS);
  38296. ExpectIntEQ(digestSz, 48);
  38297. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  38298. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  38299. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  38300. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  38301. digest, digestSz), 0);
  38302. #endif
  38303. #ifndef WOLFSSL_NOSHA3_512
  38304. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  38305. TEST_SUCCESS);
  38306. ExpectIntEQ(digestSz, 64);
  38307. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  38308. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  38309. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  38310. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  38311. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  38312. digest, digestSz), 0);
  38313. #endif
  38314. #endif
  38315. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  38316. HAVE_FIPS_VERSION <= 2)
  38317. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  38318. TEST_SUCCESS);
  38319. ExpectIntEQ(digestSz, 16);
  38320. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  38321. "\xE4\x98\xDD", digest, digestSz), 0);
  38322. #endif /* !NO_MD5 */
  38323. #endif
  38324. return EXPECT_RESULT();
  38325. }
  38326. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38327. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38328. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38329. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  38330. {
  38331. EXPECT_DECLS;
  38332. static const unsigned char key[] = "simple test key";
  38333. HMAC_CTX* hmac = NULL;
  38334. ENGINE* e = NULL;
  38335. unsigned char hash[WC_MAX_DIGEST_SIZE];
  38336. unsigned int len;
  38337. ExpectNotNull(hmac = HMAC_CTX_new());
  38338. HMAC_CTX_init(hmac);
  38339. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38340. ((! defined(HAVE_FIPS_VERSION)) || \
  38341. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38342. /* Get size on object that hasn't had a digest set - MD5. */
  38343. ExpectIntEQ(HMAC_size(hmac), 16);
  38344. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  38345. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  38346. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  38347. #else
  38348. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  38349. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  38350. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  38351. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  38352. #endif
  38353. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  38354. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  38355. /* re-using test key as data to hash */
  38356. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  38357. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  38358. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  38359. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  38360. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  38361. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  38362. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  38363. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  38364. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  38365. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  38366. ExpectIntEQ(len, md_len);
  38367. ExpectIntEQ(HMAC_size(NULL), 0);
  38368. ExpectIntEQ(HMAC_size(hmac), md_len);
  38369. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  38370. HMAC_cleanup(NULL);
  38371. HMAC_cleanup(hmac);
  38372. HMAC_CTX_free(hmac);
  38373. len = 0;
  38374. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  38375. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  38376. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  38377. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  38378. ExpectIntEQ(len, md_len);
  38379. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  38380. /* With data. */
  38381. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  38382. &len));
  38383. /* With NULL data. */
  38384. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  38385. &len));
  38386. /* With zero length data. */
  38387. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  38388. return EXPECT_RESULT();
  38389. }
  38390. #endif
  38391. static int test_wolfSSL_HMAC(void)
  38392. {
  38393. EXPECT_DECLS;
  38394. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38395. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38396. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38397. #ifndef NO_SHA256
  38398. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  38399. TEST_SUCCESS);
  38400. #endif
  38401. #ifdef WOLFSSL_SHA224
  38402. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  38403. TEST_SUCCESS);
  38404. #endif
  38405. #ifdef WOLFSSL_SHA384
  38406. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  38407. TEST_SUCCESS);
  38408. #endif
  38409. #ifdef WOLFSSL_SHA512
  38410. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  38411. TEST_SUCCESS);
  38412. #endif
  38413. #ifdef WOLFSSL_SHA3
  38414. #ifndef WOLFSSL_NOSHA3_224
  38415. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  38416. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  38417. #endif
  38418. #ifndef WOLFSSL_NOSHA3_256
  38419. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  38420. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  38421. #endif
  38422. #ifndef WOLFSSL_NOSHA3_384
  38423. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  38424. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  38425. #endif
  38426. #ifndef WOLFSSL_NOSHA3_512
  38427. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  38428. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  38429. #endif
  38430. #endif
  38431. #ifndef NO_SHA
  38432. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  38433. TEST_SUCCESS);
  38434. #endif
  38435. #endif
  38436. return EXPECT_RESULT();
  38437. }
  38438. static int test_wolfSSL_CMAC(void)
  38439. {
  38440. EXPECT_DECLS;
  38441. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  38442. defined(WOLFSSL_AES_DIRECT)
  38443. int i;
  38444. byte key[AES_256_KEY_SIZE];
  38445. CMAC_CTX* cmacCtx = NULL;
  38446. byte out[AES_BLOCK_SIZE];
  38447. size_t outLen = AES_BLOCK_SIZE;
  38448. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  38449. key[i] = i;
  38450. }
  38451. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38452. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  38453. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38454. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38455. NULL), 1);
  38456. /* re-using test key as data to hash */
  38457. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38458. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38459. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  38460. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  38461. /* No Update works. */
  38462. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38463. NULL), 1);
  38464. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38465. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38466. NULL), 1);
  38467. /* Test parameters with CMAC_Update. */
  38468. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  38469. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  38470. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  38471. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  38472. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  38473. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38474. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  38475. /* Test parameters with CMAC_Final. */
  38476. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  38477. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  38478. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  38479. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  38480. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  38481. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  38482. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38483. CMAC_CTX_free(cmacCtx);
  38484. /* Test parameters with CMAC Init. */
  38485. cmacCtx = NULL;
  38486. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38487. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38488. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  38489. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38490. NULL), 0);
  38491. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38492. NULL), 0);
  38493. /* give a key too small for the cipher, verify we get failure */
  38494. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  38495. NULL), 0);
  38496. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  38497. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  38498. /* Only AES-CBC supported. */
  38499. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  38500. NULL), 0);
  38501. #endif
  38502. CMAC_CTX_free(cmacCtx);
  38503. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  38504. cmacCtx = NULL;
  38505. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38506. /* No Init. */
  38507. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  38508. CMAC_CTX_free(cmacCtx);
  38509. /* Test AES-256-CBC */
  38510. cmacCtx = NULL;
  38511. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38512. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  38513. NULL), 1);
  38514. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38515. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38516. CMAC_CTX_free(cmacCtx);
  38517. /* Test AES-192-CBC */
  38518. cmacCtx = NULL;
  38519. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38520. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38521. NULL), 1);
  38522. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38523. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38524. CMAC_CTX_free(cmacCtx);
  38525. cmacCtx = NULL;
  38526. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38527. CMAC_CTX_free(cmacCtx);
  38528. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  38529. return EXPECT_RESULT();
  38530. }
  38531. static int test_wolfSSL_DES(void)
  38532. {
  38533. EXPECT_DECLS;
  38534. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38535. const_DES_cblock myDes;
  38536. DES_cblock iv;
  38537. DES_key_schedule key;
  38538. word32 i = 0;
  38539. DES_LONG dl = 0;
  38540. unsigned char msg[] = "hello wolfssl";
  38541. unsigned char weakKey[][8] = {
  38542. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  38543. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  38544. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  38545. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  38546. };
  38547. unsigned char semiWeakKey[][8] = {
  38548. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  38549. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  38550. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  38551. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  38552. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  38553. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  38554. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  38555. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  38556. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  38557. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  38558. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  38559. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  38560. };
  38561. DES_check_key(1);
  38562. DES_set_key(&myDes, &key);
  38563. /* check, check of odd parity */
  38564. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  38565. myDes[0] = 6; /* set even parity */
  38566. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38567. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  38568. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38569. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  38570. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  38571. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  38572. /* set odd parity for success case */
  38573. DES_set_odd_parity(&myDes);
  38574. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  38575. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  38576. myDes[3]);
  38577. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38578. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38579. ExpectIntEQ(key[i], myDes[i]);
  38580. }
  38581. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38582. /* check weak key */
  38583. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  38584. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38585. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  38586. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38587. DES_set_key_unchecked(NULL, NULL);
  38588. DES_set_key_unchecked(&myDes, NULL);
  38589. DES_set_key_unchecked(NULL, &key);
  38590. /* compare arrays, should be the same */
  38591. /* now do unchecked copy of a weak key over */
  38592. DES_set_key_unchecked(&myDes, &key);
  38593. /* compare arrays, should be the same */
  38594. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38595. ExpectIntEQ(key[i], myDes[i]);
  38596. }
  38597. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  38598. myDes[7] = 2;
  38599. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38600. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38601. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  38602. /* Test all weak keys. */
  38603. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  38604. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  38605. }
  38606. /* Test all semi-weak keys. */
  38607. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  38608. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  38609. }
  38610. /* check DES_key_sched API */
  38611. XMEMSET(key, 1, sizeof(DES_key_schedule));
  38612. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  38613. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  38614. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  38615. /* compare arrays, should be the same */
  38616. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38617. ExpectIntEQ(key[i], myDes[i]);
  38618. }
  38619. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  38620. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  38621. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  38622. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  38623. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  38624. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  38625. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  38626. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  38627. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  38628. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  38629. * DES_cbc_encrypt on the input */
  38630. XMEMSET(iv, 0, sizeof(DES_cblock));
  38631. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  38632. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  38633. ExpectIntEQ(dl, 480052723);
  38634. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  38635. return EXPECT_RESULT();
  38636. }
  38637. static int test_wolfSSL_DES_ncbc(void)
  38638. {
  38639. EXPECT_DECLS;
  38640. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38641. const_DES_cblock myDes;
  38642. DES_cblock iv = {1};
  38643. DES_key_schedule key = {0};
  38644. unsigned char msg[] = "hello wolfssl";
  38645. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38646. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38647. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38648. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38649. /* partial block test */
  38650. DES_set_key(&key, &myDes);
  38651. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38652. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38653. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38654. DES_set_key(&key, &myDes);
  38655. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38656. *((byte*)&iv) = 1;
  38657. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38658. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38659. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38660. /* full block test */
  38661. DES_set_key(&key, &myDes);
  38662. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38663. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38664. *((byte*)&iv) = 1;
  38665. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38666. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38667. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38668. DES_set_key(&key, &myDes);
  38669. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38670. *((byte*)&iv) = 1;
  38671. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38672. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38673. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38674. #endif
  38675. return EXPECT_RESULT();
  38676. }
  38677. static int test_wolfSSL_DES_ecb_encrypt(void)
  38678. {
  38679. EXPECT_DECLS;
  38680. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38681. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38682. WOLFSSL_DES_key_schedule key;
  38683. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38684. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38685. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38686. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38687. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  38688. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  38689. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  38690. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  38691. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  38692. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  38693. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  38694. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  38695. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  38696. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  38697. /* Encrypt messages */
  38698. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  38699. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  38700. {
  38701. /* Decrypt messages */
  38702. int ret1 = 0;
  38703. int ret2 = 0;
  38704. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  38705. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  38706. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  38707. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  38708. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  38709. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  38710. }
  38711. #endif
  38712. return EXPECT_RESULT();
  38713. }
  38714. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  38715. {
  38716. EXPECT_DECLS;
  38717. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38718. unsigned char input1[8], input2[8];
  38719. unsigned char output1[8], output2[8];
  38720. unsigned char back1[8], back2[8];
  38721. WOLFSSL_DES_cblock iv1, iv2;
  38722. WOLFSSL_DES_key_schedule key1, key2, key3;
  38723. int i;
  38724. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38725. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  38726. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  38727. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  38728. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  38729. XMEMSET(output1, 0, sizeof(output1));
  38730. XMEMSET(output2, 0, sizeof(output2));
  38731. XMEMSET(back1, 0, sizeof(back1));
  38732. XMEMSET(back2, 0, sizeof(back2));
  38733. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38734. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38735. /* Encrypt messages */
  38736. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  38737. DES_ENCRYPT);
  38738. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  38739. DES_ENCRYPT);
  38740. {
  38741. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38742. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38743. /* Decrypt messages */
  38744. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  38745. &iv1, DES_DECRYPT);
  38746. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  38747. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  38748. &iv2, DES_DECRYPT);
  38749. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  38750. }
  38751. for (i = 0; i < 8; i++) {
  38752. XMEMSET(output1, 0, sizeof(output1));
  38753. XMEMSET(output2, 0, sizeof(output2));
  38754. XMEMSET(back1, 0, sizeof(back1));
  38755. XMEMSET(back2, 0, sizeof(back2));
  38756. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38757. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38758. /* Encrypt partial messages */
  38759. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  38760. &iv1, DES_ENCRYPT);
  38761. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  38762. &iv2, DES_ENCRYPT);
  38763. {
  38764. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38765. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38766. /* Decrypt messages */
  38767. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  38768. &key3, &iv1, DES_DECRYPT);
  38769. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  38770. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  38771. &key3, &iv2, DES_DECRYPT);
  38772. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  38773. }
  38774. }
  38775. #endif
  38776. return EXPECT_RESULT();
  38777. }
  38778. static int test_wolfSSL_AES_encrypt(void)
  38779. {
  38780. EXPECT_DECLS;
  38781. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38782. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38783. AES_KEY enc;
  38784. AES_KEY dec;
  38785. const byte msg[] = {
  38786. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38787. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38788. };
  38789. const byte exp[] = {
  38790. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  38791. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  38792. };
  38793. const byte key[] = {
  38794. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38795. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38796. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38797. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38798. };
  38799. byte eout[sizeof(msg)];
  38800. byte dout[sizeof(msg)];
  38801. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  38802. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  38803. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  38804. wolfSSL_AES_encrypt(msg, NULL, NULL);
  38805. wolfSSL_AES_encrypt(NULL, eout, NULL);
  38806. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  38807. wolfSSL_AES_encrypt(msg, eout, NULL);
  38808. wolfSSL_AES_encrypt(msg, NULL, &enc);
  38809. wolfSSL_AES_encrypt(NULL, eout, &enc);
  38810. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  38811. wolfSSL_AES_decrypt(eout, NULL, NULL);
  38812. wolfSSL_AES_decrypt(NULL, dout, NULL);
  38813. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  38814. wolfSSL_AES_decrypt(eout, dout, NULL);
  38815. wolfSSL_AES_decrypt(eout, NULL, &dec);
  38816. wolfSSL_AES_decrypt(NULL, dout, &dec);
  38817. wolfSSL_AES_encrypt(msg, eout, &enc);
  38818. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  38819. wolfSSL_AES_decrypt(eout, dout, &dec);
  38820. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  38821. #endif
  38822. return EXPECT_RESULT();
  38823. }
  38824. static int test_wolfSSL_AES_ecb_encrypt(void)
  38825. {
  38826. EXPECT_DECLS;
  38827. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38828. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38829. AES_KEY aes;
  38830. const byte msg[] =
  38831. {
  38832. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38833. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  38834. };
  38835. const byte verify[] =
  38836. {
  38837. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  38838. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  38839. };
  38840. const byte key[] =
  38841. {
  38842. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38843. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38844. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38845. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  38846. };
  38847. byte out[AES_BLOCK_SIZE];
  38848. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  38849. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38850. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  38851. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  38852. #ifdef HAVE_AES_DECRYPT
  38853. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  38854. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38855. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  38856. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  38857. #endif
  38858. /* test bad arguments */
  38859. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  38860. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  38861. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  38862. #endif
  38863. return EXPECT_RESULT();
  38864. }
  38865. static int test_wolfSSL_AES_cbc_encrypt(void)
  38866. {
  38867. EXPECT_DECLS;
  38868. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38869. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38870. AES_KEY aes;
  38871. AES_KEY* aesN = NULL;
  38872. size_t len = 0;
  38873. size_t lenB = 0;
  38874. int keySz0 = 0;
  38875. int keySzN = -1;
  38876. byte out[AES_BLOCK_SIZE] = {0};
  38877. byte* outN = NULL;
  38878. /* Test vectors retrieved from:
  38879. * <begin URL>
  38880. * https://csrc.nist.gov/
  38881. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38882. * documents/aes/KAT_AES.zip
  38883. * </end URL>
  38884. */
  38885. const byte* pt128N = NULL;
  38886. byte* key128N = NULL;
  38887. byte* iv128N = NULL;
  38888. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38889. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38890. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38891. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38892. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38893. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38894. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38895. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38896. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38897. len = sizeof(pt128);
  38898. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38899. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38900. ExpectIntNE(XMEMCMP(b, g, h), i)
  38901. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38902. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38903. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38904. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38905. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38906. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38907. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38908. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38909. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38910. /* Stressing wolfSSL_AES_set_encrypt_key */
  38911. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38912. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38913. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38914. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38915. /* Stressing wolfSSL_AES_set_decrypt_key */
  38916. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38917. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38918. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38919. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38920. #ifdef WOLFSSL_AES_128
  38921. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38922. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38923. RESET_IV(iv128tmp, iv128);
  38924. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38925. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38926. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38927. wc_AesFree((Aes*)&aes);
  38928. #ifdef HAVE_AES_DECRYPT
  38929. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38930. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38931. RESET_IV(iv128tmp, iv128);
  38932. len = sizeof(ct128);
  38933. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38934. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38935. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38936. wc_AesFree((Aes*)&aes);
  38937. #endif
  38938. #endif /* WOLFSSL_AES_128 */
  38939. #ifdef WOLFSSL_AES_192
  38940. {
  38941. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38942. * Appendix F.2.3 */
  38943. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38944. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38945. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38946. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38947. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38948. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38949. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38950. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38951. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38952. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38953. len = sizeof(pt192);
  38954. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38955. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38956. RESET_IV(iv192tmp, iv192);
  38957. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38958. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38959. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38960. wc_AesFree((Aes*)&aes);
  38961. #ifdef HAVE_AES_DECRYPT
  38962. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38963. len = sizeof(ct192);
  38964. RESET_IV(iv192tmp, iv192);
  38965. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38966. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38967. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38968. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38969. wc_AesFree((Aes*)&aes);
  38970. #endif
  38971. }
  38972. #endif /* WOLFSSL_AES_192 */
  38973. #ifdef WOLFSSL_AES_256
  38974. {
  38975. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38976. * Appendix F.2.5 */
  38977. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38978. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38979. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38980. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38981. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38982. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38983. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38984. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38985. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38986. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38987. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38988. len = sizeof(pt256);
  38989. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38990. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38991. RESET_IV(iv256tmp, iv256);
  38992. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38993. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38994. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38995. wc_AesFree((Aes*)&aes);
  38996. #ifdef HAVE_AES_DECRYPT
  38997. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38998. len = sizeof(ct256);
  38999. RESET_IV(iv256tmp, iv256);
  39000. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39001. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39002. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39003. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39004. wc_AesFree((Aes*)&aes);
  39005. #endif
  39006. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39007. !defined(HAVE_SELFTEST)
  39008. {
  39009. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39010. byte wrapPlain[sizeof(key256)] = { 0 };
  39011. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39012. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39013. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39014. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39015. 15), WOLFSSL_FAILURE);
  39016. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39017. sizeof(key256)), sizeof(wrapCipher));
  39018. wc_AesFree((Aes*)&aes);
  39019. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39020. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39021. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39022. 23), WOLFSSL_FAILURE);
  39023. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39024. sizeof(wrapCipher)), sizeof(wrapPlain));
  39025. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39026. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39027. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39028. wc_AesFree((Aes*)&aes);
  39029. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39030. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39031. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39032. sizeof(key256)), sizeof(wrapCipher));
  39033. wc_AesFree((Aes*)&aes);
  39034. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39035. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39036. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39037. sizeof(wrapCipher)), sizeof(wrapPlain));
  39038. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39039. wc_AesFree((Aes*)&aes);
  39040. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39041. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39042. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39043. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39044. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39045. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39046. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39047. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39048. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39049. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39050. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39051. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39052. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39053. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39054. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39055. 0);
  39056. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39057. 0);
  39058. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39059. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39060. }
  39061. #endif /* HAVE_AES_KEYWRAP */
  39062. }
  39063. #endif /* WOLFSSL_AES_256 */
  39064. #endif
  39065. return EXPECT_RESULT();
  39066. }
  39067. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39068. {
  39069. EXPECT_DECLS;
  39070. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39071. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39072. AES_KEY aesEnc;
  39073. AES_KEY aesDec;
  39074. const byte msg[] = {
  39075. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39076. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39077. };
  39078. const byte exp[] = {
  39079. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39080. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39081. };
  39082. const byte key[] = {
  39083. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39084. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39085. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39086. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39087. };
  39088. const byte ivData[] = {
  39089. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39090. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39091. };
  39092. byte out[AES_BLOCK_SIZE];
  39093. byte iv[AES_BLOCK_SIZE];
  39094. word32 i;
  39095. int num;
  39096. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39097. XMEMCPY(iv, ivData, sizeof(iv));
  39098. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39099. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39100. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39101. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39102. #ifdef HAVE_AES_DECRYPT
  39103. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39104. XMEMCPY(iv, ivData, sizeof(iv));
  39105. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39106. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39107. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39108. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39109. #endif
  39110. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39111. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39112. XMEMCPY(iv, ivData, sizeof(iv));
  39113. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39114. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39115. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39116. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39117. if (i == 0) {
  39118. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39119. }
  39120. else {
  39121. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39122. }
  39123. #ifdef HAVE_AES_DECRYPT
  39124. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39125. XMEMCPY(iv, ivData, sizeof(iv));
  39126. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39127. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39128. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39129. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39130. if (i == 0) {
  39131. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39132. }
  39133. else {
  39134. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39135. }
  39136. #endif
  39137. }
  39138. if (EXPECT_SUCCESS()) {
  39139. /* test bad arguments */
  39140. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39141. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39142. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39143. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39144. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39145. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39146. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39147. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39148. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39149. }
  39150. #endif
  39151. return EXPECT_RESULT();
  39152. }
  39153. static int test_wolfSSL_CRYPTO_cts128(void)
  39154. {
  39155. EXPECT_DECLS;
  39156. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39157. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39158. byte tmp[64]; /* Largest vector size */
  39159. /* Test vectors taken form RFC3962 Appendix B */
  39160. const testVector vects[] = {
  39161. {
  39162. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39163. "\x20",
  39164. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39165. "\x97",
  39166. 17, 17
  39167. },
  39168. {
  39169. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39170. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39171. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39172. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39173. 31, 31
  39174. },
  39175. {
  39176. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39177. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39178. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39179. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39180. 32, 32
  39181. },
  39182. {
  39183. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39184. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39185. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39186. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39187. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39188. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39189. 47, 47
  39190. },
  39191. {
  39192. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39193. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39194. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  39195. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39196. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  39197. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  39198. 48, 48
  39199. },
  39200. {
  39201. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39202. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39203. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  39204. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  39205. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39206. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39207. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  39208. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  39209. 64, 64
  39210. }
  39211. };
  39212. byte keyBytes[AES_128_KEY_SIZE] = {
  39213. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  39214. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  39215. };
  39216. size_t i;
  39217. AES_KEY encKey;
  39218. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  39219. XMEMSET(tmp, 0, sizeof(tmp));
  39220. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  39221. AES_KEY decKey;
  39222. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39223. &encKey), 0);
  39224. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39225. &decKey), 0);
  39226. XMEMSET(iv, 0, sizeof(iv));
  39227. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  39228. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  39229. vects[i].outLen);
  39230. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  39231. XMEMSET(iv, 0, sizeof(iv));
  39232. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  39233. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  39234. vects[i].inLen);
  39235. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  39236. }
  39237. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39238. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39239. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39240. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39241. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39242. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  39243. (cbc128_f)AES_cbc_encrypt), 0);
  39244. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  39245. (cbc128_f)AES_cbc_encrypt), 0);
  39246. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  39247. (cbc128_f)AES_cbc_encrypt), 0);
  39248. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  39249. (cbc128_f)AES_cbc_encrypt), 0);
  39250. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  39251. (cbc128_f)AES_cbc_encrypt), 0);
  39252. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39253. /* Length too small. */
  39254. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  39255. (cbc128_f)AES_cbc_encrypt), 0);
  39256. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39257. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39258. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39259. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39260. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39261. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  39262. (cbc128_f)AES_cbc_encrypt), 0);
  39263. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  39264. (cbc128_f)AES_cbc_encrypt), 0);
  39265. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  39266. (cbc128_f)AES_cbc_encrypt), 0);
  39267. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  39268. (cbc128_f)AES_cbc_encrypt), 0);
  39269. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  39270. (cbc128_f)AES_cbc_encrypt), 0);
  39271. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39272. /* Length too small. */
  39273. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  39274. (cbc128_f)AES_cbc_encrypt), 0);
  39275. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  39276. return EXPECT_RESULT();
  39277. }
  39278. static int test_wolfSSL_RC4(void)
  39279. {
  39280. EXPECT_DECLS;
  39281. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  39282. WOLFSSL_RC4_KEY rc4Key;
  39283. unsigned char key[] = {
  39284. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39285. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39286. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39287. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39288. };
  39289. unsigned char data[] = {
  39290. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39291. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39292. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39293. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39294. };
  39295. unsigned char enc[sizeof(data)];
  39296. unsigned char dec[sizeof(data)];
  39297. word32 i;
  39298. word32 j;
  39299. wolfSSL_RC4_set_key(NULL, -1, NULL);
  39300. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  39301. wolfSSL_RC4_set_key(NULL, 0, NULL);
  39302. wolfSSL_RC4_set_key(NULL, -1, key);
  39303. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  39304. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  39305. wolfSSL_RC4_set_key(NULL, 0, key);
  39306. wolfSSL_RC4(NULL, 0, NULL, NULL);
  39307. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  39308. wolfSSL_RC4(NULL, 0, data, NULL);
  39309. wolfSSL_RC4(NULL, 0, NULL, enc);
  39310. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  39311. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  39312. wolfSSL_RC4(NULL, 0, data, enc);
  39313. ExpectIntEQ(1, 1);
  39314. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  39315. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  39316. XMEMSET(enc, 0, sizeof(enc));
  39317. XMEMSET(dec, 0, sizeof(dec));
  39318. /* Encrypt */
  39319. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39320. wolfSSL_RC4(&rc4Key, j, data, enc);
  39321. /* Decrypt */
  39322. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39323. wolfSSL_RC4(&rc4Key, j, enc, dec);
  39324. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  39325. }
  39326. }
  39327. #endif
  39328. return EXPECT_RESULT();
  39329. }
  39330. static int test_wolfSSL_OBJ(void)
  39331. {
  39332. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  39333. * mode
  39334. */
  39335. EXPECT_DECLS;
  39336. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  39337. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  39338. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  39339. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  39340. ASN1_OBJECT *obj = NULL;
  39341. ASN1_OBJECT *obj2 = NULL;
  39342. char buf[50];
  39343. XFILE fp = XBADFILE;
  39344. X509 *x509 = NULL;
  39345. X509_NAME *x509Name = NULL;
  39346. X509_NAME_ENTRY *x509NameEntry = NULL;
  39347. ASN1_OBJECT *asn1Name = NULL;
  39348. int numNames = 0;
  39349. BIO *bio = NULL;
  39350. int nid;
  39351. int i, j;
  39352. const char *f[] = {
  39353. #ifndef NO_RSA
  39354. "./certs/ca-cert.der",
  39355. #endif
  39356. #ifdef HAVE_ECC
  39357. "./certs/ca-ecc-cert.der",
  39358. "./certs/ca-ecc384-cert.der",
  39359. #endif
  39360. NULL};
  39361. ASN1_OBJECT *field_name_obj = NULL;
  39362. int lastpos = -1;
  39363. int tmp = -1;
  39364. ASN1_STRING *asn1 = NULL;
  39365. unsigned char *buf_dyn = NULL;
  39366. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  39367. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39368. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  39369. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  39370. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39371. ASN1_OBJECT_free(obj);
  39372. obj = NULL;
  39373. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  39374. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  39375. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  39376. #ifdef WOLFSSL_CERT_EXT
  39377. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  39378. #endif
  39379. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39380. ExpectNotNull(obj2 = OBJ_dup(obj));
  39381. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  39382. ASN1_OBJECT_free(obj);
  39383. obj = NULL;
  39384. ASN1_OBJECT_free(obj2);
  39385. obj2 = NULL;
  39386. for (i = 0; f[i] != NULL; i++)
  39387. {
  39388. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  39389. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  39390. if (fp != XBADFILE) {
  39391. XFCLOSE(fp);
  39392. fp = XBADFILE;
  39393. }
  39394. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  39395. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39396. /* Get the Common Name by using OBJ_txt2obj */
  39397. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  39398. do
  39399. {
  39400. lastpos = tmp;
  39401. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  39402. } while (tmp > -1);
  39403. ExpectIntNE(lastpos, -1);
  39404. ASN1_OBJECT_free(field_name_obj);
  39405. field_name_obj = NULL;
  39406. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  39407. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  39408. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  39409. /*
  39410. * All Common Names should be www.wolfssl.com
  39411. * This makes testing easier as we can test for the expected value.
  39412. */
  39413. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  39414. OPENSSL_free(buf_dyn);
  39415. buf_dyn = NULL;
  39416. bio = BIO_new(BIO_s_mem());
  39417. ExpectTrue(bio != NULL);
  39418. for (j = 0; j < numNames; j++)
  39419. {
  39420. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39421. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  39422. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39423. }
  39424. BIO_free(bio);
  39425. bio = NULL;
  39426. X509_free(x509);
  39427. x509 = NULL;
  39428. }
  39429. #ifdef HAVE_PKCS12
  39430. {
  39431. PKCS12 *p12 = NULL;
  39432. int boolRet;
  39433. EVP_PKEY *pkey = NULL;
  39434. const char *p12_f[] = {
  39435. #if !defined(NO_DES3) && !defined(NO_RSA)
  39436. "./certs/test-servercert.p12",
  39437. #endif
  39438. NULL};
  39439. for (i = 0; p12_f[i] != NULL; i++)
  39440. {
  39441. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  39442. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  39443. if (fp != XBADFILE) {
  39444. XFCLOSE(fp);
  39445. fp = XBADFILE;
  39446. }
  39447. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  39448. &pkey, &x509, NULL)) > 0);
  39449. wc_PKCS12_free(p12);
  39450. p12 = NULL;
  39451. EVP_PKEY_free(pkey);
  39452. x509Name = X509_get_issuer_name(x509);
  39453. ExpectNotNull(x509Name);
  39454. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39455. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  39456. for (j = 0; j < numNames; j++)
  39457. {
  39458. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39459. ExpectNotNull(asn1Name =
  39460. X509_NAME_ENTRY_get_object(x509NameEntry));
  39461. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39462. }
  39463. BIO_free(bio);
  39464. bio = NULL;
  39465. X509_free(x509);
  39466. x509 = NULL;
  39467. }
  39468. }
  39469. #endif /* HAVE_PKCS12 */
  39470. #endif
  39471. return EXPECT_RESULT();
  39472. }
  39473. static int test_wolfSSL_OBJ_cmp(void)
  39474. {
  39475. EXPECT_DECLS;
  39476. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  39477. ASN1_OBJECT *obj = NULL;
  39478. ASN1_OBJECT *obj2 = NULL;
  39479. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39480. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  39481. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  39482. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  39483. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  39484. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  39485. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  39486. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  39487. ASN1_OBJECT_free(obj);
  39488. ASN1_OBJECT_free(obj2);
  39489. #endif
  39490. return EXPECT_RESULT();
  39491. }
  39492. static int test_wolfSSL_OBJ_txt2nid(void)
  39493. {
  39494. EXPECT_DECLS;
  39495. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  39496. defined(WOLFSSL_APACHE_HTTPD)
  39497. int i;
  39498. static const struct {
  39499. const char* sn;
  39500. const char* ln;
  39501. const char* oid;
  39502. int nid;
  39503. } testVals[] = {
  39504. #ifdef WOLFSSL_APACHE_HTTPD
  39505. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  39506. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  39507. NID_id_on_dnsSRV },
  39508. { "msUPN", "Microsoft User Principal Name",
  39509. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  39510. #endif
  39511. { NULL, NULL, NULL, NID_undef }
  39512. };
  39513. /* Invalid cases */
  39514. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  39515. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  39516. /* Valid cases */
  39517. for (i = 0; testVals[i].sn != NULL; i++) {
  39518. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  39519. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  39520. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  39521. }
  39522. #endif
  39523. return EXPECT_RESULT();
  39524. }
  39525. static int test_wolfSSL_OBJ_txt2obj(void)
  39526. {
  39527. EXPECT_DECLS;
  39528. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  39529. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  39530. int i;
  39531. char buf[50];
  39532. ASN1_OBJECT* obj = NULL;
  39533. static const struct {
  39534. const char* oidStr;
  39535. const char* sn;
  39536. const char* ln;
  39537. } objs_list[] = {
  39538. #if defined(WOLFSSL_APACHE_HTTPD)
  39539. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  39540. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  39541. #endif
  39542. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  39543. { NULL, NULL, NULL }
  39544. };
  39545. static const struct {
  39546. const char* numeric;
  39547. const char* name;
  39548. } objs_named[] = {
  39549. /* In dictionary but not in normal list. */
  39550. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  39551. /* Made up OID. */
  39552. { "1.3.5.7", "1.3.5.7" },
  39553. { NULL, NULL }
  39554. };
  39555. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  39556. ASN1_OBJECT_free(obj);
  39557. obj = NULL;
  39558. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  39559. ASN1_OBJECT_free(obj);
  39560. obj = NULL;
  39561. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  39562. /* Test numerical value of oid (oidStr) */
  39563. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  39564. /* Convert object back to text to confirm oid is correct */
  39565. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39566. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39567. ASN1_OBJECT_free(obj);
  39568. obj = NULL;
  39569. XMEMSET(buf, 0, sizeof(buf));
  39570. /* Test short name (sn) */
  39571. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  39572. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  39573. /* Convert object back to text to confirm oid is correct */
  39574. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39575. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39576. ASN1_OBJECT_free(obj);
  39577. obj = NULL;
  39578. XMEMSET(buf, 0, sizeof(buf));
  39579. /* Test long name (ln) - should fail when no_name = 1 */
  39580. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  39581. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  39582. /* Convert object back to text to confirm oid is correct */
  39583. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39584. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39585. ASN1_OBJECT_free(obj);
  39586. obj = NULL;
  39587. XMEMSET(buf, 0, sizeof(buf));
  39588. }
  39589. for (i = 0; objs_named[i].numeric != NULL; i++) {
  39590. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  39591. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  39592. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  39593. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39594. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  39595. ASN1_OBJECT_free(obj);
  39596. obj = NULL;
  39597. }
  39598. #endif
  39599. return EXPECT_RESULT();
  39600. }
  39601. static int test_wolfSSL_PEM_write_bio_X509(void)
  39602. {
  39603. EXPECT_DECLS;
  39604. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  39605. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  39606. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  39607. !defined(NO_FILESYSTEM)
  39608. /* This test contains the hard coded expected
  39609. * lengths. Update if necessary */
  39610. XFILE fp = XBADFILE;
  39611. WOLFSSL_EVP_PKEY *priv = NULL;
  39612. BIO* input = NULL;
  39613. BIO* output = NULL;
  39614. X509* x509a = NULL;
  39615. X509* x509b = NULL;
  39616. ASN1_TIME* notBeforeA = NULL;
  39617. ASN1_TIME* notAfterA = NULL;
  39618. #ifndef NO_ASN_TIME
  39619. ASN1_TIME* notBeforeB = NULL;
  39620. ASN1_TIME* notAfterB = NULL;
  39621. #endif
  39622. int expectedLen;
  39623. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  39624. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  39625. if (fp != XBADFILE) {
  39626. XFCLOSE(fp);
  39627. fp = XBADFILE;
  39628. }
  39629. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  39630. "rb"));
  39631. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  39632. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  39633. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39634. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  39635. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  39636. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  39637. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39638. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39639. /* compare length against expected */
  39640. expectedLen = 2000;
  39641. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39642. #ifndef NO_ASN_TIME
  39643. /* read exported X509 PEM back into struct, sanity check on export,
  39644. * make sure notBefore/notAfter are the same and certs are identical. */
  39645. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39646. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  39647. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  39648. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  39649. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  39650. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39651. X509_free(x509b);
  39652. x509b = NULL;
  39653. #endif
  39654. /* Reset output buffer */
  39655. BIO_free(output);
  39656. output = NULL;
  39657. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39658. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39659. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39660. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39661. x509a->authKeyId = x509a->authKeyIdSrc;
  39662. x509a->authKeyIdSrc = NULL;
  39663. x509a->authKeyIdSrcSz = 0;
  39664. }
  39665. /* Resign to re-generate the der */
  39666. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39667. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39668. /* Check that we generate a smaller output since the AKID will
  39669. * only contain the KeyIdentifier without any additional
  39670. * information */
  39671. /* Here we copy the validity struct from the original */
  39672. expectedLen = 1688;
  39673. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39674. /* Reset buffers and x509 */
  39675. BIO_free(input);
  39676. input = NULL;
  39677. BIO_free(output);
  39678. output = NULL;
  39679. X509_free(x509a);
  39680. x509a = NULL;
  39681. /* test CA and basicConstSet values are encoded when
  39682. * the cert is a CA */
  39683. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39684. /* read PEM into X509 struct */
  39685. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39686. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39687. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39688. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39689. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  39690. * values are maintained and certs are identical.*/
  39691. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39692. ExpectIntEQ(x509b->isCa, 1);
  39693. ExpectIntEQ(x509b->basicConstSet, 1);
  39694. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39695. X509_free(x509a);
  39696. x509a = NULL;
  39697. X509_free(x509b);
  39698. x509b = NULL;
  39699. BIO_free(input);
  39700. input = NULL;
  39701. BIO_free(output);
  39702. output = NULL;
  39703. /* test CA and basicConstSet values are encoded when
  39704. * the cert is not CA */
  39705. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  39706. /* read PEM into X509 struct */
  39707. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39708. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39709. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39710. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39711. /* read exported X509 PEM back into struct, ensure isCa and
  39712. * basicConstSet values are maintained and certs are identical */
  39713. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39714. ExpectIntEQ(x509b->isCa, 0);
  39715. ExpectIntEQ(x509b->basicConstSet, 1);
  39716. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39717. wolfSSL_EVP_PKEY_free(priv);
  39718. X509_free(x509a);
  39719. X509_free(x509b);
  39720. BIO_free(input);
  39721. BIO_free(output);
  39722. #endif
  39723. return EXPECT_RESULT();
  39724. }
  39725. static int test_wolfSSL_X509_NAME_ENTRY(void)
  39726. {
  39727. EXPECT_DECLS;
  39728. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39729. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  39730. X509* x509 = NULL;
  39731. #ifndef NO_BIO
  39732. BIO* bio = NULL;
  39733. #endif
  39734. X509_NAME* nm = NULL;
  39735. X509_NAME_ENTRY* entry = NULL;
  39736. unsigned char cn[] = "another name to add";
  39737. #ifdef OPENSSL_ALL
  39738. int i;
  39739. int names_len = 0;
  39740. #endif
  39741. ExpectNotNull(x509 =
  39742. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39743. #ifndef NO_BIO
  39744. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39745. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  39746. #endif
  39747. #ifdef WOLFSSL_CERT_REQ
  39748. {
  39749. X509_REQ* req = NULL;
  39750. #ifndef NO_BIO
  39751. BIO* bReq = NULL;
  39752. #endif
  39753. ExpectNotNull(req =
  39754. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39755. #ifndef NO_BIO
  39756. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  39757. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  39758. BIO_free(bReq);
  39759. #endif
  39760. X509_free(req);
  39761. }
  39762. #endif
  39763. ExpectNotNull(nm = X509_get_subject_name(x509));
  39764. /* Test add entry */
  39765. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39766. 0x0c, cn, (int)sizeof(cn)));
  39767. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39768. #ifdef WOLFSSL_CERT_EXT
  39769. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39770. (byte*)"support@wolfssl.com", 19, -1,
  39771. 1), WOLFSSL_SUCCESS);
  39772. #endif
  39773. X509_NAME_ENTRY_free(entry);
  39774. entry = NULL;
  39775. #ifdef WOLFSSL_CERT_REQ
  39776. {
  39777. unsigned char srv_pkcs9p[] = "Server";
  39778. unsigned char fvrtDrnk[] = "tequila";
  39779. unsigned char* der = NULL;
  39780. char* subject = NULL;
  39781. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39782. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39783. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39784. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39785. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39786. ExpectNotNull(der);
  39787. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39788. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39789. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  39790. #ifdef DEBUG_WOLFSSL
  39791. if (subject != NULL) {
  39792. fprintf(stderr, "\n\t%s\n", subject);
  39793. }
  39794. #endif
  39795. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39796. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39797. }
  39798. #endif
  39799. /* Test add entry by text */
  39800. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39801. 0x0c, cn, (int)sizeof(cn)));
  39802. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39803. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39804. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39805. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39806. #endif
  39807. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39808. X509_NAME_ENTRY_free(entry);
  39809. entry = NULL;
  39810. /* Test add entry by NID */
  39811. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39812. cn, -1, -1, 0), SSL_SUCCESS);
  39813. #ifdef OPENSSL_ALL
  39814. /* stack of name entry */
  39815. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39816. for (i = 0; i < names_len; i++) {
  39817. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39818. }
  39819. #endif
  39820. #ifndef NO_BIO
  39821. BIO_free(bio);
  39822. #endif
  39823. X509_free(x509); /* free's nm */
  39824. #endif
  39825. return EXPECT_RESULT();
  39826. }
  39827. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39828. static int test_GENERAL_NAME_set0_othername(void) {
  39829. EXPECT_DECLS;
  39830. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39831. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39832. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39833. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39834. defined(WOLFSSL_FPKI)
  39835. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39836. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39837. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39838. const char * cert_fname = "./certs/server-cert.der";
  39839. const char * key_fname = "./certs/server-key.der";
  39840. X509* x509 = NULL;
  39841. GENERAL_NAME* gn = NULL;
  39842. GENERAL_NAMES* gns = NULL;
  39843. ASN1_OBJECT* upn_oid = NULL;
  39844. ASN1_UTF8STRING *utf8str = NULL;
  39845. ASN1_TYPE *value = NULL;
  39846. X509_EXTENSION * ext = NULL;
  39847. byte* pt = NULL;
  39848. byte der[4096];
  39849. int derSz = 0;
  39850. EVP_PKEY* priv = NULL;
  39851. XFILE f = XBADFILE;
  39852. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39853. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39854. if (f != XBADFILE) {
  39855. XFCLOSE(f);
  39856. f = XBADFILE;
  39857. }
  39858. ExpectNotNull(gn = GENERAL_NAME_new());
  39859. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39860. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39861. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39862. ExpectNotNull(value = ASN1_TYPE_new());
  39863. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39864. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39865. wolfSSL_ASN1_STRING_free(utf8str);
  39866. }
  39867. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39868. if (EXPECT_FAIL()) {
  39869. ASN1_TYPE_free(value);
  39870. }
  39871. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39872. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39873. if (EXPECT_FAIL()) {
  39874. GENERAL_NAME_free(gn);
  39875. gn = NULL;
  39876. }
  39877. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39878. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39879. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39880. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39881. if (f != XBADFILE) {
  39882. XFCLOSE(f);
  39883. f = XBADFILE;
  39884. }
  39885. pt = der;
  39886. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39887. (const unsigned char**)&pt, derSz));
  39888. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39889. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39890. gns = NULL;
  39891. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39892. NULL));
  39893. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39894. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39895. ExpectIntEQ(gn->type, 0);
  39896. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39897. ASN1_OBJECT_free(upn_oid);
  39898. X509_EXTENSION_free(ext);
  39899. X509_free(x509);
  39900. EVP_PKEY_free(priv);
  39901. #endif
  39902. return EXPECT_RESULT();
  39903. }
  39904. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39905. static int test_othername_and_SID_ext(void) {
  39906. EXPECT_DECLS;
  39907. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39908. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39909. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39910. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39911. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  39912. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39913. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39914. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39915. const char* csr_fname = "./certs/csr.signed.der";
  39916. const char* key_fname = "./certs/server-key.der";
  39917. byte der[4096];
  39918. int derSz = 0;
  39919. X509_REQ* x509 = NULL;
  39920. STACK_OF(X509_EXTENSION) *exts = NULL;
  39921. X509_EXTENSION * san_ext = NULL;
  39922. X509_EXTENSION * ext = NULL;
  39923. GENERAL_NAME* gn = NULL;
  39924. GENERAL_NAMES* gns = NULL;
  39925. ASN1_OBJECT* upn_oid = NULL;
  39926. ASN1_UTF8STRING *utf8str = NULL;
  39927. ASN1_TYPE *value = NULL;
  39928. ASN1_STRING *extval = NULL;
  39929. /* SID extension. SID data format explained here:
  39930. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  39931. */
  39932. uint8_t SidExtension[] = {
  39933. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  39934. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  39935. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  39936. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  39937. uint8_t expectedAltName[] = {
  39938. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  39939. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  39940. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  39941. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  39942. X509_EXTENSION *sid_ext = NULL;
  39943. ASN1_OBJECT* sid_oid = NULL;
  39944. ASN1_OCTET_STRING *sid_data = NULL;
  39945. EVP_PKEY* priv = NULL;
  39946. XFILE f = XBADFILE;
  39947. byte* pt = NULL;
  39948. BIO* bio = NULL;
  39949. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  39950. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  39951. if (f != XBADFILE) {
  39952. XFCLOSE(f);
  39953. f = XBADFILE;
  39954. }
  39955. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  39956. ExpectNotNull(gn = GENERAL_NAME_new());
  39957. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39958. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39959. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39960. ExpectNotNull(value = ASN1_TYPE_new());
  39961. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39962. if (EXPECT_FAIL()) {
  39963. ASN1_UTF8STRING_free(utf8str);
  39964. }
  39965. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39966. if (EXPECT_FAIL()) {
  39967. ASN1_TYPE_free(value);
  39968. GENERAL_NAME_free(gn);
  39969. gn = NULL;
  39970. }
  39971. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39972. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39973. if (EXPECT_FAIL()) {
  39974. GENERAL_NAME_free(gn);
  39975. }
  39976. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39977. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  39978. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  39979. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  39980. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  39981. sid_data));
  39982. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  39983. /* Ensure an empty stack doesn't raise an error. */
  39984. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39985. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  39986. if (EXPECT_FAIL()) {
  39987. X509_EXTENSION_free(san_ext);
  39988. }
  39989. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  39990. if (EXPECT_FAIL()) {
  39991. X509_EXTENSION_free(sid_ext);
  39992. }
  39993. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39994. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39995. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39996. if (f != XBADFILE)
  39997. XFCLOSE(f);
  39998. pt = der;
  39999. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40000. (const unsigned char**)&pt, derSz));
  40001. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40002. pt = der;
  40003. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40004. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40005. gns = NULL;
  40006. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40007. exts = NULL;
  40008. ASN1_OBJECT_free(upn_oid);
  40009. ASN1_OBJECT_free(sid_oid);
  40010. ASN1_OCTET_STRING_free(sid_data);
  40011. X509_REQ_free(x509);
  40012. x509 = NULL;
  40013. EVP_PKEY_free(priv);
  40014. /* At this point everything used to generate what is in der is cleaned up.
  40015. * We now read back from der to confirm the extensions were inserted
  40016. * correctly. */
  40017. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40018. ExpectNotNull(bio);
  40019. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40020. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40021. ExpectNotNull(x509);
  40022. BIO_free(bio);
  40023. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40024. x509));
  40025. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40026. /* Check the SID extension. */
  40027. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40028. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40029. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40030. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40031. /* Check the AltNames extension. */
  40032. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40033. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40034. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40035. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40036. 0);
  40037. /* Cleanup */
  40038. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40039. NULL));
  40040. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40041. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40042. ExpectIntEQ(gn->type, 0);
  40043. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40044. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40045. X509_REQ_free(x509);
  40046. #endif
  40047. return EXPECT_RESULT();
  40048. }
  40049. static int test_wolfSSL_X509_set_name(void)
  40050. {
  40051. EXPECT_DECLS;
  40052. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40053. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40054. X509* x509 = NULL;
  40055. X509_NAME* name = NULL;
  40056. ExpectNotNull(name = X509_NAME_new());
  40057. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40058. (byte*)"wolfssl.com", 11, 0, 1),
  40059. WOLFSSL_SUCCESS);
  40060. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40061. (byte*)"support@wolfssl.com", 19, -1,
  40062. 1), WOLFSSL_SUCCESS);
  40063. ExpectNotNull(x509 = X509_new());
  40064. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40065. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40066. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40067. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40068. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40069. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40070. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40071. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40072. X509_free(x509);
  40073. X509_NAME_free(name);
  40074. #endif /* OPENSSL_ALL && !NO_CERTS */
  40075. return EXPECT_RESULT();
  40076. }
  40077. static int test_wolfSSL_X509_set_notAfter(void)
  40078. {
  40079. EXPECT_DECLS;
  40080. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40081. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40082. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40083. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40084. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40085. /* Generalized time will overflow time_t if not long */
  40086. X509* x = NULL;
  40087. BIO* bio = NULL;
  40088. ASN1_TIME *asn_time = NULL;
  40089. ASN1_TIME *time_check = NULL;
  40090. const int year = 365*24*60*60;
  40091. const int day = 24*60*60;
  40092. const int hour = 60*60;
  40093. const int mini = 60;
  40094. int offset_day;
  40095. unsigned char buf[25];
  40096. time_t t;
  40097. /*
  40098. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40099. */
  40100. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40101. offset_day = 7;
  40102. /*
  40103. * Free these.
  40104. */
  40105. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40106. ExpectNotNull(asn_time);
  40107. ExpectNotNull(x = X509_new());
  40108. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40109. /*
  40110. * Tests
  40111. */
  40112. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40113. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40114. ExpectNotNull(time_check = X509_get_notAfter(x));
  40115. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40116. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40117. /* Convert to human readable format and compare to intended date */
  40118. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40119. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40120. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40121. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40122. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40123. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40124. /*
  40125. * Cleanup
  40126. */
  40127. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40128. X509_free(x);
  40129. BIO_free(bio);
  40130. #endif
  40131. return EXPECT_RESULT();
  40132. }
  40133. static int test_wolfSSL_X509_set_notBefore(void)
  40134. {
  40135. EXPECT_DECLS;
  40136. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40137. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40138. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40139. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40140. X509* x = NULL;
  40141. BIO* bio = NULL;
  40142. ASN1_TIME *asn_time = NULL;
  40143. ASN1_TIME *time_check = NULL;
  40144. const int year = 365*24*60*60;
  40145. const int day = 24*60*60;
  40146. const int hour = 60*60;
  40147. const int mini = 60;
  40148. int offset_day;
  40149. unsigned char buf[25];
  40150. time_t t;
  40151. /*
  40152. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40153. */
  40154. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40155. offset_day = 7;
  40156. /*
  40157. * Free these.
  40158. */
  40159. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40160. ExpectNotNull(asn_time);
  40161. ExpectNotNull(x = X509_new());
  40162. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40163. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40164. /*
  40165. * Main Tests
  40166. */
  40167. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40168. /* time_check == (ANS1_TIME*)x->notBefore */
  40169. ExpectNotNull(time_check = X509_get_notBefore(x));
  40170. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40171. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40172. /* Convert to human readable format and compare to intended date */
  40173. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40174. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40175. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40176. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40177. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40178. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40179. /*
  40180. * Cleanup
  40181. */
  40182. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40183. X509_free(x);
  40184. BIO_free(bio);
  40185. #endif
  40186. return EXPECT_RESULT();
  40187. }
  40188. static int test_wolfSSL_X509_set_version(void)
  40189. {
  40190. EXPECT_DECLS;
  40191. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40192. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40193. X509* x509 = NULL;
  40194. long v = 2L;
  40195. long maxInt = INT_MAX;
  40196. ExpectNotNull(x509 = X509_new());
  40197. /* These should pass. */
  40198. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  40199. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  40200. /* Fail Case: When v(long) is greater than x509->version(int). */
  40201. v = maxInt+1;
  40202. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  40203. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  40204. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  40205. /* Cleanup */
  40206. X509_free(x509);
  40207. #endif
  40208. return EXPECT_RESULT();
  40209. }
  40210. #ifndef NO_BIO
  40211. static int test_wolfSSL_BIO_gets(void)
  40212. {
  40213. EXPECT_DECLS;
  40214. #if defined(OPENSSL_EXTRA)
  40215. BIO* bio = NULL;
  40216. BIO* bio2 = NULL;
  40217. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  40218. char emp[] = "";
  40219. char bio_buffer[20];
  40220. int bufferSz = 20;
  40221. #ifdef OPENSSL_ALL
  40222. BUF_MEM* emp_bm = NULL;
  40223. BUF_MEM* msg_bm = NULL;
  40224. #endif
  40225. /* try with bad args */
  40226. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  40227. #ifdef OPENSSL_ALL
  40228. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  40229. #endif
  40230. /* try with real msg */
  40231. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40232. XMEMSET(bio_buffer, 0, bufferSz);
  40233. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  40234. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  40235. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  40236. ExpectFalse(bio2 != BIO_next(bio));
  40237. /* make buffer filled with no terminating characters */
  40238. XMEMSET(bio_buffer, 1, bufferSz);
  40239. /* BIO_gets reads a line of data */
  40240. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40241. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40242. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40243. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40244. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40245. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40246. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40247. #ifdef OPENSSL_ALL
  40248. /* test setting the mem_buf manually */
  40249. BIO_free(bio);
  40250. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40251. ExpectNotNull(emp_bm = BUF_MEM_new());
  40252. ExpectNotNull(msg_bm = BUF_MEM_new());
  40253. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  40254. if (EXPECT_SUCCESS()) {
  40255. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40256. msg_bm->data = NULL;
  40257. }
  40258. /* emp size is 1 for terminator */
  40259. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  40260. if (EXPECT_SUCCESS()) {
  40261. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40262. emp_bm->data = emp;
  40263. msg_bm->data = msg;
  40264. }
  40265. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  40266. /* check reading an empty string */
  40267. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40268. ExpectStrEQ(emp, bio_buffer);
  40269. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40270. /* BIO_gets reads a line of data */
  40271. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  40272. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40273. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40274. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40275. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40276. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40277. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40278. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40279. if (EXPECT_SUCCESS())
  40280. emp_bm->data = NULL;
  40281. BUF_MEM_free(emp_bm);
  40282. if (EXPECT_SUCCESS())
  40283. msg_bm->data = NULL;
  40284. BUF_MEM_free(msg_bm);
  40285. #endif
  40286. /* check not null terminated string */
  40287. BIO_free(bio);
  40288. bio = NULL;
  40289. msg[0] = 0x33;
  40290. msg[1] = 0x33;
  40291. msg[2] = 0x33;
  40292. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40293. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  40294. ExpectIntEQ(bio_buffer[0], msg[0]);
  40295. ExpectIntEQ(bio_buffer[1], msg[1]);
  40296. ExpectIntNE(bio_buffer[2], msg[2]);
  40297. BIO_free(bio);
  40298. bio = NULL;
  40299. msg[3] = 0x33;
  40300. bio_buffer[3] = 0x33;
  40301. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40302. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  40303. ExpectIntEQ(bio_buffer[0], msg[0]);
  40304. ExpectIntEQ(bio_buffer[1], msg[1]);
  40305. ExpectIntEQ(bio_buffer[2], msg[2]);
  40306. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  40307. /* check reading an empty string */
  40308. BIO_free(bio);
  40309. bio = NULL;
  40310. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  40311. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40312. ExpectStrEQ(emp, bio_buffer);
  40313. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40314. /* check error cases */
  40315. BIO_free(bio);
  40316. bio = NULL;
  40317. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  40318. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40319. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40320. #if !defined(NO_FILESYSTEM)
  40321. {
  40322. BIO* f_bio = NULL;
  40323. XFILE f = XBADFILE;
  40324. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  40325. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40326. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40327. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  40328. if (EXPECT_FAIL() && (f != XBADFILE)) {
  40329. XFCLOSE(f);
  40330. }
  40331. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40332. BIO_free(f_bio);
  40333. f_bio = NULL;
  40334. }
  40335. #endif /* NO_FILESYSTEM */
  40336. BIO_free(bio);
  40337. bio = NULL;
  40338. BIO_free(bio2);
  40339. bio2 = NULL;
  40340. /* try with type BIO */
  40341. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  40342. sizeof(msg));
  40343. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40344. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40345. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  40346. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  40347. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  40348. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  40349. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  40350. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40351. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40352. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40353. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40354. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40355. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40356. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40357. BIO_free(bio);
  40358. bio = NULL;
  40359. BIO_free(bio2);
  40360. bio2 = NULL;
  40361. /* check reading an empty string */
  40362. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40363. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  40364. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40365. ExpectStrEQ(emp, bio_buffer);
  40366. BIO_free(bio);
  40367. bio = NULL;
  40368. #endif
  40369. return EXPECT_RESULT();
  40370. }
  40371. static int test_wolfSSL_BIO_puts(void)
  40372. {
  40373. EXPECT_DECLS;
  40374. #if defined(OPENSSL_EXTRA)
  40375. BIO* bio = NULL;
  40376. char input[] = "hello\0world\n.....ok\n\0";
  40377. char output[128];
  40378. XMEMSET(output, 0, sizeof(output));
  40379. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40380. ExpectIntEQ(BIO_puts(bio, input), 5);
  40381. ExpectIntEQ(BIO_pending(bio), 5);
  40382. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  40383. ExpectIntEQ(BIO_pending(bio), 19);
  40384. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  40385. ExpectStrEQ(output, "helloworld\n");
  40386. ExpectIntEQ(BIO_pending(bio), 8);
  40387. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  40388. ExpectStrEQ(output, ".....ok\n");
  40389. ExpectIntEQ(BIO_pending(bio), 0);
  40390. ExpectIntEQ(BIO_puts(bio, ""), -1);
  40391. BIO_free(bio);
  40392. #endif
  40393. return EXPECT_RESULT();
  40394. }
  40395. static int test_wolfSSL_BIO_dump(void)
  40396. {
  40397. EXPECT_DECLS;
  40398. #if defined(OPENSSL_EXTRA)
  40399. BIO* bio;
  40400. static const unsigned char data[] = {
  40401. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  40402. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  40403. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  40404. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  40405. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  40406. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  40407. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  40408. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  40409. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  40410. 0xB4
  40411. };
  40412. /* Generated with OpenSSL. */
  40413. static const char expected[] =
  40414. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  40415. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  40416. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  40417. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  40418. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  40419. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  40420. static const char expectedAll[] =
  40421. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  40422. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  40423. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  40424. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  40425. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  40426. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  40427. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  40428. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  40429. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  40430. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  40431. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  40432. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  40433. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  40434. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  40435. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  40436. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  40437. char output[16 * 80];
  40438. int i;
  40439. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40440. /* Example key dumped. */
  40441. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  40442. sizeof(expected) - 1);
  40443. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  40444. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  40445. /* Try every possible value for a character. */
  40446. for (i = 0; i < 256; i++)
  40447. output[i] = i;
  40448. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  40449. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  40450. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  40451. BIO_free(bio);
  40452. #endif
  40453. return EXPECT_RESULT();
  40454. }
  40455. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40456. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40457. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40458. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  40459. {
  40460. (void)ssl;
  40461. (void)buf;
  40462. (void)sz;
  40463. (void)ctx;
  40464. return WOLFSSL_CBIO_ERR_WANT_READ;
  40465. }
  40466. #endif
  40467. static int test_wolfSSL_BIO_should_retry(void)
  40468. {
  40469. EXPECT_DECLS;
  40470. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40471. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40472. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40473. tcp_ready ready;
  40474. func_args server_args;
  40475. THREAD_TYPE serverThread;
  40476. SOCKET_T sockfd = 0;
  40477. WOLFSSL_CTX* ctx = NULL;
  40478. WOLFSSL* ssl = NULL;
  40479. char msg[64] = "hello wolfssl!";
  40480. char reply[1024];
  40481. int msgSz = (int)XSTRLEN(msg);
  40482. int ret;
  40483. BIO* bio = NULL;
  40484. XMEMSET(&server_args, 0, sizeof(func_args));
  40485. #ifdef WOLFSSL_TIRTOS
  40486. fdOpenSession(Task_self());
  40487. #endif
  40488. StartTCP();
  40489. InitTcpReady(&ready);
  40490. #if defined(USE_WINDOWS_API)
  40491. /* use RNG to get random port if using windows */
  40492. ready.port = GetRandomPort();
  40493. #endif
  40494. server_args.signal = &ready;
  40495. start_thread(test_server_nofail, &server_args, &serverThread);
  40496. wait_tcp_ready(&server_args);
  40497. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40498. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  40499. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  40500. #endif
  40501. ExpectIntEQ(WOLFSSL_SUCCESS,
  40502. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40503. ExpectIntEQ(WOLFSSL_SUCCESS,
  40504. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40505. ExpectIntEQ(WOLFSSL_SUCCESS,
  40506. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40507. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  40508. /* force retry */
  40509. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  40510. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  40511. ExpectNotNull(ssl);
  40512. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40513. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  40514. if (EXPECT_FAIL()) {
  40515. wolfSSL_free(ssl);
  40516. ssl = NULL;
  40517. }
  40518. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  40519. ExpectIntNE(BIO_should_retry(bio), 0);
  40520. ExpectIntEQ(BIO_should_read(bio), 0);
  40521. ExpectIntEQ(BIO_should_write(bio), 0);
  40522. /* now perform successful connection */
  40523. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  40524. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  40525. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  40526. ret = wolfSSL_get_error(ssl, -1);
  40527. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  40528. ExpectIntNE(BIO_should_retry(bio), 0);
  40529. if (ret == WOLFSSL_ERROR_WANT_READ)
  40530. ExpectIntEQ(BIO_should_read(bio), 1);
  40531. else
  40532. ExpectIntEQ(BIO_should_read(bio), 0);
  40533. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  40534. ExpectIntEQ(BIO_should_write(bio), 1);
  40535. else
  40536. ExpectIntEQ(BIO_should_write(bio), 0);
  40537. }
  40538. else {
  40539. ExpectIntEQ(BIO_should_retry(bio), 0);
  40540. ExpectIntEQ(BIO_should_read(bio), 0);
  40541. ExpectIntEQ(BIO_should_write(bio), 0);
  40542. }
  40543. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  40544. XSTRLEN("I hear you fa shizzle!")), 0);
  40545. BIO_free(bio);
  40546. wolfSSL_CTX_free(ctx);
  40547. CloseSocket(sockfd);
  40548. join_thread(serverThread);
  40549. FreeTcpReady(&ready);
  40550. #ifdef WOLFSSL_TIRTOS
  40551. fdOpenSession(Task_self());
  40552. #endif
  40553. #endif
  40554. return EXPECT_RESULT();
  40555. }
  40556. static int test_wolfSSL_BIO_connect(void)
  40557. {
  40558. EXPECT_DECLS;
  40559. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40560. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  40561. tcp_ready ready;
  40562. func_args server_args;
  40563. THREAD_TYPE serverThread;
  40564. BIO *tcpBio = NULL;
  40565. BIO *sslBio = NULL;
  40566. SSL_CTX* ctx = NULL;
  40567. SSL *ssl = NULL;
  40568. SSL *sslPtr;
  40569. char msg[] = "hello wolfssl!";
  40570. char reply[30];
  40571. char buff[10] = {0};
  40572. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40573. ExpectIntEQ(WOLFSSL_SUCCESS,
  40574. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40575. ExpectIntEQ(WOLFSSL_SUCCESS,
  40576. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40577. ExpectIntEQ(WOLFSSL_SUCCESS,
  40578. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40579. /* Setup server */
  40580. XMEMSET(&server_args, 0, sizeof(func_args));
  40581. StartTCP();
  40582. InitTcpReady(&ready);
  40583. #if defined(USE_WINDOWS_API)
  40584. /* use RNG to get random port if using windows */
  40585. ready.port = GetRandomPort();
  40586. #endif
  40587. server_args.signal = &ready;
  40588. start_thread(test_server_nofail, &server_args, &serverThread);
  40589. wait_tcp_ready(&server_args);
  40590. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40591. /* Start the test proper */
  40592. /* Setup the TCP BIO */
  40593. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  40594. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  40595. /* Setup the SSL object */
  40596. ExpectNotNull(ssl = SSL_new(ctx));
  40597. SSL_set_connect_state(ssl);
  40598. /* Setup the SSL BIO */
  40599. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  40600. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  40601. if (EXPECT_FAIL()) {
  40602. wolfSSL_free(ssl);
  40603. }
  40604. /* Verify that BIO_get_ssl works. */
  40605. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  40606. ExpectPtrEq(ssl, sslPtr);
  40607. /* Link BIO's so that sslBio uses tcpBio for IO */
  40608. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  40609. /* Do TCP connect */
  40610. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40611. /* Do TLS handshake */
  40612. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40613. /* Test writing */
  40614. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40615. /* Expect length of default wolfSSL reply */
  40616. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40617. /* Clean it all up */
  40618. BIO_free_all(sslBio);
  40619. /* Server clean up */
  40620. join_thread(serverThread);
  40621. FreeTcpReady(&ready);
  40622. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  40623. * after. */
  40624. XMEMSET(&server_args, 0, sizeof(func_args));
  40625. StartTCP();
  40626. InitTcpReady(&ready);
  40627. #if defined(USE_WINDOWS_API)
  40628. /* use RNG to get random port if using windows */
  40629. ready.port = GetRandomPort();
  40630. #endif
  40631. server_args.signal = &ready;
  40632. start_thread(test_server_nofail, &server_args, &serverThread);
  40633. wait_tcp_ready(&server_args);
  40634. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40635. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  40636. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  40637. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  40638. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40639. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40640. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40641. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40642. /* Attempt to close the TLS connection gracefully. */
  40643. BIO_ssl_shutdown(sslBio);
  40644. BIO_free_all(sslBio);
  40645. join_thread(serverThread);
  40646. FreeTcpReady(&ready);
  40647. SSL_CTX_free(ctx);
  40648. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40649. wc_ecc_fp_free(); /* free per thread cache */
  40650. #endif
  40651. #endif
  40652. return EXPECT_RESULT();
  40653. }
  40654. static int test_wolfSSL_BIO_tls(void)
  40655. {
  40656. EXPECT_DECLS;
  40657. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40658. SSL_CTX* ctx = NULL;
  40659. SSL *ssl = NULL;
  40660. BIO *readBio = NULL;
  40661. BIO *writeBio = NULL;
  40662. int ret;
  40663. int err = 0;
  40664. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40665. ExpectNotNull(ssl = SSL_new(ctx));
  40666. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40667. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40668. /* Qt reads data from write-bio,
  40669. * then writes the read data into plain packet.
  40670. * Qt reads data from plain packet,
  40671. * then writes the read data into read-bio.
  40672. */
  40673. SSL_set_bio(ssl, readBio, writeBio);
  40674. do {
  40675. #ifdef WOLFSSL_ASYNC_CRYPT
  40676. if (err == WC_PENDING_E) {
  40677. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40678. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40679. }
  40680. #endif
  40681. ret = SSL_connect(ssl);
  40682. err = SSL_get_error(ssl, 0);
  40683. } while (err == WC_PENDING_E);
  40684. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40685. /* in this use case, should return WANT READ
  40686. * so that Qt will read the data from plain packet for next state.
  40687. */
  40688. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  40689. SSL_free(ssl);
  40690. SSL_CTX_free(ctx);
  40691. #endif
  40692. return EXPECT_RESULT();
  40693. }
  40694. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40695. defined(HAVE_HTTP_CLIENT)
  40696. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  40697. {
  40698. BIO* clientBio;
  40699. SSL* sslClient;
  40700. SSL_CTX* ctx;
  40701. char connectAddr[20]; /* IP + port */;
  40702. (void)args;
  40703. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  40704. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  40705. AssertIntEQ(BIO_do_connect(clientBio), 1);
  40706. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40707. AssertNotNull(sslClient = SSL_new(ctx));
  40708. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  40709. SSL_set_bio(sslClient, clientBio, clientBio);
  40710. AssertIntEQ(SSL_connect(sslClient), 1);
  40711. SSL_free(sslClient);
  40712. SSL_CTX_free(ctx);
  40713. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40714. wc_ecc_fp_free(); /* free per thread cache */
  40715. #endif
  40716. WOLFSSL_RETURN_FROM_THREAD(0);
  40717. }
  40718. #endif
  40719. static int test_wolfSSL_BIO_accept(void)
  40720. {
  40721. EXPECT_DECLS;
  40722. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40723. defined(HAVE_HTTP_CLIENT)
  40724. BIO* serverBindBio = NULL;
  40725. BIO* serverAcceptBio = NULL;
  40726. SSL* sslServer = NULL;
  40727. SSL_CTX* ctx = NULL;
  40728. func_args args;
  40729. THREAD_TYPE thread;
  40730. char port[10]; /* 10 bytes should be enough to store the string
  40731. * representation of the port */
  40732. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  40733. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  40734. /* First BIO_do_accept binds the port */
  40735. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40736. XMEMSET(&args, 0, sizeof(func_args));
  40737. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  40738. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40739. /* Let's plug it into SSL to test */
  40740. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40741. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40742. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40743. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40744. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40745. ExpectNotNull(sslServer = SSL_new(ctx));
  40746. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  40747. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  40748. ExpectIntEQ(SSL_accept(sslServer), 1);
  40749. join_thread(thread);
  40750. BIO_free(serverBindBio);
  40751. SSL_free(sslServer);
  40752. SSL_CTX_free(ctx);
  40753. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40754. wc_ecc_fp_free(); /* free per thread cache */
  40755. #endif
  40756. #endif
  40757. return EXPECT_RESULT();
  40758. }
  40759. static int test_wolfSSL_BIO_write(void)
  40760. {
  40761. EXPECT_DECLS;
  40762. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  40763. BIO* bio = NULL;
  40764. BIO* bio64 = NULL;
  40765. BIO* bio_mem = NULL;
  40766. BIO* ptr = NULL;
  40767. int sz;
  40768. char msg[] = "conversion test";
  40769. char out[40];
  40770. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  40771. void* bufPtr = NULL;
  40772. BUF_MEM* buf = NULL;
  40773. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40774. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  40775. if (EXPECT_FAIL()) {
  40776. BIO_free(bio64);
  40777. }
  40778. /* now should convert to base64 then write to memory */
  40779. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40780. BIO_flush(bio);
  40781. /* test BIO chain */
  40782. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  40783. ExpectNotNull(buf);
  40784. ExpectIntEQ(buf->length, 25);
  40785. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  40786. ExpectPtrEq(buf->data, bufPtr);
  40787. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  40788. sz = sizeof(out);
  40789. XMEMSET(out, 0, sz);
  40790. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  40791. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40792. /* write then read should return the same message */
  40793. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40794. sz = sizeof(out);
  40795. XMEMSET(out, 0, sz);
  40796. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40797. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40798. /* now try encoding with no line ending */
  40799. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40800. #ifdef HAVE_EX_DATA
  40801. BIO_set_ex_data(bio64, 0, (void*) "data");
  40802. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40803. #endif
  40804. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40805. BIO_flush(bio);
  40806. sz = sizeof(out);
  40807. XMEMSET(out, 0, sz);
  40808. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40809. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40810. BIO_free_all(bio); /* frees bio64 also */
  40811. bio = NULL;
  40812. /* test with more than one bio64 in list */
  40813. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40814. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40815. if (EXPECT_FAIL()) {
  40816. BIO_free(bio64);
  40817. bio64 = NULL;
  40818. }
  40819. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40820. ExpectNotNull(BIO_push(bio64, bio_mem));
  40821. if (EXPECT_FAIL()) {
  40822. BIO_free(bio_mem);
  40823. }
  40824. /* now should convert to base64 when stored and then decode with read */
  40825. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40826. BIO_flush(bio);
  40827. sz = sizeof(out);
  40828. XMEMSET(out, 0, sz);
  40829. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40830. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40831. BIO_clear_flags(bio64, ~0);
  40832. BIO_set_retry_read(bio);
  40833. BIO_free_all(bio); /* frees bio64s also */
  40834. bio = NULL;
  40835. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40836. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40837. BIO_free(bio);
  40838. #endif
  40839. return EXPECT_RESULT();
  40840. }
  40841. static int test_wolfSSL_BIO_printf(void)
  40842. {
  40843. EXPECT_DECLS;
  40844. #if defined(OPENSSL_ALL)
  40845. BIO* bio = NULL;
  40846. int sz = 7;
  40847. char msg[] = "TLS 1.3 for the world";
  40848. char out[60];
  40849. char expected[] = "TLS 1.3 for the world : sz = 7";
  40850. XMEMSET(out, 0, sizeof(out));
  40851. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40852. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40853. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40854. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40855. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40856. BIO_free(bio);
  40857. #endif
  40858. return EXPECT_RESULT();
  40859. }
  40860. static int test_wolfSSL_BIO_f_md(void)
  40861. {
  40862. EXPECT_DECLS;
  40863. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40864. BIO* bio = NULL;
  40865. BIO* mem = NULL;
  40866. char msg[] = "message to hash";
  40867. char out[60];
  40868. EVP_MD_CTX* ctx = NULL;
  40869. const unsigned char testKey[] =
  40870. {
  40871. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40872. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40873. 0x0b, 0x0b, 0x0b, 0x0b
  40874. };
  40875. const char testData[] = "Hi There";
  40876. const unsigned char testResult[] =
  40877. {
  40878. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40879. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40880. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40881. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40882. };
  40883. const unsigned char expectedHash[] =
  40884. {
  40885. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40886. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40887. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40888. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40889. };
  40890. const unsigned char emptyHash[] =
  40891. {
  40892. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40893. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40894. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40895. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40896. };
  40897. unsigned char check[sizeof(testResult) + 1];
  40898. size_t checkSz = -1;
  40899. EVP_PKEY* key = NULL;
  40900. XMEMSET(out, 0, sizeof(out));
  40901. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40902. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40903. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40904. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  40905. /* should not be able to write/read yet since just digest wrapper and no
  40906. * data is passing through the bio */
  40907. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  40908. ExpectIntEQ(BIO_pending(bio), 0);
  40909. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  40910. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  40911. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40912. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  40913. BIO_reset(bio);
  40914. /* append BIO mem to bio in order to read/write */
  40915. ExpectNotNull(bio = BIO_push(bio, mem));
  40916. XMEMSET(out, 0, sizeof(out));
  40917. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  40918. ExpectIntEQ(BIO_pending(bio), 16);
  40919. /* this just reads the message and does not hash it (gets calls final) */
  40920. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  40921. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40922. /* create a message digest using BIO */
  40923. XMEMSET(out, 0, sizeof(out));
  40924. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  40925. ExpectIntEQ(BIO_pending(mem), 16);
  40926. ExpectIntEQ(BIO_pending(bio), 16);
  40927. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40928. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  40929. BIO_free(bio);
  40930. bio = NULL;
  40931. BIO_free(mem);
  40932. mem = NULL;
  40933. /* test with HMAC */
  40934. XMEMSET(out, 0, sizeof(out));
  40935. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40936. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40937. BIO_get_md_ctx(bio, &ctx);
  40938. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  40939. (int)sizeof(testKey)));
  40940. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  40941. ExpectNotNull(bio = BIO_push(bio, mem));
  40942. BIO_write(bio, testData, (int)strlen(testData));
  40943. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  40944. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  40945. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  40946. EVP_PKEY_free(key);
  40947. BIO_free(bio);
  40948. BIO_free(mem);
  40949. #endif
  40950. return EXPECT_RESULT();
  40951. }
  40952. static int test_wolfSSL_BIO_up_ref(void)
  40953. {
  40954. EXPECT_DECLS;
  40955. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40956. BIO* bio = NULL;
  40957. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40958. ExpectIntEQ(BIO_up_ref(NULL), 0);
  40959. ExpectIntEQ(BIO_up_ref(bio), 1);
  40960. BIO_free(bio);
  40961. ExpectIntEQ(BIO_up_ref(bio), 1);
  40962. BIO_free(bio);
  40963. BIO_free(bio);
  40964. #endif
  40965. return EXPECT_RESULT();
  40966. }
  40967. static int test_wolfSSL_BIO_reset(void)
  40968. {
  40969. EXPECT_DECLS;
  40970. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40971. BIO* bio = NULL;
  40972. byte buf[16];
  40973. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  40974. (word32)XSTRLEN("secure your data")));
  40975. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  40976. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  40977. XMEMSET(buf, 0, 16);
  40978. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  40979. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  40980. /* You cannot write to MEM BIO with read-only mode. */
  40981. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  40982. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  40983. XMEMSET(buf, 0, 16);
  40984. ExpectIntEQ(BIO_reset(bio), 0);
  40985. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  40986. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  40987. BIO_free(bio);
  40988. #endif
  40989. return EXPECT_RESULT();
  40990. }
  40991. #endif /* !NO_BIO */
  40992. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40993. /* test that the callback arg is correct */
  40994. static int certCbArg = 0;
  40995. static int certCb(WOLFSSL* ssl, void* arg)
  40996. {
  40997. if (ssl == NULL || arg != &certCbArg)
  40998. return 0;
  40999. if (wolfSSL_is_server(ssl)) {
  41000. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41001. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41002. return 0;
  41003. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41004. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41005. return 0;
  41006. }
  41007. else {
  41008. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41009. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41010. return 0;
  41011. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41012. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41013. return 0;
  41014. }
  41015. return 1;
  41016. }
  41017. static int certSetupCb(WOLFSSL_CTX* ctx)
  41018. {
  41019. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41020. return TEST_SUCCESS;
  41021. }
  41022. /**
  41023. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41024. * to stop certificate and key loading
  41025. */
  41026. static int certClearCb(WOLFSSL* ssl)
  41027. {
  41028. /* Clear the loaded certs to force the callbacks to set them up */
  41029. SSL_certs_clear(ssl);
  41030. return TEST_SUCCESS;
  41031. }
  41032. #endif
  41033. static int test_wolfSSL_cert_cb(void)
  41034. {
  41035. EXPECT_DECLS;
  41036. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41037. test_ssl_cbf func_cb_client;
  41038. test_ssl_cbf func_cb_server;
  41039. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41040. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41041. func_cb_client.ctx_ready = certSetupCb;
  41042. func_cb_client.ssl_ready = certClearCb;
  41043. func_cb_server.ctx_ready = certSetupCb;
  41044. func_cb_server.ssl_ready = certClearCb;
  41045. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41046. &func_cb_server, NULL), TEST_SUCCESS);
  41047. #endif
  41048. return EXPECT_RESULT();
  41049. }
  41050. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41051. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41052. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41053. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41054. {
  41055. EXPECT_DECLS;
  41056. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41057. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41058. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41059. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41060. return EXPECT_RESULT();
  41061. }
  41062. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41063. {
  41064. const byte* suites = NULL;
  41065. word16 suiteSz = 0;
  41066. const byte* hashSigAlgo = NULL;
  41067. word16 hashSigAlgoSz = 0;
  41068. word16 idx = 0;
  41069. int haveRSA = 0;
  41070. int haveECC = 0;
  41071. (void)arg;
  41072. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41073. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41074. return 0;
  41075. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41076. hashSigAlgoSz == 0)
  41077. return 0;
  41078. for (idx = 0; idx < suiteSz; idx += 2) {
  41079. WOLFSSL_CIPHERSUITE_INFO info =
  41080. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41081. if (info.rsaAuth)
  41082. haveRSA = 1;
  41083. else if (info.eccAuth)
  41084. haveECC = 1;
  41085. }
  41086. if (hashSigAlgoSz > 0) {
  41087. /* sigalgs extension takes precedence over ciphersuites */
  41088. haveRSA = 0;
  41089. haveECC = 0;
  41090. }
  41091. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41092. int hashAlgo = 0;
  41093. int sigAlgo = 0;
  41094. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41095. &hashAlgo, &sigAlgo) != 0)
  41096. return 0;
  41097. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41098. haveRSA = 1;
  41099. else if (sigAlgo == ECDSAk)
  41100. haveECC = 1;
  41101. }
  41102. if (haveRSA) {
  41103. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41104. != WOLFSSL_SUCCESS)
  41105. return 0;
  41106. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41107. != WOLFSSL_SUCCESS)
  41108. return 0;
  41109. }
  41110. else if (haveECC) {
  41111. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41112. != WOLFSSL_SUCCESS)
  41113. return 0;
  41114. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41115. != WOLFSSL_SUCCESS)
  41116. return 0;
  41117. }
  41118. return 1;
  41119. }
  41120. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41121. {
  41122. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41123. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41124. return TEST_SUCCESS;
  41125. }
  41126. #endif
  41127. /* Testing dynamic ciphers offered by client */
  41128. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41129. {
  41130. EXPECT_DECLS;
  41131. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41132. test_ssl_cbf func_cb_client;
  41133. test_ssl_cbf func_cb_server;
  41134. struct {
  41135. method_provider client_meth;
  41136. const char* client_ciphers;
  41137. const char* client_sigalgs;
  41138. const char* client_ca;
  41139. method_provider server_meth;
  41140. } test_params[] = {
  41141. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41142. #ifdef WOLFSSL_TLS13
  41143. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41144. {wolfTLSv1_3_client_method,
  41145. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41146. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41147. #endif
  41148. #ifdef HAVE_ECC
  41149. {wolfTLSv1_3_client_method,
  41150. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41151. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41152. #endif
  41153. #endif
  41154. #ifndef WOLFSSL_NO_TLS12
  41155. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41156. {wolfTLSv1_2_client_method,
  41157. "DHE-RSA-AES128-GCM-SHA256",
  41158. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41159. #endif
  41160. #ifdef HAVE_ECC
  41161. {wolfTLSv1_2_client_method,
  41162. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41163. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41164. #endif
  41165. #endif
  41166. #endif
  41167. };
  41168. size_t i;
  41169. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41170. if (testCount > 0) {
  41171. for (i = 0; i < testCount; i++) {
  41172. printf("\tTesting %s ciphers with %s sigalgs\n",
  41173. test_params[i].client_ciphers,
  41174. test_params[i].client_sigalgs);
  41175. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41176. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41177. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41178. test_params[i].client_ciphers;
  41179. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41180. test_params[i].client_sigalgs;
  41181. func_cb_client.method = test_params[i].client_meth;
  41182. func_cb_client.caPemFile = test_params[i].client_ca;
  41183. func_cb_client.ctx_ready =
  41184. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41185. func_cb_server.ctx_ready =
  41186. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41187. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41188. func_cb_server.method = test_params[i].server_meth;
  41189. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41190. &func_cb_server, NULL), TEST_SUCCESS);
  41191. }
  41192. }
  41193. #endif
  41194. return EXPECT_RESULT();
  41195. }
  41196. static int test_wolfSSL_ciphersuite_auth(void)
  41197. {
  41198. EXPECT_DECLS;
  41199. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41200. WOLFSSL_CIPHERSUITE_INFO info;
  41201. (void)info;
  41202. #ifndef WOLFSSL_NO_TLS12
  41203. #ifdef HAVE_CHACHA
  41204. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41205. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  41206. ExpectIntEQ(info.rsaAuth, 1);
  41207. ExpectIntEQ(info.eccAuth, 0);
  41208. ExpectIntEQ(info.eccStatic, 0);
  41209. ExpectIntEQ(info.psk, 0);
  41210. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41211. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  41212. ExpectIntEQ(info.rsaAuth, 0);
  41213. ExpectIntEQ(info.eccAuth, 1);
  41214. ExpectIntEQ(info.eccStatic, 0);
  41215. ExpectIntEQ(info.psk, 0);
  41216. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41217. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  41218. ExpectIntEQ(info.rsaAuth, 0);
  41219. ExpectIntEQ(info.eccAuth, 0);
  41220. ExpectIntEQ(info.eccStatic, 0);
  41221. ExpectIntEQ(info.psk, 1);
  41222. #endif
  41223. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  41224. #ifndef NO_RSA
  41225. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41226. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  41227. ExpectIntEQ(info.rsaAuth, 1);
  41228. ExpectIntEQ(info.eccAuth, 0);
  41229. ExpectIntEQ(info.eccStatic, 0);
  41230. ExpectIntEQ(info.psk, 0);
  41231. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41232. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  41233. ExpectIntEQ(info.rsaAuth, 1);
  41234. ExpectIntEQ(info.eccAuth, 0);
  41235. ExpectIntEQ(info.eccStatic, 1);
  41236. ExpectIntEQ(info.psk, 0);
  41237. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41238. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  41239. ExpectIntEQ(info.rsaAuth, 1);
  41240. ExpectIntEQ(info.eccAuth, 0);
  41241. ExpectIntEQ(info.eccStatic, 1);
  41242. ExpectIntEQ(info.psk, 0);
  41243. #endif
  41244. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41245. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  41246. ExpectIntEQ(info.rsaAuth, 0);
  41247. ExpectIntEQ(info.eccAuth, 1);
  41248. ExpectIntEQ(info.eccStatic, 0);
  41249. ExpectIntEQ(info.psk, 0);
  41250. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41251. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  41252. ExpectIntEQ(info.rsaAuth, 0);
  41253. ExpectIntEQ(info.eccAuth, 1);
  41254. ExpectIntEQ(info.eccStatic, 1);
  41255. ExpectIntEQ(info.psk, 0);
  41256. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  41257. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  41258. ExpectIntEQ(info.rsaAuth, 0);
  41259. ExpectIntEQ(info.eccAuth, 0);
  41260. ExpectIntEQ(info.eccStatic, 0);
  41261. ExpectIntEQ(info.psk, 1);
  41262. #endif
  41263. #endif
  41264. #ifdef WOLFSSL_TLS13
  41265. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  41266. TLS_AES_128_GCM_SHA256);
  41267. ExpectIntEQ(info.rsaAuth, 0);
  41268. ExpectIntEQ(info.eccAuth, 0);
  41269. ExpectIntEQ(info.eccStatic, 0);
  41270. ExpectIntEQ(info.psk, 0);
  41271. #endif
  41272. #endif
  41273. return EXPECT_RESULT();
  41274. }
  41275. static int test_wolfSSL_sigalg_info(void)
  41276. {
  41277. EXPECT_DECLS;
  41278. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41279. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  41280. word16 len = 0;
  41281. word16 idx = 0;
  41282. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  41283. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  41284. for (idx = 0; idx < len; idx += 2) {
  41285. int hashAlgo = 0;
  41286. int sigAlgo = 0;
  41287. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41288. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41289. ExpectIntNE(hashAlgo, 0);
  41290. ExpectIntNE(sigAlgo, 0);
  41291. }
  41292. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  41293. 0xFFFFFFFF, &len);
  41294. for (idx = 0; idx < len; idx += 2) {
  41295. int hashAlgo = 0;
  41296. int sigAlgo = 0;
  41297. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41298. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41299. ExpectIntNE(hashAlgo, 0);
  41300. }
  41301. #endif
  41302. return EXPECT_RESULT();
  41303. }
  41304. static int test_wolfSSL_SESSION(void)
  41305. {
  41306. EXPECT_DECLS;
  41307. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41308. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41309. !defined(NO_SESSION_CACHE)
  41310. WOLFSSL* ssl = NULL;
  41311. WOLFSSL_CTX* ctx = NULL;
  41312. WOLFSSL_SESSION* sess = NULL;
  41313. WOLFSSL_SESSION* sess_copy = NULL;
  41314. #ifdef OPENSSL_EXTRA
  41315. #ifdef HAVE_EXT_CACHE
  41316. unsigned char* sessDer = NULL;
  41317. unsigned char* ptr = NULL;
  41318. int sz = 0;
  41319. #endif
  41320. const unsigned char context[] = "user app context";
  41321. unsigned int contextSz = (unsigned int)sizeof(context);
  41322. #endif
  41323. int ret = 0, err = 0;
  41324. SOCKET_T sockfd;
  41325. tcp_ready ready;
  41326. func_args server_args;
  41327. THREAD_TYPE serverThread;
  41328. char msg[80];
  41329. const char* sendGET = "GET";
  41330. /* TLS v1.3 requires session tickets */
  41331. /* CHACHA and POLY1305 required for myTicketEncCb */
  41332. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  41333. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  41334. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  41335. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  41336. #else
  41337. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41338. #endif
  41339. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  41340. WOLFSSL_FILETYPE_PEM));
  41341. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  41342. WOLFSSL_FILETYPE_PEM));
  41343. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41344. WOLFSSL_SUCCESS);
  41345. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41346. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  41347. #endif
  41348. #ifdef HAVE_SESSION_TICKET
  41349. /* Use session tickets, for ticket tests below */
  41350. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  41351. #endif
  41352. XMEMSET(&server_args, 0, sizeof(func_args));
  41353. #ifdef WOLFSSL_TIRTOS
  41354. fdOpenSession(Task_self());
  41355. #endif
  41356. StartTCP();
  41357. InitTcpReady(&ready);
  41358. #if defined(USE_WINDOWS_API)
  41359. /* use RNG to get random port if using windows */
  41360. ready.port = GetRandomPort();
  41361. #endif
  41362. server_args.signal = &ready;
  41363. start_thread(test_server_nofail, &server_args, &serverThread);
  41364. wait_tcp_ready(&server_args);
  41365. /* client connection */
  41366. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41367. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  41368. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41369. #ifdef WOLFSSL_ASYNC_CRYPT
  41370. err = 0; /* Reset error */
  41371. #endif
  41372. do {
  41373. #ifdef WOLFSSL_ASYNC_CRYPT
  41374. if (err == WC_PENDING_E) {
  41375. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41376. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41377. }
  41378. #endif
  41379. ret = wolfSSL_connect(ssl);
  41380. err = wolfSSL_get_error(ssl, 0);
  41381. } while (err == WC_PENDING_E);
  41382. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  41383. #ifdef WOLFSSL_ASYNC_CRYPT
  41384. err = 0; /* Reset error */
  41385. #endif
  41386. do {
  41387. #ifdef WOLFSSL_ASYNC_CRYPT
  41388. if (err == WC_PENDING_E) {
  41389. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41390. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41391. }
  41392. #endif
  41393. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  41394. err = wolfSSL_get_error(ssl, 0);
  41395. } while (err == WC_PENDING_E);
  41396. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  41397. #ifdef WOLFSSL_ASYNC_CRYPT
  41398. err = 0; /* Reset error */
  41399. #endif
  41400. do {
  41401. #ifdef WOLFSSL_ASYNC_CRYPT
  41402. if (err == WC_PENDING_E) {
  41403. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41404. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41405. }
  41406. #endif
  41407. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  41408. err = wolfSSL_get_error(ssl, 0);
  41409. } while (err == WC_PENDING_E);
  41410. ExpectIntEQ(ret, 23);
  41411. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  41412. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  41413. #ifdef HAVE_EXT_CACHE
  41414. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  41415. * HAVE_EXT_CACHE we get new objects each time */
  41416. #endif
  41417. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41418. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  41419. sess = wolfSSL_get_session(ssl);
  41420. #ifdef OPENSSL_EXTRA
  41421. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  41422. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  41423. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  41424. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  41425. #ifdef HAVE_SESSION_TICKET
  41426. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  41427. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  41428. SESSION_TICKET_HINT_DEFAULT);
  41429. #else
  41430. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  41431. #endif
  41432. #else
  41433. (void)sess;
  41434. #endif /* OPENSSL_EXTRA */
  41435. /* Retain copy of the session for later testing */
  41436. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  41437. wolfSSL_shutdown(ssl);
  41438. wolfSSL_free(ssl); ssl = NULL;
  41439. CloseSocket(sockfd);
  41440. join_thread(serverThread);
  41441. FreeTcpReady(&ready);
  41442. #ifdef WOLFSSL_TIRTOS
  41443. fdOpenSession(Task_self());
  41444. #endif
  41445. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  41446. {
  41447. X509 *x509 = NULL;
  41448. char buf[30];
  41449. int bufSz = 0;
  41450. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  41451. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  41452. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  41453. sizeof(buf))), 0);
  41454. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  41455. if (bufSz == 7) {
  41456. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  41457. }
  41458. if (bufSz == 16) {
  41459. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  41460. }
  41461. }
  41462. #endif
  41463. #ifdef HAVE_EXT_CACHE
  41464. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  41465. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41466. sess_copy = NULL;
  41467. #endif
  41468. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  41469. /* get session from DER and update the timeout */
  41470. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  41471. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  41472. wolfSSL_SESSION_free(sess); sess = NULL;
  41473. sess = NULL;
  41474. ptr = sessDer;
  41475. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  41476. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  41477. (const unsigned char**)&ptr, sz));
  41478. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  41479. sessDer = NULL;
  41480. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  41481. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41482. #endif
  41483. /* successful set session test */
  41484. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41485. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  41486. #ifdef HAVE_SESSION_TICKET
  41487. /* Test set/get session ticket */
  41488. {
  41489. const char* ticket = "This is a session ticket";
  41490. char buf[64] = {0};
  41491. word32 bufSz = (word32)sizeof(buf);
  41492. ExpectIntEQ(SSL_SUCCESS,
  41493. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  41494. (word32)XSTRLEN(ticket)));
  41495. ExpectIntEQ(SSL_SUCCESS,
  41496. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  41497. ExpectStrEQ(ticket, buf);
  41498. }
  41499. #endif
  41500. #ifdef OPENSSL_EXTRA
  41501. /* session timeout case */
  41502. /* make the session to be expired */
  41503. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  41504. XSLEEP_MS(1200);
  41505. /* SSL_set_session should reject specified session but return success
  41506. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  41507. */
  41508. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41509. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  41510. #else
  41511. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  41512. #endif
  41513. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41514. #ifdef WOLFSSL_SESSION_ID_CTX
  41515. /* fail case with miss match session context IDs (use compatibility API) */
  41516. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  41517. SSL_SUCCESS);
  41518. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41519. wolfSSL_free(ssl); ssl = NULL;
  41520. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  41521. SSL_FAILURE);
  41522. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  41523. SSL_SUCCESS);
  41524. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41525. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41526. #endif
  41527. #endif /* OPENSSL_EXTRA */
  41528. wolfSSL_free(ssl);
  41529. wolfSSL_SESSION_free(sess);
  41530. wolfSSL_CTX_free(ctx);
  41531. #endif
  41532. return EXPECT_RESULT();
  41533. }
  41534. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41535. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41536. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41537. !defined(WOLFSSL_NO_TLS12)
  41538. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  41539. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  41540. {
  41541. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41542. /* returns previous timeout value */
  41543. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  41544. #else
  41545. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  41546. #endif
  41547. }
  41548. /* set the session to timeout in a second */
  41549. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  41550. {
  41551. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  41552. }
  41553. /* store the client side session from the first successful connection */
  41554. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  41555. {
  41556. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  41557. NULL); /* ref count 1 */
  41558. }
  41559. /* wait till session is expired then set it in the WOLFSSL struct for use */
  41560. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  41561. {
  41562. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  41563. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41564. WOLFSSL_SUCCESS);
  41565. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  41566. }
  41567. /* set expired session in the WOLFSSL struct for use */
  41568. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  41569. {
  41570. XSLEEP_MS(1200); /* wait a second for session to expire */
  41571. /* set the expired session, call to set session fails but continuing on
  41572. after failure should be handled here */
  41573. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41574. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41575. WOLFSSL_SUCCESS);
  41576. #else
  41577. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41578. WOLFSSL_SUCCESS);
  41579. #endif
  41580. }
  41581. /* check that the expired session was not reused */
  41582. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  41583. {
  41584. /* since the session has expired it should not have been reused */
  41585. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  41586. }
  41587. #endif
  41588. static int test_wolfSSL_SESSION_expire_downgrade(void)
  41589. {
  41590. EXPECT_DECLS;
  41591. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41592. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41593. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41594. !defined(WOLFSSL_NO_TLS12)
  41595. WOLFSSL_CTX* ctx = NULL;
  41596. callback_functions server_cbf, client_cbf;
  41597. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  41598. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  41599. /* force server side to use TLS 1.2 */
  41600. server_cbf.ctx = ctx;
  41601. server_cbf.method = wolfTLSv1_2_server_method;
  41602. client_cbf.method = wolfSSLv23_client_method;
  41603. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41604. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  41605. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  41606. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41607. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41608. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41609. /* set the previously created session and wait till expired */
  41610. server_cbf.ctx = ctx;
  41611. client_cbf.method = wolfSSLv23_client_method;
  41612. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41613. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  41614. client_cbf.on_result =
  41615. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41616. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41617. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41618. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41619. /* set the previously created expired session */
  41620. server_cbf.ctx = ctx;
  41621. client_cbf.method = wolfSSLv23_client_method;
  41622. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41623. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  41624. client_cbf.on_result =
  41625. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41626. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41627. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41628. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41629. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  41630. wolfSSL_CTX_free(ctx);
  41631. #endif
  41632. return EXPECT_RESULT();
  41633. }
  41634. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41635. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41636. static int clientSessRemCountMalloc = 0;
  41637. static int serverSessRemCountMalloc = 0;
  41638. static int clientSessRemCountFree = 0;
  41639. static int serverSessRemCountFree = 0;
  41640. static WOLFSSL_CTX* serverSessCtx = NULL;
  41641. static WOLFSSL_SESSION* serverSess = NULL;
  41642. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41643. !defined(NO_SESSION_CACHE_REF)
  41644. static WOLFSSL_CTX* clientSessCtx = NULL;
  41645. static WOLFSSL_SESSION* clientSess = NULL;
  41646. #endif
  41647. static int serverSessRemIdx = 3;
  41648. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  41649. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  41650. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  41651. {
  41652. int* side;
  41653. (void)ctx;
  41654. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  41655. if (side != NULL) {
  41656. if (*side == WOLFSSL_CLIENT_END)
  41657. clientSessRemCountFree++;
  41658. else
  41659. serverSessRemCountFree++;
  41660. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41661. }
  41662. }
  41663. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41664. {
  41665. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41666. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41667. !defined(NO_SESSION_CACHE_REF)
  41668. {
  41669. EXPECT_DECLS;
  41670. /* Allow downgrade, set min version, and disable TLS 1.3.
  41671. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41672. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41673. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41674. * the session in the cache. In this case we need to downgrade to
  41675. * previous versions to just use the legacy session ID field. */
  41676. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41677. SSL_SUCCESS);
  41678. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41679. SSL_SUCCESS);
  41680. return EXPECT_RESULT();
  41681. }
  41682. #else
  41683. return TEST_SUCCESS;
  41684. #endif
  41685. }
  41686. static int SessRemSslSetupCb(WOLFSSL* ssl)
  41687. {
  41688. EXPECT_DECLS;
  41689. int* side;
  41690. if (SSL_is_server(ssl)) {
  41691. side = &sessRemCtx_Server;
  41692. serverSessRemCountMalloc++;
  41693. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  41694. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  41695. SSL_SUCCESS);
  41696. }
  41697. else {
  41698. side = &sessRemCtx_Client;
  41699. clientSessRemCountMalloc++;
  41700. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41701. !defined(NO_SESSION_CACHE_REF)
  41702. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  41703. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  41704. SSL_SUCCESS);
  41705. #endif
  41706. }
  41707. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  41708. serverSessRemIdx, side), SSL_SUCCESS);
  41709. return EXPECT_RESULT();
  41710. }
  41711. #endif
  41712. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  41713. {
  41714. EXPECT_DECLS;
  41715. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41716. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41717. /* Check that the remove callback gets called for external data in a
  41718. * session object */
  41719. test_ssl_cbf func_cb;
  41720. XMEMSET(&func_cb, 0, sizeof(func_cb));
  41721. func_cb.ctx_ready = SessRemCtxSetupCb;
  41722. func_cb.on_result = SessRemSslSetupCb;
  41723. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  41724. NULL), TEST_SUCCESS);
  41725. /* Both should have been allocated */
  41726. ExpectIntEQ(clientSessRemCountMalloc, 1);
  41727. ExpectIntEQ(serverSessRemCountMalloc, 1);
  41728. /* This should not be called yet. Session wasn't evicted from cache yet. */
  41729. ExpectIntEQ(clientSessRemCountFree, 0);
  41730. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41731. !defined(NO_SESSION_CACHE_REF)
  41732. /* Force a cache lookup */
  41733. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41734. /* Force a cache update */
  41735. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  41736. /* This should set the timeout to 0 and call the remove callback from within
  41737. * the session cache. */
  41738. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  41739. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41740. ExpectIntEQ(clientSessRemCountFree, 1);
  41741. #endif
  41742. /* Server session is in the cache so ex_data isn't free'd with the SSL
  41743. * object */
  41744. ExpectIntEQ(serverSessRemCountFree, 0);
  41745. /* Force a cache lookup */
  41746. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41747. /* Force a cache update */
  41748. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  41749. /* This should set the timeout to 0 and call the remove callback from within
  41750. * the session cache. */
  41751. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  41752. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41753. ExpectIntEQ(serverSessRemCountFree, 1);
  41754. /* Need to free the references that we kept */
  41755. SSL_CTX_free(serverSessCtx);
  41756. SSL_SESSION_free(serverSess);
  41757. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41758. !defined(NO_SESSION_CACHE_REF)
  41759. SSL_CTX_free(clientSessCtx);
  41760. SSL_SESSION_free(clientSess);
  41761. #endif
  41762. #endif
  41763. return EXPECT_RESULT();
  41764. }
  41765. static int test_wolfSSL_ticket_keys(void)
  41766. {
  41767. EXPECT_DECLS;
  41768. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  41769. !defined(NO_WOLFSSL_SERVER)
  41770. WOLFSSL_CTX* ctx = NULL;
  41771. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  41772. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41773. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  41774. WOLFSSL_FAILURE);
  41775. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  41776. WOLFSSL_FAILURE);
  41777. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  41778. WOLFSSL_FAILURE);
  41779. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  41780. WOLFSSL_FAILURE);
  41781. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41782. WOLFSSL_FAILURE);
  41783. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41784. WOLFSSL_FAILURE);
  41785. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41786. WOLFSSL_FAILURE);
  41787. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  41788. WOLFSSL_FAILURE);
  41789. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  41790. WOLFSSL_FAILURE);
  41791. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  41792. WOLFSSL_FAILURE);
  41793. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  41794. WOLFSSL_FAILURE);
  41795. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41796. WOLFSSL_FAILURE);
  41797. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41798. WOLFSSL_FAILURE);
  41799. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41800. WOLFSSL_FAILURE);
  41801. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41802. WOLFSSL_SUCCESS);
  41803. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41804. WOLFSSL_SUCCESS);
  41805. wolfSSL_CTX_free(ctx);
  41806. #endif
  41807. return EXPECT_RESULT();
  41808. }
  41809. #ifndef NO_BIO
  41810. static int test_wolfSSL_d2i_PUBKEY(void)
  41811. {
  41812. EXPECT_DECLS;
  41813. #if defined(OPENSSL_EXTRA)
  41814. BIO* bio = NULL;
  41815. EVP_PKEY* pkey = NULL;
  41816. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41817. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  41818. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  41819. /* RSA PUBKEY test */
  41820. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  41821. sizeof_client_keypub_der_2048), 0);
  41822. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41823. EVP_PKEY_free(pkey);
  41824. pkey = NULL;
  41825. #endif
  41826. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  41827. /* ECC PUBKEY test */
  41828. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  41829. sizeof_ecc_clikeypub_der_256), 0);
  41830. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41831. EVP_PKEY_free(pkey);
  41832. pkey = NULL;
  41833. #endif
  41834. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  41835. /* DSA PUBKEY test */
  41836. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  41837. sizeof_dsa_pub_key_der_2048), 0);
  41838. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41839. EVP_PKEY_free(pkey);
  41840. pkey = NULL;
  41841. #endif
  41842. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  41843. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  41844. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41845. (HAVE_FIPS_VERSION > 2))
  41846. /* DH PUBKEY test */
  41847. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  41848. sizeof_dh_pub_key_der_2048), 0);
  41849. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41850. EVP_PKEY_free(pkey);
  41851. pkey = NULL;
  41852. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41853. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  41854. BIO_free(bio);
  41855. (void)pkey;
  41856. #endif
  41857. return EXPECT_RESULT();
  41858. }
  41859. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41860. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  41861. {
  41862. EXPECT_DECLS;
  41863. BIO* bio = NULL;
  41864. EVP_PKEY* pkey = NULL;
  41865. WOLFSSL_CTX* ctx = NULL;
  41866. #if defined(WOLFSSL_KEY_GEN)
  41867. unsigned char buff[4096];
  41868. unsigned char* bufPtr = buff;
  41869. #endif
  41870. /* test creating new EVP_PKEY with bad arg */
  41871. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  41872. /* test loading RSA key using BIO */
  41873. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41874. {
  41875. XFILE file = XBADFILE;
  41876. const char* fname = "./certs/server-key.der";
  41877. size_t sz = 0;
  41878. byte* buf = NULL;
  41879. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41880. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41881. ExpectTrue((sz = XFTELL(file)) != 0);
  41882. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41883. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41884. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41885. if (file != XBADFILE) {
  41886. XFCLOSE(file);
  41887. }
  41888. /* Test using BIO new mem and loading DER private key */
  41889. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41890. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41891. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41892. BIO_free(bio);
  41893. bio = NULL;
  41894. EVP_PKEY_free(pkey);
  41895. pkey = NULL;
  41896. }
  41897. #endif
  41898. /* test loading ECC key using BIO */
  41899. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  41900. {
  41901. XFILE file = XBADFILE;
  41902. const char* fname = "./certs/ecc-key.der";
  41903. size_t sz = 0;
  41904. byte* buf = NULL;
  41905. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41906. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41907. ExpectTrue((sz = XFTELL(file)) != 0);
  41908. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41909. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41910. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41911. if (file != XBADFILE)
  41912. XFCLOSE(file);
  41913. /* Test using BIO new mem and loading DER private key */
  41914. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41915. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41916. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41917. BIO_free(bio);
  41918. bio = NULL;
  41919. EVP_PKEY_free(pkey);
  41920. pkey = NULL;
  41921. }
  41922. #endif
  41923. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41924. #ifndef NO_WOLFSSL_SERVER
  41925. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41926. #else
  41927. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41928. #endif
  41929. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  41930. {
  41931. RSA* rsa = NULL;
  41932. /* Tests bad parameters */
  41933. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  41934. /* RSA not set yet, expecting to fail*/
  41935. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  41936. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  41937. /* set RSA using bio*/
  41938. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  41939. sizeof_client_key_der_2048), 0);
  41940. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  41941. ExpectNotNull(rsa);
  41942. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  41943. /* i2d RSAprivate key tests */
  41944. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  41945. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  41946. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41947. sizeof_client_key_der_2048);
  41948. bufPtr -= sizeof_client_key_der_2048;
  41949. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41950. sizeof_client_key_der_2048), 0);
  41951. bufPtr = NULL;
  41952. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41953. sizeof_client_key_der_2048);
  41954. ExpectNotNull(bufPtr);
  41955. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41956. sizeof_client_key_der_2048), 0);
  41957. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  41958. RSA_free(rsa);
  41959. rsa = RSA_new();
  41960. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  41961. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  41962. RSA_free(rsa);
  41963. }
  41964. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  41965. SSL_CTX_free(ctx);
  41966. ctx = NULL;
  41967. BIO_free(bio);
  41968. bio = NULL;
  41969. return EXPECT_RESULT();
  41970. }
  41971. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  41972. #endif /* !NO_BIO */
  41973. static int test_wolfSSL_sk_GENERAL_NAME(void)
  41974. {
  41975. EXPECT_DECLS;
  41976. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41977. !defined(NO_RSA)
  41978. X509* x509 = NULL;
  41979. GENERAL_NAME* gn = NULL;
  41980. unsigned char buf[4096];
  41981. const unsigned char* bufPt = NULL;
  41982. int bytes = 0;
  41983. int i;
  41984. int j;
  41985. XFILE f = XBADFILE;
  41986. STACK_OF(GENERAL_NAME)* sk = NULL;
  41987. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  41988. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41989. if (f != XBADFILE)
  41990. XFCLOSE(f);
  41991. for (j = 0; j < 2; ++j) {
  41992. bufPt = buf;
  41993. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41994. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41995. NID_subject_alt_name, NULL, NULL));
  41996. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  41997. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  41998. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  41999. if (gn != NULL) {
  42000. switch (gn->type) {
  42001. case GEN_DNS:
  42002. fprintf(stderr, "found type GEN_DNS\n");
  42003. break;
  42004. case GEN_EMAIL:
  42005. fprintf(stderr, "found type GEN_EMAIL\n");
  42006. break;
  42007. case GEN_URI:
  42008. fprintf(stderr, "found type GEN_URI\n");
  42009. break;
  42010. }
  42011. }
  42012. }
  42013. X509_free(x509);
  42014. x509 = NULL;
  42015. if (j == 0) {
  42016. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42017. }
  42018. else {
  42019. /*
  42020. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42021. * it was supposed to. This is a regression test for that bug.
  42022. */
  42023. GENERAL_NAMES_free(sk);
  42024. }
  42025. sk = NULL;
  42026. }
  42027. #endif
  42028. return EXPECT_RESULT();
  42029. }
  42030. static int test_wolfSSL_GENERAL_NAME_print(void)
  42031. {
  42032. EXPECT_DECLS;
  42033. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42034. X509* x509 = NULL;
  42035. GENERAL_NAME* gn = NULL;
  42036. unsigned char buf[4096];
  42037. const unsigned char* bufPt = NULL;
  42038. int bytes;
  42039. XFILE f = XBADFILE;
  42040. STACK_OF(GENERAL_NAME)* sk = NULL;
  42041. BIO* out = NULL;
  42042. unsigned char outbuf[128];
  42043. X509_EXTENSION* ext = NULL;
  42044. AUTHORITY_INFO_ACCESS* aia = NULL;
  42045. ACCESS_DESCRIPTION* ad = NULL;
  42046. ASN1_IA5STRING *dnsname = NULL;
  42047. const unsigned char v4Addr[] = {192,168,53,1};
  42048. const unsigned char v6Addr[] =
  42049. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42050. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42051. const unsigned char email[] =
  42052. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42053. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42054. const char* dnsStr = "DNS:example.com";
  42055. const char* uriStr = "URI:http://127.0.0.1:22220";
  42056. const char* v4addStr = "IP Address:192.168.53.1";
  42057. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42058. const char* emailStr = "email:info@wolfssl.com";
  42059. const char* othrStr = "othername:<unsupported>";
  42060. const char* x400Str = "X400Name:<unsupported>";
  42061. const char* ediStr = "EdiPartyName:<unsupported>";
  42062. /* BIO to output */
  42063. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42064. /* test for NULL param */
  42065. gn = NULL;
  42066. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42067. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42068. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42069. /* test for GEN_DNS */
  42070. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42071. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42072. if (f != XBADFILE) {
  42073. XFCLOSE(f);
  42074. f = XBADFILE;
  42075. }
  42076. bufPt = buf;
  42077. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42078. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42079. NID_subject_alt_name, NULL, NULL));
  42080. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42081. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42082. XMEMSET(outbuf, 0, sizeof(outbuf));
  42083. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42084. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42085. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42086. gn = NULL;
  42087. sk = NULL;
  42088. X509_free(x509);
  42089. x509 = NULL;
  42090. /* Lets test for setting as well. */
  42091. ExpectNotNull(gn = GENERAL_NAME_new());
  42092. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42093. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42094. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42095. dnsname = NULL;
  42096. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42097. XMEMSET(outbuf, 0, sizeof(outbuf));
  42098. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42099. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42100. GENERAL_NAME_free(gn);
  42101. /* test for GEN_URI */
  42102. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42103. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42104. if (f != XBADFILE) {
  42105. XFCLOSE(f);
  42106. f = XBADFILE;
  42107. }
  42108. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42109. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42110. ext));
  42111. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42112. if (ad != NULL) {
  42113. gn = ad->location;
  42114. }
  42115. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42116. gn = NULL;
  42117. XMEMSET(outbuf,0,sizeof(outbuf));
  42118. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42119. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42120. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42121. aia = NULL;
  42122. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42123. ExpectNotNull(aia);
  42124. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42125. aia = NULL;
  42126. X509_free(x509);
  42127. x509 = NULL;
  42128. /* test for GEN_IPADD */
  42129. /* ip v4 address */
  42130. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42131. if (gn != NULL) {
  42132. gn->type = GEN_IPADD;
  42133. if (gn->d.iPAddress != NULL) {
  42134. gn->d.iPAddress->length = sizeof(v4Addr);
  42135. }
  42136. }
  42137. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42138. sizeof(v4Addr)), 1);
  42139. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42140. XMEMSET(outbuf,0,sizeof(outbuf));
  42141. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42142. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42143. GENERAL_NAME_free(gn);
  42144. gn = NULL;
  42145. /* ip v6 address */
  42146. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42147. if (gn != NULL) {
  42148. gn->type = GEN_IPADD;
  42149. if (gn->d.iPAddress != NULL) {
  42150. gn->d.iPAddress->length = sizeof(v6Addr);
  42151. }
  42152. }
  42153. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42154. sizeof(v6Addr)), 1);
  42155. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42156. XMEMSET(outbuf,0,sizeof(outbuf));
  42157. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42158. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42159. GENERAL_NAME_free(gn);
  42160. gn = NULL;
  42161. /* test for GEN_EMAIL */
  42162. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42163. if (gn != NULL) {
  42164. gn->type = GEN_EMAIL;
  42165. if (gn->d.rfc822Name != NULL) {
  42166. gn->d.rfc822Name->length = sizeof(email);
  42167. }
  42168. }
  42169. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42170. 1);
  42171. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42172. XMEMSET(outbuf,0,sizeof(outbuf));
  42173. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42174. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42175. GENERAL_NAME_free(gn);
  42176. gn = NULL;
  42177. /* test for GEN_OTHERNAME */
  42178. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42179. if (gn != NULL) {
  42180. gn->type = GEN_OTHERNAME;
  42181. }
  42182. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42183. XMEMSET(outbuf,0,sizeof(outbuf));
  42184. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42185. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42186. GENERAL_NAME_free(gn);
  42187. gn = NULL;
  42188. /* test for GEN_X400 */
  42189. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42190. if (gn != NULL) {
  42191. gn->type = GEN_X400;
  42192. }
  42193. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42194. XMEMSET(outbuf,0,sizeof(outbuf));
  42195. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42196. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  42197. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42198. if (gn != NULL) {
  42199. gn->type = GEN_IA5;
  42200. }
  42201. GENERAL_NAME_free(gn);
  42202. gn = NULL;
  42203. /* test for GEN_EDIPARTY */
  42204. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42205. if (gn != NULL) {
  42206. gn->type = GEN_EDIPARTY;
  42207. }
  42208. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42209. XMEMSET(outbuf,0,sizeof(outbuf));
  42210. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42211. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  42212. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42213. if (gn != NULL) {
  42214. gn->type = GEN_IA5;
  42215. }
  42216. GENERAL_NAME_free(gn);
  42217. gn = NULL;
  42218. BIO_free(out);
  42219. #endif /* OPENSSL_ALL */
  42220. return EXPECT_RESULT();
  42221. }
  42222. static int test_wolfSSL_sk_DIST_POINT(void)
  42223. {
  42224. EXPECT_DECLS;
  42225. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42226. !defined(NO_RSA)
  42227. X509* x509 = NULL;
  42228. unsigned char buf[4096];
  42229. const unsigned char* bufPt;
  42230. int bytes = 0;
  42231. int i = 0;
  42232. int j = 0;
  42233. XFILE f = XBADFILE;
  42234. DIST_POINT* dp = NULL;
  42235. DIST_POINT_NAME* dpn = NULL;
  42236. GENERAL_NAME* gn = NULL;
  42237. ASN1_IA5STRING* uri = NULL;
  42238. STACK_OF(DIST_POINT)* dps = NULL;
  42239. STACK_OF(GENERAL_NAME)* gns = NULL;
  42240. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  42241. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  42242. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42243. if (f != XBADFILE)
  42244. XFCLOSE(f);
  42245. bufPt = buf;
  42246. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42247. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  42248. NID_crl_distribution_points, NULL, NULL));
  42249. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  42250. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  42251. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  42252. ExpectNotNull(dpn = dp->distpoint);
  42253. /* this should be type 0, fullname */
  42254. ExpectIntEQ(dpn->type, 0);
  42255. ExpectNotNull(gns = dp->distpoint->name.fullname);
  42256. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42257. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  42258. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  42259. ExpectIntEQ(gn->type, GEN_URI);
  42260. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  42261. ExpectNotNull(uri->data);
  42262. ExpectIntGT(uri->length, 0);
  42263. }
  42264. }
  42265. X509_free(x509);
  42266. CRL_DIST_POINTS_free(dps);
  42267. #endif
  42268. return EXPECT_RESULT();
  42269. }
  42270. static int test_wolfSSL_verify_mode(void)
  42271. {
  42272. EXPECT_DECLS;
  42273. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42274. WOLFSSL* ssl = NULL;
  42275. WOLFSSL_CTX* ctx = NULL;
  42276. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42277. ExpectNotNull(ssl = SSL_new(ctx));
  42278. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42279. SSL_free(ssl);
  42280. ssl = NULL;
  42281. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42282. ExpectNotNull(ssl = SSL_new(ctx));
  42283. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42284. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42285. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42286. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  42287. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42288. SSL_free(ssl);
  42289. ssl = NULL;
  42290. wolfSSL_CTX_set_verify(ctx,
  42291. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42292. ExpectNotNull(ssl = SSL_new(ctx));
  42293. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42294. ExpectIntEQ(SSL_get_verify_mode(ssl),
  42295. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42296. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  42297. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42298. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42299. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42300. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42301. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42302. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42303. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42304. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  42305. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  42306. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42307. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  42308. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  42309. #endif
  42310. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42311. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42312. SSL_free(ssl);
  42313. SSL_CTX_free(ctx);
  42314. #endif
  42315. return EXPECT_RESULT();
  42316. }
  42317. static int test_wolfSSL_verify_depth(void)
  42318. {
  42319. EXPECT_DECLS;
  42320. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42321. WOLFSSL* ssl = NULL;
  42322. WOLFSSL_CTX* ctx = NULL;
  42323. long depth;
  42324. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42325. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  42326. ExpectNotNull(ssl = SSL_new(ctx));
  42327. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  42328. SSL_free(ssl);
  42329. ssl = NULL;
  42330. SSL_CTX_set_verify_depth(ctx, -1);
  42331. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  42332. SSL_CTX_set_verify_depth(ctx, 2);
  42333. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  42334. ExpectNotNull(ssl = SSL_new(ctx));
  42335. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  42336. SSL_free(ssl);
  42337. SSL_CTX_free(ctx);
  42338. #endif
  42339. return EXPECT_RESULT();
  42340. }
  42341. static int test_wolfSSL_verify_result(void)
  42342. {
  42343. EXPECT_DECLS;
  42344. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  42345. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  42346. WOLFSSL* ssl = NULL;
  42347. WOLFSSL_CTX* ctx = NULL;
  42348. long result = 0xDEADBEEF;
  42349. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  42350. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42351. ExpectNotNull(ssl = SSL_new(ctx));
  42352. wolfSSL_set_verify_result(ssl, result);
  42353. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  42354. SSL_free(ssl);
  42355. SSL_CTX_free(ctx);
  42356. #endif
  42357. return EXPECT_RESULT();
  42358. }
  42359. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42360. static void sslMsgCb(int w, int version, int type, const void* buf,
  42361. size_t sz, SSL* ssl, void* arg)
  42362. {
  42363. int i;
  42364. unsigned char* pt = (unsigned char*)buf;
  42365. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  42366. (w)?"Writing":"Reading", (int)sz, version, type);
  42367. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  42368. fprintf(stderr, "\n");
  42369. (void)ssl;
  42370. (void)arg;
  42371. }
  42372. #endif /* OPENSSL_EXTRA */
  42373. static int test_wolfSSL_msg_callback(void)
  42374. {
  42375. EXPECT_DECLS;
  42376. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42377. WOLFSSL* ssl = NULL;
  42378. WOLFSSL_CTX* ctx = NULL;
  42379. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42380. ExpectNotNull(ssl = SSL_new(ctx));
  42381. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  42382. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  42383. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  42384. SSL_free(ssl);
  42385. SSL_CTX_free(ctx);
  42386. #endif
  42387. return EXPECT_RESULT();
  42388. }
  42389. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  42390. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42391. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42392. static void binary_dump(void *ptr, int size)
  42393. {
  42394. #ifdef WOLFSSL_EVP_PRINT
  42395. int i = 0;
  42396. unsigned char *p = (unsigned char *) ptr;
  42397. fprintf(stderr, "{");
  42398. while ((p != NULL) && (i < size)) {
  42399. if ((i % 8) == 0) {
  42400. fprintf(stderr, "\n");
  42401. fprintf(stderr, " ");
  42402. }
  42403. fprintf(stderr, "0x%02x, ", p[i]);
  42404. i++;
  42405. }
  42406. fprintf(stderr, "\n};\n");
  42407. #else
  42408. (void) ptr;
  42409. (void) size;
  42410. #endif
  42411. }
  42412. static int last_val = 0x0f;
  42413. static int check_result(unsigned char *data, int len)
  42414. {
  42415. int i;
  42416. for ( ; len; ) {
  42417. last_val = (last_val + 1) % 16;
  42418. for (i = 0; i < 16; len--, i++, data++)
  42419. if (*data != last_val) {
  42420. return -1;
  42421. }
  42422. }
  42423. return 0;
  42424. }
  42425. static int r_offset;
  42426. static int w_offset;
  42427. static void init_offset(void)
  42428. {
  42429. r_offset = 0;
  42430. w_offset = 0;
  42431. }
  42432. static void get_record(unsigned char *data, unsigned char *buf, int len)
  42433. {
  42434. XMEMCPY(buf, data+r_offset, len);
  42435. r_offset += len;
  42436. }
  42437. static void set_record(unsigned char *data, unsigned char *buf, int len)
  42438. {
  42439. XMEMCPY(data+w_offset, buf, len);
  42440. w_offset += len;
  42441. }
  42442. static void set_plain(unsigned char *plain, int rec)
  42443. {
  42444. int i, j;
  42445. unsigned char *p = plain;
  42446. #define BLOCKSZ 16
  42447. for (i=0; i<(rec/BLOCKSZ); i++) {
  42448. for (j=0; j<BLOCKSZ; j++)
  42449. *p++ = (i % 16);
  42450. }
  42451. }
  42452. #endif
  42453. static int test_wolfSSL_EVP_Cipher_extra(void)
  42454. {
  42455. EXPECT_DECLS;
  42456. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42457. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42458. /* aes128-cbc, keylen=16, ivlen=16 */
  42459. byte aes128_cbc_key[] = {
  42460. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42461. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42462. };
  42463. byte aes128_cbc_iv[] = {
  42464. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  42465. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  42466. };
  42467. /* teset data size table */
  42468. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  42469. int test_drive2[] = {8, 3, 8, 512, 0};
  42470. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  42471. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  42472. int test_drive_len[100];
  42473. int ret = 0;
  42474. EVP_CIPHER_CTX *evp = NULL;
  42475. int ilen = 0;
  42476. int klen = 0;
  42477. int i, j;
  42478. const EVP_CIPHER *type;
  42479. byte *iv;
  42480. byte *key;
  42481. int ivlen;
  42482. int keylen;
  42483. #define RECORDS 16
  42484. #define BUFFSZ 512
  42485. byte plain [BUFFSZ * RECORDS];
  42486. byte cipher[BUFFSZ * RECORDS];
  42487. byte inb[BUFFSZ];
  42488. byte outb[BUFFSZ+16];
  42489. int outl = 0;
  42490. int inl;
  42491. iv = aes128_cbc_iv;
  42492. ivlen = sizeof(aes128_cbc_iv);
  42493. key = aes128_cbc_key;
  42494. keylen = sizeof(aes128_cbc_key);
  42495. type = EVP_aes_128_cbc();
  42496. set_plain(plain, BUFFSZ * RECORDS);
  42497. SSL_library_init();
  42498. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42499. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42500. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42501. klen = EVP_CIPHER_CTX_key_length(evp);
  42502. if (klen > 0 && keylen != klen) {
  42503. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42504. }
  42505. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42506. if (ilen > 0 && ivlen != ilen) {
  42507. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42508. }
  42509. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42510. for (j = 0; j<RECORDS; j++)
  42511. {
  42512. inl = BUFFSZ;
  42513. get_record(plain, inb, inl);
  42514. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  42515. set_record(cipher, outb, outl);
  42516. }
  42517. for (i = 0; test_drive[i]; i++) {
  42518. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42519. init_offset();
  42520. test_drive_len[i] = 0;
  42521. for (j = 0; test_drive[i][j]; j++)
  42522. {
  42523. inl = test_drive[i][j];
  42524. test_drive_len[i] += inl;
  42525. get_record(plain, inb, inl);
  42526. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  42527. 0);
  42528. /* output to cipher buffer, so that following Dec test can detect
  42529. if any error */
  42530. set_record(cipher, outb, outl);
  42531. }
  42532. EVP_CipherFinal(evp, outb, &outl);
  42533. if (outl > 0)
  42534. set_record(cipher, outb, outl);
  42535. }
  42536. for (i = 0; test_drive[i]; i++) {
  42537. last_val = 0x0f;
  42538. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  42539. init_offset();
  42540. for (j = 0; test_drive[i][j]; j++) {
  42541. inl = test_drive[i][j];
  42542. get_record(cipher, inb, inl);
  42543. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  42544. 0);
  42545. binary_dump(outb, outl);
  42546. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  42547. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  42548. }
  42549. ret = EVP_CipherFinal(evp, outb, &outl);
  42550. binary_dump(outb, outl);
  42551. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  42552. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  42553. ExpectTrue(ret);
  42554. }
  42555. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  42556. EVP_CIPHER_CTX_free(evp);
  42557. evp = NULL;
  42558. /* Do an extra test to verify correct behavior with empty input. */
  42559. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42560. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42561. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42562. klen = EVP_CIPHER_CTX_key_length(evp);
  42563. if (klen > 0 && keylen != klen) {
  42564. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42565. }
  42566. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42567. if (ilen > 0 && ivlen != ilen) {
  42568. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42569. }
  42570. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42571. /* outl should be set to 0 after passing NULL, 0 for input args. */
  42572. outl = -1;
  42573. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  42574. ExpectIntEQ(outl, 0);
  42575. EVP_CIPHER_CTX_free(evp);
  42576. #endif /* test_EVP_Cipher */
  42577. return EXPECT_RESULT();
  42578. }
  42579. static int test_wolfSSL_PEM_read_DHparams(void)
  42580. {
  42581. EXPECT_DECLS;
  42582. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  42583. !defined(NO_FILESYSTEM)
  42584. DH* dh = NULL;
  42585. XFILE fp = XBADFILE;
  42586. unsigned char derOut[300];
  42587. unsigned char* derOutBuf = derOut;
  42588. int derOutSz = 0;
  42589. unsigned char derExpected[300];
  42590. int derExpectedSz = 0;
  42591. XMEMSET(derOut, 0, sizeof(derOut));
  42592. XMEMSET(derExpected, 0, sizeof(derExpected));
  42593. /* open DH param file, read into DH struct */
  42594. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  42595. /* bad args */
  42596. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  42597. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  42598. /* good args */
  42599. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42600. if (fp != XBADFILE) {
  42601. XFCLOSE(fp);
  42602. fp = XBADFILE;
  42603. }
  42604. /* read in certs/dh2048.der for comparison against exported params */
  42605. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  42606. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  42607. fp), 0);
  42608. if (fp != XBADFILE) {
  42609. XFCLOSE(fp);
  42610. fp = XBADFILE;
  42611. }
  42612. /* export DH back to DER and compare */
  42613. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  42614. ExpectIntEQ(derOutSz, derExpectedSz);
  42615. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  42616. DH_free(dh);
  42617. dh = NULL;
  42618. /* Test parsing with X9.42 header */
  42619. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  42620. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42621. if (fp != XBADFILE)
  42622. XFCLOSE(fp);
  42623. DH_free(dh);
  42624. dh = NULL;
  42625. #endif
  42626. return EXPECT_RESULT();
  42627. }
  42628. static int test_wolfSSL_X509_get_serialNumber(void)
  42629. {
  42630. EXPECT_DECLS;
  42631. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42632. ASN1_INTEGER* a = NULL;
  42633. BIGNUM* bn = NULL;
  42634. X509* x509 = NULL;
  42635. char *serialHex = NULL;
  42636. byte serial[3];
  42637. int serialSz;
  42638. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42639. SSL_FILETYPE_PEM));
  42640. ExpectNotNull(a = X509_get_serialNumber(x509));
  42641. /* check on value of ASN1 Integer */
  42642. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42643. a = NULL;
  42644. /* test setting serial number and then retrieving it */
  42645. ExpectNotNull(a = ASN1_INTEGER_new());
  42646. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  42647. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42648. serialSz = sizeof(serial);
  42649. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42650. WOLFSSL_SUCCESS);
  42651. ExpectIntEQ(serialSz, 1);
  42652. ExpectIntEQ(serial[0], 3);
  42653. ASN1_INTEGER_free(a);
  42654. a = NULL;
  42655. /* test setting serial number with 0's in it */
  42656. serial[0] = 0x01;
  42657. serial[1] = 0x00;
  42658. serial[2] = 0x02;
  42659. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42660. if (a != NULL) {
  42661. a->data[0] = ASN_INTEGER;
  42662. a->data[1] = sizeof(serial);
  42663. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42664. a->length = sizeof(serial) + 2;
  42665. }
  42666. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42667. XMEMSET(serial, 0, sizeof(serial));
  42668. serialSz = sizeof(serial);
  42669. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42670. WOLFSSL_SUCCESS);
  42671. ExpectIntEQ(serialSz, 3);
  42672. ExpectIntEQ(serial[0], 0x01);
  42673. ExpectIntEQ(serial[1], 0x00);
  42674. ExpectIntEQ(serial[2], 0x02);
  42675. ASN1_INTEGER_free(a);
  42676. a = NULL;
  42677. X509_free(x509); /* free's a */
  42678. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42679. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42680. ExpectStrEQ(serialHex, "01");
  42681. #else
  42682. ExpectStrEQ(serialHex, "1");
  42683. #endif
  42684. OPENSSL_free(serialHex);
  42685. ExpectIntEQ(BN_get_word(bn), 1);
  42686. BN_free(bn);
  42687. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42688. ExpectNotNull(a = ASN1_INTEGER_new());
  42689. if (a != NULL) {
  42690. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42691. DYNAMIC_TYPE_OPENSSL));
  42692. a->isDynamic = 1;
  42693. ASN1_INTEGER_free(a);
  42694. }
  42695. #endif
  42696. return EXPECT_RESULT();
  42697. }
  42698. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42699. {
  42700. EXPECT_DECLS;
  42701. #if defined(OPENSSL_EXTRA)
  42702. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  42703. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  42704. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  42705. #endif
  42706. return EXPECT_RESULT();
  42707. }
  42708. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42709. {
  42710. EXPECT_DECLS;
  42711. #if defined(OPENSSL_EXTRA)
  42712. #define MAX_HEXSTR_BUFSZ 9
  42713. #define NUM_CASES 5
  42714. struct Output {
  42715. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42716. long ret;
  42717. };
  42718. int i;
  42719. int j;
  42720. const char* inputs[NUM_CASES] = {
  42721. "aabcd1357e",
  42722. "01:12:23:34:a5:b6:c7:d8:e9",
  42723. ":01:02",
  42724. "012",
  42725. ":ab:ac:d"
  42726. };
  42727. struct Output expectedOutputs[NUM_CASES] = {
  42728. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42729. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42730. {{0x01, 0x02}, 2},
  42731. {{0x00}, 0},
  42732. {{0x00}, 0}
  42733. };
  42734. long len = 0;
  42735. unsigned char* returnedBuf = NULL;
  42736. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  42737. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42738. if (returnedBuf == NULL) {
  42739. ExpectIntEQ(expectedOutputs[i].ret, 0);
  42740. continue;
  42741. }
  42742. ExpectIntEQ(expectedOutputs[i].ret, len);
  42743. for (j = 0; j < len; ++j) {
  42744. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42745. }
  42746. OPENSSL_free(returnedBuf);
  42747. returnedBuf = NULL;
  42748. }
  42749. #endif
  42750. return EXPECT_RESULT();
  42751. }
  42752. static int test_wolfSSL_X509_CA_num(void)
  42753. {
  42754. EXPECT_DECLS;
  42755. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42756. defined(HAVE_ECC) && !defined(NO_RSA)
  42757. WOLFSSL_X509_STORE *store = NULL;
  42758. WOLFSSL_X509 *x509_1 = NULL;
  42759. WOLFSSL_X509 *x509_2 = NULL;
  42760. int ca_num = 0;
  42761. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  42762. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42763. WOLFSSL_FILETYPE_PEM));
  42764. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  42765. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  42766. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  42767. WOLFSSL_FILETYPE_PEM));
  42768. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  42769. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  42770. wolfSSL_X509_free(x509_1);
  42771. wolfSSL_X509_free(x509_2);
  42772. wolfSSL_X509_STORE_free(store);
  42773. #endif
  42774. return EXPECT_RESULT();
  42775. }
  42776. static int test_wolfSSL_X509_check_ca(void)
  42777. {
  42778. EXPECT_DECLS;
  42779. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42780. WOLFSSL_X509 *x509 = NULL;
  42781. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42782. WOLFSSL_FILETYPE_PEM));
  42783. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42784. wolfSSL_X509_free(x509);
  42785. #endif
  42786. return EXPECT_RESULT();
  42787. }
  42788. static int test_wolfSSL_X509_check_ip_asc(void)
  42789. {
  42790. EXPECT_DECLS;
  42791. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42792. WOLFSSL_X509 *x509 = NULL;
  42793. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  42794. WOLFSSL_FILETYPE_PEM));
  42795. #if 0
  42796. /* TODO: add cert gen for testing positive case */
  42797. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42798. #endif
  42799. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42800. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42801. wolfSSL_X509_free(x509);
  42802. #endif
  42803. return EXPECT_RESULT();
  42804. }
  42805. static int test_wolfSSL_make_cert(void)
  42806. {
  42807. EXPECT_DECLS;
  42808. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42809. defined(WOLFSSL_CERT_EXT)
  42810. int ret = 0;
  42811. Cert cert;
  42812. CertName name;
  42813. RsaKey key;
  42814. WC_RNG rng;
  42815. byte der[FOURK_BUF];
  42816. word32 idx = 0;
  42817. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42818. #ifdef OPENSSL_EXTRA
  42819. const unsigned char* pt = NULL;
  42820. int certSz = 0;
  42821. X509* x509 = NULL;
  42822. X509_NAME* x509name = NULL;
  42823. X509_NAME_ENTRY* entry = NULL;
  42824. ASN1_STRING* entryValue = NULL;
  42825. #endif
  42826. XMEMSET(&name, 0, sizeof(CertName));
  42827. /* set up cert name */
  42828. XMEMCPY(name.country, "US", sizeof("US"));
  42829. name.countryEnc = CTC_PRINTABLE;
  42830. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42831. name.stateEnc = CTC_UTF8;
  42832. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42833. name.localityEnc = CTC_UTF8;
  42834. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42835. name.surEnc = CTC_UTF8;
  42836. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42837. name.orgEnc = CTC_UTF8;
  42838. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42839. name.unitEnc = CTC_UTF8;
  42840. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42841. name.commonNameEnc = CTC_UTF8;
  42842. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42843. name.serialDevEnc = CTC_PRINTABLE;
  42844. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42845. name.userIdEnc = CTC_PRINTABLE;
  42846. #ifdef WOLFSSL_MULTI_ATTRIB
  42847. #if CTC_MAX_ATTRIB > 2
  42848. {
  42849. NameAttrib* n;
  42850. n = &name.name[0];
  42851. n->id = ASN_DOMAIN_COMPONENT;
  42852. n->type = CTC_UTF8;
  42853. n->sz = sizeof("com");
  42854. XMEMCPY(n->value, "com", sizeof("com"));
  42855. n = &name.name[1];
  42856. n->id = ASN_DOMAIN_COMPONENT;
  42857. n->type = CTC_UTF8;
  42858. n->sz = sizeof("wolfssl");
  42859. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42860. }
  42861. #endif
  42862. #endif /* WOLFSSL_MULTI_ATTRIB */
  42863. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42864. #ifndef HAVE_FIPS
  42865. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42866. #else
  42867. ExpectIntEQ(wc_InitRng(&rng), 0);
  42868. #endif
  42869. /* load test RSA key */
  42870. idx = 0;
  42871. #if defined(USE_CERT_BUFFERS_1024)
  42872. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42873. sizeof_server_key_der_1024), 0);
  42874. #elif defined(USE_CERT_BUFFERS_2048)
  42875. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42876. sizeof_server_key_der_2048), 0);
  42877. #else
  42878. /* error case, no RSA key loaded, happens later */
  42879. (void)idx;
  42880. #endif
  42881. XMEMSET(&cert, 0 , sizeof(Cert));
  42882. ExpectIntEQ(wc_InitCert(&cert), 0);
  42883. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42884. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42885. cert.serialSz = (int)sizeof(mySerial);
  42886. cert.isCA = 1;
  42887. #ifndef NO_SHA256
  42888. cert.sigType = CTC_SHA256wRSA;
  42889. #else
  42890. cert.sigType = CTC_SHAwRSA;
  42891. #endif
  42892. /* add SKID from the Public Key */
  42893. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42894. /* add AKID from the Public Key */
  42895. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42896. ret = 0;
  42897. do {
  42898. #if defined(WOLFSSL_ASYNC_CRYPT)
  42899. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42900. #endif
  42901. if (ret >= 0) {
  42902. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42903. }
  42904. } while (ret == WC_PENDING_E);
  42905. ExpectIntGT(ret, 0);
  42906. #ifdef OPENSSL_EXTRA
  42907. /* der holds a certificate with DC's now check X509 parsing of it */
  42908. certSz = ret;
  42909. pt = der;
  42910. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  42911. ExpectNotNull(x509name = X509_get_subject_name(x509));
  42912. #ifdef WOLFSSL_MULTI_ATTRIB
  42913. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42914. -1)), 5);
  42915. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42916. idx)), 6);
  42917. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42918. idx)), -1);
  42919. #endif /* WOLFSSL_MULTI_ATTRIB */
  42920. /* compare DN at index 0 */
  42921. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  42922. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42923. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  42924. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  42925. #ifndef WOLFSSL_MULTI_ATTRIB
  42926. /* compare Serial Number */
  42927. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  42928. -1)), 7);
  42929. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42930. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42931. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  42932. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  42933. #endif
  42934. #ifdef WOLFSSL_MULTI_ATTRIB
  42935. /* get first and second DC and compare result */
  42936. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42937. -1)), 5);
  42938. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42939. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42940. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  42941. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42942. idx)), 6);
  42943. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42944. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42945. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  42946. #endif /* WOLFSSL_MULTI_ATTRIB */
  42947. /* try invalid index locations for regression test and sanity check */
  42948. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  42949. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  42950. X509_free(x509);
  42951. #endif /* OPENSSL_EXTRA */
  42952. wc_FreeRsaKey(&key);
  42953. wc_FreeRng(&rng);
  42954. #endif
  42955. return EXPECT_RESULT();
  42956. }
  42957. static int test_wolfSSL_X509_get_version(void)
  42958. {
  42959. EXPECT_DECLS;
  42960. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42961. WOLFSSL_X509 *x509 = NULL;
  42962. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42963. WOLFSSL_FILETYPE_PEM));
  42964. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  42965. wolfSSL_X509_free(x509);
  42966. #endif
  42967. return EXPECT_RESULT();
  42968. }
  42969. #if defined(OPENSSL_ALL)
  42970. static int test_wolfSSL_sk_CIPHER_description(void)
  42971. {
  42972. EXPECT_DECLS;
  42973. #if !defined(NO_RSA)
  42974. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42975. int i;
  42976. int numCiphers = 0;
  42977. const SSL_METHOD *method = NULL;
  42978. const SSL_CIPHER *cipher = NULL;
  42979. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42980. SSL_CTX *ctx = NULL;
  42981. SSL *ssl = NULL;
  42982. char buf[256];
  42983. char test_str[9] = "0000000";
  42984. const char badStr[] = "unknown";
  42985. const char certPath[] = "./certs/client-cert.pem";
  42986. XMEMSET(buf, 0, sizeof(buf));
  42987. ExpectNotNull(method = TLSv1_2_client_method());
  42988. ExpectNotNull(ctx = SSL_CTX_new(method));
  42989. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42990. SSL_CTX_set_verify_depth(ctx, 4);
  42991. SSL_CTX_set_options(ctx, flags);
  42992. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42993. WOLFSSL_SUCCESS);
  42994. ExpectNotNull(ssl = SSL_new(ctx));
  42995. /* SSL_get_ciphers returns a stack of all configured ciphers
  42996. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  42997. */
  42998. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42999. /* loop through the amount of supportedCiphers */
  43000. numCiphers = sk_num(supportedCiphers);
  43001. for (i = 0; i < numCiphers; ++i) {
  43002. int j;
  43003. /* sk_value increments "sk->data.cipher->cipherOffset".
  43004. * wolfSSL_sk_CIPHER_description sets the description for
  43005. * the cipher based on the provided offset.
  43006. */
  43007. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43008. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43009. }
  43010. /* Search cipher description string for "unknown" descriptor */
  43011. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43012. int k = 0;
  43013. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43014. test_str[k] = badStr[k];
  43015. j++;
  43016. k++;
  43017. }
  43018. }
  43019. /* Fail if test_str == badStr == "unknown" */
  43020. ExpectStrNE(test_str,badStr);
  43021. }
  43022. SSL_free(ssl);
  43023. SSL_CTX_free(ctx);
  43024. #endif
  43025. return EXPECT_RESULT();
  43026. }
  43027. static int test_wolfSSL_get_ciphers_compat(void)
  43028. {
  43029. EXPECT_DECLS;
  43030. #if !defined(NO_RSA)
  43031. const SSL_METHOD *method = NULL;
  43032. const char certPath[] = "./certs/client-cert.pem";
  43033. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43034. SSL_CTX *ctx = NULL;
  43035. WOLFSSL *ssl = NULL;
  43036. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43037. ExpectNotNull(method = SSLv23_client_method());
  43038. ExpectNotNull(ctx = SSL_CTX_new(method));
  43039. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43040. SSL_CTX_set_verify_depth(ctx, 4);
  43041. SSL_CTX_set_options(ctx, flags);
  43042. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43043. WOLFSSL_SUCCESS);
  43044. ExpectNotNull(ssl = SSL_new(ctx));
  43045. /* Test Bad NULL input */
  43046. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43047. /* Test for Good input */
  43048. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43049. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43050. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43051. SSL_free(ssl);
  43052. SSL_CTX_free(ctx);
  43053. #endif
  43054. return EXPECT_RESULT();
  43055. }
  43056. static int test_wolfSSL_X509_PUBKEY_get(void)
  43057. {
  43058. EXPECT_DECLS;
  43059. WOLFSSL_X509_PUBKEY pubkey;
  43060. WOLFSSL_X509_PUBKEY* key;
  43061. WOLFSSL_EVP_PKEY evpkey ;
  43062. WOLFSSL_EVP_PKEY* evpPkey;
  43063. WOLFSSL_EVP_PKEY* retEvpPkey;
  43064. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43065. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43066. key = &pubkey;
  43067. evpPkey = &evpkey;
  43068. evpPkey->type = WOLFSSL_SUCCESS;
  43069. key->pkey = evpPkey;
  43070. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43071. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43072. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43073. key->pkey = NULL;
  43074. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43075. return EXPECT_RESULT();
  43076. }
  43077. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43078. {
  43079. EXPECT_DECLS;
  43080. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43081. DSA *dsa = NULL;
  43082. DSA *setDsa = NULL;
  43083. EVP_PKEY *pkey = NULL;
  43084. EVP_PKEY *set1Pkey = NULL;
  43085. SHA_CTX sha;
  43086. byte signature[DSA_SIG_SIZE];
  43087. byte hash[WC_SHA_DIGEST_SIZE];
  43088. word32 bytes;
  43089. int answer;
  43090. #ifdef USE_CERT_BUFFERS_1024
  43091. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43092. int dsaKeySz = sizeof_dsa_key_der_1024;
  43093. byte tmp[ONEK_BUF];
  43094. XMEMSET(tmp, 0, sizeof(tmp));
  43095. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43096. bytes = dsaKeySz;
  43097. #elif defined(USE_CERT_BUFFERS_2048)
  43098. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43099. int dsaKeySz = sizeof_dsa_key_der_2048;
  43100. byte tmp[TWOK_BUF];
  43101. XMEMSET(tmp, 0, sizeof(tmp));
  43102. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43103. bytes = dsaKeySz;
  43104. #else
  43105. byte tmp[TWOK_BUF];
  43106. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43107. int dsaKeySz;
  43108. XFILE fp = XBADFILE;
  43109. XMEMSET(tmp, 0, sizeof(tmp));
  43110. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43111. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43112. if (fp != XBADFILE)
  43113. XFCLOSE(fp);
  43114. #endif /* END USE_CERT_BUFFERS_1024 */
  43115. /* Create hash to later Sign and Verify */
  43116. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43117. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43118. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43119. /* Initialize pkey with der format dsa key */
  43120. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43121. (long)dsaKeySz));
  43122. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43123. /* Should Fail: NULL argument */
  43124. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43125. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43126. /* Should Pass: Initialized pkey argument */
  43127. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43128. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43129. #ifdef USE_CERT_BUFFERS_1024
  43130. ExpectIntEQ(DSA_bits(dsa), 1024);
  43131. #else
  43132. ExpectIntEQ(DSA_bits(dsa), 2048);
  43133. #endif
  43134. /* Sign */
  43135. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43136. /* Verify. */
  43137. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43138. WOLFSSL_SUCCESS);
  43139. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43140. /* Should Fail: set1Pkey not initialized */
  43141. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43142. /* Initialize set1Pkey */
  43143. set1Pkey = EVP_PKEY_new();
  43144. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43145. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43146. WOLFSSL_SUCCESS);
  43147. /* Should Pass: set dsa into set1Pkey */
  43148. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43149. DSA_free(dsa);
  43150. DSA_free(setDsa);
  43151. EVP_PKEY_free(pkey);
  43152. EVP_PKEY_free(set1Pkey);
  43153. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43154. return EXPECT_RESULT();
  43155. } /* END test_EVP_PKEY_set1_get1_DSA */
  43156. static int test_wolfSSL_DSA_generate_parameters(void)
  43157. {
  43158. EXPECT_DECLS;
  43159. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43160. !defined(HAVE_FIPS)
  43161. DSA *dsa = NULL;
  43162. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43163. NULL));
  43164. DSA_free(dsa);
  43165. #endif
  43166. return EXPECT_RESULT();
  43167. }
  43168. static int test_wolfSSL_DSA_SIG(void)
  43169. {
  43170. EXPECT_DECLS;
  43171. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43172. !defined(HAVE_FIPS)
  43173. DSA *dsa = NULL;
  43174. DSA *dsa2 = NULL;
  43175. DSA_SIG *sig = NULL;
  43176. const BIGNUM *p = NULL;
  43177. const BIGNUM *q = NULL;
  43178. const BIGNUM *g = NULL;
  43179. const BIGNUM *pub = NULL;
  43180. const BIGNUM *priv = NULL;
  43181. BIGNUM *dup_p = NULL;
  43182. BIGNUM *dup_q = NULL;
  43183. BIGNUM *dup_g = NULL;
  43184. BIGNUM *dup_pub = NULL;
  43185. BIGNUM *dup_priv = NULL;
  43186. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43187. ExpectNotNull(dsa = DSA_new());
  43188. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43189. NULL), 1);
  43190. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43191. DSA_get0_pqg(dsa, &p, &q, &g);
  43192. DSA_get0_key(dsa, &pub, &priv);
  43193. ExpectNotNull(dup_p = BN_dup(p));
  43194. ExpectNotNull(dup_q = BN_dup(q));
  43195. ExpectNotNull(dup_g = BN_dup(g));
  43196. ExpectNotNull(dup_pub = BN_dup(pub));
  43197. ExpectNotNull(dup_priv = BN_dup(priv));
  43198. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  43199. ExpectNotNull(dsa2 = DSA_new());
  43200. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  43201. if (EXPECT_FAIL()) {
  43202. BN_free(dup_p);
  43203. BN_free(dup_q);
  43204. BN_free(dup_g);
  43205. }
  43206. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  43207. if (EXPECT_FAIL()) {
  43208. BN_free(dup_pub);
  43209. BN_free(dup_priv);
  43210. }
  43211. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  43212. DSA_free(dsa);
  43213. DSA_free(dsa2);
  43214. DSA_SIG_free(sig);
  43215. #endif
  43216. return EXPECT_RESULT();
  43217. }
  43218. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  43219. {
  43220. EXPECT_DECLS;
  43221. #ifdef HAVE_ECC
  43222. WOLFSSL_EC_KEY* ecKey = NULL;
  43223. WOLFSSL_EC_KEY* ecGet1 = NULL;
  43224. EVP_PKEY* pkey = NULL;
  43225. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43226. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43227. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  43228. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  43229. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  43230. /* Should fail since ecKey is empty */
  43231. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  43232. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43233. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  43234. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  43235. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  43236. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  43237. wolfSSL_EC_KEY_free(ecKey);
  43238. wolfSSL_EC_KEY_free(ecGet1);
  43239. EVP_PKEY_free(pkey);
  43240. #endif /* HAVE_ECC */
  43241. return EXPECT_RESULT();
  43242. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  43243. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  43244. {
  43245. EXPECT_DECLS;
  43246. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43247. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43248. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43249. DH *dh = NULL;
  43250. DH *setDh = NULL;
  43251. EVP_PKEY *pkey = NULL;
  43252. XFILE f = XBADFILE;
  43253. unsigned char buf[4096];
  43254. const unsigned char* pt = buf;
  43255. const char* dh2048 = "./certs/dh2048.der";
  43256. long len = 0;
  43257. int code = -1;
  43258. XMEMSET(buf, 0, sizeof(buf));
  43259. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43260. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43261. if (f != XBADFILE)
  43262. XFCLOSE(f);
  43263. /* Load dh2048.der into DH with internal format */
  43264. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43265. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43266. ExpectIntEQ(code, 0);
  43267. code = -1;
  43268. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43269. /* Set DH into PKEY */
  43270. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43271. /* Get DH from PKEY */
  43272. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  43273. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  43274. ExpectIntEQ(code, 0);
  43275. EVP_PKEY_free(pkey);
  43276. DH_free(setDh);
  43277. setDh = NULL;
  43278. DH_free(dh);
  43279. dh = NULL;
  43280. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  43281. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43282. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43283. return EXPECT_RESULT();
  43284. } /* END test_EVP_PKEY_set1_get1_DH */
  43285. static int test_wolfSSL_CTX_ctrl(void)
  43286. {
  43287. EXPECT_DECLS;
  43288. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43289. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43290. char caFile[] = "./certs/client-ca.pem";
  43291. char clientFile[] = "./certs/client-cert.pem";
  43292. SSL_CTX* ctx = NULL;
  43293. X509* x509 = NULL;
  43294. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43295. byte buf[6000];
  43296. char file[] = "./certs/dsaparams.pem";
  43297. XFILE f = XBADFILE;
  43298. int bytes = 0;
  43299. BIO* bio = NULL;
  43300. DSA* dsa = NULL;
  43301. DH* dh = NULL;
  43302. #endif
  43303. #ifdef HAVE_ECC
  43304. WOLFSSL_EC_KEY* ecKey = NULL;
  43305. #endif
  43306. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43307. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  43308. WOLFSSL_FILETYPE_PEM));
  43309. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43310. if (EXPECT_FAIL()) {
  43311. wolfSSL_X509_free(x509);
  43312. }
  43313. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  43314. WOLFSSL_FILETYPE_PEM));
  43315. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43316. /* Initialize DH */
  43317. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  43318. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  43319. if (f != XBADFILE)
  43320. XFCLOSE(f);
  43321. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  43322. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  43323. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  43324. #endif
  43325. #ifdef HAVE_ECC
  43326. /* Initialize WOLFSSL_EC_KEY */
  43327. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43328. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43329. #endif
  43330. /* additional test of getting EVP_PKEY key size from X509
  43331. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43332. * allowed with user RSA */
  43333. {
  43334. EVP_PKEY* pkey = NULL;
  43335. #if defined(HAVE_ECC)
  43336. X509* ecX509 = NULL;
  43337. #endif /* HAVE_ECC */
  43338. ExpectNotNull(pkey = X509_get_pubkey(x509));
  43339. /* current RSA key is 2048 bit (256 bytes) */
  43340. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  43341. EVP_PKEY_free(pkey);
  43342. pkey = NULL;
  43343. #if defined(HAVE_ECC)
  43344. #if defined(USE_CERT_BUFFERS_256)
  43345. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43346. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43347. SSL_FILETYPE_ASN1));
  43348. #else
  43349. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43350. cliEccCertFile, SSL_FILETYPE_PEM));
  43351. #endif
  43352. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  43353. /* current ECC key is 256 bit (32 bytes) */
  43354. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  43355. X509_free(ecX509);
  43356. EVP_PKEY_free(pkey);
  43357. #endif /* HAVE_ECC */
  43358. }
  43359. /* Tests should fail with passed in NULL pointer */
  43360. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  43361. SSL_FAILURE);
  43362. #if !defined(NO_DH) && !defined(NO_DSA)
  43363. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  43364. SSL_FAILURE);
  43365. #endif
  43366. #ifdef HAVE_ECC
  43367. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  43368. SSL_FAILURE);
  43369. #endif
  43370. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  43371. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  43372. */
  43373. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  43374. SSL_SUCCESS);
  43375. if (EXPECT_FAIL()) {
  43376. wolfSSL_X509_free(x509);
  43377. }
  43378. /* Test with SSL_CTRL_OPTIONS
  43379. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  43380. */
  43381. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  43382. NULL) == SSL_OP_NO_TLSv1);
  43383. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  43384. /* Test with SSL_CTRL_SET_TMP_DH
  43385. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  43386. */
  43387. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43388. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  43389. SSL_SUCCESS);
  43390. #endif
  43391. /* Test with SSL_CTRL_SET_TMP_ECDH
  43392. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  43393. */
  43394. #ifdef HAVE_ECC
  43395. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  43396. SSL_SUCCESS);
  43397. #endif
  43398. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43399. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  43400. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43401. #endif
  43402. /* Test for min/max proto */
  43403. #ifndef WOLFSSL_NO_TLS12
  43404. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43405. 0, NULL), SSL_SUCCESS);
  43406. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43407. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43408. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  43409. #endif
  43410. #ifdef WOLFSSL_TLS13
  43411. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43412. 0, NULL), SSL_SUCCESS);
  43413. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43414. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  43415. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  43416. #ifndef WOLFSSL_NO_TLS12
  43417. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43418. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43419. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  43420. #endif
  43421. #endif
  43422. /* Cleanup and Pass */
  43423. #if !defined(NO_DH) && !defined(NO_DSA)
  43424. #ifndef NO_BIO
  43425. BIO_free(bio);
  43426. DSA_free(dsa);
  43427. DH_free(dh);
  43428. dh = NULL;
  43429. #endif
  43430. #endif
  43431. #ifdef HAVE_ECC
  43432. wolfSSL_EC_KEY_free(ecKey);
  43433. #endif
  43434. SSL_CTX_free(ctx);
  43435. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43436. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  43437. return EXPECT_RESULT();
  43438. }
  43439. static int test_wolfSSL_EVP_PKEY_assign(void)
  43440. {
  43441. EXPECT_DECLS;
  43442. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  43443. int type;
  43444. WOLFSSL_EVP_PKEY* pkey = NULL;
  43445. #ifndef NO_RSA
  43446. WOLFSSL_RSA* rsa = NULL;
  43447. #endif
  43448. #ifndef NO_DSA
  43449. WOLFSSL_DSA* dsa = NULL;
  43450. #endif
  43451. #ifdef HAVE_ECC
  43452. WOLFSSL_EC_KEY* ecKey = NULL;
  43453. #endif
  43454. #ifndef NO_RSA
  43455. type = EVP_PKEY_RSA;
  43456. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43457. ExpectNotNull(rsa = wolfSSL_RSA_new());
  43458. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  43459. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43460. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  43461. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  43462. if (EXPECT_FAIL()) {
  43463. wolfSSL_RSA_free(rsa);
  43464. }
  43465. wolfSSL_EVP_PKEY_free(pkey);
  43466. pkey = NULL;
  43467. #endif /* NO_RSA */
  43468. #ifndef NO_DSA
  43469. type = EVP_PKEY_DSA;
  43470. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43471. ExpectNotNull(dsa = wolfSSL_DSA_new());
  43472. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  43473. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43474. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  43475. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  43476. if (EXPECT_FAIL()) {
  43477. wolfSSL_DSA_free(dsa);
  43478. }
  43479. wolfSSL_EVP_PKEY_free(pkey);
  43480. pkey = NULL;
  43481. #endif /* NO_DSA */
  43482. #ifdef HAVE_ECC
  43483. type = EVP_PKEY_EC;
  43484. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43485. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43486. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  43487. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43488. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  43489. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  43490. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43491. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  43492. if (EXPECT_FAIL()) {
  43493. wolfSSL_EC_KEY_free(ecKey);
  43494. }
  43495. wolfSSL_EVP_PKEY_free(pkey);
  43496. pkey = NULL;
  43497. #endif /* HAVE_ECC */
  43498. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  43499. return EXPECT_RESULT();
  43500. }
  43501. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  43502. {
  43503. EXPECT_DECLS;
  43504. #if !defined(NO_DH) && \
  43505. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43506. XFILE f = XBADFILE;
  43507. unsigned char buf[4096];
  43508. const unsigned char* pt = buf;
  43509. const char* params1 = "./certs/dh2048.der";
  43510. long len = 0;
  43511. WOLFSSL_DH* dh = NULL;
  43512. WOLFSSL_EVP_PKEY* pkey = NULL;
  43513. XMEMSET(buf, 0, sizeof(buf));
  43514. /* Load DH parameters DER. */
  43515. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  43516. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43517. if (f != XBADFILE)
  43518. XFCLOSE(f);
  43519. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43520. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  43521. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43522. /* Bad cases */
  43523. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  43524. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  43525. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  43526. /* Good case */
  43527. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  43528. if (EXPECT_FAIL()) {
  43529. wolfSSL_DH_free(dh);
  43530. }
  43531. EVP_PKEY_free(pkey);
  43532. #endif
  43533. return EXPECT_RESULT();
  43534. }
  43535. static int test_wolfSSL_EVP_PKEY_base_id(void)
  43536. {
  43537. EXPECT_DECLS;
  43538. WOLFSSL_EVP_PKEY* pkey = NULL;
  43539. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43540. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  43541. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  43542. EVP_PKEY_free(pkey);
  43543. return EXPECT_RESULT();
  43544. }
  43545. static int test_wolfSSL_EVP_PKEY_id(void)
  43546. {
  43547. EXPECT_DECLS;
  43548. WOLFSSL_EVP_PKEY* pkey = NULL;
  43549. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43550. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  43551. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  43552. EVP_PKEY_free(pkey);
  43553. return EXPECT_RESULT();
  43554. }
  43555. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  43556. {
  43557. EXPECT_DECLS;
  43558. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  43559. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  43560. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  43561. ECC_MIN_KEY_SZ <= 256)
  43562. EVP_PKEY_CTX* ctx = NULL;
  43563. EVP_PKEY* pkey = NULL;
  43564. /* Test error conditions. */
  43565. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  43566. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43567. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  43568. #ifndef NO_RSA
  43569. EVP_PKEY_CTX_free(ctx);
  43570. /* Parameter generation for RSA not supported yet. */
  43571. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  43572. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  43573. #endif
  43574. #ifdef HAVE_ECC
  43575. EVP_PKEY_CTX_free(ctx);
  43576. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43577. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  43578. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  43579. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  43580. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  43581. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  43582. WOLFSSL_SUCCESS);
  43583. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43584. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43585. #endif
  43586. EVP_PKEY_CTX_free(ctx);
  43587. EVP_PKEY_free(pkey);
  43588. #endif
  43589. return EXPECT_RESULT();
  43590. }
  43591. static int test_wolfSSL_EVP_PKEY_keygen(void)
  43592. {
  43593. EXPECT_DECLS;
  43594. WOLFSSL_EVP_PKEY* pkey = NULL;
  43595. EVP_PKEY_CTX* ctx = NULL;
  43596. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43597. WOLFSSL_EVP_PKEY* params = NULL;
  43598. DH* dh = NULL;
  43599. const BIGNUM* pubkey = NULL;
  43600. const BIGNUM* privkey = NULL;
  43601. ASN1_INTEGER* asn1int = NULL;
  43602. unsigned int length = 0;
  43603. byte* derBuffer = NULL;
  43604. #endif
  43605. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43606. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43607. /* Bad cases */
  43608. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  43609. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  43610. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  43611. /* Good case */
  43612. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  43613. EVP_PKEY_CTX_free(ctx);
  43614. ctx = NULL;
  43615. EVP_PKEY_free(pkey);
  43616. pkey = NULL;
  43617. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43618. /* Test DH keygen */
  43619. {
  43620. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43621. ExpectNotNull(dh = DH_get_2048_256());
  43622. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43623. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  43624. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43625. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43626. DH_free(dh);
  43627. dh = NULL;
  43628. EVP_PKEY_CTX_free(ctx);
  43629. EVP_PKEY_free(params);
  43630. /* try exporting generated key to DER, to verify */
  43631. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43632. DH_get0_key(dh, &pubkey, &privkey);
  43633. ExpectNotNull(pubkey);
  43634. ExpectNotNull(privkey);
  43635. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43636. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43637. ASN1_INTEGER_free(asn1int);
  43638. DH_free(dh);
  43639. dh = NULL;
  43640. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43641. EVP_PKEY_free(pkey);
  43642. }
  43643. #endif
  43644. return EXPECT_RESULT();
  43645. }
  43646. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43647. {
  43648. EXPECT_DECLS;
  43649. WOLFSSL_EVP_PKEY* pkey = NULL;
  43650. EVP_PKEY_CTX *ctx = NULL;
  43651. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43652. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43653. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43654. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  43655. EVP_PKEY_CTX_free(ctx);
  43656. EVP_PKEY_free(pkey);
  43657. return EXPECT_RESULT();
  43658. }
  43659. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43660. {
  43661. EXPECT_DECLS;
  43662. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43663. WOLFSSL_EVP_PKEY* pkey = NULL;
  43664. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43665. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43666. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43667. EVP_PKEY_free(pkey);
  43668. #endif
  43669. return EXPECT_RESULT();
  43670. }
  43671. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43672. {
  43673. EXPECT_DECLS;
  43674. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43675. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43676. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43677. !defined(NO_FILESYSTEM)
  43678. WOLFSSL_EVP_PKEY* params = NULL;
  43679. WOLFSSL_EVP_PKEY* copy = NULL;
  43680. DH* dh = NULL;
  43681. BIGNUM* p1;
  43682. BIGNUM* g1;
  43683. BIGNUM* q1;
  43684. BIGNUM* p2;
  43685. BIGNUM* g2;
  43686. BIGNUM* q2;
  43687. /* create DH with DH_get_2048_256 params */
  43688. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43689. ExpectNotNull(dh = DH_get_2048_256());
  43690. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43691. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43692. (const BIGNUM**)&q1,
  43693. (const BIGNUM**)&g1);
  43694. DH_free(dh);
  43695. dh = NULL;
  43696. /* create DH with random generated DH params */
  43697. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  43698. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43699. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43700. DH_free(dh);
  43701. dh = NULL;
  43702. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43703. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  43704. ExpectNotNull(dh->p);
  43705. ExpectNotNull(dh->g);
  43706. ExpectNotNull(dh->q);
  43707. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43708. (const BIGNUM**)&q2,
  43709. (const BIGNUM**)&g2);
  43710. ExpectIntEQ(BN_cmp(p1, p2), 0);
  43711. ExpectIntEQ(BN_cmp(q1, q2), 0);
  43712. ExpectIntEQ(BN_cmp(g1, g2), 0);
  43713. DH_free(dh);
  43714. dh = NULL;
  43715. EVP_PKEY_free(copy);
  43716. EVP_PKEY_free(params);
  43717. #endif
  43718. return EXPECT_RESULT();
  43719. }
  43720. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43721. {
  43722. EXPECT_DECLS;
  43723. WOLFSSL_EVP_PKEY* pkey = NULL;
  43724. EVP_PKEY_CTX* ctx = NULL;
  43725. int bits = 2048;
  43726. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43727. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43728. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43729. WOLFSSL_SUCCESS);
  43730. EVP_PKEY_CTX_free(ctx);
  43731. EVP_PKEY_free(pkey);
  43732. return EXPECT_RESULT();
  43733. }
  43734. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43735. {
  43736. EXPECT_DECLS;
  43737. /* This is large enough to be used for all key sizes */
  43738. byte key[AES_256_KEY_SIZE] = {0};
  43739. byte iv[AES_BLOCK_SIZE] = {0};
  43740. int i;
  43741. int nids[] = {
  43742. #ifdef HAVE_AES_CBC
  43743. NID_aes_128_cbc,
  43744. #endif
  43745. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43746. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43747. #ifdef HAVE_AESGCM
  43748. NID_aes_128_gcm,
  43749. #endif
  43750. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43751. #ifdef WOLFSSL_AES_COUNTER
  43752. NID_aes_128_ctr,
  43753. #endif
  43754. #ifndef NO_DES3
  43755. NID_des_cbc,
  43756. NID_des_ede3_cbc,
  43757. #endif
  43758. };
  43759. int iv_lengths[] = {
  43760. #ifdef HAVE_AES_CBC
  43761. AES_BLOCK_SIZE,
  43762. #endif
  43763. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43764. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43765. #ifdef HAVE_AESGCM
  43766. GCM_NONCE_MID_SZ,
  43767. #endif
  43768. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43769. #ifdef WOLFSSL_AES_COUNTER
  43770. AES_BLOCK_SIZE,
  43771. #endif
  43772. #ifndef NO_DES3
  43773. DES_BLOCK_SIZE,
  43774. DES_BLOCK_SIZE,
  43775. #endif
  43776. };
  43777. int nidsLen = (sizeof(nids)/sizeof(int));
  43778. for (i = 0; i < nidsLen; i++) {
  43779. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43780. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43781. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43782. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43783. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43784. EVP_CIPHER_CTX_free(ctx);
  43785. }
  43786. return EXPECT_RESULT();
  43787. }
  43788. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43789. {
  43790. EXPECT_DECLS;
  43791. byte key[AES_256_KEY_SIZE] = {0};
  43792. byte iv[AES_BLOCK_SIZE] = {0};
  43793. int i;
  43794. int nids[] = {
  43795. #ifdef HAVE_AES_CBC
  43796. NID_aes_128_cbc,
  43797. NID_aes_256_cbc,
  43798. #endif
  43799. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43800. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43801. #ifdef HAVE_AESGCM
  43802. NID_aes_128_gcm,
  43803. NID_aes_256_gcm,
  43804. #endif
  43805. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43806. #ifdef WOLFSSL_AES_COUNTER
  43807. NID_aes_128_ctr,
  43808. NID_aes_256_ctr,
  43809. #endif
  43810. #ifndef NO_DES3
  43811. NID_des_cbc,
  43812. NID_des_ede3_cbc,
  43813. #endif
  43814. };
  43815. int key_lengths[] = {
  43816. #ifdef HAVE_AES_CBC
  43817. AES_128_KEY_SIZE,
  43818. AES_256_KEY_SIZE,
  43819. #endif
  43820. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43821. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43822. #ifdef HAVE_AESGCM
  43823. AES_128_KEY_SIZE,
  43824. AES_256_KEY_SIZE,
  43825. #endif
  43826. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43827. #ifdef WOLFSSL_AES_COUNTER
  43828. AES_128_KEY_SIZE,
  43829. AES_256_KEY_SIZE,
  43830. #endif
  43831. #ifndef NO_DES3
  43832. DES_KEY_SIZE,
  43833. DES3_KEY_SIZE,
  43834. #endif
  43835. };
  43836. int nidsLen = (sizeof(nids)/sizeof(int));
  43837. for (i = 0; i < nidsLen; i++) {
  43838. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43839. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43840. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43841. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43842. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  43843. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  43844. WOLFSSL_SUCCESS);
  43845. EVP_CIPHER_CTX_free(ctx);
  43846. }
  43847. return EXPECT_RESULT();
  43848. }
  43849. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43850. {
  43851. EXPECT_DECLS;
  43852. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43853. int ivLen, keyLen;
  43854. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43855. #ifdef HAVE_AESGCM
  43856. byte key[AES_128_KEY_SIZE] = {0};
  43857. byte iv[AES_BLOCK_SIZE] = {0};
  43858. const EVP_CIPHER *init = EVP_aes_128_gcm();
  43859. #else
  43860. byte key[DES3_KEY_SIZE] = {0};
  43861. byte iv[DES_BLOCK_SIZE] = {0};
  43862. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43863. #endif
  43864. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43865. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43866. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43867. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43868. /* Bad cases */
  43869. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  43870. WOLFSSL_FAILURE);
  43871. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  43872. WOLFSSL_FAILURE);
  43873. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43874. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43875. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  43876. WOLFSSL_FAILURE);
  43877. /* Good case */
  43878. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43879. EVP_CIPHER_CTX_free(ctx);
  43880. #endif
  43881. return EXPECT_RESULT();
  43882. }
  43883. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43884. {
  43885. EXPECT_DECLS;
  43886. WOLFSSL_ENGINE* e = NULL;
  43887. int id = 0;
  43888. EVP_PKEY_CTX *ctx = NULL;
  43889. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43890. EVP_PKEY_CTX_free(ctx);
  43891. return EXPECT_RESULT();
  43892. }
  43893. static int test_wolfSSL_EVP_rc4(void)
  43894. {
  43895. EXPECT_DECLS;
  43896. #if !defined(NO_RC4)
  43897. ExpectNotNull(wolfSSL_EVP_rc4());
  43898. #endif
  43899. return EXPECT_RESULT();
  43900. }
  43901. static int test_wolfSSL_EVP_enc_null(void)
  43902. {
  43903. EXPECT_DECLS;
  43904. ExpectNotNull(wolfSSL_EVP_enc_null());
  43905. return EXPECT_RESULT();
  43906. }
  43907. static int test_wolfSSL_EVP_rc2_cbc(void)
  43908. {
  43909. EXPECT_DECLS;
  43910. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  43911. ExpectNull(wolfSSL_EVP_rc2_cbc());
  43912. #endif
  43913. return EXPECT_RESULT();
  43914. }
  43915. static int test_wolfSSL_EVP_mdc2(void)
  43916. {
  43917. EXPECT_DECLS;
  43918. #if !defined(NO_WOLFSSL_STUB)
  43919. ExpectNull(wolfSSL_EVP_mdc2());
  43920. #endif
  43921. return EXPECT_RESULT();
  43922. }
  43923. static int test_wolfSSL_EVP_md4(void)
  43924. {
  43925. EXPECT_DECLS;
  43926. #if !defined(NO_MD4)
  43927. ExpectNotNull(wolfSSL_EVP_md4());
  43928. #endif
  43929. return EXPECT_RESULT();
  43930. }
  43931. static int test_wolfSSL_EVP_aes_256_gcm(void)
  43932. {
  43933. EXPECT_DECLS;
  43934. #ifdef HAVE_AESGCM
  43935. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  43936. #endif
  43937. return EXPECT_RESULT();
  43938. }
  43939. static int test_wolfSSL_EVP_aes_192_gcm(void)
  43940. {
  43941. EXPECT_DECLS;
  43942. #ifdef HAVE_AESGCM
  43943. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  43944. #endif
  43945. return EXPECT_RESULT();
  43946. }
  43947. static int test_wolfSSL_EVP_aes_256_ccm(void)
  43948. {
  43949. EXPECT_DECLS;
  43950. #ifdef HAVE_AESCCM
  43951. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  43952. #endif
  43953. return EXPECT_RESULT();
  43954. }
  43955. static int test_wolfSSL_EVP_aes_192_ccm(void)
  43956. {
  43957. EXPECT_DECLS;
  43958. #ifdef HAVE_AESCCM
  43959. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  43960. #endif
  43961. return EXPECT_RESULT();
  43962. }
  43963. static int test_wolfSSL_EVP_aes_128_ccm(void)
  43964. {
  43965. EXPECT_DECLS;
  43966. #ifdef HAVE_AESCCM
  43967. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  43968. #endif
  43969. return EXPECT_RESULT();
  43970. }
  43971. static int test_wolfSSL_EVP_ripemd160(void)
  43972. {
  43973. EXPECT_DECLS;
  43974. #if !defined(NO_WOLFSSL_STUB)
  43975. ExpectNull(wolfSSL_EVP_ripemd160());
  43976. #endif
  43977. return EXPECT_RESULT();
  43978. }
  43979. static int test_wolfSSL_EVP_get_digestbynid(void)
  43980. {
  43981. EXPECT_DECLS;
  43982. #ifndef NO_MD5
  43983. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  43984. #endif
  43985. #ifndef NO_SHA
  43986. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  43987. #endif
  43988. #ifndef NO_SHA256
  43989. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  43990. #endif
  43991. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  43992. return EXPECT_RESULT();
  43993. }
  43994. static int test_wolfSSL_EVP_MD_nid(void)
  43995. {
  43996. EXPECT_DECLS;
  43997. #ifndef NO_MD5
  43998. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  43999. #endif
  44000. #ifndef NO_SHA
  44001. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44002. #endif
  44003. #ifndef NO_SHA256
  44004. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44005. #endif
  44006. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44007. return EXPECT_RESULT();
  44008. }
  44009. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44010. {
  44011. EXPECT_DECLS;
  44012. #if defined(HAVE_ECC)
  44013. WOLFSSL_EVP_PKEY* pkey = NULL;
  44014. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44015. ExpectNotNull(pkey = EVP_PKEY_new());
  44016. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44017. EVP_PKEY_free(pkey);
  44018. #endif
  44019. return EXPECT_RESULT();
  44020. }
  44021. static int test_wolfSSL_EVP_X_STATE(void)
  44022. {
  44023. EXPECT_DECLS;
  44024. #if !defined(NO_DES3) && !defined(NO_RC4)
  44025. byte key[DES3_KEY_SIZE] = {0};
  44026. byte iv[DES_IV_SIZE] = {0};
  44027. EVP_CIPHER_CTX *ctx = NULL;
  44028. const EVP_CIPHER *init = NULL;
  44029. /* Bad test cases */
  44030. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44031. ExpectNotNull(init = EVP_des_ede3_cbc());
  44032. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44033. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44034. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44035. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44036. EVP_CIPHER_CTX_free(ctx);
  44037. ctx = NULL;
  44038. /* Good test case */
  44039. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44040. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44041. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44042. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44043. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44044. EVP_CIPHER_CTX_free(ctx);
  44045. #endif
  44046. return EXPECT_RESULT();
  44047. }
  44048. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44049. {
  44050. EXPECT_DECLS;
  44051. #if !defined(NO_DES3) && !defined(NO_RC4)
  44052. byte key[DES3_KEY_SIZE] = {0};
  44053. byte iv[DES_IV_SIZE] = {0};
  44054. EVP_CIPHER_CTX *ctx = NULL;
  44055. const EVP_CIPHER *init = NULL;
  44056. /* Bad test cases */
  44057. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44058. ExpectNotNull(init = EVP_des_ede3_cbc());
  44059. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44060. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44061. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44062. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44063. EVP_CIPHER_CTX_free(ctx);
  44064. ctx = NULL;
  44065. /* Good test case */
  44066. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44067. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44068. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44069. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44070. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44071. EVP_CIPHER_CTX_free(ctx);
  44072. #endif
  44073. return EXPECT_RESULT();
  44074. }
  44075. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44076. {
  44077. EXPECT_DECLS;
  44078. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44079. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44080. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44081. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44082. #ifdef HAVE_AES_CBC
  44083. #ifdef WOLFSSL_AES_128
  44084. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44085. #endif
  44086. #ifdef WOLFSSL_AES_192
  44087. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44088. #endif
  44089. #ifdef WOLFSSL_AES_256
  44090. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44091. #endif
  44092. #endif
  44093. #ifdef HAVE_AESGCM
  44094. #ifdef WOLFSSL_AES_128
  44095. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44096. #endif
  44097. #ifdef WOLFSSL_AES_192
  44098. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44099. #endif
  44100. #ifdef WOLFSSL_AES_256
  44101. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44102. #endif
  44103. #endif
  44104. #ifdef HAVE_AESCCM
  44105. #ifdef WOLFSSL_AES_128
  44106. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44107. #endif
  44108. #ifdef WOLFSSL_AES_192
  44109. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44110. #endif
  44111. #ifdef WOLFSSL_AES_256
  44112. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44113. #endif
  44114. #endif
  44115. #ifdef WOLFSSL_AES_COUNTER
  44116. #ifdef WOLFSSL_AES_128
  44117. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44118. #endif
  44119. #ifdef WOLFSSL_AES_192
  44120. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44121. #endif
  44122. #ifdef WOLFSSL_AES_256
  44123. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44124. #endif
  44125. #endif
  44126. #ifdef HAVE_AES_ECB
  44127. #ifdef WOLFSSL_AES_128
  44128. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44129. #endif
  44130. #ifdef WOLFSSL_AES_192
  44131. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44132. #endif
  44133. #ifdef WOLFSSL_AES_256
  44134. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44135. #endif
  44136. #endif
  44137. #ifdef WOLFSSL_AES_OFB
  44138. #ifdef WOLFSSL_AES_128
  44139. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44140. #endif
  44141. #ifdef WOLFSSL_AES_192
  44142. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44143. #endif
  44144. #ifdef WOLFSSL_AES_256
  44145. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44146. #endif
  44147. #endif
  44148. #ifndef NO_RC4
  44149. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44150. #endif
  44151. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44152. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44153. #endif
  44154. #endif
  44155. #ifdef WOLFSSL_SM4_ECB
  44156. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44157. #endif
  44158. #ifdef WOLFSSL_SM4_CBC
  44159. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44160. #endif
  44161. #ifdef WOLFSSL_SM4_CTR
  44162. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44163. #endif
  44164. #ifdef WOLFSSL_SM4_GCM
  44165. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44166. #endif
  44167. #ifdef WOLFSSL_SM4_CCM
  44168. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44169. #endif
  44170. return EXPECT_RESULT();
  44171. }
  44172. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44173. {
  44174. EXPECT_DECLS;
  44175. int nids[] = {
  44176. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44177. #ifdef WOLFSSL_AES_128
  44178. NID_aes_128_cbc,
  44179. #endif
  44180. #ifdef WOLFSSL_AES_192
  44181. NID_aes_192_cbc,
  44182. #endif
  44183. #ifdef WOLFSSL_AES_256
  44184. NID_aes_256_cbc,
  44185. #endif
  44186. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44187. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44188. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44189. #ifdef HAVE_AESGCM
  44190. #ifdef WOLFSSL_AES_128
  44191. NID_aes_128_gcm,
  44192. #endif
  44193. #ifdef WOLFSSL_AES_192
  44194. NID_aes_192_gcm,
  44195. #endif
  44196. #ifdef WOLFSSL_AES_256
  44197. NID_aes_256_gcm,
  44198. #endif
  44199. #endif /* HAVE_AESGCM */
  44200. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44201. #ifdef WOLFSSL_AES_COUNTER
  44202. #ifdef WOLFSSL_AES_128
  44203. NID_aes_128_ctr,
  44204. #endif
  44205. #ifdef WOLFSSL_AES_192
  44206. NID_aes_192_ctr,
  44207. #endif
  44208. #ifdef WOLFSSL_AES_256
  44209. NID_aes_256_ctr,
  44210. #endif
  44211. #endif
  44212. #ifndef NO_DES3
  44213. NID_des_cbc,
  44214. NID_des_ede3_cbc,
  44215. #endif
  44216. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44217. NID_chacha20_poly1305,
  44218. #endif
  44219. };
  44220. int iv_lengths[] = {
  44221. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44222. #ifdef WOLFSSL_AES_128
  44223. AES_BLOCK_SIZE,
  44224. #endif
  44225. #ifdef WOLFSSL_AES_192
  44226. AES_BLOCK_SIZE,
  44227. #endif
  44228. #ifdef WOLFSSL_AES_256
  44229. AES_BLOCK_SIZE,
  44230. #endif
  44231. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44232. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44233. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44234. #ifdef HAVE_AESGCM
  44235. #ifdef WOLFSSL_AES_128
  44236. GCM_NONCE_MID_SZ,
  44237. #endif
  44238. #ifdef WOLFSSL_AES_192
  44239. GCM_NONCE_MID_SZ,
  44240. #endif
  44241. #ifdef WOLFSSL_AES_256
  44242. GCM_NONCE_MID_SZ,
  44243. #endif
  44244. #endif /* HAVE_AESGCM */
  44245. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44246. #ifdef WOLFSSL_AES_COUNTER
  44247. #ifdef WOLFSSL_AES_128
  44248. AES_BLOCK_SIZE,
  44249. #endif
  44250. #ifdef WOLFSSL_AES_192
  44251. AES_BLOCK_SIZE,
  44252. #endif
  44253. #ifdef WOLFSSL_AES_256
  44254. AES_BLOCK_SIZE,
  44255. #endif
  44256. #endif
  44257. #ifndef NO_DES3
  44258. DES_BLOCK_SIZE,
  44259. DES_BLOCK_SIZE,
  44260. #endif
  44261. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44262. CHACHA20_POLY1305_AEAD_IV_SIZE,
  44263. #endif
  44264. };
  44265. int i;
  44266. int nidsLen = (sizeof(nids)/sizeof(int));
  44267. for (i = 0; i < nidsLen; i++) {
  44268. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  44269. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  44270. }
  44271. return EXPECT_RESULT();
  44272. }
  44273. static int test_wolfSSL_EVP_SignInit_ex(void)
  44274. {
  44275. EXPECT_DECLS;
  44276. WOLFSSL_EVP_MD_CTX mdCtx;
  44277. WOLFSSL_ENGINE* e = 0;
  44278. const EVP_MD* md = EVP_sha256();
  44279. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44280. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  44281. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44282. return EXPECT_RESULT();
  44283. }
  44284. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  44285. {
  44286. EXPECT_DECLS;
  44287. #if !defined(NO_SHA256)
  44288. WOLFSSL_EVP_MD_CTX mdCtx;
  44289. unsigned int s = 0;
  44290. unsigned char md[WC_SHA256_DIGEST_SIZE];
  44291. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  44292. /* Bad Case */
  44293. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44294. (HAVE_FIPS_VERSION > 2))
  44295. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44296. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  44297. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44298. #else
  44299. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44300. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  44301. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44302. #endif
  44303. /* Good Case */
  44304. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44305. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  44306. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  44307. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44308. #endif
  44309. return EXPECT_RESULT();
  44310. }
  44311. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  44312. {
  44313. EXPECT_DECLS;
  44314. #if defined(OPENSSL_EXTRA)
  44315. EVP_PKEY* pkey = NULL;
  44316. EVP_PKEY_CTX* ctx = NULL;
  44317. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44318. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44319. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  44320. /* void */
  44321. EVP_PKEY_CTX_free(ctx);
  44322. #else
  44323. /* int */
  44324. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  44325. #endif
  44326. EVP_PKEY_free(pkey);
  44327. #endif
  44328. return EXPECT_RESULT();
  44329. }
  44330. static int test_wolfSSL_EVP_PKEY_param_check(void)
  44331. {
  44332. EXPECT_DECLS;
  44333. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  44334. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44335. DH *dh = NULL;
  44336. DH *setDh = NULL;
  44337. EVP_PKEY *pkey = NULL;
  44338. EVP_PKEY_CTX* ctx = NULL;
  44339. FILE* f = NULL;
  44340. unsigned char buf[512];
  44341. const unsigned char* pt = buf;
  44342. const char* dh2048 = "./certs/dh2048.der";
  44343. long len = 0;
  44344. int code = -1;
  44345. XMEMSET(buf, 0, sizeof(buf));
  44346. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44347. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44348. if (f != XBADFILE)
  44349. XFCLOSE(f);
  44350. /* Load dh2048.der into DH with internal format */
  44351. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  44352. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44353. ExpectIntEQ(code, 0);
  44354. code = -1;
  44355. pkey = wolfSSL_EVP_PKEY_new();
  44356. /* Set DH into PKEY */
  44357. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44358. /* create ctx from pkey */
  44359. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44360. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  44361. /* TODO: more invalid cases */
  44362. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  44363. EVP_PKEY_CTX_free(ctx);
  44364. EVP_PKEY_free(pkey);
  44365. DH_free(setDh);
  44366. setDh = NULL;
  44367. DH_free(dh);
  44368. dh = NULL;
  44369. #endif
  44370. #endif
  44371. return EXPECT_RESULT();
  44372. }
  44373. static int test_wolfSSL_EVP_BytesToKey(void)
  44374. {
  44375. EXPECT_DECLS;
  44376. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44377. byte key[AES_BLOCK_SIZE] = {0};
  44378. byte iv[AES_BLOCK_SIZE] = {0};
  44379. int count = 0;
  44380. const EVP_MD* md = EVP_sha256();
  44381. const EVP_CIPHER *type;
  44382. const unsigned char *salt = (unsigned char *)"salt1234";
  44383. int sz = 5;
  44384. const byte data[] = {
  44385. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44386. 0x72,0x6c,0x64
  44387. };
  44388. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  44389. /* Bad cases */
  44390. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  44391. 0);
  44392. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  44393. 16);
  44394. md = "2";
  44395. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44396. WOLFSSL_FAILURE);
  44397. /* Good case */
  44398. md = EVP_sha256();
  44399. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44400. 16);
  44401. #endif
  44402. return EXPECT_RESULT();
  44403. }
  44404. static int test_evp_cipher_aes_gcm(void)
  44405. {
  44406. EXPECT_DECLS;
  44407. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  44408. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  44409. (HAVE_FIPS_VERSION >= 2)))
  44410. /*
  44411. * This test checks data at various points in the encrypt/decrypt process
  44412. * against known values produced using the same test with OpenSSL. This
  44413. * interop testing is critical for verifying the correctness of our
  44414. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  44415. * a flow supported by OpenSSL that uses the control command
  44416. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  44417. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  44418. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  44419. * wolfSSL OpenSSH clients because there was a bug in this flow that
  44420. * happened to "cancel out" if both sides of the connection had the bug.
  44421. */
  44422. enum {
  44423. NUM_ENCRYPTIONS = 3,
  44424. AAD_SIZE = 4
  44425. };
  44426. byte plainText1[] = {
  44427. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  44428. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44429. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  44430. };
  44431. byte plainText2[] = {
  44432. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  44433. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  44434. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  44435. };
  44436. byte plainText3[] = {
  44437. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  44438. 0x5b, 0x57, 0x10
  44439. };
  44440. byte* plainTexts[NUM_ENCRYPTIONS] = {
  44441. plainText1,
  44442. plainText2,
  44443. plainText3
  44444. };
  44445. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  44446. sizeof(plainText1),
  44447. sizeof(plainText2),
  44448. sizeof(plainText3)
  44449. };
  44450. byte aad1[AAD_SIZE] = {
  44451. 0x00, 0x00, 0x00, 0x01
  44452. };
  44453. byte aad2[AAD_SIZE] = {
  44454. 0x00, 0x00, 0x00, 0x10
  44455. };
  44456. byte aad3[AAD_SIZE] = {
  44457. 0x00, 0x00, 0x01, 0x00
  44458. };
  44459. byte* aads[NUM_ENCRYPTIONS] = {
  44460. aad1,
  44461. aad2,
  44462. aad3
  44463. };
  44464. const byte iv[GCM_NONCE_MID_SZ] = {
  44465. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  44466. };
  44467. byte currentIv[GCM_NONCE_MID_SZ];
  44468. const byte key[] = {
  44469. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  44470. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  44471. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  44472. };
  44473. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  44474. {
  44475. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44476. 0xEF
  44477. },
  44478. {
  44479. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44480. 0xF0
  44481. },
  44482. {
  44483. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44484. 0xF1
  44485. }
  44486. };
  44487. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  44488. {
  44489. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  44490. 0x3D, 0x32, 0x18, 0x34, 0xA9
  44491. },
  44492. {
  44493. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  44494. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  44495. },
  44496. {
  44497. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  44498. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  44499. }
  44500. };
  44501. const byte expCipherText1[] = {
  44502. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  44503. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  44504. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  44505. };
  44506. const byte expCipherText2[] = {
  44507. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  44508. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  44509. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  44510. };
  44511. const byte expCipherText3[] = {
  44512. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  44513. 0x80, 0x5E, 0x6B,
  44514. };
  44515. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  44516. expCipherText1,
  44517. expCipherText2,
  44518. expCipherText3
  44519. };
  44520. byte* cipherText = NULL;
  44521. byte* calcPlainText = NULL;
  44522. byte tag[AES_BLOCK_SIZE];
  44523. EVP_CIPHER_CTX* encCtx = NULL;
  44524. EVP_CIPHER_CTX* decCtx = NULL;
  44525. int i, j, outl;
  44526. /****************************************************/
  44527. for (i = 0; i < 3; ++i) {
  44528. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  44529. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  44530. /* First iteration, set key before IV. */
  44531. if (i == 0) {
  44532. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  44533. SSL_SUCCESS);
  44534. /*
  44535. * The call to EVP_CipherInit below (with NULL key) should clear the
  44536. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  44537. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  44538. * behavior.
  44539. */
  44540. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44541. (void*)iv), SSL_SUCCESS);
  44542. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  44543. SSL_SUCCESS);
  44544. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44545. currentIv), SSL_FAILURE);
  44546. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  44547. SSL_SUCCESS);
  44548. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  44549. SSL_SUCCESS);
  44550. }
  44551. /* Second iteration, IV before key. */
  44552. else {
  44553. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  44554. SSL_SUCCESS);
  44555. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  44556. SSL_SUCCESS);
  44557. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  44558. SSL_SUCCESS);
  44559. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  44560. SSL_SUCCESS);
  44561. }
  44562. /*
  44563. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  44564. * been issued first.
  44565. */
  44566. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44567. currentIv), SSL_FAILURE);
  44568. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44569. (void*)iv), SSL_SUCCESS);
  44570. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44571. (void*)iv), SSL_SUCCESS);
  44572. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  44573. /*************** Encrypt ***************/
  44574. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44575. currentIv), SSL_SUCCESS);
  44576. /* Check current IV against expected. */
  44577. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44578. /* Add AAD. */
  44579. if (i == 2) {
  44580. /* Test streaming API. */
  44581. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  44582. AAD_SIZE), SSL_SUCCESS);
  44583. }
  44584. else {
  44585. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  44586. AAD_SIZE);
  44587. }
  44588. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44589. DYNAMIC_TYPE_TMP_BUFFER));
  44590. /* Encrypt plaintext. */
  44591. if (i == 2) {
  44592. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  44593. plainTexts[j], plainTextSzs[j]),
  44594. SSL_SUCCESS);
  44595. }
  44596. else {
  44597. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  44598. plainTextSzs[j]), plainTextSzs[j]);
  44599. }
  44600. if (i == 2) {
  44601. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  44602. SSL_SUCCESS);
  44603. }
  44604. else {
  44605. /*
  44606. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  44607. * akin to calling EVP_CipherFinal.
  44608. */
  44609. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  44610. }
  44611. /* Check ciphertext against expected. */
  44612. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  44613. 0);
  44614. /* Get and check tag against expected. */
  44615. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  44616. sizeof(tag), tag), SSL_SUCCESS);
  44617. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  44618. /*************** Decrypt ***************/
  44619. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44620. currentIv), SSL_SUCCESS);
  44621. /* Check current IV against expected. */
  44622. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44623. /* Add AAD. */
  44624. if (i == 2) {
  44625. /* Test streaming API. */
  44626. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44627. AAD_SIZE), SSL_SUCCESS);
  44628. }
  44629. else {
  44630. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44631. AAD_SIZE);
  44632. }
  44633. /* Set expected tag. */
  44634. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44635. sizeof(tag), tag), SSL_SUCCESS);
  44636. /* Decrypt ciphertext. */
  44637. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44638. DYNAMIC_TYPE_TMP_BUFFER));
  44639. if (i == 2) {
  44640. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44641. cipherText, plainTextSzs[j]),
  44642. SSL_SUCCESS);
  44643. }
  44644. else {
  44645. /* This first EVP_Cipher call will check the tag, too. */
  44646. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44647. plainTextSzs[j]), plainTextSzs[j]);
  44648. }
  44649. if (i == 2) {
  44650. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44651. SSL_SUCCESS);
  44652. }
  44653. else {
  44654. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44655. }
  44656. /* Check plaintext against expected. */
  44657. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44658. 0);
  44659. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44660. cipherText = NULL;
  44661. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44662. calcPlainText = NULL;
  44663. }
  44664. EVP_CIPHER_CTX_free(encCtx);
  44665. encCtx = NULL;
  44666. EVP_CIPHER_CTX_free(decCtx);
  44667. decCtx = NULL;
  44668. }
  44669. #endif
  44670. return EXPECT_RESULT();
  44671. }
  44672. static int test_wolfSSL_OBJ_ln(void)
  44673. {
  44674. EXPECT_DECLS;
  44675. const int nid_set[] = {
  44676. NID_commonName,
  44677. NID_serialNumber,
  44678. NID_countryName,
  44679. NID_localityName,
  44680. NID_stateOrProvinceName,
  44681. NID_organizationName,
  44682. NID_organizationalUnitName,
  44683. NID_domainComponent,
  44684. NID_businessCategory,
  44685. NID_jurisdictionCountryName,
  44686. NID_jurisdictionStateOrProvinceName,
  44687. NID_emailAddress
  44688. };
  44689. const char* ln_set[] = {
  44690. "commonName",
  44691. "serialNumber",
  44692. "countryName",
  44693. "localityName",
  44694. "stateOrProvinceName",
  44695. "organizationName",
  44696. "organizationalUnitName",
  44697. "domainComponent",
  44698. "businessCategory",
  44699. "jurisdictionCountryName",
  44700. "jurisdictionStateOrProvinceName",
  44701. "emailAddress",
  44702. };
  44703. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44704. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44705. #ifdef HAVE_ECC
  44706. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44707. {
  44708. EC_builtin_curve r[27];
  44709. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44710. nCurves = EC_get_builtin_curves(r, nCurves);
  44711. for (i = 0; i < nCurves; i++) {
  44712. /* skip ECC_CURVE_INVALID */
  44713. if (r[i].nid != ECC_CURVE_INVALID) {
  44714. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44715. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44716. }
  44717. }
  44718. }
  44719. #endif
  44720. #endif
  44721. for (i = 0; i < maxIdx; i++) {
  44722. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44723. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44724. }
  44725. return EXPECT_RESULT();
  44726. }
  44727. static int test_wolfSSL_OBJ_sn(void)
  44728. {
  44729. EXPECT_DECLS;
  44730. int i = 0, maxIdx = 7;
  44731. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44732. NID_stateOrProvinceName,NID_organizationName,
  44733. NID_organizationalUnitName,NID_emailAddress};
  44734. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44735. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44736. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44737. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44738. WOLFSSL_EMAIL_ADDR};
  44739. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44740. for (i = 0; i < maxIdx; i++) {
  44741. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44742. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44743. }
  44744. return EXPECT_RESULT();
  44745. }
  44746. #if !defined(NO_BIO)
  44747. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44748. {
  44749. return lh_strhash(s[3]);
  44750. }
  44751. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44752. {
  44753. return XSTRCMP(a[3], b[3]);
  44754. }
  44755. #endif
  44756. static int test_wolfSSL_TXT_DB(void)
  44757. {
  44758. EXPECT_DECLS;
  44759. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44760. BIO *bio = NULL;
  44761. TXT_DB *db = NULL;
  44762. const int columns = 6;
  44763. const char *fields[6] = {
  44764. "V",
  44765. "320926161116Z",
  44766. "",
  44767. "12BD",
  44768. "unknown",
  44769. "/CN=rsa doe",
  44770. };
  44771. char** fields_copy = NULL;
  44772. /* Test read */
  44773. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  44774. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44775. ExpectNotNull(db = TXT_DB_read(bio, columns));
  44776. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44777. DYNAMIC_TYPE_OPENSSL));
  44778. if (fields_copy != NULL) {
  44779. XMEMCPY(fields_copy, fields, sizeof(fields));
  44780. }
  44781. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44782. if (EXPECT_FAIL()) {
  44783. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  44784. }
  44785. BIO_free(bio);
  44786. bio = NULL;
  44787. /* Test write */
  44788. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44789. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  44790. BIO_free(bio);
  44791. /* Test index */
  44792. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44793. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44794. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44795. fields[3] = "12DA";
  44796. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44797. fields[3] = "FFFF";
  44798. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44799. fields[3] = "";
  44800. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44801. TXT_DB_free(db);
  44802. #endif
  44803. return EXPECT_RESULT();
  44804. }
  44805. static int test_wolfSSL_NCONF(void)
  44806. {
  44807. EXPECT_DECLS;
  44808. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44809. const char* confFile = "./tests/NCONF_test.cnf";
  44810. CONF* conf = NULL;
  44811. long eline = 0;
  44812. long num = 0;
  44813. ExpectNotNull(conf = NCONF_new(NULL));
  44814. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44815. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44816. ExpectIntEQ(num, 1234);
  44817. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44818. ExpectIntEQ(num, 4321);
  44819. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44820. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44821. "./test-dir/file1");
  44822. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44823. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44824. NCONF_free(conf);
  44825. #endif
  44826. return EXPECT_RESULT();
  44827. }
  44828. #endif /* OPENSSL_ALL */
  44829. static int test_wolfSSL_X509V3_EXT_get(void) {
  44830. EXPECT_DECLS;
  44831. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44832. XFILE f = XBADFILE;
  44833. int numOfExt =0;
  44834. int extNid = 0;
  44835. int i = 0;
  44836. WOLFSSL_X509* x509 = NULL;
  44837. WOLFSSL_X509_EXTENSION* ext = NULL;
  44838. const WOLFSSL_v3_ext_method* method = NULL;
  44839. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44840. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44841. if (f != XBADFILE)
  44842. XFCLOSE(f);
  44843. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44844. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44845. for (i = 0; i < numOfExt; i++) {
  44846. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44847. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  44848. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44849. ExpectIntEQ(method->ext_nid, extNid);
  44850. }
  44851. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44852. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44853. wolfSSL_X509_free(x509);
  44854. #endif
  44855. return EXPECT_RESULT();
  44856. }
  44857. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44858. {
  44859. EXPECT_DECLS;
  44860. #ifdef OPENSSL_ALL
  44861. const char *ext_names[] = {
  44862. "subjectKeyIdentifier",
  44863. "authorityKeyIdentifier",
  44864. "subjectAltName",
  44865. "keyUsage",
  44866. "extendedKeyUsage",
  44867. };
  44868. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44869. int ext_nids[] = {
  44870. NID_subject_key_identifier,
  44871. NID_authority_key_identifier,
  44872. NID_subject_alt_name,
  44873. NID_key_usage,
  44874. NID_ext_key_usage,
  44875. };
  44876. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44877. const char *ext_values[] = {
  44878. "hash",
  44879. "hash",
  44880. "DNS:example.com, IP:127.0.0.1",
  44881. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  44882. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  44883. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  44884. "OCSPSigning",
  44885. };
  44886. size_t i;
  44887. X509_EXTENSION* ext = NULL;
  44888. X509* x509 = NULL;
  44889. unsigned int keyUsageFlags;
  44890. unsigned int extKeyUsageFlags;
  44891. ExpectNotNull(x509 = X509_new());
  44892. /* keyUsage / extKeyUsage should match string above */
  44893. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44894. | KU_NON_REPUDIATION
  44895. | KU_KEY_ENCIPHERMENT
  44896. | KU_DATA_ENCIPHERMENT
  44897. | KU_KEY_AGREEMENT
  44898. | KU_KEY_CERT_SIGN
  44899. | KU_CRL_SIGN
  44900. | KU_ENCIPHER_ONLY
  44901. | KU_DECIPHER_ONLY;
  44902. extKeyUsageFlags = XKU_SSL_CLIENT
  44903. | XKU_SSL_SERVER
  44904. | XKU_CODE_SIGN
  44905. | XKU_SMIME
  44906. | XKU_TIMESTAMP
  44907. | XKU_OCSP_SIGN;
  44908. for (i = 0; i < ext_names_count; i++) {
  44909. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44910. ext_values[i]));
  44911. X509_EXTENSION_free(ext);
  44912. ext = NULL;
  44913. }
  44914. for (i = 0; i < ext_nids_count; i++) {
  44915. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  44916. ext_values[i]));
  44917. X509_EXTENSION_free(ext);
  44918. ext = NULL;
  44919. }
  44920. /* Test adding extension to X509 */
  44921. for (i = 0; i < ext_nids_count; i++) {
  44922. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44923. ext_values[i]));
  44924. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  44925. if (ext_nids[i] == NID_key_usage) {
  44926. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44927. }
  44928. else if (ext_nids[i] == NID_ext_key_usage) {
  44929. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44930. }
  44931. X509_EXTENSION_free(ext);
  44932. ext = NULL;
  44933. }
  44934. X509_free(x509);
  44935. #endif
  44936. return EXPECT_RESULT();
  44937. }
  44938. static int test_wolfSSL_X509V3_EXT(void) {
  44939. EXPECT_DECLS;
  44940. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44941. XFILE f = XBADFILE;
  44942. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  44943. char* str = NULL;
  44944. unsigned char* data = NULL;
  44945. const WOLFSSL_v3_ext_method* method = NULL;
  44946. WOLFSSL_X509* x509 = NULL;
  44947. WOLFSSL_X509_EXTENSION* ext = NULL;
  44948. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  44949. WOLFSSL_ASN1_OBJECT *obj = NULL;
  44950. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  44951. WOLFSSL_ASN1_STRING* asn1str = NULL;
  44952. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  44953. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  44954. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  44955. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  44956. WOLFSSL_GENERAL_NAME* gn = NULL;
  44957. /* Check NULL argument */
  44958. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  44959. /* Using OCSP cert with X509V3 extensions */
  44960. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  44961. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44962. if (f != XBADFILE)
  44963. XFCLOSE(f);
  44964. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44965. /* Basic Constraints */
  44966. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44967. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44968. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  44969. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  44970. ExpectIntEQ(bc->ca, 1);
  44971. ExpectNull(bc->pathlen);
  44972. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  44973. bc = NULL;
  44974. i++;
  44975. /* Subject Key Identifier */
  44976. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44977. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44978. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  44979. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44980. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  44981. asn1str));
  44982. X509_EXTENSION_free(ext2);
  44983. ext2 = NULL;
  44984. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44985. ExpectNotNull(method->i2s);
  44986. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  44987. wolfSSL_ASN1_STRING_free(asn1str);
  44988. asn1str = NULL;
  44989. if (str != NULL) {
  44990. actual = strcmp(str,
  44991. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44992. }
  44993. ExpectIntEQ(actual, 0);
  44994. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44995. str = NULL;
  44996. i++;
  44997. /* Authority Key Identifier */
  44998. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44999. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45000. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45001. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45002. ext));
  45003. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45004. ExpectNotNull(asn1str = aKeyId->keyid);
  45005. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45006. asn1str));
  45007. asn1str = NULL;
  45008. if (str != NULL) {
  45009. actual = strcmp(str,
  45010. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45011. }
  45012. ExpectIntEQ(actual, 0);
  45013. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45014. str = NULL;
  45015. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45016. aKeyId = NULL;
  45017. i++;
  45018. /* Key Usage */
  45019. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45020. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45021. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45022. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45023. #if defined(WOLFSSL_QT)
  45024. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45025. #else
  45026. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45027. #endif
  45028. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45029. if (data != NULL) {
  45030. #ifdef BIG_ENDIAN_ORDER
  45031. actual = data[1];
  45032. #else
  45033. actual = data[0];
  45034. #endif
  45035. }
  45036. ExpectIntEQ(actual, expected);
  45037. wolfSSL_ASN1_STRING_free(asn1str);
  45038. asn1str = NULL;
  45039. #if 1
  45040. i++;
  45041. /* Authority Info Access */
  45042. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45043. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45044. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45045. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45046. ext));
  45047. #if defined(WOLFSSL_QT)
  45048. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45049. #else
  45050. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45051. #endif
  45052. /* URI entry is an ACCESS_DESCRIPTION type */
  45053. #if defined(WOLFSSL_QT)
  45054. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45055. #else
  45056. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45057. #endif
  45058. ExpectNotNull(adObj = ad->method);
  45059. /* Make sure nid is OCSP */
  45060. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45061. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45062. ExpectNotNull(gn = ad->location);
  45063. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45064. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45065. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45066. #if defined(WOLFSSL_QT)
  45067. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45068. #else
  45069. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45070. #endif
  45071. if (str != NULL) {
  45072. actual = strcmp(str, "http://127.0.0.1:22220");
  45073. }
  45074. ExpectIntEQ(actual, 0);
  45075. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45076. aia = NULL;
  45077. #else
  45078. (void) aia; (void) ad; (void) adObj; (void) gn;
  45079. #endif
  45080. wolfSSL_X509_free(x509);
  45081. #endif
  45082. return EXPECT_RESULT();
  45083. }
  45084. static int test_wolfSSL_X509_get_extension_flags(void)
  45085. {
  45086. EXPECT_DECLS;
  45087. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45088. XFILE f = XBADFILE;
  45089. X509* x509 = NULL;
  45090. unsigned int extFlags;
  45091. unsigned int keyUsageFlags;
  45092. unsigned int extKeyUsageFlags;
  45093. /* client-int-cert.pem has the following extension flags. */
  45094. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45095. /* and the following key usage flags. */
  45096. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45097. | KU_NON_REPUDIATION
  45098. | KU_KEY_ENCIPHERMENT;
  45099. /* and the following extended key usage flags. */
  45100. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45101. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45102. XBADFILE);
  45103. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45104. if (f != XBADFILE) {
  45105. XFCLOSE(f);
  45106. f = XBADFILE;
  45107. }
  45108. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45109. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45110. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45111. X509_free(x509);
  45112. x509 = NULL;
  45113. /* client-cert-ext.pem has the following extension flags. */
  45114. extFlags = EXFLAG_KUSAGE;
  45115. /* and the following key usage flags. */
  45116. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45117. | KU_KEY_CERT_SIGN
  45118. | KU_CRL_SIGN;
  45119. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45120. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45121. if (f != XBADFILE)
  45122. XFCLOSE(f);
  45123. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45124. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45125. X509_free(x509);
  45126. #endif /* OPENSSL_ALL */
  45127. return EXPECT_RESULT();
  45128. }
  45129. static int test_wolfSSL_X509_get_ext(void)
  45130. {
  45131. EXPECT_DECLS;
  45132. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45133. int ret = 0;
  45134. XFILE f = XBADFILE;
  45135. WOLFSSL_X509* x509 = NULL;
  45136. WOLFSSL_X509_EXTENSION* foundExtension;
  45137. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45138. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45139. if (f != XBADFILE)
  45140. XFCLOSE(f);
  45141. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45142. /* wolfSSL_X509_get_ext() valid input */
  45143. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45144. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45145. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45146. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45147. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45148. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45149. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45150. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45151. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45152. wolfSSL_X509_free(x509);
  45153. #endif
  45154. return EXPECT_RESULT();
  45155. }
  45156. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45157. {
  45158. EXPECT_DECLS;
  45159. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45160. int rc = 0;
  45161. XFILE f = XBADFILE;
  45162. WOLFSSL_X509* x509 = NULL;
  45163. ASN1_OBJECT* obj = NULL;
  45164. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45165. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45166. if (f != XBADFILE)
  45167. XFCLOSE(f);
  45168. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45169. -1), 0);
  45170. /* Start search from last location (should fail) */
  45171. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45172. rc), -1);
  45173. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45174. -2), -1);
  45175. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45176. -1), -1);
  45177. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45178. /* NID_ext_key_usage, check also its nid and oid */
  45179. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45180. -1);
  45181. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45182. x509, rc)));
  45183. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45184. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45185. wolfSSL_X509_free(x509);
  45186. #endif
  45187. return EXPECT_RESULT();
  45188. }
  45189. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45190. {
  45191. EXPECT_DECLS;
  45192. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45193. int rc = 0;
  45194. XFILE f = XBADFILE;
  45195. WOLFSSL_X509* x509 = NULL;
  45196. WOLFSSL_X509_EXTENSION* ext = NULL;
  45197. WOLFSSL_ASN1_STRING* sanString = NULL;
  45198. byte* sanDer = NULL;
  45199. const byte expectedDer[] = {
  45200. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  45201. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  45202. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45203. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45204. if (f != XBADFILE)
  45205. XFCLOSE(f);
  45206. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  45207. ExpectNotNull(ext = X509_get_ext(x509, rc));
  45208. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  45209. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  45210. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  45211. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  45212. X509_free(x509);
  45213. #endif
  45214. return EXPECT_RESULT();
  45215. }
  45216. static int test_wolfSSL_X509_EXTENSION_new(void)
  45217. {
  45218. EXPECT_DECLS;
  45219. #if defined (OPENSSL_ALL)
  45220. WOLFSSL_X509_EXTENSION* ext = NULL;
  45221. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  45222. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  45223. wolfSSL_X509_EXTENSION_free(ext);
  45224. #endif
  45225. return EXPECT_RESULT();
  45226. }
  45227. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  45228. {
  45229. EXPECT_DECLS;
  45230. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45231. WOLFSSL_X509* x509 = NULL;
  45232. WOLFSSL_X509_EXTENSION* ext = NULL;
  45233. WOLFSSL_ASN1_OBJECT* o = NULL;
  45234. XFILE file = XBADFILE;
  45235. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45236. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45237. if (file != XBADFILE)
  45238. XFCLOSE(file);
  45239. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  45240. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45241. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  45242. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  45243. ExpectIntEQ(o->nid, 128);
  45244. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  45245. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  45246. wolfSSL_X509_free(x509);
  45247. #endif
  45248. return EXPECT_RESULT();
  45249. }
  45250. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  45251. {
  45252. EXPECT_DECLS;
  45253. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45254. WOLFSSL_X509* x509 = NULL;
  45255. WOLFSSL_X509_EXTENSION* ext = NULL;
  45256. WOLFSSL_ASN1_STRING* str = NULL;
  45257. XFILE file = XBADFILE;
  45258. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45259. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45260. if (file != XBADFILE)
  45261. XFCLOSE(file);
  45262. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45263. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  45264. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  45265. wolfSSL_X509_free(x509);
  45266. #endif
  45267. return EXPECT_RESULT();
  45268. }
  45269. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  45270. {
  45271. EXPECT_DECLS;
  45272. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45273. WOLFSSL_X509* x509 = NULL;
  45274. WOLFSSL_X509_EXTENSION* ext = NULL;
  45275. XFILE file = XBADFILE;
  45276. int crit = 0;
  45277. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45278. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45279. if (file != XBADFILE)
  45280. XFCLOSE(file);
  45281. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45282. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  45283. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  45284. wolfSSL_X509_free(x509);
  45285. #endif
  45286. return EXPECT_RESULT();
  45287. }
  45288. static int test_wolfSSL_X509V3_EXT_print(void)
  45289. {
  45290. EXPECT_DECLS;
  45291. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  45292. !defined(NO_RSA)
  45293. {
  45294. XFILE f = XBADFILE;
  45295. WOLFSSL_X509* x509 = NULL;
  45296. X509_EXTENSION * ext = NULL;
  45297. int loc = 0;
  45298. BIO *bio = NULL;
  45299. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  45300. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45301. if (f != XBADFILE)
  45302. fclose(f);
  45303. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  45304. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45305. NID_basic_constraints, -1), -1);
  45306. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45307. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45308. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45309. NID_subject_key_identifier, -1), -1);
  45310. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45311. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45312. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45313. NID_authority_key_identifier, -1), -1);
  45314. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45315. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45316. wolfSSL_BIO_free(bio);
  45317. wolfSSL_X509_free(x509);
  45318. }
  45319. {
  45320. X509 *x509 = NULL;
  45321. BIO *bio = NULL;
  45322. X509_EXTENSION *ext = NULL;
  45323. unsigned int i = 0;
  45324. unsigned int idx = 0;
  45325. /* Some NIDs to test with */
  45326. int nids[] = {
  45327. /* NID_key_usage, currently X509_get_ext returns this as a bit
  45328. * string, which messes up X509V3_EXT_print */
  45329. /* NID_ext_key_usage, */
  45330. NID_subject_alt_name,
  45331. };
  45332. int* n = NULL;
  45333. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  45334. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  45335. WOLFSSL_FILETYPE_PEM));
  45336. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  45337. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  45338. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  45339. * update the index */
  45340. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  45341. ExpectNotNull(ext = X509_get_ext(x509, idx));
  45342. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  45343. ExpectIntGT(fprintf(stderr, "\n"), 0);
  45344. }
  45345. BIO_free(bio);
  45346. X509_free(x509);
  45347. }
  45348. #endif
  45349. return EXPECT_RESULT();
  45350. }
  45351. static int test_wolfSSL_X509_cmp(void)
  45352. {
  45353. EXPECT_DECLS;
  45354. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45355. XFILE file1 = XBADFILE;
  45356. XFILE file2 = XBADFILE;
  45357. WOLFSSL_X509* cert1 = NULL;
  45358. WOLFSSL_X509* cert2 = NULL;
  45359. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45360. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  45361. XBADFILE);
  45362. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  45363. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  45364. if (file1 != XBADFILE)
  45365. fclose(file1);
  45366. if (file2 != XBADFILE)
  45367. fclose(file2);
  45368. /* wolfSSL_X509_cmp() testing matching certs */
  45369. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  45370. /* wolfSSL_X509_cmp() testing mismatched certs */
  45371. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  45372. /* wolfSSL_X509_cmp() testing NULL, valid args */
  45373. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  45374. /* wolfSSL_X509_cmp() testing valid, NULL args */
  45375. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  45376. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  45377. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  45378. wolfSSL_X509_free(cert1);
  45379. wolfSSL_X509_free(cert2);
  45380. #endif
  45381. return EXPECT_RESULT();
  45382. }
  45383. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  45384. {
  45385. EXPECT_DECLS;
  45386. #if defined(OPENSSL_ALL)
  45387. EVP_PKEY* pkey;
  45388. pkey = EVP_PKEY_new();
  45389. ExpectNotNull(pkey);
  45390. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  45391. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45392. EVP_PKEY_free(pkey);
  45393. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45394. EVP_PKEY_free(pkey);
  45395. EVP_PKEY_free(pkey);
  45396. #endif
  45397. return EXPECT_RESULT();
  45398. }
  45399. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  45400. {
  45401. EXPECT_DECLS;
  45402. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45403. EVP_PKEY* pkey = NULL;
  45404. const unsigned char* p;
  45405. unsigned char *der = NULL;
  45406. unsigned char *tmp = NULL;
  45407. int derLen;
  45408. p = client_keypub_der_2048;
  45409. /* Check that key can be successfully decoded. */
  45410. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  45411. sizeof_client_keypub_der_2048));
  45412. /* Check that key can be successfully encoded. */
  45413. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45414. /* Ensure that the encoded version matches the original. */
  45415. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45416. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45417. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45418. * is advanced. */
  45419. tmp = der;
  45420. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45421. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45422. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45423. ExpectTrue(der + derLen == tmp);
  45424. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45425. EVP_PKEY_free(pkey);
  45426. #endif
  45427. return EXPECT_RESULT();
  45428. }
  45429. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  45430. {
  45431. EXPECT_DECLS;
  45432. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  45433. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45434. EVP_PKEY* pkey = NULL;
  45435. const unsigned char* p;
  45436. unsigned char *der = NULL;
  45437. unsigned char *tmp = NULL;
  45438. int derLen;
  45439. unsigned char pub_buf[65];
  45440. const int pub_len = 65;
  45441. BN_CTX* ctx;
  45442. EC_GROUP* curve = NULL;
  45443. EC_KEY* ephemeral_key = NULL;
  45444. const EC_POINT* h;
  45445. /* Generate an x963 key pair and get public part into pub_buf */
  45446. ExpectNotNull(ctx = BN_CTX_new());
  45447. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  45448. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  45449. NID_X9_62_prime256v1));
  45450. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  45451. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  45452. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  45453. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  45454. /* Prepare the EVP_PKEY */
  45455. ExpectNotNull(pkey = EVP_PKEY_new());
  45456. p = pub_buf;
  45457. /* Check that key can be successfully decoded. */
  45458. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  45459. pub_len));
  45460. /* Check that key can be successfully encoded. */
  45461. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45462. /* Ensure that the encoded version matches the original. */
  45463. ExpectIntEQ(derLen, pub_len);
  45464. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45465. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45466. * is advanced. */
  45467. tmp = der;
  45468. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45469. ExpectIntEQ(derLen, pub_len);
  45470. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45471. ExpectTrue(der + derLen == tmp);
  45472. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45473. EVP_PKEY_free(pkey);
  45474. EC_KEY_free(ephemeral_key);
  45475. EC_GROUP_free(curve);
  45476. #endif
  45477. return EXPECT_RESULT();
  45478. }
  45479. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  45480. {
  45481. EXPECT_DECLS;
  45482. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  45483. DSA* dsa = NULL;
  45484. byte derIn[] = {
  45485. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  45486. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  45487. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  45488. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  45489. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  45490. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  45491. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  45492. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  45493. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  45494. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  45495. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  45496. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  45497. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  45498. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  45499. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  45500. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  45501. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  45502. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  45503. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  45504. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  45505. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  45506. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  45507. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  45508. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  45509. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  45510. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  45511. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  45512. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  45513. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  45514. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  45515. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  45516. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  45517. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  45518. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  45519. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  45520. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  45521. 0x1e, 0xde, 0xc4
  45522. };
  45523. int derInLen = sizeof(derIn);
  45524. byte* derOut = NULL;
  45525. int derOutLen;
  45526. byte* p = derIn;
  45527. /* Check that params can be successfully decoded. */
  45528. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  45529. /* Check that params can be successfully encoded. */
  45530. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  45531. /* Ensure that the encoded version matches the original. */
  45532. ExpectIntEQ(derInLen, derOutLen);
  45533. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  45534. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45535. DSA_free(dsa);
  45536. #endif
  45537. return EXPECT_RESULT();
  45538. }
  45539. static int test_wolfSSL_i2d_PrivateKey(void)
  45540. {
  45541. EXPECT_DECLS;
  45542. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  45543. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45544. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45545. {
  45546. EVP_PKEY* pkey = NULL;
  45547. const unsigned char* server_key =
  45548. (const unsigned char*)server_key_der_2048;
  45549. unsigned char buf[FOURK_BUF];
  45550. unsigned char* pt = NULL;
  45551. int bufSz = 0;
  45552. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  45553. (long)sizeof_server_key_der_2048));
  45554. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  45555. pt = buf;
  45556. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  45557. ExpectIntNE((pt - buf), 0);
  45558. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  45559. EVP_PKEY_free(pkey);
  45560. }
  45561. #endif
  45562. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45563. {
  45564. EVP_PKEY* pkey = NULL;
  45565. const unsigned char* client_key =
  45566. (const unsigned char*)ecc_clikey_der_256;
  45567. unsigned char buf[FOURK_BUF];
  45568. unsigned char* pt = NULL;
  45569. int bufSz = 0;
  45570. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  45571. (long)sizeof_ecc_clikey_der_256)));
  45572. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  45573. pt = buf;
  45574. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  45575. ExpectIntNE((pt - buf), 0);
  45576. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  45577. EVP_PKEY_free(pkey);
  45578. }
  45579. #endif
  45580. #endif
  45581. return EXPECT_RESULT();
  45582. }
  45583. static int test_wolfSSL_OCSP_id_get0_info(void)
  45584. {
  45585. EXPECT_DECLS;
  45586. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  45587. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45588. X509* cert = NULL;
  45589. X509* issuer = NULL;
  45590. OCSP_CERTID* id = NULL;
  45591. OCSP_CERTID* id2 = NULL;
  45592. ASN1_STRING* name = NULL;
  45593. ASN1_OBJECT* pmd = NULL;
  45594. ASN1_STRING* keyHash = NULL;
  45595. ASN1_INTEGER* serial = NULL;
  45596. ASN1_INTEGER* x509Int = NULL;
  45597. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  45598. SSL_FILETYPE_PEM));
  45599. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  45600. SSL_FILETYPE_PEM));
  45601. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  45602. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  45603. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  45604. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  45605. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  45606. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  45607. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  45608. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  45609. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  45610. ExpectNotNull(serial);
  45611. /* compare serial number to one in cert, should be equal */
  45612. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  45613. ExpectIntEQ(x509Int->length, serial->length);
  45614. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  45615. /* test OCSP_id_cmp */
  45616. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45617. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  45618. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  45619. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  45620. if (id != NULL) {
  45621. id->issuerHash[0] = ~id->issuerHash[0];
  45622. }
  45623. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  45624. OCSP_CERTID_free(id);
  45625. OCSP_CERTID_free(id2);
  45626. X509_free(cert); /* free's x509Int */
  45627. X509_free(issuer);
  45628. #endif
  45629. return EXPECT_RESULT();
  45630. }
  45631. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45632. {
  45633. EXPECT_DECLS;
  45634. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45635. WOLFSSL_OCSP_CERTID certId;
  45636. byte* targetBuffer = NULL;
  45637. byte* p;
  45638. /* OCSP CertID bytes taken from PCAP */
  45639. byte rawCertId[] = {
  45640. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45641. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45642. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45643. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45644. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45645. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45646. 0xcf, 0xbc, 0x91
  45647. };
  45648. int ret = 0;
  45649. int i;
  45650. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45651. certId.rawCertId = rawCertId;
  45652. certId.rawCertIdSize = sizeof(rawCertId);
  45653. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  45654. DYNAMIC_TYPE_TMP_BUFFER));
  45655. p = targetBuffer;
  45656. /* Function returns the size of the encoded data. */
  45657. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45658. /* If target buffer is not null, function increments targetBuffer to point
  45659. * just past the end of the encoded data. */
  45660. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45661. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45662. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45663. }
  45664. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45665. targetBuffer = NULL;
  45666. /* If target buffer is null, function allocates memory for a buffer and
  45667. * copies the encoded data into it. targetBuffer then points to the start of
  45668. * this newly allocate buffer. */
  45669. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45670. sizeof(rawCertId));
  45671. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45672. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45673. }
  45674. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45675. #endif
  45676. return EXPECT_RESULT();
  45677. }
  45678. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45679. {
  45680. EXPECT_DECLS;
  45681. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45682. WOLFSSL_OCSP_CERTID* certId;
  45683. WOLFSSL_OCSP_CERTID* certIdGood;
  45684. WOLFSSL_OCSP_CERTID* certIdBad;
  45685. const unsigned char* rawCertIdPtr;
  45686. const unsigned char rawCertId[] = {
  45687. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45688. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45689. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45690. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45691. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45692. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45693. 0xcf, 0xbc, 0x91
  45694. };
  45695. rawCertIdPtr = &rawCertId[0];
  45696. /* If the cert ID is NULL the function should allocate it and copy the
  45697. * data to it. */
  45698. certId = NULL;
  45699. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45700. sizeof(rawCertId)));
  45701. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45702. if (certId != NULL) {
  45703. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45704. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45705. }
  45706. /* If the cert ID is not NULL the function will just copy the data to it. */
  45707. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45708. DYNAMIC_TYPE_TMP_BUFFER));
  45709. ExpectNotNull(certId);
  45710. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  45711. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45712. rawCertIdPtr = &rawCertId[0];
  45713. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45714. sizeof(rawCertId)));
  45715. ExpectPtrEq(certIdGood, certId);
  45716. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45717. if (certId != NULL) {
  45718. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45719. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45720. certId = NULL;
  45721. }
  45722. /* The below tests should fail when passed bad parameters. NULL should
  45723. * always be returned. */
  45724. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  45725. sizeof(rawCertId)));
  45726. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  45727. sizeof(rawCertId)));
  45728. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  45729. #endif
  45730. return EXPECT_RESULT();
  45731. }
  45732. static int test_wolfSSL_OCSP_id_cmp(void)
  45733. {
  45734. EXPECT_DECLS;
  45735. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45736. OCSP_CERTID id1;
  45737. OCSP_CERTID id2;
  45738. XMEMSET(&id1, 0, sizeof(id1));
  45739. XMEMSET(&id2, 0, sizeof(id2));
  45740. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45741. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45742. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  45743. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  45744. #endif
  45745. return EXPECT_RESULT();
  45746. }
  45747. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45748. {
  45749. EXPECT_DECLS;
  45750. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45751. WOLFSSL_OCSP_SINGLERESP single;
  45752. const WOLFSSL_OCSP_CERTID* certId;
  45753. XMEMSET(&single, 0, sizeof(single));
  45754. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45755. ExpectPtrEq(&single, certId);
  45756. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  45757. #endif
  45758. return EXPECT_RESULT();
  45759. }
  45760. static int test_wolfSSL_OCSP_single_get0_status(void)
  45761. {
  45762. EXPECT_DECLS;
  45763. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45764. WOLFSSL_OCSP_SINGLERESP single;
  45765. CertStatus certStatus;
  45766. WOLFSSL_ASN1_TIME* thisDate;
  45767. WOLFSSL_ASN1_TIME* nextDate;
  45768. int ret, i;
  45769. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45770. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45771. /* Fill the date fields with some dummy data. */
  45772. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45773. certStatus.thisDateParsed.data[i] = i;
  45774. certStatus.nextDateParsed.data[i] = i;
  45775. }
  45776. certStatus.status = CERT_GOOD;
  45777. single.status = &certStatus;
  45778. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45779. &nextDate);
  45780. ExpectIntEQ(ret, CERT_GOOD);
  45781. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  45782. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  45783. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  45784. CERT_GOOD);
  45785. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  45786. NULL), CERT_GOOD);
  45787. #endif
  45788. return EXPECT_RESULT();
  45789. }
  45790. static int test_wolfSSL_OCSP_resp_count(void)
  45791. {
  45792. EXPECT_DECLS;
  45793. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45794. WOLFSSL_OCSP_BASICRESP basicResp;
  45795. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45796. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45797. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45798. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45799. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45800. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  45801. basicResp.single = &singleRespOne;
  45802. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  45803. singleRespOne.next = &singleRespTwo;
  45804. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  45805. #endif
  45806. return EXPECT_RESULT();
  45807. }
  45808. static int test_wolfSSL_OCSP_resp_get0(void)
  45809. {
  45810. EXPECT_DECLS;
  45811. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45812. WOLFSSL_OCSP_BASICRESP basicResp;
  45813. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45814. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45815. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45816. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45817. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45818. basicResp.single = &singleRespOne;
  45819. singleRespOne.next = &singleRespTwo;
  45820. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  45821. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  45822. #endif
  45823. return EXPECT_RESULT();
  45824. }
  45825. static int test_wolfSSL_EVP_PKEY_derive(void)
  45826. {
  45827. EXPECT_DECLS;
  45828. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45829. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45830. EVP_PKEY_CTX *ctx = NULL;
  45831. unsigned char *skey = NULL;
  45832. size_t skeylen;
  45833. EVP_PKEY *pkey = NULL;
  45834. EVP_PKEY *peerkey = NULL;
  45835. const unsigned char* key;
  45836. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45837. /* DH */
  45838. key = dh_key_der_2048;
  45839. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45840. sizeof_dh_key_der_2048)));
  45841. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45842. key = dh_key_der_2048;
  45843. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45844. sizeof_dh_key_der_2048)));
  45845. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45846. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45847. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45848. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45849. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45850. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45851. DYNAMIC_TYPE_OPENSSL));
  45852. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45853. EVP_PKEY_CTX_free(ctx);
  45854. ctx = NULL;
  45855. EVP_PKEY_free(peerkey);
  45856. peerkey = NULL;
  45857. EVP_PKEY_free(pkey);
  45858. pkey = NULL;
  45859. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45860. skey = NULL;
  45861. #endif
  45862. #ifdef HAVE_ECC
  45863. /* ECDH */
  45864. key = ecc_clikey_der_256;
  45865. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45866. sizeof_ecc_clikey_der_256)));
  45867. key = ecc_clikeypub_der_256;
  45868. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45869. sizeof_ecc_clikeypub_der_256)));
  45870. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45871. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45872. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45873. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45874. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45875. DYNAMIC_TYPE_OPENSSL));
  45876. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45877. EVP_PKEY_CTX_free(ctx);
  45878. EVP_PKEY_free(peerkey);
  45879. EVP_PKEY_free(pkey);
  45880. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45881. #endif /* HAVE_ECC */
  45882. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45883. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45884. return EXPECT_RESULT();
  45885. }
  45886. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45887. {
  45888. EXPECT_DECLS;
  45889. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45890. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45891. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45892. int ret;
  45893. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45894. int pwdlen = sizeof(pwd);
  45895. const byte salt[] = {'N','a','C','l'};
  45896. int saltlen = sizeof(salt);
  45897. byte key[80];
  45898. word64 numOvr32 = (word64)INT32_MAX + 1;
  45899. /* expected derived key for N:16, r:1, p:1 */
  45900. const byte expectedKey[] = {
  45901. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45902. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45903. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  45904. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  45905. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  45906. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  45907. 0xE7, 0xE9, 0xC0, 0x9A};
  45908. /* N r p mx key keylen */
  45909. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  45910. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  45911. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  45912. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  45913. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  45914. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  45915. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  45916. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  45917. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  45918. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  45919. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  45920. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  45921. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  45922. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  45923. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  45924. ExpectIntEQ(ret, 1); /* should succeed */
  45925. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  45926. key, 64);
  45927. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  45928. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  45929. key, 64);
  45930. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  45931. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  45932. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  45933. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  45934. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  45935. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  45936. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  45937. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  45938. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  45939. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  45940. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  45941. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  45942. ExpectIntEQ(ret, 1);
  45943. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  45944. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  45945. #endif /* !NO_PWDBASED && !NO_SHA256 */
  45946. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  45947. return EXPECT_RESULT();
  45948. }
  45949. static int test_no_op_functions(void)
  45950. {
  45951. EXPECT_DECLS;
  45952. #if defined(OPENSSL_EXTRA)
  45953. /* this makes sure wolfSSL can compile and run these no-op functions */
  45954. SSL_load_error_strings();
  45955. ENGINE_load_builtin_engines();
  45956. OpenSSL_add_all_ciphers();
  45957. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  45958. #endif
  45959. return EXPECT_RESULT();
  45960. }
  45961. static int test_wolfSSL_CRYPTO_memcmp(void)
  45962. {
  45963. EXPECT_DECLS;
  45964. #ifdef OPENSSL_EXTRA
  45965. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45966. "implementation of TLS/SSL for embedded devices to the cloud.";
  45967. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45968. "implementation of TLS/SSL for embedded devices to the cloud.";
  45969. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45970. "implementation of TLS/SSL for embedded devices to the cloud!";
  45971. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  45972. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  45973. #endif
  45974. return EXPECT_RESULT();
  45975. }
  45976. /*----------------------------------------------------------------------------*
  45977. | wolfCrypt ASN
  45978. *----------------------------------------------------------------------------*/
  45979. static int test_wc_CreateEncryptedPKCS8Key(void)
  45980. {
  45981. EXPECT_DECLS;
  45982. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  45983. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  45984. WC_RNG rng;
  45985. byte* encKey = NULL;
  45986. word32 encKeySz = 0;
  45987. word32 decKeySz = 0;
  45988. const char password[] = "Lorem ipsum dolor sit amet";
  45989. word32 passwordSz = (word32)XSTRLEN(password);
  45990. word32 tradIdx = 0;
  45991. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45992. ExpectIntEQ(wc_InitRng(&rng), 0);
  45993. PRIVATE_KEY_UNLOCK();
  45994. /* Call with NULL for out buffer to get necessary length. */
  45995. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45996. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  45997. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45998. LENGTH_ONLY_E);
  45999. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46000. DYNAMIC_TYPE_TMP_BUFFER));
  46001. /* Call with the allocated out buffer. */
  46002. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46003. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  46004. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46005. 0);
  46006. /* Decrypt the encrypted PKCS8 key we just made. */
  46007. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46008. passwordSz)), 0);
  46009. /* encKey now holds the decrypted key (decrypted in place). */
  46010. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46011. /* Check that the decrypted key matches the key prior to encryption. */
  46012. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46013. sizeof_server_key_der_2048), 0);
  46014. PRIVATE_KEY_LOCK();
  46015. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46016. wc_FreeRng(&rng);
  46017. #endif
  46018. return EXPECT_RESULT();
  46019. }
  46020. static int test_wc_GetPkcs8TraditionalOffset(void)
  46021. {
  46022. EXPECT_DECLS;
  46023. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46024. int length;
  46025. int derSz = 0;
  46026. word32 inOutIdx;
  46027. const char* path = "./certs/server-keyPkcs8.der";
  46028. XFILE file = XBADFILE;
  46029. byte der[2048];
  46030. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46031. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46032. if (file != XBADFILE)
  46033. XFCLOSE(file);
  46034. /* valid case */
  46035. inOutIdx = 0;
  46036. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46037. 0);
  46038. /* inOutIdx > sz */
  46039. inOutIdx = 4000;
  46040. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46041. BAD_FUNC_ARG);
  46042. /* null input */
  46043. inOutIdx = 0;
  46044. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46045. BAD_FUNC_ARG);
  46046. /* invalid input, fill buffer with 1's */
  46047. XMEMSET(der, 1, sizeof(der));
  46048. inOutIdx = 0;
  46049. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46050. ASN_PARSE_E);
  46051. #endif /* NO_ASN */
  46052. return EXPECT_RESULT();
  46053. }
  46054. static int test_wc_SetSubjectRaw(void)
  46055. {
  46056. EXPECT_DECLS;
  46057. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46058. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46059. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46060. WOLFSSL_X509* x509 = NULL;
  46061. int peerCertSz;
  46062. const byte* peerCertBuf = NULL;
  46063. Cert forgedCert;
  46064. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46065. WOLFSSL_FILETYPE_ASN1));
  46066. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46067. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46068. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46069. wolfSSL_FreeX509(x509);
  46070. #endif
  46071. return EXPECT_RESULT();
  46072. }
  46073. static int test_wc_GetSubjectRaw(void)
  46074. {
  46075. EXPECT_DECLS;
  46076. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46077. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46078. Cert cert;
  46079. byte *subjectRaw;
  46080. ExpectIntEQ(0, wc_InitCert(&cert));
  46081. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46082. #endif
  46083. return EXPECT_RESULT();
  46084. }
  46085. static int test_wc_SetIssuerRaw(void)
  46086. {
  46087. EXPECT_DECLS;
  46088. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46089. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46090. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46091. WOLFSSL_X509* x509 = NULL;
  46092. int peerCertSz;
  46093. const byte* peerCertBuf;
  46094. Cert forgedCert;
  46095. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46096. WOLFSSL_FILETYPE_ASN1));
  46097. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46098. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46099. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46100. wolfSSL_FreeX509(x509);
  46101. #endif
  46102. return EXPECT_RESULT();
  46103. }
  46104. static int test_wc_SetIssueBuffer(void)
  46105. {
  46106. EXPECT_DECLS;
  46107. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46108. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46109. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46110. WOLFSSL_X509* x509 = NULL;
  46111. int peerCertSz;
  46112. const byte* peerCertBuf;
  46113. Cert forgedCert;
  46114. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46115. WOLFSSL_FILETYPE_ASN1));
  46116. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46117. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46118. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46119. wolfSSL_FreeX509(x509);
  46120. #endif
  46121. return EXPECT_RESULT();
  46122. }
  46123. /*
  46124. * Testing wc_SetSubjectKeyId
  46125. */
  46126. static int test_wc_SetSubjectKeyId(void)
  46127. {
  46128. EXPECT_DECLS;
  46129. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46130. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46131. Cert cert;
  46132. const char* file = "certs/ecc-client-keyPub.pem";
  46133. ExpectIntEQ(0, wc_InitCert(&cert));
  46134. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46135. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46136. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46137. #endif
  46138. return EXPECT_RESULT();
  46139. } /* END test_wc_SetSubjectKeyId */
  46140. /*
  46141. * Testing wc_SetSubject
  46142. */
  46143. static int test_wc_SetSubject(void)
  46144. {
  46145. EXPECT_DECLS;
  46146. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46147. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46148. Cert cert;
  46149. const char* file = "./certs/ca-ecc-cert.pem";
  46150. ExpectIntEQ(0, wc_InitCert(&cert));
  46151. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46152. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46153. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46154. #endif
  46155. return EXPECT_RESULT();
  46156. } /* END test_wc_SetSubject */
  46157. static int test_CheckCertSignature(void)
  46158. {
  46159. EXPECT_DECLS;
  46160. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46161. WOLFSSL_CERT_MANAGER* cm = NULL;
  46162. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46163. XFILE fp = XBADFILE;
  46164. byte cert[4096];
  46165. int certSz;
  46166. #endif
  46167. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  46168. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46169. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  46170. #ifndef NO_RSA
  46171. #ifdef USE_CERT_BUFFERS_1024
  46172. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  46173. sizeof_server_cert_der_1024, NULL, cm));
  46174. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46175. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46176. WOLFSSL_FILETYPE_ASN1));
  46177. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  46178. sizeof_server_cert_der_1024, NULL, cm));
  46179. #elif defined(USE_CERT_BUFFERS_2048)
  46180. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  46181. sizeof_server_cert_der_2048, NULL, cm));
  46182. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46183. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46184. WOLFSSL_FILETYPE_ASN1));
  46185. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  46186. sizeof_server_cert_der_2048, NULL, cm));
  46187. #endif
  46188. #endif
  46189. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46190. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  46191. sizeof_serv_ecc_der_256, NULL, cm));
  46192. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46193. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  46194. WOLFSSL_FILETYPE_ASN1));
  46195. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  46196. NULL, cm));
  46197. #endif
  46198. #if !defined(NO_FILESYSTEM)
  46199. wolfSSL_CertManagerFree(cm);
  46200. cm = NULL;
  46201. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46202. #ifndef NO_RSA
  46203. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  46204. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46205. if (fp != XBADFILE) {
  46206. XFCLOSE(fp);
  46207. fp = XBADFILE;
  46208. }
  46209. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46210. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46211. "./certs/ca-cert.pem", NULL));
  46212. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46213. #endif
  46214. #ifdef HAVE_ECC
  46215. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  46216. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46217. if (fp != XBADFILE) {
  46218. XFCLOSE(fp);
  46219. fp = XBADFILE;
  46220. }
  46221. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46222. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46223. "./certs/ca-ecc-cert.pem", NULL));
  46224. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46225. #endif
  46226. #endif
  46227. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46228. (void)fp;
  46229. (void)cert;
  46230. (void)certSz;
  46231. #endif
  46232. wolfSSL_CertManagerFree(cm);
  46233. #endif
  46234. return EXPECT_RESULT();
  46235. }
  46236. static int test_wc_ParseCert(void)
  46237. {
  46238. EXPECT_DECLS;
  46239. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46240. DecodedCert decodedCert;
  46241. const byte* rawCert = client_cert_der_2048;
  46242. const int rawCertSize = sizeof_client_cert_der_2048;
  46243. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  46244. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46245. #ifndef IGNORE_NAME_CONSTRAINTS
  46246. /* check that the subjects emailAddress was not put in the alt name list */
  46247. ExpectNotNull(decodedCert.subjectEmail);
  46248. ExpectNull(decodedCert.altEmailNames);
  46249. #endif
  46250. wc_FreeDecodedCert(&decodedCert);
  46251. #endif
  46252. return EXPECT_RESULT();
  46253. }
  46254. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  46255. * the API safely errors out on badly-formed ASN input.
  46256. * NOTE: Test not compatible with released FIPS implementations!
  46257. */
  46258. static int test_wc_ParseCert_Error(void)
  46259. {
  46260. EXPECT_DECLS;
  46261. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  46262. (!defined(HAVE_FIPS) || \
  46263. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  46264. DecodedCert decodedCert;
  46265. int i;
  46266. /* Certificate data */
  46267. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  46268. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46269. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46270. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  46271. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  46272. /* Test data */
  46273. const struct testStruct {
  46274. const byte* c;
  46275. const int cSz;
  46276. const int expRet;
  46277. } t[] = {
  46278. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  46279. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  46280. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  46281. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  46282. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  46283. };
  46284. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  46285. for (i = 0; i < tSz; i++) {
  46286. WOLFSSL_MSG_EX("i == %d", i);
  46287. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  46288. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  46289. wc_FreeDecodedCert(&decodedCert);
  46290. }
  46291. #endif
  46292. return EXPECT_RESULT();
  46293. }
  46294. static int test_MakeCertWithPathLen(void)
  46295. {
  46296. EXPECT_DECLS;
  46297. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  46298. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46299. const byte expectedPathLen = 7;
  46300. Cert cert;
  46301. DecodedCert decodedCert;
  46302. byte der[FOURK_BUF];
  46303. int derSize = 0;
  46304. WC_RNG rng;
  46305. ecc_key key;
  46306. int ret;
  46307. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46308. XMEMSET(&key, 0, sizeof(ecc_key));
  46309. XMEMSET(&cert, 0, sizeof(Cert));
  46310. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46311. ExpectIntEQ(wc_InitRng(&rng), 0);
  46312. ExpectIntEQ(wc_ecc_init(&key), 0);
  46313. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46314. ExpectIntEQ(wc_InitCert(&cert), 0);
  46315. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46316. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46317. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46318. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46319. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46320. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46321. CTC_NAME_SIZE);
  46322. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46323. CTC_NAME_SIZE);
  46324. cert.selfSigned = 1;
  46325. cert.isCA = 1;
  46326. cert.pathLen = expectedPathLen;
  46327. cert.pathLenSet = 1;
  46328. cert.sigType = CTC_SHA256wECDSA;
  46329. #ifdef WOLFSSL_CERT_EXT
  46330. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  46331. #endif
  46332. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46333. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46334. FOURK_BUF, NULL, &key, &rng), 0);
  46335. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46336. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46337. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  46338. wc_FreeDecodedCert(&decodedCert);
  46339. ret = wc_ecc_free(&key);
  46340. ExpectIntEQ(ret, 0);
  46341. ret = wc_FreeRng(&rng);
  46342. ExpectIntEQ(ret, 0);
  46343. #endif
  46344. return EXPECT_RESULT();
  46345. }
  46346. static int test_MakeCertWithCaFalse(void)
  46347. {
  46348. EXPECT_DECLS;
  46349. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  46350. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46351. const byte expectedIsCa = 0;
  46352. Cert cert;
  46353. DecodedCert decodedCert;
  46354. byte der[FOURK_BUF];
  46355. int derSize = 0;
  46356. WC_RNG rng;
  46357. ecc_key key;
  46358. int ret;
  46359. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46360. XMEMSET(&key, 0, sizeof(ecc_key));
  46361. XMEMSET(&cert, 0, sizeof(Cert));
  46362. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46363. ExpectIntEQ(wc_InitRng(&rng), 0);
  46364. ExpectIntEQ(wc_ecc_init(&key), 0);
  46365. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46366. ExpectIntEQ(wc_InitCert(&cert), 0);
  46367. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46368. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46369. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46370. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46371. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46372. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46373. CTC_NAME_SIZE);
  46374. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46375. CTC_NAME_SIZE);
  46376. cert.selfSigned = 1;
  46377. cert.isCA = expectedIsCa;
  46378. cert.isCaSet = 1;
  46379. cert.sigType = CTC_SHA256wECDSA;
  46380. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46381. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46382. FOURK_BUF, NULL, &key, &rng), 0);
  46383. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46384. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46385. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  46386. wc_FreeDecodedCert(&decodedCert);
  46387. ret = wc_ecc_free(&key);
  46388. ExpectIntEQ(ret, 0);
  46389. ret = wc_FreeRng(&rng);
  46390. ExpectIntEQ(ret, 0);
  46391. #endif
  46392. return EXPECT_RESULT();
  46393. }
  46394. /*----------------------------------------------------------------------------*
  46395. | wolfCrypt ECC
  46396. *----------------------------------------------------------------------------*/
  46397. static int test_wc_ecc_get_curve_size_from_name(void)
  46398. {
  46399. EXPECT_DECLS;
  46400. #ifdef HAVE_ECC
  46401. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46402. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  46403. #endif
  46404. /* invalid case */
  46405. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  46406. /* NULL input */
  46407. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  46408. #endif /* HAVE_ECC */
  46409. return EXPECT_RESULT();
  46410. }
  46411. static int test_wc_ecc_get_curve_id_from_name(void)
  46412. {
  46413. EXPECT_DECLS;
  46414. #ifdef HAVE_ECC
  46415. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46416. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  46417. ECC_SECP256R1);
  46418. #endif
  46419. /* invalid case */
  46420. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  46421. /* NULL input */
  46422. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  46423. #endif /* HAVE_ECC */
  46424. return EXPECT_RESULT();
  46425. }
  46426. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  46427. !defined(HAVE_SELFTEST) && \
  46428. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  46429. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  46430. {
  46431. EXPECT_DECLS;
  46432. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46433. ecc_key* key;
  46434. const ecc_set_type* params = NULL;
  46435. int ret;
  46436. #endif
  46437. WOLFSSL_EC_KEY *ecKey = NULL;
  46438. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46439. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  46440. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  46441. if (EXPECT_SUCCESS()) {
  46442. ret = EC_KEY_generate_key(ecKey);
  46443. } else
  46444. ret = 0;
  46445. if (ret == 1) {
  46446. /* normal test */
  46447. key = (ecc_key*)ecKey->internal;
  46448. if (key != NULL) {
  46449. params = key->dp;
  46450. }
  46451. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  46452. ECC_SECP256R1);
  46453. }
  46454. #endif
  46455. /* invalid case, NULL input*/
  46456. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  46457. wolfSSL_EC_KEY_free(ecKey);
  46458. return EXPECT_RESULT();
  46459. }
  46460. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46461. static int test_wc_ecc_get_curve_id_from_params(void)
  46462. {
  46463. EXPECT_DECLS;
  46464. #ifdef HAVE_ECC
  46465. const byte prime[] =
  46466. {
  46467. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46468. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46469. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46470. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  46471. };
  46472. const byte primeInvalid[] =
  46473. {
  46474. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46476. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46477. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  46478. };
  46479. const byte Af[] =
  46480. {
  46481. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46483. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46484. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  46485. };
  46486. const byte Bf[] =
  46487. {
  46488. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  46489. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  46490. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  46491. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  46492. };
  46493. const byte order[] =
  46494. {
  46495. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  46496. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46497. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  46498. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  46499. };
  46500. const byte Gx[] =
  46501. {
  46502. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  46503. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  46504. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  46505. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  46506. };
  46507. const byte Gy[] =
  46508. {
  46509. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  46510. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  46511. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  46512. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  46513. };
  46514. int cofactor = 1;
  46515. int fieldSize = 256;
  46516. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46517. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46518. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  46519. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  46520. ECC_SECP256R1);
  46521. #endif
  46522. /* invalid case, fieldSize = 0 */
  46523. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  46524. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46525. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46526. /* invalid case, NULL prime */
  46527. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  46528. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46529. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  46530. /* invalid case, invalid prime */
  46531. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46532. primeInvalid, sizeof(primeInvalid),
  46533. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46534. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46535. #endif
  46536. return EXPECT_RESULT();
  46537. }
  46538. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  46539. {
  46540. EXPECT_DECLS;
  46541. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46542. WOLFSSL_RSA* rsa = NULL;
  46543. WOLFSSL_EVP_PKEY* pkey = NULL;
  46544. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46545. const char* in = "What is easy to do is easy not to do.";
  46546. size_t inlen = XSTRLEN(in);
  46547. size_t outEncLen = 0;
  46548. byte* outEnc = NULL;
  46549. byte* outDec = NULL;
  46550. size_t outDecLen = 0;
  46551. size_t rsaKeySz = 2048/8; /* Bytes */
  46552. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46553. byte* inTmp = NULL;
  46554. byte* outEncTmp = NULL;
  46555. byte* outDecTmp = NULL;
  46556. #endif
  46557. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46558. DYNAMIC_TYPE_TMP_BUFFER));
  46559. if (outEnc != NULL) {
  46560. XMEMSET(outEnc, 0, rsaKeySz);
  46561. }
  46562. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46563. DYNAMIC_TYPE_TMP_BUFFER));
  46564. if (outDec != NULL) {
  46565. XMEMSET(outDec, 0, rsaKeySz);
  46566. }
  46567. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46568. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46569. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46570. if (EXPECT_FAIL()) {
  46571. RSA_free(rsa);
  46572. }
  46573. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46574. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46575. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46576. WOLFSSL_SUCCESS);
  46577. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  46578. since ctx uses it.*/
  46579. ExpectIntEQ(pkey->ref.count, 2);
  46580. EVP_PKEY_free(pkey);
  46581. ExpectIntEQ(pkey->ref.count, 1);
  46582. /* Encrypt data */
  46583. /* Check that we can get the required output buffer length by passing in a
  46584. * NULL output buffer. */
  46585. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  46586. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46587. ExpectIntEQ(rsaKeySz, outEncLen);
  46588. /* Now do the actual encryption. */
  46589. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  46590. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46591. /* Decrypt data */
  46592. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46593. /* Check that we can get the required output buffer length by passing in a
  46594. * NULL output buffer. */
  46595. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  46596. WOLFSSL_SUCCESS);
  46597. ExpectIntEQ(rsaKeySz, outDecLen);
  46598. /* Now do the actual decryption. */
  46599. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  46600. WOLFSSL_SUCCESS);
  46601. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  46602. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46603. /* The input length must be the same size as the RSA key.*/
  46604. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46605. DYNAMIC_TYPE_TMP_BUFFER));
  46606. if (inTmp != NULL) {
  46607. XMEMSET(inTmp, 9, rsaKeySz);
  46608. }
  46609. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46610. DYNAMIC_TYPE_TMP_BUFFER));
  46611. if (outEncTmp != NULL) {
  46612. XMEMSET(outEncTmp, 0, rsaKeySz);
  46613. }
  46614. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46615. DYNAMIC_TYPE_TMP_BUFFER));
  46616. if (outDecTmp != NULL) {
  46617. XMEMSET(outDecTmp, 0, rsaKeySz);
  46618. }
  46619. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46620. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46621. WOLFSSL_SUCCESS);
  46622. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  46623. WOLFSSL_SUCCESS);
  46624. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46625. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  46626. outEncLen), WOLFSSL_SUCCESS);
  46627. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  46628. #endif
  46629. EVP_PKEY_CTX_free(ctx);
  46630. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46631. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46632. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46633. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46634. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46635. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46636. #endif
  46637. #endif
  46638. return EXPECT_RESULT();
  46639. }
  46640. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46641. !defined(HAVE_SELFTEST)
  46642. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46643. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46644. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46645. #endif
  46646. #endif
  46647. #endif
  46648. #if defined(OPENSSL_EXTRA)
  46649. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46650. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46651. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46652. #endif
  46653. #endif
  46654. #endif
  46655. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46656. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46657. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46658. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46659. #endif
  46660. #endif
  46661. #endif
  46662. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46663. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46664. {
  46665. EXPECT_DECLS;
  46666. #if defined(OPENSSL_EXTRA)
  46667. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46668. !defined(HAVE_SELFTEST)
  46669. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46670. WOLFSSL_RSA* rsa = NULL;
  46671. #endif
  46672. #endif
  46673. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46674. WOLFSSL_DSA* dsa = NULL;
  46675. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46676. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46677. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46678. WOLFSSL_EC_KEY* ecKey = NULL;
  46679. #endif
  46680. #endif
  46681. WOLFSSL_EVP_PKEY* pkey = NULL;
  46682. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46683. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46684. const char* in = "What is easy to do is easy not to do.";
  46685. size_t inlen = XSTRLEN(in);
  46686. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46687. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46688. SHA256_CTX c;
  46689. byte* sig = NULL;
  46690. byte* sigVerify = NULL;
  46691. size_t siglen;
  46692. size_t siglenOnlyLen;
  46693. size_t keySz = 2048/8; /* Bytes */
  46694. ExpectNotNull(sig =
  46695. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46696. ExpectNotNull(sigVerify =
  46697. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46698. siglen = keySz;
  46699. ExpectNotNull(XMEMSET(sig, 0, keySz));
  46700. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  46701. /* Generate hash */
  46702. SHA256_Init(&c);
  46703. SHA256_Update(&c, in, inlen);
  46704. SHA256_Final(hash, &c);
  46705. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46706. /* workaround for small stack cache case */
  46707. wc_Sha256Free((wc_Sha256*)&c);
  46708. #endif
  46709. /* Generate key */
  46710. ExpectNotNull(pkey = EVP_PKEY_new());
  46711. switch (keyType) {
  46712. case EVP_PKEY_RSA:
  46713. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46714. !defined(HAVE_SELFTEST)
  46715. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46716. {
  46717. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46718. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46719. }
  46720. #endif
  46721. #endif
  46722. break;
  46723. case EVP_PKEY_DSA:
  46724. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46725. ExpectNotNull(dsa = DSA_new());
  46726. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46727. NULL, 0, NULL, NULL, NULL), 1);
  46728. ExpectIntEQ(DSA_generate_key(dsa), 1);
  46729. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46730. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46731. break;
  46732. case EVP_PKEY_EC:
  46733. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46734. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46735. {
  46736. ExpectNotNull(ecKey = EC_KEY_new());
  46737. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  46738. ExpectIntEQ(
  46739. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46740. if (EXPECT_FAIL()) {
  46741. EC_KEY_free(ecKey);
  46742. }
  46743. }
  46744. #endif
  46745. #endif
  46746. break;
  46747. }
  46748. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46749. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46750. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46751. !defined(HAVE_SELFTEST)
  46752. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46753. if (keyType == EVP_PKEY_RSA)
  46754. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46755. WOLFSSL_SUCCESS);
  46756. #endif
  46757. #endif
  46758. /* Check returning only length */
  46759. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46760. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46761. ExpectIntGT(siglenOnlyLen, 0);
  46762. /* Sign data */
  46763. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46764. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46765. ExpectIntGE(siglenOnlyLen, siglen);
  46766. /* Verify signature */
  46767. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46768. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46769. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46770. !defined(HAVE_SELFTEST)
  46771. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46772. if (keyType == EVP_PKEY_RSA)
  46773. ExpectIntEQ(
  46774. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46775. WOLFSSL_SUCCESS);
  46776. #endif
  46777. #endif
  46778. ExpectIntEQ(EVP_PKEY_verify(
  46779. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46780. WOLFSSL_SUCCESS);
  46781. ExpectIntEQ(EVP_PKEY_verify(
  46782. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46783. WOLFSSL_FAILURE);
  46784. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46785. !defined(HAVE_SELFTEST)
  46786. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46787. if (keyType == EVP_PKEY_RSA) {
  46788. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46789. /* Try RSA sign/verify with no padding. */
  46790. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46791. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46792. WOLFSSL_SUCCESS);
  46793. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46794. siglen), WOLFSSL_SUCCESS);
  46795. ExpectIntGE(siglenOnlyLen, siglen);
  46796. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46797. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46798. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46799. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46800. siglen), WOLFSSL_SUCCESS);
  46801. #endif
  46802. /* Wrong padding schemes. */
  46803. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46804. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46805. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46806. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46807. siglen), WOLFSSL_SUCCESS);
  46808. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46809. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46810. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46811. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46812. siglen), WOLFSSL_SUCCESS);
  46813. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46814. WOLFSSL_SUCCESS);
  46815. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46816. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46817. }
  46818. #endif
  46819. #endif
  46820. /* error cases */
  46821. siglen = keySz; /* Reset because sig size may vary slightly */
  46822. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46823. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46824. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46825. WOLFSSL_SUCCESS);
  46826. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46827. WOLFSSL_SUCCESS);
  46828. EVP_PKEY_free(pkey);
  46829. pkey = NULL;
  46830. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46831. DSA_free(dsa);
  46832. dsa = NULL;
  46833. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46834. EVP_PKEY_CTX_free(ctx_verify);
  46835. ctx_verify = NULL;
  46836. EVP_PKEY_CTX_free(ctx);
  46837. ctx = NULL;
  46838. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46839. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46840. #endif /* OPENSSL_EXTRA */
  46841. return EXPECT_RESULT();
  46842. }
  46843. #endif
  46844. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  46845. {
  46846. EXPECT_DECLS;
  46847. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46848. !defined(HAVE_SELFTEST)
  46849. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46850. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  46851. #endif
  46852. #endif
  46853. return EXPECT_RESULT();
  46854. }
  46855. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  46856. {
  46857. EXPECT_DECLS;
  46858. #if defined(OPENSSL_EXTRA)
  46859. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46860. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  46861. #endif
  46862. #endif
  46863. return EXPECT_RESULT();
  46864. }
  46865. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  46866. {
  46867. EXPECT_DECLS;
  46868. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46869. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46870. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  46871. #endif
  46872. #endif
  46873. return EXPECT_RESULT();
  46874. }
  46875. static int test_EVP_PKEY_rsa(void)
  46876. {
  46877. EXPECT_DECLS;
  46878. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46879. WOLFSSL_RSA* rsa = NULL;
  46880. WOLFSSL_EVP_PKEY* pkey = NULL;
  46881. ExpectNotNull(rsa = wolfSSL_RSA_new());
  46882. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46883. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46884. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46885. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46886. if (EXPECT_FAIL()) {
  46887. wolfSSL_RSA_free(rsa);
  46888. }
  46889. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46890. wolfSSL_EVP_PKEY_free(pkey);
  46891. #endif
  46892. return EXPECT_RESULT();
  46893. }
  46894. static int test_EVP_PKEY_ec(void)
  46895. {
  46896. EXPECT_DECLS;
  46897. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46898. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46899. WOLFSSL_EC_KEY* ecKey = NULL;
  46900. WOLFSSL_EVP_PKEY* pkey = NULL;
  46901. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  46902. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46903. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  46904. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  46905. /* Should fail since ecKey is empty */
  46906. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  46907. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  46908. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46909. if (EXPECT_FAIL()) {
  46910. wolfSSL_EC_KEY_free(ecKey);
  46911. }
  46912. wolfSSL_EVP_PKEY_free(pkey);
  46913. #endif
  46914. #endif
  46915. return EXPECT_RESULT();
  46916. }
  46917. static int test_EVP_PKEY_cmp(void)
  46918. {
  46919. EXPECT_DECLS;
  46920. #if defined(OPENSSL_EXTRA)
  46921. EVP_PKEY *a = NULL;
  46922. EVP_PKEY *b = NULL;
  46923. const unsigned char *in;
  46924. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46925. in = client_key_der_2048;
  46926. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46927. &in, (long)sizeof_client_key_der_2048));
  46928. in = client_key_der_2048;
  46929. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46930. &in, (long)sizeof_client_key_der_2048));
  46931. /* Test success case RSA */
  46932. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46933. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46934. #else
  46935. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46936. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46937. EVP_PKEY_free(b);
  46938. b = NULL;
  46939. EVP_PKEY_free(a);
  46940. a = NULL;
  46941. #endif
  46942. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46943. in = ecc_clikey_der_256;
  46944. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46945. &in, (long)sizeof_ecc_clikey_der_256));
  46946. in = ecc_clikey_der_256;
  46947. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46948. &in, (long)sizeof_ecc_clikey_der_256));
  46949. /* Test success case ECC */
  46950. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46951. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46952. #else
  46953. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46954. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46955. EVP_PKEY_free(b);
  46956. b = NULL;
  46957. EVP_PKEY_free(a);
  46958. a = NULL;
  46959. #endif
  46960. /* Test failure cases */
  46961. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  46962. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46963. in = client_key_der_2048;
  46964. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46965. &in, (long)sizeof_client_key_der_2048));
  46966. in = ecc_clikey_der_256;
  46967. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46968. &in, (long)sizeof_ecc_clikey_der_256));
  46969. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46970. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  46971. #else
  46972. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46973. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46974. EVP_PKEY_free(b);
  46975. b = NULL;
  46976. EVP_PKEY_free(a);
  46977. a = NULL;
  46978. #endif
  46979. /* invalid or empty failure cases */
  46980. a = EVP_PKEY_new();
  46981. b = EVP_PKEY_new();
  46982. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46983. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  46984. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  46985. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  46986. #ifdef NO_RSA
  46987. /* Type check will fail since RSA is the default EVP key type */
  46988. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  46989. #else
  46990. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46991. #endif
  46992. #else
  46993. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  46994. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  46995. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  46996. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46997. #endif
  46998. EVP_PKEY_free(b);
  46999. EVP_PKEY_free(a);
  47000. (void)in;
  47001. #endif
  47002. return EXPECT_RESULT();
  47003. }
  47004. static int test_ERR_load_crypto_strings(void)
  47005. {
  47006. #if defined(OPENSSL_ALL)
  47007. ERR_load_crypto_strings();
  47008. return TEST_SUCCESS;
  47009. #else
  47010. return TEST_SKIPPED;
  47011. #endif
  47012. }
  47013. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47014. static void free_x509(X509* x)
  47015. {
  47016. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47017. }
  47018. #endif
  47019. static int test_sk_X509(void)
  47020. {
  47021. EXPECT_DECLS;
  47022. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47023. {
  47024. STACK_OF(X509)* s = NULL;
  47025. ExpectNotNull(s = sk_X509_new_null());
  47026. ExpectIntEQ(sk_X509_num(s), 0);
  47027. sk_X509_pop_free(s, NULL);
  47028. ExpectNotNull(s = sk_X509_new_null());
  47029. ExpectIntEQ(sk_X509_num(s), 0);
  47030. sk_X509_pop_free(s, NULL);
  47031. ExpectNotNull(s = sk_X509_new_null());
  47032. sk_X509_push(s, (X509*)1);
  47033. ExpectIntEQ(sk_X509_num(s), 1);
  47034. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47035. sk_X509_push(s, (X509*)2);
  47036. ExpectIntEQ(sk_X509_num(s), 2);
  47037. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47038. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47039. sk_X509_push(s, (X509*)2);
  47040. sk_X509_pop_free(s, free_x509);
  47041. }
  47042. {
  47043. /* Push a list of 10 X509s onto stack, then verify that
  47044. * value(), push(), shift(), and pop() behave as expected. */
  47045. STACK_OF(X509)* s = NULL;
  47046. X509* xList[10];
  47047. int i = 0;
  47048. const int len = (sizeof(xList) / sizeof(xList[0]));
  47049. for (i = 0; i < len; ++i) {
  47050. xList[i] = NULL;
  47051. ExpectNotNull(xList[i] = X509_new());
  47052. }
  47053. /* test push, pop, and free */
  47054. ExpectNotNull(s = sk_X509_new_null());
  47055. for (i = 0; i < len; ++i) {
  47056. sk_X509_push(s, xList[i]);
  47057. ExpectIntEQ(sk_X509_num(s), i + 1);
  47058. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47059. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47060. }
  47061. /* pop returns and removes last pushed on stack, which is index 0
  47062. * in sk_x509_value */
  47063. for (i = 0; i < len; ++i) {
  47064. X509 * x = sk_X509_value(s, 0);
  47065. X509 * y = sk_X509_pop(s);
  47066. X509 * z = xList[len - 1 - i];
  47067. ExpectIntEQ((x == y), 1);
  47068. ExpectIntEQ((x == z), 1);
  47069. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47070. }
  47071. sk_free(s);
  47072. s = NULL;
  47073. /* test push, shift, and free */
  47074. ExpectNotNull(s = sk_X509_new_null());
  47075. for (i = 0; i < len; ++i) {
  47076. sk_X509_push(s, xList[i]);
  47077. ExpectIntEQ(sk_X509_num(s), i + 1);
  47078. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47079. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47080. }
  47081. /* shift returns and removes first pushed on stack, which is index i
  47082. * in sk_x509_value() */
  47083. for (i = 0; i < len; ++i) {
  47084. X509 * x = sk_X509_value(s, len - 1 - i);
  47085. X509 * y = sk_X509_shift(s);
  47086. X509 * z = xList[i];
  47087. ExpectIntEQ((x == y), 1);
  47088. ExpectIntEQ((x == z), 1);
  47089. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47090. }
  47091. sk_free(s);
  47092. for (i = 0; i < len; ++i)
  47093. X509_free(xList[i]);
  47094. }
  47095. #endif
  47096. return EXPECT_RESULT();
  47097. }
  47098. static int test_sk_X509_CRL(void)
  47099. {
  47100. EXPECT_DECLS;
  47101. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47102. X509_CRL* crl = NULL;
  47103. XFILE fp = XBADFILE;
  47104. STACK_OF(X509_CRL)* s = NULL;
  47105. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47106. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47107. NULL, NULL));
  47108. if (fp != XBADFILE)
  47109. XFCLOSE(fp);
  47110. ExpectNotNull(s = sk_X509_CRL_new());
  47111. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47112. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47113. if (EXPECT_FAIL()) {
  47114. X509_CRL_free(crl);
  47115. }
  47116. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47117. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47118. sk_X509_CRL_free(s);
  47119. #endif
  47120. return EXPECT_RESULT();
  47121. }
  47122. static int test_X509_get_signature_nid(void)
  47123. {
  47124. EXPECT_DECLS;
  47125. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47126. X509* x509 = NULL;
  47127. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47128. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47129. SSL_FILETYPE_PEM));
  47130. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47131. X509_free(x509);
  47132. #endif
  47133. return EXPECT_RESULT();
  47134. }
  47135. static int test_X509_REQ(void)
  47136. {
  47137. EXPECT_DECLS;
  47138. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  47139. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  47140. X509_NAME* name = NULL;
  47141. #ifndef NO_RSA
  47142. X509_NAME* subject = NULL;
  47143. #endif
  47144. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47145. X509_REQ* req = NULL;
  47146. EVP_PKEY* priv = NULL;
  47147. EVP_PKEY* pub = NULL;
  47148. unsigned char* der = NULL;
  47149. int len;
  47150. #endif
  47151. #ifndef NO_RSA
  47152. EVP_MD_CTX *mctx = NULL;
  47153. EVP_PKEY_CTX *pkctx = NULL;
  47154. #ifdef USE_CERT_BUFFERS_1024
  47155. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  47156. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  47157. #elif defined(USE_CERT_BUFFERS_2048)
  47158. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  47159. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  47160. #endif
  47161. #endif
  47162. #ifdef HAVE_ECC
  47163. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  47164. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  47165. #endif
  47166. ExpectNotNull(name = X509_NAME_new());
  47167. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  47168. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  47169. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  47170. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  47171. #ifndef NO_RSA
  47172. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  47173. (long)sizeof_client_key_der_2048));
  47174. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  47175. (long)sizeof_client_keypub_der_2048));
  47176. ExpectNotNull(req = X509_REQ_new());
  47177. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  47178. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  47179. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47180. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  47181. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  47182. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47183. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  47184. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  47185. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  47186. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47187. len = i2d_X509_REQ(req, &der);
  47188. DEBUG_WRITE_DER(der, len, "req.der");
  47189. #ifdef USE_CERT_BUFFERS_1024
  47190. ExpectIntEQ(len, 381);
  47191. #else
  47192. ExpectIntEQ(len, 643);
  47193. #endif
  47194. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47195. der = NULL;
  47196. mctx = EVP_MD_CTX_new();
  47197. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  47198. WOLFSSL_SUCCESS);
  47199. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  47200. EVP_MD_CTX_free(mctx);
  47201. mctx = NULL;
  47202. X509_REQ_free(NULL);
  47203. X509_REQ_free(req);
  47204. req = NULL;
  47205. /* Test getting the subject from a newly created X509_REQ */
  47206. ExpectNotNull(req = X509_REQ_new());
  47207. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  47208. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  47209. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  47210. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  47211. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  47212. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  47213. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  47214. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  47215. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  47216. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  47217. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  47218. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  47219. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  47220. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47221. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47222. len = i2d_X509_REQ(req, &der);
  47223. DEBUG_WRITE_DER(der, len, "req2.der");
  47224. #ifdef USE_CERT_BUFFERS_1024
  47225. ExpectIntEQ(len, 435);
  47226. #else
  47227. ExpectIntEQ(len, 696);
  47228. #endif
  47229. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47230. der = NULL;
  47231. EVP_PKEY_free(pub);
  47232. pub = NULL;
  47233. EVP_PKEY_free(priv);
  47234. priv = NULL;
  47235. X509_REQ_free(req);
  47236. req = NULL;
  47237. #endif
  47238. #ifdef HAVE_ECC
  47239. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  47240. sizeof_ecc_clikey_der_256));
  47241. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  47242. sizeof_ecc_clikeypub_der_256));
  47243. ExpectNotNull(req = X509_REQ_new());
  47244. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47245. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47246. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47247. /* Signature is random and may be shorter or longer. */
  47248. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  47249. ExpectIntLE(len, 253);
  47250. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47251. X509_REQ_free(req);
  47252. EVP_PKEY_free(pub);
  47253. EVP_PKEY_free(priv);
  47254. #ifdef FP_ECC
  47255. wc_ecc_fp_free();
  47256. #endif
  47257. #endif /* HAVE_ECC */
  47258. X509_NAME_free(name);
  47259. #endif
  47260. return EXPECT_RESULT();
  47261. }
  47262. static int test_wolfssl_PKCS7(void)
  47263. {
  47264. EXPECT_DECLS;
  47265. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47266. !defined(NO_RSA)
  47267. PKCS7* pkcs7 = NULL;
  47268. byte data[FOURK_BUF];
  47269. word32 len = sizeof(data);
  47270. const byte* p = data;
  47271. byte content[] = "Test data to encode.";
  47272. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47273. BIO* bio = NULL;
  47274. byte key[sizeof(client_key_der_2048)];
  47275. word32 keySz = (word32)sizeof(key);
  47276. byte* out = NULL;
  47277. #endif
  47278. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  47279. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  47280. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  47281. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  47282. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47283. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  47284. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  47285. PKCS7_free(pkcs7);
  47286. pkcs7 = NULL;
  47287. /* fail case, without PKCS7_NOVERIFY */
  47288. p = data;
  47289. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47290. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47291. 0), WOLFSSL_FAILURE);
  47292. PKCS7_free(pkcs7);
  47293. pkcs7 = NULL;
  47294. /* success case, with PKCS7_NOVERIFY */
  47295. p = data;
  47296. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47297. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47298. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  47299. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47300. /* test i2d */
  47301. XMEMCPY(key, client_key_der_2048, keySz);
  47302. if (pkcs7 != NULL) {
  47303. pkcs7->privateKey = key;
  47304. pkcs7->privateKeySz = (word32)sizeof(key);
  47305. pkcs7->encryptOID = RSAk;
  47306. #ifdef NO_SHA
  47307. pkcs7->hashOID = SHA256h;
  47308. #else
  47309. pkcs7->hashOID = SHAh;
  47310. #endif
  47311. }
  47312. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47313. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  47314. #ifndef NO_ASN_TIME
  47315. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  47316. #else
  47317. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  47318. #endif
  47319. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47320. BIO_free(bio);
  47321. #endif
  47322. PKCS7_free(NULL);
  47323. PKCS7_free(pkcs7);
  47324. #endif
  47325. return EXPECT_RESULT();
  47326. }
  47327. static int test_wolfSSL_PKCS7_sign(void)
  47328. {
  47329. EXPECT_DECLS;
  47330. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47331. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47332. PKCS7* p7 = NULL;
  47333. PKCS7* p7Ver = NULL;
  47334. byte* out = NULL;
  47335. byte* tmpPtr = NULL;
  47336. int outLen = 0;
  47337. int flags = 0;
  47338. byte data[] = "Test data to encode.";
  47339. const char* cert = "./certs/server-cert.pem";
  47340. const char* key = "./certs/server-key.pem";
  47341. const char* ca = "./certs/ca-cert.pem";
  47342. WOLFSSL_BIO* certBio = NULL;
  47343. WOLFSSL_BIO* keyBio = NULL;
  47344. WOLFSSL_BIO* caBio = NULL;
  47345. WOLFSSL_BIO* inBio = NULL;
  47346. X509* signCert = NULL;
  47347. EVP_PKEY* signKey = NULL;
  47348. X509* caCert = NULL;
  47349. X509_STORE* store = NULL;
  47350. #ifndef NO_PKCS7_STREAM
  47351. int z;
  47352. int ret;
  47353. #endif /* !NO_PKCS7_STREAM */
  47354. /* read signer cert/key into BIO */
  47355. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47356. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47357. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47358. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47359. /* read CA cert into store (for verify) */
  47360. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47361. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47362. ExpectNotNull(store = X509_STORE_new());
  47363. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47364. /* data to be signed into BIO */
  47365. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47366. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47367. /* PKCS7_sign, bad args: signer NULL */
  47368. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  47369. /* PKCS7_sign, bad args: signer key NULL */
  47370. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  47371. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  47372. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  47373. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  47374. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  47375. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  47376. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  47377. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  47378. {
  47379. flags = PKCS7_BINARY;
  47380. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47381. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47382. /* verify with d2i_PKCS7 */
  47383. tmpPtr = out;
  47384. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47385. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47386. PKCS7_free(p7Ver);
  47387. p7Ver = NULL;
  47388. /* verify with wc_PKCS7_VerifySignedData */
  47389. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47390. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47391. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47392. #ifndef NO_PKCS7_STREAM
  47393. /* verify with wc_PKCS7_VerifySignedData streaming */
  47394. wc_PKCS7_Free(p7Ver);
  47395. p7Ver = NULL;
  47396. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47397. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47398. /* test for streaming */
  47399. ret = -1;
  47400. for (z = 0; z < outLen && ret != 0; z++) {
  47401. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47402. if (ret < 0){
  47403. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47404. }
  47405. }
  47406. ExpectIntEQ(ret, 0);
  47407. #endif /* !NO_PKCS7_STREAM */
  47408. /* compare the signer found to expected signer */
  47409. ExpectIntNE(p7Ver->verifyCertSz, 0);
  47410. tmpPtr = NULL;
  47411. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  47412. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  47413. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  47414. tmpPtr = NULL;
  47415. wc_PKCS7_Free(p7Ver);
  47416. p7Ver = NULL;
  47417. ExpectNotNull(out);
  47418. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47419. out = NULL;
  47420. PKCS7_free(p7);
  47421. p7 = NULL;
  47422. }
  47423. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  47424. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  47425. {
  47426. /* re-populate input BIO, may have been consumed */
  47427. BIO_free(inBio);
  47428. inBio = NULL;
  47429. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47430. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47431. flags = PKCS7_BINARY | PKCS7_STREAM;
  47432. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47433. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47434. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47435. /* PKCS7_final, bad args: PKCS7 null */
  47436. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  47437. /* PKCS7_final, bad args: PKCS7 null */
  47438. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  47439. tmpPtr = out;
  47440. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47441. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47442. PKCS7_free(p7Ver);
  47443. p7Ver = NULL;
  47444. ExpectNotNull(out);
  47445. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47446. out = NULL;
  47447. PKCS7_free(p7);
  47448. p7 = NULL;
  47449. }
  47450. /* TEST SUCCESS: Detached, not streaming, not MIME */
  47451. {
  47452. /* re-populate input BIO, may have been consumed */
  47453. BIO_free(inBio);
  47454. inBio = NULL;
  47455. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47456. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47457. flags = PKCS7_BINARY | PKCS7_DETACHED;
  47458. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47459. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47460. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47461. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47462. if (p7Ver != NULL) {
  47463. p7Ver->content = data;
  47464. p7Ver->contentSz = sizeof(data);
  47465. }
  47466. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47467. wc_PKCS7_Free(p7Ver);
  47468. p7Ver = NULL;
  47469. #ifndef NO_PKCS7_STREAM
  47470. /* verify with wc_PKCS7_VerifySignedData streaming */
  47471. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47472. if (p7Ver != NULL) {
  47473. p7Ver->content = data;
  47474. p7Ver->contentSz = sizeof(data);
  47475. }
  47476. /* test for streaming */
  47477. ret = -1;
  47478. for (z = 0; z < outLen && ret != 0; z++) {
  47479. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47480. if (ret < 0){
  47481. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47482. }
  47483. }
  47484. ExpectIntEQ(ret, 0);
  47485. wc_PKCS7_Free(p7Ver);
  47486. p7Ver = NULL;
  47487. #endif /* !NO_PKCS7_STREAM */
  47488. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  47489. * yet support detached content */
  47490. tmpPtr = out;
  47491. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47492. PKCS7_free(p7Ver);
  47493. p7Ver = NULL;
  47494. ExpectNotNull(out);
  47495. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47496. out = NULL;
  47497. PKCS7_free(p7);
  47498. p7 = NULL;
  47499. }
  47500. /* TEST SUCCESS: Detached, streaming, not MIME */
  47501. {
  47502. /* re-populate input BIO, may have been consumed */
  47503. BIO_free(inBio);
  47504. inBio = NULL;
  47505. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47506. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47507. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  47508. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47509. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47510. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47511. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47512. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47513. if (p7Ver != NULL) {
  47514. p7Ver->content = data;
  47515. p7Ver->contentSz = sizeof(data);
  47516. }
  47517. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47518. wc_PKCS7_Free(p7Ver);
  47519. p7Ver = NULL;
  47520. ExpectNotNull(out);
  47521. #ifndef NO_PKCS7_STREAM
  47522. /* verify with wc_PKCS7_VerifySignedData streaming */
  47523. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47524. if (p7Ver != NULL) {
  47525. p7Ver->content = data;
  47526. p7Ver->contentSz = sizeof(data);
  47527. }
  47528. /* test for streaming */
  47529. ret = -1;
  47530. for (z = 0; z < outLen && ret != 0; z++) {
  47531. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47532. if (ret < 0){
  47533. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47534. }
  47535. }
  47536. ExpectIntEQ(ret, 0);
  47537. ExpectNotNull(out);
  47538. wc_PKCS7_Free(p7Ver);
  47539. p7Ver = NULL;
  47540. #endif /* !NO_PKCS7_STREAM */
  47541. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47542. PKCS7_free(p7);
  47543. p7 = NULL;
  47544. }
  47545. X509_STORE_free(store);
  47546. X509_free(caCert);
  47547. X509_free(signCert);
  47548. EVP_PKEY_free(signKey);
  47549. BIO_free(inBio);
  47550. BIO_free(keyBio);
  47551. BIO_free(certBio);
  47552. BIO_free(caBio);
  47553. #endif
  47554. return EXPECT_RESULT();
  47555. }
  47556. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  47557. {
  47558. EXPECT_DECLS;
  47559. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  47560. PKCS7_SIGNED* pkcs7 = NULL;
  47561. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  47562. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  47563. PKCS7_SIGNED_free(pkcs7);
  47564. #endif
  47565. return EXPECT_RESULT();
  47566. }
  47567. #ifndef NO_BIO
  47568. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  47569. {
  47570. EXPECT_DECLS;
  47571. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  47572. PKCS7* pkcs7 = NULL;
  47573. BIO* bio = NULL;
  47574. const byte* cert_buf = NULL;
  47575. int ret = 0;
  47576. WC_RNG rng;
  47577. const byte data[] = { /* Hello World */
  47578. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  47579. 0x72,0x6c,0x64
  47580. };
  47581. #ifndef NO_RSA
  47582. #if defined(USE_CERT_BUFFERS_2048)
  47583. byte key[sizeof(client_key_der_2048)];
  47584. byte cert[sizeof(client_cert_der_2048)];
  47585. word32 keySz = (word32)sizeof(key);
  47586. word32 certSz = (word32)sizeof(cert);
  47587. XMEMSET(key, 0, keySz);
  47588. XMEMSET(cert, 0, certSz);
  47589. XMEMCPY(key, client_key_der_2048, keySz);
  47590. XMEMCPY(cert, client_cert_der_2048, certSz);
  47591. #elif defined(USE_CERT_BUFFERS_1024)
  47592. byte key[sizeof_client_key_der_1024];
  47593. byte cert[sizeof(sizeof_client_cert_der_1024)];
  47594. word32 keySz = (word32)sizeof(key);
  47595. word32 certSz = (word32)sizeof(cert);
  47596. XMEMSET(key, 0, keySz);
  47597. XMEMSET(cert, 0, certSz);
  47598. XMEMCPY(key, client_key_der_1024, keySz);
  47599. XMEMCPY(cert, client_cert_der_1024, certSz);
  47600. #else
  47601. unsigned char cert[ONEK_BUF];
  47602. unsigned char key[ONEK_BUF];
  47603. XFILE fp = XBADFILE;
  47604. int certSz;
  47605. int keySz;
  47606. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  47607. XBADFILE);
  47608. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  47609. fp), 0);
  47610. if (fp != XBADFILE) {
  47611. XFCLOSE(fp);
  47612. fp = XBADFILE;
  47613. }
  47614. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  47615. XBADFILE);
  47616. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  47617. 0);
  47618. if (fp != XBADFILE) {
  47619. XFCLOSE(fp);
  47620. fp = XBADFILE;
  47621. }
  47622. #endif
  47623. #elif defined(HAVE_ECC)
  47624. #if defined(USE_CERT_BUFFERS_256)
  47625. unsigned char cert[sizeof(cliecc_cert_der_256)];
  47626. unsigned char key[sizeof(ecc_clikey_der_256)];
  47627. int certSz = (int)sizeof(cert);
  47628. int keySz = (int)sizeof(key);
  47629. XMEMSET(cert, 0, certSz);
  47630. XMEMSET(key, 0, keySz);
  47631. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47632. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47633. #else
  47634. unsigned char cert[ONEK_BUF];
  47635. unsigned char key[ONEK_BUF];
  47636. XFILE fp = XBADFILE;
  47637. int certSz, keySz;
  47638. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  47639. XBADFILE);
  47640. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  47641. fp), 0);
  47642. if (fp != XBADFILE) {
  47643. XFCLOSE(fp);
  47644. fp = XBADFILE;
  47645. }
  47646. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  47647. XBADFILE);
  47648. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  47649. 0);
  47650. if (fp != XBADFILE) {
  47651. XFCLOSE(fp);
  47652. fp = XBADFILE;
  47653. }
  47654. #endif
  47655. #else
  47656. #error PKCS7 requires ECC or RSA
  47657. #endif
  47658. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47659. /* initialize with DER encoded cert */
  47660. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47661. /* init rng */
  47662. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47663. ExpectIntEQ(wc_InitRng(&rng), 0);
  47664. if (pkcs7 != NULL) {
  47665. pkcs7->rng = &rng;
  47666. pkcs7->content = (byte*)data; /* not used for ex */
  47667. pkcs7->contentSz = (word32)sizeof(data);
  47668. pkcs7->contentOID = SIGNED_DATA;
  47669. pkcs7->privateKey = key;
  47670. pkcs7->privateKeySz = (word32)sizeof(key);
  47671. pkcs7->encryptOID = RSAk;
  47672. #ifdef NO_SHA
  47673. pkcs7->hashOID = SHA256h;
  47674. #else
  47675. pkcs7->hashOID = SHAh;
  47676. #endif
  47677. pkcs7->signedAttribs = NULL;
  47678. pkcs7->signedAttribsSz = 0;
  47679. }
  47680. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47681. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47682. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47683. /* Read PKCS#7 PEM from BIO */
  47684. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47685. ExpectIntGE(ret, 0);
  47686. BIO_free(bio);
  47687. wc_PKCS7_Free(pkcs7);
  47688. wc_FreeRng(&rng);
  47689. #endif
  47690. return EXPECT_RESULT();
  47691. }
  47692. #ifdef HAVE_SMIME
  47693. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47694. {
  47695. EXPECT_DECLS;
  47696. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47697. !defined(NO_RSA)
  47698. PKCS7* pkcs7 = NULL;
  47699. BIO* bio = NULL;
  47700. BIO* bcont = NULL;
  47701. BIO* out = NULL;
  47702. const byte* outBuf = NULL;
  47703. int outBufLen = 0;
  47704. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47705. XFILE smimeTestFile = XBADFILE;
  47706. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  47707. XBADFILE);
  47708. /* smime-test.p7s */
  47709. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47710. ExpectNotNull(bio);
  47711. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47712. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47713. ExpectNotNull(pkcs7);
  47714. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47715. PKCS7_NOVERIFY), SSL_SUCCESS);
  47716. XFCLOSE(smimeTestFile);
  47717. if (bcont) BIO_free(bcont);
  47718. bcont = NULL;
  47719. wolfSSL_PKCS7_free(pkcs7);
  47720. pkcs7 = NULL;
  47721. /* smime-test-multipart.p7s */
  47722. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47723. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47724. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47725. ExpectNotNull(pkcs7);
  47726. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47727. PKCS7_NOVERIFY), SSL_SUCCESS);
  47728. XFCLOSE(smimeTestFile);
  47729. if (bcont) BIO_free(bcont);
  47730. bcont = NULL;
  47731. wolfSSL_PKCS7_free(pkcs7);
  47732. pkcs7 = NULL;
  47733. /* smime-test-multipart-badsig.p7s */
  47734. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47735. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47736. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47737. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  47738. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47739. PKCS7_NOVERIFY), SSL_FAILURE);
  47740. XFCLOSE(smimeTestFile);
  47741. if (bcont) BIO_free(bcont);
  47742. bcont = NULL;
  47743. wolfSSL_PKCS7_free(pkcs7);
  47744. pkcs7 = NULL;
  47745. /* smime-test-canon.p7s */
  47746. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47747. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47748. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47749. ExpectNotNull(pkcs7);
  47750. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47751. PKCS7_NOVERIFY), SSL_SUCCESS);
  47752. XFCLOSE(smimeTestFile);
  47753. if (bcont) BIO_free(bcont);
  47754. bcont = NULL;
  47755. wolfSSL_PKCS7_free(pkcs7);
  47756. pkcs7 = NULL;
  47757. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47758. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47759. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47760. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47761. ExpectNotNull(pkcs7);
  47762. out = wolfSSL_BIO_new(BIO_s_mem());
  47763. ExpectNotNull(out);
  47764. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47765. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47766. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47767. /* Content-Type should not show up at beginning of output buffer */
  47768. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  47769. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47770. BIO_free(out);
  47771. BIO_free(bio);
  47772. if (bcont) BIO_free(bcont);
  47773. wolfSSL_PKCS7_free(pkcs7);
  47774. #endif
  47775. return EXPECT_RESULT();
  47776. }
  47777. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47778. {
  47779. EXPECT_DECLS;
  47780. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47781. PKCS7* p7 = NULL;
  47782. PKCS7* p7Ver = NULL;
  47783. int flags = 0;
  47784. byte data[] = "Test data to encode.";
  47785. const char* cert = "./certs/server-cert.pem";
  47786. const char* key = "./certs/server-key.pem";
  47787. const char* ca = "./certs/ca-cert.pem";
  47788. WOLFSSL_BIO* certBio = NULL;
  47789. WOLFSSL_BIO* keyBio = NULL;
  47790. WOLFSSL_BIO* caBio = NULL;
  47791. WOLFSSL_BIO* inBio = NULL;
  47792. WOLFSSL_BIO* outBio = NULL;
  47793. WOLFSSL_BIO* content = NULL;
  47794. X509* signCert = NULL;
  47795. EVP_PKEY* signKey = NULL;
  47796. X509* caCert = NULL;
  47797. X509_STORE* store = NULL;
  47798. /* read signer cert/key into BIO */
  47799. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47800. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47801. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47802. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47803. /* read CA cert into store (for verify) */
  47804. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47805. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47806. ExpectNotNull(store = X509_STORE_new());
  47807. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47808. /* generate and verify SMIME: not detached */
  47809. {
  47810. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47811. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47812. flags = PKCS7_STREAM;
  47813. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47814. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47815. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47816. /* bad arg: out NULL */
  47817. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47818. /* bad arg: pkcs7 NULL */
  47819. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47820. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47821. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47822. BIO_free(content);
  47823. content = NULL;
  47824. BIO_free(inBio);
  47825. inBio = NULL;
  47826. BIO_free(outBio);
  47827. outBio = NULL;
  47828. PKCS7_free(p7Ver);
  47829. p7Ver = NULL;
  47830. PKCS7_free(p7);
  47831. p7 = NULL;
  47832. }
  47833. /* generate and verify SMIME: not detached, add Content-Type */
  47834. {
  47835. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47836. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47837. flags = PKCS7_STREAM | PKCS7_TEXT;
  47838. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47839. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47840. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47841. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47842. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47843. BIO_free(content);
  47844. content = NULL;
  47845. BIO_free(inBio);
  47846. inBio = NULL;
  47847. BIO_free(outBio);
  47848. outBio = NULL;
  47849. PKCS7_free(p7Ver);
  47850. p7Ver = NULL;
  47851. PKCS7_free(p7);
  47852. p7 = NULL;
  47853. }
  47854. /* generate and verify SMIME: detached */
  47855. {
  47856. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47857. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47858. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47859. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47860. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47861. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47862. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47863. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47864. BIO_free(content);
  47865. content = NULL;
  47866. BIO_free(inBio);
  47867. inBio = NULL;
  47868. BIO_free(outBio);
  47869. outBio = NULL;
  47870. PKCS7_free(p7Ver);
  47871. p7Ver = NULL;
  47872. PKCS7_free(p7);
  47873. p7 = NULL;
  47874. }
  47875. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47876. {
  47877. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47878. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47879. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47880. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47881. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47882. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47883. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47884. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47885. BIO_free(content);
  47886. content = NULL;
  47887. BIO_free(inBio);
  47888. inBio = NULL;
  47889. BIO_free(outBio);
  47890. outBio = NULL;
  47891. PKCS7_free(p7Ver);
  47892. p7Ver = NULL;
  47893. PKCS7_free(p7);
  47894. p7 = NULL;
  47895. }
  47896. X509_STORE_free(store);
  47897. X509_free(caCert);
  47898. X509_free(signCert);
  47899. EVP_PKEY_free(signKey);
  47900. BIO_free(keyBio);
  47901. BIO_free(certBio);
  47902. BIO_free(caBio);
  47903. #endif
  47904. return EXPECT_RESULT();
  47905. }
  47906. #endif /* HAVE_SMIME */
  47907. #endif /* !NO_BIO */
  47908. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  47909. * returns 0) */
  47910. static int test_X509_STORE_No_SSL_CTX(void)
  47911. {
  47912. EXPECT_DECLS;
  47913. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47914. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47915. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47916. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47917. defined(HAVE_CRL) && !defined(NO_RSA)
  47918. X509_STORE * store = NULL;
  47919. X509_STORE_CTX * storeCtx = NULL;
  47920. X509_CRL * crl = NULL;
  47921. X509 * ca = NULL;
  47922. X509 * cert = NULL;
  47923. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47924. const char srvCert[] = "./certs/server-cert.pem";
  47925. const char caCert[] = "./certs/ca-cert.pem";
  47926. const char caDir[] = "./certs/crl/hash_pem";
  47927. XFILE fp = XBADFILE;
  47928. X509_LOOKUP * lookup = NULL;
  47929. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47930. /* Set up store with CA */
  47931. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47932. SSL_FILETYPE_PEM)));
  47933. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47934. /* Add CRL lookup directory to store
  47935. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  47936. * of crl.pem */
  47937. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47938. X509_LOOKUP_hash_dir())));
  47939. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  47940. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  47941. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47942. SSL_SUCCESS);
  47943. /* Add CRL to store NOT containing the verified certificate, which
  47944. * forces use of the CRL lookup directory */
  47945. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47946. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47947. NULL, NULL));
  47948. if (fp != XBADFILE)
  47949. XFCLOSE(fp);
  47950. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47951. /* Create verification context outside of an SSL session */
  47952. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47953. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47954. SSL_FILETYPE_PEM)));
  47955. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47956. /* Perform verification, which should NOT indicate CRL missing due to the
  47957. * store CM's X509 store pointer being NULL */
  47958. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47959. X509_CRL_free(crl);
  47960. X509_STORE_free(store);
  47961. X509_STORE_CTX_free(storeCtx);
  47962. X509_free(cert);
  47963. X509_free(ca);
  47964. #endif
  47965. return EXPECT_RESULT();
  47966. }
  47967. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  47968. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  47969. static int test_X509_LOOKUP_add_dir(void)
  47970. {
  47971. EXPECT_DECLS;
  47972. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47973. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47974. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47975. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47976. defined(HAVE_CRL) && !defined(NO_RSA)
  47977. X509_STORE * store = NULL;
  47978. X509_STORE_CTX * storeCtx = NULL;
  47979. X509_CRL * crl = NULL;
  47980. X509 * ca = NULL;
  47981. X509 * cert = NULL;
  47982. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47983. const char srvCert[] = "./certs/server-cert.pem";
  47984. const char caCert[] = "./certs/ca-cert.pem";
  47985. const char caDir[] = "./certs/crl/hash_der";
  47986. XFILE fp = XBADFILE;
  47987. X509_LOOKUP * lookup = NULL;
  47988. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47989. /* Set up store with CA */
  47990. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47991. SSL_FILETYPE_PEM)));
  47992. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47993. /* Add CRL lookup directory to store.
  47994. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  47995. * of crl.der */
  47996. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47997. X509_LOOKUP_hash_dir())));
  47998. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  47999. SSL_SUCCESS);
  48000. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48001. SSL_SUCCESS);
  48002. /* Add CRL to store NOT containing the verified certificate, which
  48003. * forces use of the CRL lookup directory */
  48004. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48005. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48006. NULL, NULL));
  48007. if (fp != XBADFILE) {
  48008. XFCLOSE(fp);
  48009. fp = XBADFILE;
  48010. }
  48011. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48012. /* Create verification context outside of an SSL session */
  48013. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48014. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48015. SSL_FILETYPE_PEM)));
  48016. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48017. /* Perform verification, which should NOT return CRL missing */
  48018. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48019. X509_CRL_free(crl);
  48020. crl = NULL;
  48021. X509_STORE_free(store);
  48022. store = NULL;
  48023. X509_STORE_CTX_free(storeCtx);
  48024. storeCtx = NULL;
  48025. X509_free(cert);
  48026. cert = NULL;
  48027. X509_free(ca);
  48028. ca = NULL;
  48029. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48030. * We should get CRL_MISSING at the end, because the lookup
  48031. * dir has only ASN1 CRLs. */
  48032. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48033. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48034. SSL_FILETYPE_PEM)));
  48035. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48036. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48037. X509_LOOKUP_hash_dir())));
  48038. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48039. SSL_SUCCESS);
  48040. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48041. SSL_SUCCESS);
  48042. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48043. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48044. NULL, NULL));
  48045. if (fp != XBADFILE) {
  48046. XFCLOSE(fp);
  48047. fp = XBADFILE;
  48048. }
  48049. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48050. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48051. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48052. SSL_FILETYPE_PEM)));
  48053. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48054. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48055. * in dir containing only ASN1/DER. */
  48056. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48057. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  48058. X509_CRL_free(crl);
  48059. X509_STORE_free(store);
  48060. X509_STORE_CTX_free(storeCtx);
  48061. X509_free(cert);
  48062. X509_free(ca);
  48063. #endif
  48064. return EXPECT_RESULT();
  48065. }
  48066. /*----------------------------------------------------------------------------*
  48067. | Certificate Failure Checks
  48068. *----------------------------------------------------------------------------*/
  48069. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48070. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48071. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48072. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48073. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48074. int type)
  48075. {
  48076. int ret;
  48077. WOLFSSL_CERT_MANAGER* cm = NULL;
  48078. switch (type) {
  48079. case TESTING_RSA:
  48080. #ifdef NO_RSA
  48081. fprintf(stderr, "RSA disabled, skipping test\n");
  48082. return ASN_SIG_CONFIRM_E;
  48083. #else
  48084. break;
  48085. #endif
  48086. case TESTING_ECC:
  48087. #ifndef HAVE_ECC
  48088. fprintf(stderr, "ECC disabled, skipping test\n");
  48089. return ASN_SIG_CONFIRM_E;
  48090. #else
  48091. break;
  48092. #endif
  48093. default:
  48094. fprintf(stderr, "Bad function argument\n");
  48095. return BAD_FUNC_ARG;
  48096. }
  48097. cm = wolfSSL_CertManagerNew();
  48098. if (cm == NULL) {
  48099. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48100. return -1;
  48101. }
  48102. #ifndef NO_FILESYSTEM
  48103. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48104. if (ret != WOLFSSL_SUCCESS) {
  48105. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48106. wolfSSL_CertManagerFree(cm);
  48107. return ret;
  48108. }
  48109. #else
  48110. (void)ca;
  48111. #endif
  48112. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  48113. WOLFSSL_FILETYPE_ASN1);
  48114. /* Let ExpectIntEQ handle return code */
  48115. wolfSSL_CertManagerFree(cm);
  48116. return ret;
  48117. }
  48118. #endif
  48119. #if !defined(NO_FILESYSTEM)
  48120. static int test_RsaSigFailure_cm(void)
  48121. {
  48122. EXPECT_DECLS;
  48123. #ifndef NO_RSA
  48124. const char* ca_cert = "./certs/ca-cert.pem";
  48125. const char* server_cert = "./certs/server-cert.der";
  48126. byte* cert_buf = NULL;
  48127. size_t cert_sz = 0;
  48128. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48129. if (cert_buf != NULL) {
  48130. /* corrupt DER - invert last byte, which is signature */
  48131. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48132. /* test bad cert */
  48133. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48134. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48135. WOLFSSL_FATAL_ERROR);
  48136. #else
  48137. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48138. ASN_SIG_CONFIRM_E);
  48139. #endif
  48140. }
  48141. /* load_file() uses malloc. */
  48142. if (cert_buf != NULL) {
  48143. free(cert_buf);
  48144. }
  48145. #endif /* !NO_RSA */
  48146. return EXPECT_RESULT();
  48147. }
  48148. static int test_EccSigFailure_cm(void)
  48149. {
  48150. EXPECT_DECLS;
  48151. #ifdef HAVE_ECC
  48152. /* self-signed ECC cert, so use server cert as CA */
  48153. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  48154. const char* server_cert = "./certs/server-ecc.der";
  48155. byte* cert_buf = NULL;
  48156. size_t cert_sz = 0;
  48157. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48158. if (cert_buf != NULL) {
  48159. /* corrupt DER - invert last byte, which is signature */
  48160. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48161. /* test bad cert */
  48162. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48163. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48164. WOLFSSL_FATAL_ERROR);
  48165. #else
  48166. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48167. ASN_SIG_CONFIRM_E);
  48168. #endif
  48169. }
  48170. /* load_file() uses malloc. */
  48171. if (cert_buf != NULL) {
  48172. free(cert_buf);
  48173. }
  48174. #ifdef FP_ECC
  48175. wc_ecc_fp_free();
  48176. #endif
  48177. #endif /* HAVE_ECC */
  48178. return EXPECT_RESULT();
  48179. }
  48180. #endif /* !NO_FILESYSTEM */
  48181. #endif /* NO_CERTS */
  48182. #ifdef WOLFSSL_TLS13
  48183. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  48184. #ifdef WC_SHA384_DIGEST_SIZE
  48185. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  48186. #else
  48187. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  48188. #endif
  48189. #endif
  48190. #ifdef WOLFSSL_EARLY_DATA
  48191. static const char earlyData[] = "Early Data";
  48192. static char earlyDataBuffer[1];
  48193. #endif
  48194. static int test_tls13_apis(void)
  48195. {
  48196. EXPECT_DECLS;
  48197. int ret;
  48198. #ifndef WOLFSSL_NO_TLS12
  48199. #ifndef NO_WOLFSSL_CLIENT
  48200. WOLFSSL_CTX* clientTls12Ctx = NULL;
  48201. WOLFSSL* clientTls12Ssl = NULL;
  48202. #endif
  48203. #ifndef NO_WOLFSSL_SERVER
  48204. WOLFSSL_CTX* serverTls12Ctx = NULL;
  48205. WOLFSSL* serverTls12Ssl = NULL;
  48206. #endif
  48207. #endif
  48208. #ifndef NO_WOLFSSL_CLIENT
  48209. WOLFSSL_CTX* clientCtx = NULL;
  48210. WOLFSSL* clientSsl = NULL;
  48211. #endif
  48212. #ifndef NO_WOLFSSL_SERVER
  48213. WOLFSSL_CTX* serverCtx = NULL;
  48214. WOLFSSL* serverSsl = NULL;
  48215. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48216. const char* ourCert = svrCertFile;
  48217. const char* ourKey = svrKeyFile;
  48218. #endif
  48219. #endif
  48220. int required;
  48221. #ifdef WOLFSSL_EARLY_DATA
  48222. int outSz;
  48223. #endif
  48224. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  48225. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  48226. #ifdef HAVE_PQC
  48227. WOLFSSL_KYBER_LEVEL1
  48228. #else
  48229. WOLFSSL_ECC_SECP256R1
  48230. #endif
  48231. };
  48232. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  48233. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  48234. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  48235. int numGroups = 2;
  48236. #endif
  48237. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48238. char groupList[] =
  48239. #ifndef NO_ECC_SECP
  48240. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  48241. "P-521:"
  48242. #endif
  48243. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  48244. "P-384:"
  48245. #endif
  48246. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  48247. "P-256"
  48248. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  48249. ":P256_KYBER_LEVEL1"
  48250. #endif
  48251. #endif
  48252. #endif /* !defined(NO_ECC_SECP) */
  48253. #ifdef HAVE_PQC
  48254. ":KYBER_LEVEL1"
  48255. #endif
  48256. "";
  48257. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48258. (void)ret;
  48259. #ifndef WOLFSSL_NO_TLS12
  48260. #ifndef NO_WOLFSSL_CLIENT
  48261. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  48262. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  48263. #endif
  48264. #ifndef NO_WOLFSSL_SERVER
  48265. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  48266. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48267. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  48268. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  48269. WOLFSSL_FILETYPE_PEM);
  48270. #endif
  48271. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  48272. #endif
  48273. #endif
  48274. #ifndef NO_WOLFSSL_CLIENT
  48275. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  48276. clientSsl = wolfSSL_new(clientCtx);
  48277. #endif
  48278. #ifndef NO_WOLFSSL_SERVER
  48279. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  48280. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48281. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  48282. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  48283. #endif
  48284. serverSsl = wolfSSL_new(serverCtx);
  48285. ExpectNotNull(serverSsl);
  48286. #endif
  48287. #ifdef WOLFSSL_SEND_HRR_COOKIE
  48288. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  48289. #ifndef NO_WOLFSSL_CLIENT
  48290. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  48291. #endif
  48292. #ifndef NO_WOLFSSL_SERVER
  48293. #ifndef WOLFSSL_NO_TLS12
  48294. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  48295. BAD_FUNC_ARG);
  48296. #endif
  48297. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  48298. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  48299. WOLFSSL_SUCCESS);
  48300. #endif
  48301. #endif
  48302. #ifdef HAVE_SUPPORTED_CURVES
  48303. #ifdef HAVE_ECC
  48304. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48305. BAD_FUNC_ARG);
  48306. #ifndef NO_WOLFSSL_SERVER
  48307. do {
  48308. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  48309. #ifdef WOLFSSL_ASYNC_CRYPT
  48310. if (ret == WC_PENDING_E)
  48311. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  48312. #endif
  48313. }
  48314. while (ret == WC_PENDING_E);
  48315. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48316. #endif
  48317. #ifndef NO_WOLFSSL_CLIENT
  48318. #ifndef WOLFSSL_NO_TLS12
  48319. do {
  48320. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  48321. #ifdef WOLFSSL_ASYNC_CRYPT
  48322. if (ret == WC_PENDING_E)
  48323. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  48324. #endif
  48325. }
  48326. while (ret == WC_PENDING_E);
  48327. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48328. #endif
  48329. do {
  48330. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  48331. #ifdef WOLFSSL_ASYNC_CRYPT
  48332. if (ret == WC_PENDING_E)
  48333. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  48334. #endif
  48335. }
  48336. while (ret == WC_PENDING_E);
  48337. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48338. #endif
  48339. #elif defined(HAVE_CURVE25519)
  48340. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  48341. #ifndef NO_WOLFSSL_SERVER
  48342. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  48343. WOLFSSL_SUCCESS);
  48344. #endif
  48345. #ifndef NO_WOLFSSL_CLIENT
  48346. #ifndef WOLFSSL_NO_TLS12
  48347. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  48348. WOLFSSL_SUCCESS);
  48349. #endif
  48350. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  48351. WOLFSSL_SUCCESS);
  48352. #endif
  48353. #elif defined(HAVE_CURVE448)
  48354. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  48355. #ifndef NO_WOLFSSL_SERVER
  48356. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  48357. WOLFSSL_SUCCESS);
  48358. #endif
  48359. #ifndef NO_WOLFSSL_CLIENT
  48360. #ifndef WOLFSSL_NO_TLS12
  48361. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  48362. WOLFSSL_SUCCESS);
  48363. #endif
  48364. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  48365. WOLFSSL_SUCCESS);
  48366. #endif
  48367. #else
  48368. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48369. BAD_FUNC_ARG);
  48370. #ifndef NO_WOLFSSL_CLIENT
  48371. #ifndef WOLFSSL_NO_TLS12
  48372. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  48373. NOT_COMPILED_IN);
  48374. #endif
  48375. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  48376. NOT_COMPILED_IN);
  48377. #endif
  48378. #endif
  48379. #if defined(HAVE_PQC)
  48380. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  48381. #ifndef NO_WOLFSSL_SERVER
  48382. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  48383. WOLFSSL_SUCCESS);
  48384. #endif
  48385. #ifndef NO_WOLFSSL_CLIENT
  48386. #ifndef WOLFSSL_NO_TLS12
  48387. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  48388. BAD_FUNC_ARG);
  48389. #endif
  48390. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  48391. WOLFSSL_SUCCESS);
  48392. #endif
  48393. #endif
  48394. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  48395. #ifndef NO_WOLFSSL_SERVER
  48396. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  48397. #endif
  48398. #ifndef NO_WOLFSSL_CLIENT
  48399. #ifndef WOLFSSL_NO_TLS12
  48400. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  48401. #endif
  48402. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  48403. #endif
  48404. #endif /* HAVE_SUPPORTED_CURVES */
  48405. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48406. #ifndef NO_WOLFSSL_CLIENT
  48407. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  48408. #endif
  48409. #ifndef NO_WOLFSSL_SERVER
  48410. #ifndef WOLFSSL_NO_TLS12
  48411. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  48412. #endif
  48413. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  48414. #endif
  48415. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48416. #ifndef NO_WOLFSSL_CLIENT
  48417. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  48418. #endif
  48419. #ifndef NO_WOLFSSL_SERVER
  48420. #ifndef WOLFSSL_NO_TLS12
  48421. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  48422. #endif
  48423. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  48424. #endif
  48425. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48426. #ifndef NO_WOLFSSL_CLIENT
  48427. #ifndef WOLFSSL_NO_TLS12
  48428. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  48429. #endif
  48430. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  48431. #endif
  48432. #ifndef NO_WOLFSSL_SERVER
  48433. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  48434. #endif
  48435. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48436. #ifndef NO_WOLFSSL_CLIENT
  48437. #ifndef WOLFSSL_NO_TLS12
  48438. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  48439. #endif
  48440. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  48441. #endif
  48442. #ifndef NO_WOLFSSL_SERVER
  48443. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  48444. #endif
  48445. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  48446. #ifndef NO_WOLFSSL_CLIENT
  48447. #ifndef WOLFSSL_NO_TLS12
  48448. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  48449. #endif
  48450. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  48451. #endif
  48452. #ifndef NO_WOLFSSL_SERVER
  48453. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  48454. #endif
  48455. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  48456. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  48457. #ifndef NO_WOLFSSL_CLIENT
  48458. #ifndef WOLFSSL_NO_TLS12
  48459. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  48460. BAD_FUNC_ARG);
  48461. #endif
  48462. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  48463. #endif
  48464. #ifndef NO_WOLFSSL_SERVER
  48465. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  48466. #endif
  48467. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  48468. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48469. #ifndef NO_WOLFSSL_SERVER
  48470. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  48471. #endif
  48472. #ifndef NO_WOLFSSL_CLIENT
  48473. #ifndef WOLFSSL_NO_TLS12
  48474. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  48475. BAD_FUNC_ARG);
  48476. #endif
  48477. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  48478. #endif
  48479. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48480. #ifndef NO_WOLFSSL_SERVER
  48481. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  48482. #endif
  48483. #ifndef NO_WOLFSSL_CLIENT
  48484. #ifndef WOLFSSL_NO_TLS12
  48485. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  48486. BAD_FUNC_ARG);
  48487. #endif
  48488. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  48489. #endif
  48490. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  48491. #ifndef NO_WOLFSSL_CLIENT
  48492. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  48493. #endif
  48494. #ifndef NO_WOLFSSL_SERVER
  48495. #ifndef WOLFSSL_NO_TLS12
  48496. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  48497. BAD_FUNC_ARG);
  48498. #endif
  48499. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  48500. #endif
  48501. #endif
  48502. #ifdef HAVE_ECC
  48503. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  48504. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  48505. #ifndef NO_WOLFSSL_SERVER
  48506. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  48507. #endif
  48508. #ifndef NO_WOLFSSL_CLIENT
  48509. #ifndef WOLFSSL_NO_TLS12
  48510. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  48511. #endif
  48512. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  48513. #endif
  48514. #endif
  48515. #ifdef HAVE_SUPPORTED_CURVES
  48516. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48517. #ifndef NO_WOLFSSL_CLIENT
  48518. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  48519. #endif
  48520. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48521. #ifndef NO_WOLFSSL_CLIENT
  48522. #ifndef WOLFSSL_NO_TLS12
  48523. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  48524. BAD_FUNC_ARG);
  48525. #endif
  48526. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  48527. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48528. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  48529. WOLFSSL_SUCCESS);
  48530. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  48531. BAD_FUNC_ARG);
  48532. #endif
  48533. #ifndef NO_WOLFSSL_SERVER
  48534. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  48535. WOLFSSL_SUCCESS);
  48536. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  48537. BAD_FUNC_ARG);
  48538. #endif
  48539. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48540. #ifndef NO_WOLFSSL_CLIENT
  48541. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  48542. #endif
  48543. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48544. #ifndef NO_WOLFSSL_CLIENT
  48545. #ifndef WOLFSSL_NO_TLS12
  48546. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  48547. BAD_FUNC_ARG);
  48548. #endif
  48549. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  48550. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48551. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  48552. WOLFSSL_SUCCESS);
  48553. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  48554. BAD_FUNC_ARG);
  48555. #endif
  48556. #ifndef NO_WOLFSSL_SERVER
  48557. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  48558. WOLFSSL_SUCCESS);
  48559. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  48560. BAD_FUNC_ARG);
  48561. #endif
  48562. #ifdef OPENSSL_EXTRA
  48563. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48564. #ifndef NO_WOLFSSL_CLIENT
  48565. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  48566. WOLFSSL_FAILURE);
  48567. #endif
  48568. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  48569. WOLFSSL_FAILURE);
  48570. #ifndef NO_WOLFSSL_CLIENT
  48571. #ifndef WOLFSSL_NO_TLS12
  48572. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  48573. WOLFSSL_FAILURE);
  48574. #endif
  48575. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  48576. WOLFSSL_SUCCESS);
  48577. #endif
  48578. #ifndef NO_WOLFSSL_SERVER
  48579. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  48580. WOLFSSL_SUCCESS);
  48581. #endif
  48582. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48583. #ifndef NO_WOLFSSL_CLIENT
  48584. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  48585. #endif
  48586. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  48587. #ifndef NO_WOLFSSL_CLIENT
  48588. #ifndef WOLFSSL_NO_TLS12
  48589. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  48590. WOLFSSL_FAILURE);
  48591. #endif
  48592. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  48593. WOLFSSL_SUCCESS);
  48594. #endif
  48595. #ifndef NO_WOLFSSL_SERVER
  48596. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  48597. WOLFSSL_SUCCESS);
  48598. #endif
  48599. #endif /* OPENSSL_EXTRA */
  48600. #endif /* HAVE_SUPPORTED_CURVES */
  48601. #endif /* HAVE_ECC */
  48602. #ifdef WOLFSSL_EARLY_DATA
  48603. #ifndef OPENSSL_EXTRA
  48604. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48605. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48606. #else
  48607. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48608. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48609. #endif
  48610. #ifndef NO_WOLFSSL_CLIENT
  48611. #ifndef OPENSSL_EXTRA
  48612. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48613. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48614. #else
  48615. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48616. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48617. #endif
  48618. #endif
  48619. #ifndef NO_WOLFSSL_SERVER
  48620. #ifndef WOLFSSL_NO_TLS12
  48621. #ifndef OPENSSL_EXTRA
  48622. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48623. BAD_FUNC_ARG);
  48624. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48625. #else
  48626. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48627. BAD_FUNC_ARG);
  48628. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48629. #endif
  48630. #endif
  48631. #ifndef OPENSSL_EXTRA
  48632. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48633. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  48634. WOLFSSL_SUCCESS);
  48635. #else
  48636. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  48637. #endif
  48638. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  48639. #else
  48640. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  48641. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  48642. #endif
  48643. #endif
  48644. #ifndef OPENSSL_EXTRA
  48645. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48646. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48647. #else
  48648. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48649. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48650. #endif
  48651. #ifndef NO_WOLFSSL_CLIENT
  48652. #ifndef OPENSSL_EXTRA
  48653. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48654. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48655. #else
  48656. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48657. #endif
  48658. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48659. #else
  48660. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48661. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48662. #endif
  48663. #endif
  48664. #ifndef NO_WOLFSSL_SERVER
  48665. #ifndef WOLFSSL_NO_TLS12
  48666. #ifndef OPENSSL_EXTRA
  48667. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48668. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48669. #else
  48670. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48671. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48672. #endif
  48673. #endif
  48674. #ifndef OPENSSL_EXTRA
  48675. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48676. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48677. #else
  48678. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48679. #endif
  48680. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48681. #else
  48682. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48683. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48684. #endif
  48685. #endif
  48686. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48687. &outSz), BAD_FUNC_ARG);
  48688. #ifndef NO_WOLFSSL_CLIENT
  48689. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48690. &outSz), BAD_FUNC_ARG);
  48691. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48692. BAD_FUNC_ARG);
  48693. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48694. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  48695. #endif
  48696. #ifndef NO_WOLFSSL_SERVER
  48697. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48698. sizeof(earlyData), &outSz), SIDE_ERROR);
  48699. #endif
  48700. #ifndef NO_WOLFSSL_CLIENT
  48701. #ifndef WOLFSSL_NO_TLS12
  48702. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48703. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  48704. #endif
  48705. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48706. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  48707. #endif
  48708. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48709. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48710. #ifndef NO_WOLFSSL_SERVER
  48711. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48712. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48713. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  48714. &outSz), BAD_FUNC_ARG);
  48715. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48716. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  48717. #endif
  48718. #ifndef NO_WOLFSSL_CLIENT
  48719. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48720. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  48721. #endif
  48722. #ifndef NO_WOLFSSL_SERVER
  48723. #ifndef WOLFSSL_NO_TLS12
  48724. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48725. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48726. #endif
  48727. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48728. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  48729. #endif
  48730. #endif
  48731. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48732. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  48733. #endif
  48734. #ifndef NO_WOLFSSL_SERVER
  48735. wolfSSL_free(serverSsl);
  48736. wolfSSL_CTX_free(serverCtx);
  48737. #endif
  48738. #ifndef NO_WOLFSSL_CLIENT
  48739. wolfSSL_free(clientSsl);
  48740. wolfSSL_CTX_free(clientCtx);
  48741. #endif
  48742. #ifndef WOLFSSL_NO_TLS12
  48743. #ifndef NO_WOLFSSL_SERVER
  48744. wolfSSL_free(serverTls12Ssl);
  48745. wolfSSL_CTX_free(serverTls12Ctx);
  48746. #endif
  48747. #ifndef NO_WOLFSSL_CLIENT
  48748. wolfSSL_free(clientTls12Ssl);
  48749. wolfSSL_CTX_free(clientTls12Ctx);
  48750. #endif
  48751. #endif
  48752. return EXPECT_RESULT();
  48753. }
  48754. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48755. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48756. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48757. /* Called when writing. */
  48758. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48759. {
  48760. (void)ssl;
  48761. (void)buf;
  48762. (void)sz;
  48763. (void)ctx;
  48764. /* Force error return from wolfSSL_accept_TLSv13(). */
  48765. return WANT_WRITE;
  48766. }
  48767. /* Called when reading. */
  48768. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48769. {
  48770. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48771. int len = (int)msg->length;
  48772. (void)ssl;
  48773. (void)sz;
  48774. /* Pass back as much of message as will fit in buffer. */
  48775. if (len > sz)
  48776. len = sz;
  48777. XMEMCPY(buf, msg->buffer, len);
  48778. /* Move over returned data. */
  48779. msg->buffer += len;
  48780. msg->length -= len;
  48781. /* Amount actually copied. */
  48782. return len;
  48783. }
  48784. #endif
  48785. static int test_tls13_cipher_suites(void)
  48786. {
  48787. EXPECT_DECLS;
  48788. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48789. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48790. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48791. WOLFSSL_CTX* ctx = NULL;
  48792. WOLFSSL *ssl = NULL;
  48793. int i;
  48794. byte clientHello[] = {
  48795. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48796. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48797. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48798. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48799. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48800. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48801. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48802. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48803. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48804. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48805. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48806. 0x13, 0x01,
  48807. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48808. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48809. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48810. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48811. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48812. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48813. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48814. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48815. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48816. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48817. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48818. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48819. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48820. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48821. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48822. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48823. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48824. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48825. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48826. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48827. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48828. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48829. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48830. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48831. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48832. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48833. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48834. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48835. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48836. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48837. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48838. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48839. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48840. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48841. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48842. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48843. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48844. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48845. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48846. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48847. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48848. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48849. };
  48850. WOLFSSL_BUFFER_INFO msg;
  48851. /* Offset into ClientHello message data of first cipher suite. */
  48852. const int csOff = 78;
  48853. /* Server cipher list. */
  48854. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48855. /* Suite list with duplicates. */
  48856. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48857. "TLS13-AES128-GCM-SHA256:"
  48858. "TLS13-AES256-GCM-SHA384:"
  48859. "TLS13-AES256-GCM-SHA384:"
  48860. "TLS13-AES128-GCM-SHA256";
  48861. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48862. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48863. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48864. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48865. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48866. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48867. #endif
  48868. /* Set up wolfSSL context. */
  48869. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48870. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48871. WOLFSSL_FILETYPE_PEM));
  48872. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48873. WOLFSSL_FILETYPE_PEM));
  48874. /* Read from 'msg'. */
  48875. wolfSSL_SetIORecv(ctx, CsRecv);
  48876. /* No where to send to - dummy sender. */
  48877. wolfSSL_SetIOSend(ctx, CsSend);
  48878. /* Test cipher suite list with many copies of a cipher suite. */
  48879. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48880. msg.buffer = clientHello;
  48881. msg.length = (unsigned int)sizeof(clientHello);
  48882. wolfSSL_SetIOReadCtx(ssl, &msg);
  48883. /* Force server to have as many occurrences of same cipher suite as
  48884. * possible. */
  48885. if (ssl != NULL) {
  48886. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48887. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48888. for (i = 0; i < suites->suiteSz; i += 2) {
  48889. suites->suites[i + 0] = TLS13_BYTE;
  48890. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48891. }
  48892. }
  48893. /* Test multiple occurrences of same cipher suite. */
  48894. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48895. wolfSSL_free(ssl);
  48896. ssl = NULL;
  48897. /* Set client order opposite to server order:
  48898. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48899. clientHello[csOff + 0] = TLS13_BYTE;
  48900. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48901. clientHello[csOff + 2] = TLS13_BYTE;
  48902. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48903. /* Test server order negotiation. */
  48904. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48905. msg.buffer = clientHello;
  48906. msg.length = (unsigned int)sizeof(clientHello);
  48907. wolfSSL_SetIOReadCtx(ssl, &msg);
  48908. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48909. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48910. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  48911. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48912. /* Check refined order - server order. */
  48913. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48914. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48915. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48916. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48917. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48918. wolfSSL_free(ssl);
  48919. ssl = NULL;
  48920. /* Test client order negotiation. */
  48921. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48922. msg.buffer = clientHello;
  48923. msg.length = (unsigned int)sizeof(clientHello);
  48924. wolfSSL_SetIOReadCtx(ssl, &msg);
  48925. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48926. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48927. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  48928. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  48929. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48930. /* Check refined order - client order. */
  48931. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48932. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48933. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48934. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48935. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48936. wolfSSL_free(ssl);
  48937. ssl = NULL;
  48938. /* Check duplicate detection is working. */
  48939. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  48940. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48941. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48942. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48943. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48944. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48945. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48946. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  48947. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  48948. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48949. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48950. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48951. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48952. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48953. #endif
  48954. wolfSSL_CTX_free(ctx);
  48955. #endif
  48956. return EXPECT_RESULT();
  48957. }
  48958. #endif
  48959. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  48960. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48961. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48962. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48963. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  48964. const unsigned char* priv, unsigned int privSz,
  48965. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  48966. unsigned char* out, unsigned int* outlen,
  48967. void* ctx)
  48968. {
  48969. int result;
  48970. /* Test fail when context associated with WOLFSSL is NULL */
  48971. if (ctx == NULL) {
  48972. return -1;
  48973. }
  48974. (void)ssl;
  48975. /* return 0 on success */
  48976. PRIVATE_KEY_UNLOCK();
  48977. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  48978. PRIVATE_KEY_LOCK();
  48979. return result;
  48980. }
  48981. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  48982. EXPECT_DECLS;
  48983. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  48984. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  48985. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  48986. WOLFSSL_SUCCESS);
  48987. #endif
  48988. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  48989. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  48990. WOLFSSL_SUCCESS);
  48991. #endif
  48992. return EXPECT_RESULT();
  48993. }
  48994. static int test_dh_ssl_setup(WOLFSSL* ssl)
  48995. {
  48996. EXPECT_DECLS;
  48997. static int dh_test_ctx = 1;
  48998. int ret;
  48999. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49000. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49001. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49002. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49003. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49004. }
  49005. return EXPECT_RESULT();
  49006. }
  49007. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49008. {
  49009. EXPECT_DECLS;
  49010. int ret;
  49011. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49012. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49013. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49014. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49015. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49016. }
  49017. return EXPECT_RESULT();
  49018. }
  49019. #endif
  49020. static int test_DhCallbacks(void)
  49021. {
  49022. EXPECT_DECLS;
  49023. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49024. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49025. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49026. WOLFSSL_CTX *ctx = NULL;
  49027. WOLFSSL *ssl = NULL;
  49028. int test;
  49029. test_ssl_cbf func_cb_client;
  49030. test_ssl_cbf func_cb_server;
  49031. /* Test that DH callback APIs work. */
  49032. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49033. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49034. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49035. /* load client ca cert */
  49036. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49037. WOLFSSL_SUCCESS);
  49038. /* test with NULL arguments */
  49039. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49040. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49041. /* test success case */
  49042. test = 1;
  49043. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49044. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49045. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49046. wolfSSL_free(ssl);
  49047. wolfSSL_CTX_free(ctx);
  49048. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49049. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49050. /* set callbacks to use DH functions */
  49051. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49052. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49053. func_cb_client.method = wolfTLSv1_2_client_method;
  49054. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49055. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49056. func_cb_server.method = wolfTLSv1_2_server_method;
  49057. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49058. &func_cb_server, NULL), TEST_SUCCESS);
  49059. /* Test fail */
  49060. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49061. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49062. /* set callbacks to use DH functions */
  49063. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49064. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49065. func_cb_client.method = wolfTLSv1_2_client_method;
  49066. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49067. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49068. func_cb_server.method = wolfTLSv1_2_server_method;
  49069. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49070. &func_cb_server, NULL), TEST_FAIL);
  49071. #endif
  49072. return EXPECT_RESULT();
  49073. }
  49074. #endif /* HAVE_PK_CALLBACKS */
  49075. #ifdef HAVE_HASHDRBG
  49076. #ifdef TEST_RESEED_INTERVAL
  49077. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49078. {
  49079. EXPECT_DECLS;
  49080. int i;
  49081. WC_RNG rng;
  49082. byte key[32];
  49083. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49084. ExpectIntEQ(wc_InitRng(&rng), 0);
  49085. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49086. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49087. }
  49088. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49089. return EXPECT_RESULT();
  49090. }
  49091. #endif /* TEST_RESEED_INTERVAL */
  49092. static int test_wc_RNG_GenerateBlock(void)
  49093. {
  49094. EXPECT_DECLS;
  49095. int i;
  49096. WC_RNG rng;
  49097. byte key[32];
  49098. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49099. ExpectIntEQ(wc_InitRng(&rng), 0);
  49100. for (i = 0; i < 10; i++) {
  49101. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49102. }
  49103. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49104. return EXPECT_RESULT();
  49105. }
  49106. #endif /* HAVE_HASHDRBG */
  49107. /*
  49108. * Testing get_rand_digit
  49109. */
  49110. static int test_get_rand_digit(void)
  49111. {
  49112. EXPECT_DECLS;
  49113. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  49114. WC_RNG rng;
  49115. mp_digit d;
  49116. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49117. ExpectIntEQ(wc_InitRng(&rng), 0);
  49118. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  49119. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  49120. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  49121. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  49122. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49123. #endif
  49124. return EXPECT_RESULT();
  49125. } /* End test_get_rand_digit*/
  49126. /*
  49127. * Testing get_digit_count
  49128. */
  49129. static int test_get_digit_count(void)
  49130. {
  49131. EXPECT_DECLS;
  49132. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  49133. mp_int a;
  49134. XMEMSET(&a, 0, sizeof(mp_int));
  49135. ExpectIntEQ(mp_init(&a), 0);
  49136. ExpectIntEQ(get_digit_count(NULL), 0);
  49137. ExpectIntEQ(get_digit_count(&a), 0);
  49138. mp_clear(&a);
  49139. #endif
  49140. return EXPECT_RESULT();
  49141. } /* End test_get_digit_count*/
  49142. /*
  49143. * Testing mp_cond_copy
  49144. */
  49145. static int test_mp_cond_copy(void)
  49146. {
  49147. EXPECT_DECLS;
  49148. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  49149. defined(WOLFSSL_PUBLIC_MP)
  49150. mp_int a;
  49151. mp_int b;
  49152. int copy = 0;
  49153. XMEMSET(&a, 0, sizeof(mp_int));
  49154. XMEMSET(&b, 0, sizeof(mp_int));
  49155. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49156. ExpectIntEQ(mp_init(&b), MP_OKAY);
  49157. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  49158. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  49159. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  49160. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  49161. mp_clear(&a);
  49162. mp_clear(&b);
  49163. #endif
  49164. return EXPECT_RESULT();
  49165. } /* End test_mp_cond_copy*/
  49166. /*
  49167. * Testing mp_rand
  49168. */
  49169. static int test_mp_rand(void)
  49170. {
  49171. EXPECT_DECLS;
  49172. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  49173. mp_int a;
  49174. WC_RNG rng;
  49175. int digits = 1;
  49176. XMEMSET(&a, 0, sizeof(mp_int));
  49177. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49178. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49179. ExpectIntEQ(wc_InitRng(&rng), 0);
  49180. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  49181. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  49182. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  49183. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  49184. mp_clear(&a);
  49185. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49186. #endif
  49187. return EXPECT_RESULT();
  49188. } /* End test_mp_rand*/
  49189. /*
  49190. * Testing get_digit
  49191. */
  49192. static int test_get_digit(void)
  49193. {
  49194. EXPECT_DECLS;
  49195. #if defined(WOLFSSL_PUBLIC_MP)
  49196. mp_int a;
  49197. int n = 0;
  49198. XMEMSET(&a, 0, sizeof(mp_int));
  49199. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49200. ExpectIntEQ(get_digit(NULL, n), 0);
  49201. ExpectIntEQ(get_digit(&a, n), 0);
  49202. mp_clear(&a);
  49203. #endif
  49204. return EXPECT_RESULT();
  49205. } /* End test_get_digit*/
  49206. /*
  49207. * Testing wc_export_int
  49208. */
  49209. static int test_wc_export_int(void)
  49210. {
  49211. EXPECT_DECLS;
  49212. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  49213. defined(WOLFSSL_PUBLIC_MP)
  49214. mp_int mp;
  49215. byte buf[32];
  49216. word32 keySz = (word32)sizeof(buf);
  49217. word32 len = (word32)sizeof(buf);
  49218. XMEMSET(&mp, 0, sizeof(mp_int));
  49219. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  49220. ExpectIntEQ(mp_set(&mp, 1234), 0);
  49221. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49222. BAD_FUNC_ARG);
  49223. len = sizeof(buf)-1;
  49224. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49225. BUFFER_E);
  49226. len = sizeof(buf);
  49227. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  49228. len = 4; /* test input too small */
  49229. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  49230. len = sizeof(buf);
  49231. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  49232. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  49233. ExpectIntEQ(len, 5);
  49234. mp_clear(&mp);
  49235. #endif
  49236. return EXPECT_RESULT();
  49237. } /* End test_wc_export_int*/
  49238. static int test_wc_InitRngNonce(void)
  49239. {
  49240. EXPECT_DECLS;
  49241. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49242. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49243. HAVE_FIPS_VERSION >= 2))
  49244. WC_RNG rng;
  49245. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49246. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49247. word32 nonceSz = sizeof(nonce);
  49248. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  49249. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49250. #endif
  49251. return EXPECT_RESULT();
  49252. } /* End test_wc_InitRngNonce*/
  49253. /*
  49254. * Testing wc_InitRngNonce_ex
  49255. */
  49256. static int test_wc_InitRngNonce_ex(void)
  49257. {
  49258. EXPECT_DECLS;
  49259. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49260. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49261. HAVE_FIPS_VERSION >= 2))
  49262. WC_RNG rng;
  49263. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49264. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49265. word32 nonceSz = sizeof(nonce);
  49266. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  49267. 0);
  49268. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49269. #endif
  49270. return EXPECT_RESULT();
  49271. } /* End test_wc_InitRngNonce_ex */
  49272. static int test_wolfSSL_X509_CRL(void)
  49273. {
  49274. EXPECT_DECLS;
  49275. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  49276. X509_CRL *crl = NULL;
  49277. char pem[][100] = {
  49278. "./certs/crl/crl.pem",
  49279. "./certs/crl/crl2.pem",
  49280. "./certs/crl/caEccCrl.pem",
  49281. "./certs/crl/eccCliCRL.pem",
  49282. "./certs/crl/eccSrvCRL.pem",
  49283. ""
  49284. };
  49285. #ifndef NO_BIO
  49286. BIO *bio = NULL;
  49287. #endif
  49288. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49289. char der[][100] = {
  49290. "./certs/crl/crl.der",
  49291. "./certs/crl/crl2.der",
  49292. ""};
  49293. #endif
  49294. XFILE fp = XBADFILE;
  49295. int i;
  49296. for (i = 0; pem[i][0] != '\0'; i++)
  49297. {
  49298. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49299. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49300. NULL, NULL));
  49301. ExpectNotNull(crl);
  49302. X509_CRL_free(crl);
  49303. if (fp != XBADFILE) {
  49304. XFCLOSE(fp);
  49305. fp = XBADFILE;
  49306. }
  49307. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49308. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  49309. NULL));
  49310. if (EXPECT_FAIL()) {
  49311. crl = NULL;
  49312. }
  49313. ExpectNotNull(crl);
  49314. X509_CRL_free(crl);
  49315. crl = NULL;
  49316. if (fp != XBADFILE) {
  49317. XFCLOSE(fp);
  49318. fp = XBADFILE;
  49319. }
  49320. }
  49321. #ifndef NO_BIO
  49322. for (i = 0; pem[i][0] != '\0'; i++)
  49323. {
  49324. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  49325. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  49326. X509_CRL_free(crl);
  49327. crl = NULL;
  49328. BIO_free(bio);
  49329. bio = NULL;
  49330. }
  49331. #endif
  49332. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49333. for (i = 0; der[i][0] != '\0'; i++) {
  49334. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  49335. ExpectTrue((fp != XBADFILE));
  49336. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  49337. ExpectNotNull(crl);
  49338. X509_CRL_free(crl);
  49339. if (fp != XBADFILE) {
  49340. XFCLOSE(fp);
  49341. fp = XBADFILE;
  49342. }
  49343. fp = XFOPEN(der[i], "rb");
  49344. ExpectTrue((fp != XBADFILE));
  49345. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  49346. if (EXPECT_FAIL()) {
  49347. crl = NULL;
  49348. }
  49349. ExpectNotNull(crl);
  49350. X509_CRL_free(crl);
  49351. crl = NULL;
  49352. if (fp != XBADFILE) {
  49353. XFCLOSE(fp);
  49354. fp = XBADFILE;
  49355. }
  49356. }
  49357. #endif
  49358. #endif
  49359. return EXPECT_RESULT();
  49360. }
  49361. static int test_wolfSSL_X509_load_crl_file(void)
  49362. {
  49363. EXPECT_DECLS;
  49364. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49365. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  49366. int i;
  49367. char pem[][100] = {
  49368. "./certs/crl/crl.pem",
  49369. "./certs/crl/crl2.pem",
  49370. "./certs/crl/caEccCrl.pem",
  49371. "./certs/crl/eccCliCRL.pem",
  49372. "./certs/crl/eccSrvCRL.pem",
  49373. #ifdef WC_RSA_PSS
  49374. "./certs/crl/crl_rsapss.pem",
  49375. #endif
  49376. ""
  49377. };
  49378. char der[][100] = {
  49379. "./certs/crl/crl.der",
  49380. "./certs/crl/crl2.der",
  49381. ""
  49382. };
  49383. WOLFSSL_X509_STORE* store = NULL;
  49384. WOLFSSL_X509_LOOKUP* lookup = NULL;
  49385. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49386. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49387. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49388. X509_FILETYPE_PEM), 1);
  49389. #ifdef WC_RSA_PSS
  49390. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  49391. X509_FILETYPE_PEM), 1);
  49392. #endif
  49393. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49394. X509_FILETYPE_PEM), 1);
  49395. if (store) {
  49396. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49397. WOLFSSL_FILETYPE_PEM), 1);
  49398. /* since store hasn't yet known the revoked cert*/
  49399. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49400. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49401. }
  49402. for (i = 0; pem[i][0] != '\0'; i++) {
  49403. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  49404. 1);
  49405. }
  49406. if (store) {
  49407. /* since store knows crl list */
  49408. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49409. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49410. CRL_CERT_REVOKED);
  49411. #ifdef WC_RSA_PSS
  49412. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49413. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  49414. CRL_CERT_REVOKED);
  49415. #endif
  49416. }
  49417. /* once feeing store */
  49418. X509_STORE_free(store);
  49419. store = NULL;
  49420. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49421. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49422. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49423. X509_FILETYPE_PEM), 1);
  49424. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49425. X509_FILETYPE_PEM), 1);
  49426. if (store) {
  49427. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49428. WOLFSSL_FILETYPE_PEM), 1);
  49429. /* since store hasn't yet known the revoked cert*/
  49430. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49431. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49432. }
  49433. for (i = 0; der[i][0] != '\0'; i++) {
  49434. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  49435. 1);
  49436. }
  49437. if (store) {
  49438. /* since store knows crl list */
  49439. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49440. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49441. CRL_CERT_REVOKED);
  49442. }
  49443. /* test for incorrect parameter */
  49444. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  49445. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  49446. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  49447. X509_STORE_free(store);
  49448. store = NULL;
  49449. #endif
  49450. return EXPECT_RESULT();
  49451. }
  49452. static int test_wolfSSL_i2d_X509(void)
  49453. {
  49454. EXPECT_DECLS;
  49455. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  49456. const unsigned char* cert_buf = server_cert_der_2048;
  49457. unsigned char* out = NULL;
  49458. unsigned char* tmp = NULL;
  49459. X509* cert = NULL;
  49460. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  49461. /* Pointer should be advanced */
  49462. ExpectPtrGT(cert_buf, server_cert_der_2048);
  49463. ExpectIntGT(i2d_X509(cert, &out), 0);
  49464. ExpectNotNull(out);
  49465. tmp = out;
  49466. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  49467. ExpectPtrGT(tmp, out);
  49468. if (out != NULL)
  49469. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  49470. X509_free(cert);
  49471. #endif
  49472. return EXPECT_RESULT();
  49473. }
  49474. static int test_wolfSSL_d2i_X509_REQ(void)
  49475. {
  49476. EXPECT_DECLS;
  49477. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  49478. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  49479. !defined(WOLFSSL_SP_MATH)
  49480. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  49481. * were generated by libest
  49482. * ./certs/csr.attr.der contains sample attributes
  49483. * ./certs/csr.ext.der contains sample extensions */
  49484. const char* csrFile = "./certs/csr.signed.der";
  49485. const char* csrPopFile = "./certs/csr.attr.der";
  49486. const char* csrExtFile = "./certs/csr.ext.der";
  49487. /* ./certs/csr.dsa.pem is generated using
  49488. * openssl req -newkey dsa:certs/dsaparams.pem \
  49489. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  49490. * -outform PEM
  49491. * with the passphrase "wolfSSL"
  49492. */
  49493. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49494. const char* csrDsaFile = "./certs/csr.dsa.pem";
  49495. XFILE f = XBADFILE;
  49496. #endif
  49497. BIO* bio = NULL;
  49498. X509* req = NULL;
  49499. EVP_PKEY *pub_key = NULL;
  49500. {
  49501. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  49502. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49503. /*
  49504. * Extract the public key from the CSR
  49505. */
  49506. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49507. /*
  49508. * Verify the signature in the CSR
  49509. */
  49510. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49511. X509_free(req);
  49512. req = NULL;
  49513. BIO_free(bio);
  49514. bio = NULL;
  49515. EVP_PKEY_free(pub_key);
  49516. pub_key = NULL;
  49517. }
  49518. {
  49519. #ifdef OPENSSL_ALL
  49520. X509_ATTRIBUTE* attr = NULL;
  49521. ASN1_TYPE *at = NULL;
  49522. #endif
  49523. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  49524. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49525. /*
  49526. * Extract the public key from the CSR
  49527. */
  49528. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49529. /*
  49530. * Verify the signature in the CSR
  49531. */
  49532. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49533. #ifdef OPENSSL_ALL
  49534. /*
  49535. * Obtain the challenge password from the CSR
  49536. */
  49537. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49538. -1), 1);
  49539. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  49540. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49541. ExpectNotNull(at->value.asn1_string);
  49542. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  49543. "2xIE+qqp/rhyTXP+");
  49544. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  49545. #endif
  49546. X509_free(req);
  49547. req = NULL;
  49548. BIO_free(bio);
  49549. bio = NULL;
  49550. EVP_PKEY_free(pub_key);
  49551. pub_key = NULL;
  49552. }
  49553. {
  49554. #ifdef OPENSSL_ALL
  49555. X509_ATTRIBUTE* attr = NULL;
  49556. ASN1_TYPE *at = NULL;
  49557. STACK_OF(X509_EXTENSION) *exts = NULL;
  49558. #endif
  49559. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  49560. /* This CSR contains an Extension Request attribute so
  49561. * we test extension parsing in a CSR attribute here. */
  49562. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49563. /*
  49564. * Extract the public key from the CSR
  49565. */
  49566. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49567. /*
  49568. * Verify the signature in the CSR
  49569. */
  49570. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49571. #ifdef OPENSSL_ALL
  49572. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  49573. req));
  49574. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  49575. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  49576. /*
  49577. * Obtain the challenge password from the CSR
  49578. */
  49579. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49580. -1), 0);
  49581. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  49582. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49583. ExpectNotNull(at->value.asn1_string);
  49584. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  49585. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  49586. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  49587. #endif
  49588. X509_free(req);
  49589. req = NULL;
  49590. BIO_free(bio);
  49591. bio = NULL;
  49592. EVP_PKEY_free(pub_key);
  49593. pub_key = NULL;
  49594. }
  49595. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49596. {
  49597. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  49598. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  49599. /*
  49600. * Extract the public key from the CSR
  49601. */
  49602. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49603. /*
  49604. * Verify the signature in the CSR
  49605. */
  49606. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49607. X509_free(req);
  49608. req = NULL;
  49609. BIO_free(bio);
  49610. /* Run the same test, but with a file pointer instead of a BIO.
  49611. * (PEM_read_X509_REQ)*/
  49612. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  49613. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  49614. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49615. X509_free(req);
  49616. EVP_PKEY_free(pub_key);
  49617. }
  49618. #endif /* !NO_DSA && !HAVE_SELFTEST */
  49619. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  49620. return EXPECT_RESULT();
  49621. }
  49622. static int test_wolfSSL_PEM_read_X509(void)
  49623. {
  49624. EXPECT_DECLS;
  49625. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49626. !defined(NO_RSA)
  49627. X509 *x509 = NULL;
  49628. XFILE fp = XBADFILE;
  49629. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  49630. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49631. X509_free(x509);
  49632. if (fp != XBADFILE)
  49633. XFCLOSE(fp);
  49634. #endif
  49635. return EXPECT_RESULT();
  49636. }
  49637. static int test_wolfSSL_PEM_read(void)
  49638. {
  49639. EXPECT_DECLS;
  49640. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49641. const char* filename = "./certs/server-keyEnc.pem";
  49642. XFILE fp = XBADFILE;
  49643. char* name = NULL;
  49644. char* header = NULL;
  49645. byte* data = NULL;
  49646. long len;
  49647. EVP_CIPHER_INFO cipher;
  49648. WOLFSSL_BIO* bio = NULL;
  49649. byte* fileData = NULL;
  49650. size_t fileDataSz = 0;
  49651. byte* out;
  49652. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  49653. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  49654. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  49655. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  49656. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  49657. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49658. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49659. ExpectIntGT(XSTRLEN(header), 0);
  49660. ExpectIntGT(len, 0);
  49661. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49662. name = NULL;
  49663. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49664. header = NULL;
  49665. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49666. data = NULL;
  49667. BIO_free(bio);
  49668. bio = NULL;
  49669. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  49670. /* Fail cases. */
  49671. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  49672. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  49673. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  49674. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  49675. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49676. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49677. ExpectIntGT(XSTRLEN(header), 0);
  49678. ExpectIntGT(len, 0);
  49679. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49680. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49681. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49682. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49683. DYNAMIC_TYPE_TMP_BUFFER));
  49684. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49685. if (fp != XBADFILE) {
  49686. XFCLOSE(fp);
  49687. fp = XBADFILE;
  49688. }
  49689. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49690. /* Fail cases. */
  49691. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49692. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49693. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49694. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49695. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49696. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49697. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49698. /* Fail cases. */
  49699. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  49700. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  49701. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  49702. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  49703. /* Pass case */
  49704. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  49705. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49706. name = NULL;
  49707. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49708. header = NULL;
  49709. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49710. data = NULL;
  49711. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  49712. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49713. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49714. ExpectIntGT(XSTRLEN(header), 0);
  49715. ExpectIntGT(len, 0);
  49716. /* Fail cases. */
  49717. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49718. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49719. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49720. #ifndef NO_DES3
  49721. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49722. #endif
  49723. /* Fail cases. */
  49724. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  49725. (void*)"yassl123"), WOLFSSL_FAILURE);
  49726. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49727. (void*)"yassl123"), WOLFSSL_FAILURE);
  49728. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49729. (void*)"yassl123"), WOLFSSL_FAILURE);
  49730. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49731. (void*)"yassl123"), WOLFSSL_FAILURE);
  49732. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  49733. (void*)"yassl123"), WOLFSSL_FAILURE);
  49734. #if !defined(NO_DES3) && !defined(NO_MD5)
  49735. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49736. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49737. #else
  49738. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49739. (void*)"yassl123"), WOLFSSL_FAILURE);
  49740. #endif
  49741. BIO_free(bio);
  49742. bio = NULL;
  49743. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49744. fileData = NULL;
  49745. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49746. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49747. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49748. name = NULL;
  49749. header = NULL;
  49750. data = NULL;
  49751. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49752. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49753. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49754. ExpectIntEQ(XSTRLEN(header), 0);
  49755. ExpectIntGT(len, 0);
  49756. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49757. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49758. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49759. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49760. DYNAMIC_TYPE_TMP_BUFFER));
  49761. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49762. if (fp != XBADFILE)
  49763. XFCLOSE(fp);
  49764. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49765. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49766. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49767. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49768. BIO_free(bio);
  49769. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49770. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49771. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49772. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49773. #endif
  49774. return EXPECT_RESULT();
  49775. }
  49776. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49777. {
  49778. EXPECT_DECLS;
  49779. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49780. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49781. const byte iv[12] = { 0 };
  49782. const byte key[16] = { 0 };
  49783. const byte cleartext[16] = { 0 };
  49784. const byte aad[] = {
  49785. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49786. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49787. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49788. 0x4f
  49789. };
  49790. byte out1Part[16];
  49791. byte outTag1Part[16];
  49792. byte out2Part[16];
  49793. byte outTag2Part[16];
  49794. byte decryptBuf[16];
  49795. int len = 0;
  49796. int tlen;
  49797. EVP_CIPHER_CTX* ctx = NULL;
  49798. /* ENCRYPT */
  49799. /* Send AAD and data in 1 part */
  49800. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49801. tlen = 0;
  49802. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49803. 1);
  49804. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49805. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49806. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49807. sizeof(cleartext)), 1);
  49808. tlen += len;
  49809. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49810. tlen += len;
  49811. ExpectIntEQ(tlen, sizeof(cleartext));
  49812. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49813. outTag1Part), 1);
  49814. EVP_CIPHER_CTX_free(ctx);
  49815. ctx = NULL;
  49816. /* DECRYPT */
  49817. /* Send AAD and data in 1 part */
  49818. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49819. tlen = 0;
  49820. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49821. 1);
  49822. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49823. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49824. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49825. sizeof(cleartext)), 1);
  49826. tlen += len;
  49827. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49828. outTag1Part), 1);
  49829. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49830. tlen += len;
  49831. ExpectIntEQ(tlen, sizeof(cleartext));
  49832. EVP_CIPHER_CTX_free(ctx);
  49833. ctx = NULL;
  49834. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49835. /* ENCRYPT */
  49836. /* Send AAD and data in 2 parts */
  49837. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49838. tlen = 0;
  49839. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49840. 1);
  49841. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49842. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49843. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49844. 1);
  49845. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49846. tlen += len;
  49847. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49848. sizeof(cleartext) - 1), 1);
  49849. tlen += len;
  49850. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49851. tlen += len;
  49852. ExpectIntEQ(tlen, sizeof(cleartext));
  49853. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49854. outTag2Part), 1);
  49855. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49856. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49857. EVP_CIPHER_CTX_free(ctx);
  49858. ctx = NULL;
  49859. /* DECRYPT */
  49860. /* Send AAD and data in 2 parts */
  49861. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49862. tlen = 0;
  49863. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49864. 1);
  49865. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49866. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49867. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49868. 1);
  49869. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49870. tlen += len;
  49871. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49872. sizeof(cleartext) - 1), 1);
  49873. tlen += len;
  49874. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49875. outTag1Part), 1);
  49876. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49877. tlen += len;
  49878. ExpectIntEQ(tlen, sizeof(cleartext));
  49879. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49880. /* Test AAD reuse */
  49881. EVP_CIPHER_CTX_free(ctx);
  49882. #endif
  49883. return EXPECT_RESULT();
  49884. }
  49885. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49886. {
  49887. EXPECT_DECLS;
  49888. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49889. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49890. /* Zero length plain text */
  49891. byte key[] = {
  49892. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49893. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49894. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49895. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49896. }; /* align */
  49897. byte iv[] = {
  49898. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49899. }; /* align */
  49900. byte plaintxt[1];
  49901. int ivSz = 12;
  49902. int plaintxtSz = 0;
  49903. unsigned char tag[16];
  49904. unsigned char tag_kat[] = {
  49905. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  49906. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  49907. };
  49908. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49909. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49910. int ciphertxtSz = 0;
  49911. int decryptedtxtSz = 0;
  49912. int len = 0;
  49913. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49914. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49915. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  49916. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49917. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49918. plaintxtSz));
  49919. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49920. ciphertxtSz += len;
  49921. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49922. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49923. ExpectIntEQ(0, ciphertxtSz);
  49924. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49925. EVP_CIPHER_CTX_init(de);
  49926. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  49927. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49928. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49929. decryptedtxtSz = len;
  49930. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49931. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49932. decryptedtxtSz += len;
  49933. ExpectIntEQ(0, decryptedtxtSz);
  49934. EVP_CIPHER_CTX_free(en);
  49935. EVP_CIPHER_CTX_free(de);
  49936. #endif
  49937. return EXPECT_RESULT();
  49938. }
  49939. static int test_wolfssl_EVP_aes_gcm(void)
  49940. {
  49941. EXPECT_DECLS;
  49942. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49943. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49944. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49945. byte *key = (byte*)"01234567890123456789012345678901";
  49946. /* A 128 bit IV */
  49947. byte *iv = (byte*)"0123456789012345";
  49948. int ivSz = AES_BLOCK_SIZE;
  49949. /* Message to be encrypted */
  49950. byte *plaintxt = (byte*)"for things to change you have to change";
  49951. /* Additional non-confidential data */
  49952. byte *aad = (byte*)"Don't spend major time on minor things.";
  49953. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49954. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49955. int aadSz = (int)XSTRLEN((char*)aad);
  49956. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49957. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49958. int ciphertxtSz = 0;
  49959. int decryptedtxtSz = 0;
  49960. int len = 0;
  49961. int i = 0;
  49962. EVP_CIPHER_CTX en[2];
  49963. EVP_CIPHER_CTX de[2];
  49964. for (i = 0; i < 2; i++) {
  49965. EVP_CIPHER_CTX_init(&en[i]);
  49966. if (i == 0) {
  49967. /* Default uses 96-bits IV length */
  49968. #ifdef WOLFSSL_AES_128
  49969. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49970. key, iv));
  49971. #elif defined(WOLFSSL_AES_192)
  49972. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49973. key, iv));
  49974. #elif defined(WOLFSSL_AES_256)
  49975. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49976. key, iv));
  49977. #endif
  49978. }
  49979. else {
  49980. #ifdef WOLFSSL_AES_128
  49981. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49982. NULL, NULL));
  49983. #elif defined(WOLFSSL_AES_192)
  49984. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49985. NULL, NULL));
  49986. #elif defined(WOLFSSL_AES_256)
  49987. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49988. NULL, NULL));
  49989. #endif
  49990. /* non-default must to set the IV length first */
  49991. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  49992. ivSz, NULL));
  49993. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49994. }
  49995. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49996. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49997. plaintxtSz));
  49998. ciphertxtSz = len;
  49999. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50000. ciphertxtSz += len;
  50001. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50002. AES_BLOCK_SIZE, tag));
  50003. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50004. EVP_CIPHER_CTX_init(&de[i]);
  50005. if (i == 0) {
  50006. /* Default uses 96-bits IV length */
  50007. #ifdef WOLFSSL_AES_128
  50008. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50009. key, iv));
  50010. #elif defined(WOLFSSL_AES_192)
  50011. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50012. key, iv));
  50013. #elif defined(WOLFSSL_AES_256)
  50014. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50015. key, iv));
  50016. #endif
  50017. }
  50018. else {
  50019. #ifdef WOLFSSL_AES_128
  50020. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50021. NULL, NULL));
  50022. #elif defined(WOLFSSL_AES_192)
  50023. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50024. NULL, NULL));
  50025. #elif defined(WOLFSSL_AES_256)
  50026. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50027. NULL, NULL));
  50028. #endif
  50029. /* non-default must to set the IV length first */
  50030. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50031. ivSz, NULL));
  50032. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50033. }
  50034. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50035. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50036. ciphertxtSz));
  50037. decryptedtxtSz = len;
  50038. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50039. AES_BLOCK_SIZE, tag));
  50040. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50041. decryptedtxtSz += len;
  50042. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50043. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50044. /* modify tag*/
  50045. if (i == 0) {
  50046. /* Default uses 96-bits IV length */
  50047. #ifdef WOLFSSL_AES_128
  50048. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50049. key, iv));
  50050. #elif defined(WOLFSSL_AES_192)
  50051. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50052. key, iv));
  50053. #elif defined(WOLFSSL_AES_256)
  50054. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50055. key, iv));
  50056. #endif
  50057. }
  50058. else {
  50059. #ifdef WOLFSSL_AES_128
  50060. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50061. NULL, NULL));
  50062. #elif defined(WOLFSSL_AES_192)
  50063. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50064. NULL, NULL));
  50065. #elif defined(WOLFSSL_AES_256)
  50066. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50067. NULL, NULL));
  50068. #endif
  50069. /* non-default must to set the IV length first */
  50070. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50071. ivSz, NULL));
  50072. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50073. }
  50074. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50075. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50076. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50077. AES_BLOCK_SIZE, tag));
  50078. /* fail due to wrong tag */
  50079. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50080. ciphertxtSz));
  50081. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50082. ExpectIntEQ(0, len);
  50083. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50084. }
  50085. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50086. return EXPECT_RESULT();
  50087. }
  50088. static int test_wolfssl_EVP_aria_gcm(void)
  50089. {
  50090. int res = TEST_SKIPPED;
  50091. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50092. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50093. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50094. byte *key = (byte*)"01234567890123456789012345678901";
  50095. /* A 128 bit IV */
  50096. byte *iv = (byte*)"0123456789012345";
  50097. int ivSz = ARIA_BLOCK_SIZE;
  50098. /* Message to be encrypted */
  50099. const int plaintxtSz = 40;
  50100. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50101. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50102. /* Additional non-confidential data */
  50103. byte *aad = (byte*)"Don't spend major time on minor things.";
  50104. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50105. int aadSz = (int)XSTRLEN((char*)aad);
  50106. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50107. byte decryptedtxt[plaintxtSz];
  50108. int ciphertxtSz = 0;
  50109. int decryptedtxtSz = 0;
  50110. int len = 0;
  50111. int i = 0;
  50112. #define TEST_ARIA_GCM_COUNT 6
  50113. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  50114. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  50115. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  50116. EVP_CIPHER_CTX_init(&en[i]);
  50117. switch (i) {
  50118. case 0:
  50119. /* Default uses 96-bits IV length */
  50120. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  50121. break;
  50122. case 1:
  50123. /* Default uses 96-bits IV length */
  50124. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  50125. break;
  50126. case 2:
  50127. /* Default uses 96-bits IV length */
  50128. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  50129. break;
  50130. case 3:
  50131. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50132. /* non-default must to set the IV length first */
  50133. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50134. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50135. break;
  50136. case 4:
  50137. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50138. /* non-default must to set the IV length first */
  50139. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50140. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50141. break;
  50142. case 5:
  50143. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50144. /* non-default must to set the IV length first */
  50145. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50146. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50147. break;
  50148. }
  50149. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  50150. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50151. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  50152. ciphertxtSz = len;
  50153. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50154. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  50155. ciphertxtSz += len;
  50156. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  50157. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50158. EVP_CIPHER_CTX_init(&de[i]);
  50159. switch (i) {
  50160. case 0:
  50161. /* Default uses 96-bits IV length */
  50162. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  50163. break;
  50164. case 1:
  50165. /* Default uses 96-bits IV length */
  50166. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  50167. break;
  50168. case 2:
  50169. /* Default uses 96-bits IV length */
  50170. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  50171. break;
  50172. case 3:
  50173. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50174. /* non-default must to set the IV length first */
  50175. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50176. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50177. break;
  50178. case 4:
  50179. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50180. /* non-default must to set the IV length first */
  50181. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50182. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50183. break;
  50184. case 5:
  50185. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50186. /* non-default must to set the IV length first */
  50187. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50188. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50189. break;
  50190. }
  50191. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50192. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50193. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50194. decryptedtxtSz = len;
  50195. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50196. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50197. decryptedtxtSz += len;
  50198. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  50199. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50200. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50201. /* modify tag*/
  50202. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50203. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50204. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50205. /* fail due to wrong tag */
  50206. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50207. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50208. AssertIntEQ(0, len);
  50209. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50210. }
  50211. res = TEST_RES_CHECK(1);
  50212. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50213. return res;
  50214. }
  50215. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  50216. {
  50217. EXPECT_DECLS;
  50218. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50219. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50220. /* Zero length plain text */
  50221. byte key[] = {
  50222. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50223. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50224. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50225. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50226. }; /* align */
  50227. byte iv[] = {
  50228. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50229. }; /* align */
  50230. byte plaintxt[1];
  50231. int ivSz = 12;
  50232. int plaintxtSz = 0;
  50233. unsigned char tag[16];
  50234. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50235. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50236. int ciphertxtSz = 0;
  50237. int decryptedtxtSz = 0;
  50238. int len = 0;
  50239. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50240. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50241. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  50242. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50243. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50244. plaintxtSz));
  50245. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50246. ciphertxtSz += len;
  50247. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50248. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50249. ExpectIntEQ(0, ciphertxtSz);
  50250. EVP_CIPHER_CTX_init(de);
  50251. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  50252. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50253. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50254. decryptedtxtSz = len;
  50255. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50256. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50257. decryptedtxtSz += len;
  50258. ExpectIntEQ(0, decryptedtxtSz);
  50259. EVP_CIPHER_CTX_free(en);
  50260. EVP_CIPHER_CTX_free(de);
  50261. #endif
  50262. return EXPECT_RESULT();
  50263. }
  50264. static int test_wolfssl_EVP_aes_ccm(void)
  50265. {
  50266. EXPECT_DECLS;
  50267. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50268. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50269. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50270. byte *key = (byte*)"01234567890123456789012345678901";
  50271. /* A 128 bit IV */
  50272. byte *iv = (byte*)"0123456789012";
  50273. int ivSz = (int)XSTRLEN((char*)iv);
  50274. /* Message to be encrypted */
  50275. byte *plaintxt = (byte*)"for things to change you have to change";
  50276. /* Additional non-confidential data */
  50277. byte *aad = (byte*)"Don't spend major time on minor things.";
  50278. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50279. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50280. int aadSz = (int)XSTRLEN((char*)aad);
  50281. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50282. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50283. int ciphertxtSz = 0;
  50284. int decryptedtxtSz = 0;
  50285. int len = 0;
  50286. int i = 0;
  50287. int ret;
  50288. EVP_CIPHER_CTX en[2];
  50289. EVP_CIPHER_CTX de[2];
  50290. for (i = 0; i < 2; i++) {
  50291. EVP_CIPHER_CTX_init(&en[i]);
  50292. if (i == 0) {
  50293. /* Default uses 96-bits IV length */
  50294. #ifdef WOLFSSL_AES_128
  50295. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50296. key, iv));
  50297. #elif defined(WOLFSSL_AES_192)
  50298. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50299. key, iv));
  50300. #elif defined(WOLFSSL_AES_256)
  50301. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50302. key, iv));
  50303. #endif
  50304. }
  50305. else {
  50306. #ifdef WOLFSSL_AES_128
  50307. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50308. NULL, NULL));
  50309. #elif defined(WOLFSSL_AES_192)
  50310. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50311. NULL, NULL));
  50312. #elif defined(WOLFSSL_AES_256)
  50313. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50314. NULL, NULL));
  50315. #endif
  50316. /* non-default must to set the IV length first */
  50317. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50318. ivSz, NULL));
  50319. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50320. }
  50321. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50322. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50323. plaintxtSz));
  50324. ciphertxtSz = len;
  50325. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50326. ciphertxtSz += len;
  50327. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50328. AES_BLOCK_SIZE, tag));
  50329. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  50330. ExpectIntEQ(ret, 1);
  50331. EVP_CIPHER_CTX_init(&de[i]);
  50332. if (i == 0) {
  50333. /* Default uses 96-bits IV length */
  50334. #ifdef WOLFSSL_AES_128
  50335. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50336. key, iv));
  50337. #elif defined(WOLFSSL_AES_192)
  50338. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50339. key, iv));
  50340. #elif defined(WOLFSSL_AES_256)
  50341. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50342. key, iv));
  50343. #endif
  50344. }
  50345. else {
  50346. #ifdef WOLFSSL_AES_128
  50347. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50348. NULL, NULL));
  50349. #elif defined(WOLFSSL_AES_192)
  50350. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50351. NULL, NULL));
  50352. #elif defined(WOLFSSL_AES_256)
  50353. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50354. NULL, NULL));
  50355. #endif
  50356. /* non-default must to set the IV length first */
  50357. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50358. ivSz, NULL));
  50359. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50360. }
  50361. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50362. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50363. ciphertxtSz));
  50364. decryptedtxtSz = len;
  50365. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50366. AES_BLOCK_SIZE, tag));
  50367. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50368. decryptedtxtSz += len;
  50369. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50370. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50371. /* modify tag*/
  50372. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50373. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50374. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50375. AES_BLOCK_SIZE, tag));
  50376. /* fail due to wrong tag */
  50377. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50378. ciphertxtSz));
  50379. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50380. ExpectIntEQ(0, len);
  50381. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  50382. ExpectIntEQ(ret, 1);
  50383. }
  50384. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  50385. return EXPECT_RESULT();
  50386. }
  50387. static int test_wolfssl_EVP_chacha20_poly1305(void)
  50388. {
  50389. EXPECT_DECLS;
  50390. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  50391. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  50392. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  50393. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50394. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  50395. byte cipherText[sizeof(plainText)];
  50396. byte decryptedText[sizeof(plainText)];
  50397. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  50398. EVP_CIPHER_CTX* ctx = NULL;
  50399. int outSz;
  50400. /* Encrypt. */
  50401. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50402. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50403. NULL), WOLFSSL_SUCCESS);
  50404. /* Invalid IV length. */
  50405. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50406. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  50407. /* Valid IV length. */
  50408. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50409. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50410. /* Invalid tag length. */
  50411. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50412. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  50413. /* Valid tag length. */
  50414. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50415. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  50416. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50417. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50418. WOLFSSL_SUCCESS);
  50419. ExpectIntEQ(outSz, sizeof(aad));
  50420. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50421. sizeof(plainText)), WOLFSSL_SUCCESS);
  50422. ExpectIntEQ(outSz, sizeof(plainText));
  50423. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50424. ExpectIntEQ(outSz, 0);
  50425. /* Invalid tag length. */
  50426. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50427. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  50428. /* Valid tag length. */
  50429. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50430. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50431. EVP_CIPHER_CTX_free(ctx);
  50432. ctx = NULL;
  50433. /* Decrypt. */
  50434. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50435. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50436. NULL), WOLFSSL_SUCCESS);
  50437. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50438. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50439. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50440. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50441. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50442. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50443. WOLFSSL_SUCCESS);
  50444. ExpectIntEQ(outSz, sizeof(aad));
  50445. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50446. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50447. ExpectIntEQ(outSz, sizeof(cipherText));
  50448. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50449. WOLFSSL_SUCCESS);
  50450. ExpectIntEQ(outSz, 0);
  50451. EVP_CIPHER_CTX_free(ctx);
  50452. ctx = NULL;
  50453. /* Test partial Inits. CipherInit() allow setting of key and iv
  50454. * in separate calls. */
  50455. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50456. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  50457. key, NULL, 1), WOLFSSL_SUCCESS);
  50458. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50459. WOLFSSL_SUCCESS);
  50460. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  50461. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  50462. ExpectIntEQ(outSz, sizeof(aad));
  50463. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50464. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50465. ExpectIntEQ(outSz, sizeof(cipherText));
  50466. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50467. WOLFSSL_SUCCESS);
  50468. ExpectIntEQ(outSz, 0);
  50469. EVP_CIPHER_CTX_free(ctx);
  50470. #endif
  50471. return EXPECT_RESULT();
  50472. }
  50473. static int test_wolfssl_EVP_chacha20(void)
  50474. {
  50475. EXPECT_DECLS;
  50476. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  50477. byte key[CHACHA_MAX_KEY_SZ];
  50478. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  50479. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50480. byte cipherText[sizeof(plainText)];
  50481. byte decryptedText[sizeof(plainText)];
  50482. EVP_CIPHER_CTX* ctx = NULL;
  50483. int outSz;
  50484. XMEMSET(key, 0, sizeof(key));
  50485. XMEMSET(iv, 0, sizeof(iv));
  50486. /* Encrypt. */
  50487. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50488. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50489. NULL), WOLFSSL_SUCCESS);
  50490. /* Any tag length must fail - not an AEAD cipher. */
  50491. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50492. 16, NULL), WOLFSSL_FAILURE);
  50493. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50494. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50495. sizeof(plainText)), WOLFSSL_SUCCESS);
  50496. ExpectIntEQ(outSz, sizeof(plainText));
  50497. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50498. ExpectIntEQ(outSz, 0);
  50499. EVP_CIPHER_CTX_free(ctx);
  50500. ctx = NULL;
  50501. /* Decrypt. */
  50502. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50503. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50504. NULL), WOLFSSL_SUCCESS);
  50505. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50506. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50507. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50508. ExpectIntEQ(outSz, sizeof(cipherText));
  50509. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50510. WOLFSSL_SUCCESS);
  50511. ExpectIntEQ(outSz, 0);
  50512. EVP_CIPHER_CTX_free(ctx);
  50513. ctx = NULL;
  50514. /* Test partial Inits. CipherInit() allow setting of key and iv
  50515. * in separate calls. */
  50516. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50517. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  50518. key, NULL, 1), WOLFSSL_SUCCESS);
  50519. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50520. WOLFSSL_SUCCESS);
  50521. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50522. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50523. ExpectIntEQ(outSz, sizeof(cipherText));
  50524. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50525. WOLFSSL_SUCCESS);
  50526. ExpectIntEQ(outSz, 0);
  50527. EVP_CIPHER_CTX_free(ctx);
  50528. #endif
  50529. return EXPECT_RESULT();
  50530. }
  50531. static int test_wolfssl_EVP_sm4_ecb(void)
  50532. {
  50533. int res = TEST_SKIPPED;
  50534. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  50535. EXPECT_DECLS;
  50536. byte key[SM4_KEY_SIZE];
  50537. byte plainText[SM4_BLOCK_SIZE] = {
  50538. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50539. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50540. };
  50541. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50542. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50543. EVP_CIPHER_CTX* ctx;
  50544. int outSz;
  50545. XMEMSET(key, 0, sizeof(key));
  50546. /* Encrypt. */
  50547. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50548. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50549. WOLFSSL_SUCCESS);
  50550. /* Any tag length must fail - not an AEAD cipher. */
  50551. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50552. WOLFSSL_FAILURE);
  50553. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  50554. WOLFSSL_SUCCESS);
  50555. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50556. sizeof(plainText)), WOLFSSL_SUCCESS);
  50557. ExpectIntEQ(outSz, sizeof(plainText));
  50558. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50559. WOLFSSL_SUCCESS);
  50560. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50561. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50562. EVP_CIPHER_CTX_free(ctx);
  50563. /* Decrypt. */
  50564. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50565. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50566. WOLFSSL_SUCCESS);
  50567. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  50568. WOLFSSL_SUCCESS);
  50569. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50570. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50571. ExpectIntEQ(outSz, sizeof(plainText));
  50572. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50573. WOLFSSL_SUCCESS);
  50574. ExpectIntEQ(outSz, 0);
  50575. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50576. EVP_CIPHER_CTX_free(ctx);
  50577. res = EXPECT_RESULT();
  50578. #endif
  50579. return res;
  50580. }
  50581. static int test_wolfssl_EVP_sm4_cbc(void)
  50582. {
  50583. int res = TEST_SKIPPED;
  50584. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  50585. EXPECT_DECLS;
  50586. byte key[SM4_KEY_SIZE];
  50587. byte iv[SM4_BLOCK_SIZE];
  50588. byte plainText[SM4_BLOCK_SIZE] = {
  50589. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50590. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50591. };
  50592. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50593. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50594. EVP_CIPHER_CTX* ctx;
  50595. int outSz;
  50596. XMEMSET(key, 0, sizeof(key));
  50597. XMEMSET(iv, 0, sizeof(iv));
  50598. /* Encrypt. */
  50599. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50600. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50601. WOLFSSL_SUCCESS);
  50602. /* Any tag length must fail - not an AEAD cipher. */
  50603. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50604. WOLFSSL_FAILURE);
  50605. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50606. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50607. sizeof(plainText)), WOLFSSL_SUCCESS);
  50608. ExpectIntEQ(outSz, sizeof(plainText));
  50609. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50610. WOLFSSL_SUCCESS);
  50611. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50612. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50613. EVP_CIPHER_CTX_free(ctx);
  50614. /* Decrypt. */
  50615. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50616. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50617. WOLFSSL_SUCCESS);
  50618. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50619. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50620. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50621. ExpectIntEQ(outSz, sizeof(plainText));
  50622. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50623. WOLFSSL_SUCCESS);
  50624. ExpectIntEQ(outSz, 0);
  50625. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50626. EVP_CIPHER_CTX_free(ctx);
  50627. /* Test partial Inits. CipherInit() allow setting of key and iv
  50628. * in separate calls. */
  50629. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50630. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  50631. WOLFSSL_SUCCESS);
  50632. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  50633. WOLFSSL_SUCCESS);
  50634. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50635. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50636. ExpectIntEQ(outSz, sizeof(plainText));
  50637. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50638. WOLFSSL_SUCCESS);
  50639. ExpectIntEQ(outSz, 0);
  50640. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50641. EVP_CIPHER_CTX_free(ctx);
  50642. res = EXPECT_RESULT();
  50643. #endif
  50644. return res;
  50645. }
  50646. static int test_wolfssl_EVP_sm4_ctr(void)
  50647. {
  50648. int res = TEST_SKIPPED;
  50649. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  50650. EXPECT_DECLS;
  50651. byte key[SM4_KEY_SIZE];
  50652. byte iv[SM4_BLOCK_SIZE];
  50653. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50654. byte cipherText[sizeof(plainText)];
  50655. byte decryptedText[sizeof(plainText)];
  50656. EVP_CIPHER_CTX* ctx;
  50657. int outSz;
  50658. XMEMSET(key, 0, sizeof(key));
  50659. XMEMSET(iv, 0, sizeof(iv));
  50660. /* Encrypt. */
  50661. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50662. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50663. WOLFSSL_SUCCESS);
  50664. /* Any tag length must fail - not an AEAD cipher. */
  50665. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50666. WOLFSSL_FAILURE);
  50667. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50668. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50669. sizeof(plainText)), WOLFSSL_SUCCESS);
  50670. ExpectIntEQ(outSz, sizeof(plainText));
  50671. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50672. ExpectIntEQ(outSz, 0);
  50673. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50674. EVP_CIPHER_CTX_free(ctx);
  50675. /* Decrypt. */
  50676. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50677. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50678. WOLFSSL_SUCCESS);
  50679. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50680. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50681. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50682. ExpectIntEQ(outSz, sizeof(cipherText));
  50683. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50684. WOLFSSL_SUCCESS);
  50685. ExpectIntEQ(outSz, 0);
  50686. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50687. EVP_CIPHER_CTX_free(ctx);
  50688. /* Test partial Inits. CipherInit() allow setting of key and iv
  50689. * in separate calls. */
  50690. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50691. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  50692. WOLFSSL_SUCCESS);
  50693. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50694. WOLFSSL_SUCCESS);
  50695. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50696. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50697. ExpectIntEQ(outSz, sizeof(cipherText));
  50698. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50699. WOLFSSL_SUCCESS);
  50700. ExpectIntEQ(outSz, 0);
  50701. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50702. EVP_CIPHER_CTX_free(ctx);
  50703. res = EXPECT_RESULT();
  50704. #endif
  50705. return res;
  50706. }
  50707. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  50708. {
  50709. int res = TEST_SKIPPED;
  50710. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50711. /* Zero length plain text */
  50712. EXPECT_DECLS;
  50713. byte key[] = {
  50714. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50718. }; /* align */
  50719. byte iv[] = {
  50720. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50721. }; /* align */
  50722. byte plaintxt[1];
  50723. int ivSz = 12;
  50724. int plaintxtSz = 0;
  50725. unsigned char tag[16];
  50726. unsigned char tag_kat[16] = {
  50727. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  50728. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  50729. };
  50730. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50731. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50732. int ciphertxtSz = 0;
  50733. int decryptedtxtSz = 0;
  50734. int len = 0;
  50735. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50736. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50737. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  50738. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50739. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50740. plaintxtSz));
  50741. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50742. ciphertxtSz += len;
  50743. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50744. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50745. ExpectIntEQ(0, ciphertxtSz);
  50746. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50747. EVP_CIPHER_CTX_init(de);
  50748. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  50749. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50750. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50751. decryptedtxtSz = len;
  50752. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50753. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50754. decryptedtxtSz += len;
  50755. ExpectIntEQ(0, decryptedtxtSz);
  50756. EVP_CIPHER_CTX_free(en);
  50757. EVP_CIPHER_CTX_free(de);
  50758. res = EXPECT_RESULT();
  50759. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50760. return res;
  50761. }
  50762. static int test_wolfssl_EVP_sm4_gcm(void)
  50763. {
  50764. int res = TEST_SKIPPED;
  50765. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50766. EXPECT_DECLS;
  50767. byte *key = (byte*)"0123456789012345";
  50768. /* A 128 bit IV */
  50769. byte *iv = (byte*)"0123456789012345";
  50770. int ivSz = SM4_BLOCK_SIZE;
  50771. /* Message to be encrypted */
  50772. byte *plaintxt = (byte*)"for things to change you have to change";
  50773. /* Additional non-confidential data */
  50774. byte *aad = (byte*)"Don't spend major time on minor things.";
  50775. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50776. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50777. int aadSz = (int)XSTRLEN((char*)aad);
  50778. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50779. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50780. int ciphertxtSz = 0;
  50781. int decryptedtxtSz = 0;
  50782. int len = 0;
  50783. int i = 0;
  50784. EVP_CIPHER_CTX en[2];
  50785. EVP_CIPHER_CTX de[2];
  50786. for (i = 0; i < 2; i++) {
  50787. EVP_CIPHER_CTX_init(&en[i]);
  50788. if (i == 0) {
  50789. /* Default uses 96-bits IV length */
  50790. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  50791. iv));
  50792. }
  50793. else {
  50794. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  50795. NULL));
  50796. /* non-default must to set the IV length first */
  50797. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50798. ivSz, NULL));
  50799. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50800. }
  50801. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50802. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50803. plaintxtSz));
  50804. ciphertxtSz = len;
  50805. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50806. ciphertxtSz += len;
  50807. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50808. SM4_BLOCK_SIZE, tag));
  50809. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50810. EVP_CIPHER_CTX_init(&de[i]);
  50811. if (i == 0) {
  50812. /* Default uses 96-bits IV length */
  50813. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  50814. iv));
  50815. }
  50816. else {
  50817. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  50818. NULL));
  50819. /* non-default must to set the IV length first */
  50820. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50821. ivSz, NULL));
  50822. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50823. }
  50824. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50825. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50826. ciphertxtSz));
  50827. decryptedtxtSz = len;
  50828. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50829. SM4_BLOCK_SIZE, tag));
  50830. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50831. decryptedtxtSz += len;
  50832. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50833. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50834. /* modify tag*/
  50835. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50836. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50837. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50838. SM4_BLOCK_SIZE, tag));
  50839. /* fail due to wrong tag */
  50840. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50841. ciphertxtSz));
  50842. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50843. ExpectIntEQ(0, len);
  50844. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50845. }
  50846. res = EXPECT_RESULT();
  50847. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50848. return res;
  50849. }
  50850. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  50851. {
  50852. int res = TEST_SKIPPED;
  50853. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50854. /* Zero length plain text */
  50855. EXPECT_DECLS;
  50856. byte key[] = {
  50857. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50858. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50859. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50860. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50861. }; /* align */
  50862. byte iv[] = {
  50863. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50864. }; /* align */
  50865. byte plaintxt[1];
  50866. int ivSz = 12;
  50867. int plaintxtSz = 0;
  50868. unsigned char tag[16];
  50869. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50870. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50871. int ciphertxtSz = 0;
  50872. int decryptedtxtSz = 0;
  50873. int len = 0;
  50874. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50875. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50876. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  50877. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50878. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50879. plaintxtSz));
  50880. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50881. ciphertxtSz += len;
  50882. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50883. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50884. ExpectIntEQ(0, ciphertxtSz);
  50885. EVP_CIPHER_CTX_init(de);
  50886. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  50887. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50888. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50889. decryptedtxtSz = len;
  50890. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50891. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50892. decryptedtxtSz += len;
  50893. ExpectIntEQ(0, decryptedtxtSz);
  50894. EVP_CIPHER_CTX_free(en);
  50895. EVP_CIPHER_CTX_free(de);
  50896. res = EXPECT_RESULT();
  50897. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50898. return res;
  50899. }
  50900. static int test_wolfssl_EVP_sm4_ccm(void)
  50901. {
  50902. int res = TEST_SKIPPED;
  50903. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50904. EXPECT_DECLS;
  50905. byte *key = (byte*)"0123456789012345";
  50906. byte *iv = (byte*)"0123456789012";
  50907. int ivSz = (int)XSTRLEN((char*)iv);
  50908. /* Message to be encrypted */
  50909. byte *plaintxt = (byte*)"for things to change you have to change";
  50910. /* Additional non-confidential data */
  50911. byte *aad = (byte*)"Don't spend major time on minor things.";
  50912. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50913. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50914. int aadSz = (int)XSTRLEN((char*)aad);
  50915. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50916. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50917. int ciphertxtSz = 0;
  50918. int decryptedtxtSz = 0;
  50919. int len = 0;
  50920. int i = 0;
  50921. EVP_CIPHER_CTX en[2];
  50922. EVP_CIPHER_CTX de[2];
  50923. for (i = 0; i < 2; i++) {
  50924. EVP_CIPHER_CTX_init(&en[i]);
  50925. if (i == 0) {
  50926. /* Default uses 96-bits IV length */
  50927. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  50928. iv));
  50929. }
  50930. else {
  50931. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  50932. NULL));
  50933. /* non-default must to set the IV length first */
  50934. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50935. ivSz, NULL));
  50936. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50937. }
  50938. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50939. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50940. plaintxtSz));
  50941. ciphertxtSz = len;
  50942. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50943. ciphertxtSz += len;
  50944. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50945. SM4_BLOCK_SIZE, tag));
  50946. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50947. EVP_CIPHER_CTX_init(&de[i]);
  50948. if (i == 0) {
  50949. /* Default uses 96-bits IV length */
  50950. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  50951. iv));
  50952. }
  50953. else {
  50954. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  50955. NULL));
  50956. /* non-default must to set the IV length first */
  50957. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50958. ivSz, NULL));
  50959. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50960. }
  50961. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50962. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50963. ciphertxtSz));
  50964. decryptedtxtSz = len;
  50965. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50966. SM4_BLOCK_SIZE, tag));
  50967. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50968. decryptedtxtSz += len;
  50969. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50970. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50971. /* modify tag*/
  50972. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50973. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50974. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50975. SM4_BLOCK_SIZE, tag));
  50976. /* fail due to wrong tag */
  50977. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50978. ciphertxtSz));
  50979. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50980. ExpectIntEQ(0, len);
  50981. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50982. }
  50983. res = EXPECT_RESULT();
  50984. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50985. return res;
  50986. }
  50987. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  50988. {
  50989. EXPECT_DECLS;
  50990. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  50991. EVP_PKEY_CTX* ctx = NULL;
  50992. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  50993. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  50994. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  50995. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  50996. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  50997. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  50998. byte outKey[34];
  50999. size_t outKeySz = sizeof(outKey);
  51000. /* These expected outputs were gathered by running the same test below using
  51001. * OpenSSL. */
  51002. const byte extractAndExpand[] = {
  51003. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51004. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51005. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51006. };
  51007. const byte extractOnly[] = {
  51008. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51009. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51010. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51011. };
  51012. const byte expandOnly[] = {
  51013. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51014. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51015. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51016. };
  51017. const byte extractAndExpandAddInfo[] = {
  51018. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51019. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51020. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51021. };
  51022. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51023. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51024. /* NULL ctx. */
  51025. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51026. /* NULL md. */
  51027. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51028. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51029. /* NULL ctx. */
  51030. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51031. WOLFSSL_FAILURE);
  51032. /* NULL salt is ok. */
  51033. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51034. WOLFSSL_SUCCESS);
  51035. /* Salt length <= 0. */
  51036. /* Length 0 salt is ok. */
  51037. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51038. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51039. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51040. WOLFSSL_SUCCESS);
  51041. /* NULL ctx. */
  51042. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51043. WOLFSSL_FAILURE);
  51044. /* NULL key. */
  51045. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51046. WOLFSSL_FAILURE);
  51047. /* Key length <= 0 */
  51048. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51049. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51050. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51051. WOLFSSL_SUCCESS);
  51052. /* NULL ctx. */
  51053. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51054. WOLFSSL_FAILURE);
  51055. /* NULL info is ok. */
  51056. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51057. WOLFSSL_SUCCESS);
  51058. /* Info length <= 0 */
  51059. /* Length 0 info is ok. */
  51060. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51061. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51062. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51063. WOLFSSL_SUCCESS);
  51064. /* NULL ctx. */
  51065. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51066. WOLFSSL_FAILURE);
  51067. /* Extract and expand (default). */
  51068. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51069. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51070. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51071. /* Extract only. */
  51072. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51073. WOLFSSL_SUCCESS);
  51074. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51075. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51076. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51077. outKeySz = sizeof(outKey);
  51078. /* Expand only. */
  51079. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51080. WOLFSSL_SUCCESS);
  51081. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51082. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51083. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51084. outKeySz = sizeof(outKey);
  51085. /* Extract and expand with appended additional info. */
  51086. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51087. WOLFSSL_SUCCESS);
  51088. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51089. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51090. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51091. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51092. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51093. EVP_PKEY_CTX_free(ctx);
  51094. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51095. return EXPECT_RESULT();
  51096. }
  51097. #ifndef NO_BIO
  51098. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51099. {
  51100. EXPECT_DECLS;
  51101. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51102. BIO* bio = NULL;
  51103. X509_INFO* info = NULL;
  51104. STACK_OF(X509_INFO)* sk = NULL;
  51105. char* subject = NULL;
  51106. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51107. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51108. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51109. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51110. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51111. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  51112. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  51113. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  51114. /* using dereference to maintain testing for Apache port*/
  51115. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51116. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51117. 0, 0));
  51118. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  51119. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51120. X509_INFO_free(info);
  51121. info = NULL;
  51122. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51123. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51124. 0, 0));
  51125. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  51126. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51127. X509_INFO_free(info);
  51128. ExpectNull(info = sk_X509_INFO_pop(sk));
  51129. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  51130. BIO_free(bio);
  51131. #endif
  51132. return EXPECT_RESULT();
  51133. }
  51134. #endif /* !NO_BIO */
  51135. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  51136. {
  51137. EXPECT_DECLS;
  51138. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51139. X509 *x509 = NULL;
  51140. X509_NAME* name = NULL;
  51141. int idx = 0;
  51142. X509_NAME_ENTRY *ne = NULL;
  51143. ASN1_OBJECT *object = NULL;
  51144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  51145. WOLFSSL_FILETYPE_PEM));
  51146. ExpectNotNull(name = X509_get_subject_name(x509));
  51147. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  51148. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  51149. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  51150. X509_free(x509);
  51151. #endif
  51152. return EXPECT_RESULT();
  51153. }
  51154. static int test_wolfSSL_X509_STORE_get1_certs(void)
  51155. {
  51156. EXPECT_DECLS;
  51157. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  51158. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51159. X509_STORE_CTX *storeCtx = NULL;
  51160. X509_STORE *store = NULL;
  51161. X509 *caX509 = NULL;
  51162. X509 *svrX509 = NULL;
  51163. X509_NAME *subject = NULL;
  51164. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  51165. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  51166. SSL_FILETYPE_PEM));
  51167. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  51168. SSL_FILETYPE_PEM)));
  51169. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  51170. ExpectNotNull(store = X509_STORE_new());
  51171. ExpectNotNull(subject = X509_get_subject_name(caX509));
  51172. /* Errors */
  51173. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  51174. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  51175. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  51176. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  51177. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  51178. SSL_SUCCESS);
  51179. /* Should find the cert */
  51180. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51181. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  51182. sk_X509_pop_free(certs, NULL);
  51183. certs = NULL;
  51184. /* Should not find the cert */
  51185. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  51186. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51187. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  51188. sk_X509_pop_free(certs, NULL);
  51189. certs = NULL;
  51190. X509_STORE_free(store);
  51191. X509_STORE_CTX_free(storeCtx);
  51192. X509_free(svrX509);
  51193. X509_free(caX509);
  51194. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  51195. return EXPECT_RESULT();
  51196. }
  51197. static int test_wolfSSL_dup_CA_list(void)
  51198. {
  51199. int res = TEST_SKIPPED;
  51200. #if defined(OPENSSL_ALL)
  51201. EXPECT_DECLS;
  51202. STACK_OF(X509_NAME) *originalStack = NULL;
  51203. STACK_OF(X509_NAME) *copyStack = NULL;
  51204. int originalCount = 0;
  51205. int copyCount = 0;
  51206. X509_NAME *name = NULL;
  51207. int i;
  51208. originalStack = sk_X509_NAME_new_null();
  51209. ExpectNotNull(originalStack);
  51210. for (i = 0; i < 3; i++) {
  51211. name = X509_NAME_new();
  51212. ExpectNotNull(name);
  51213. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  51214. }
  51215. copyStack = SSL_dup_CA_list(originalStack);
  51216. ExpectNotNull(copyStack);
  51217. originalCount = sk_X509_NAME_num(originalStack);
  51218. copyCount = sk_X509_NAME_num(copyStack);
  51219. AssertIntEQ(originalCount, copyCount);
  51220. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  51221. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  51222. originalStack = NULL;
  51223. copyStack = NULL;
  51224. res = EXPECT_RESULT();
  51225. #endif /* OPENSSL_ALL */
  51226. return res;
  51227. }
  51228. static int test_ForceZero(void)
  51229. {
  51230. EXPECT_DECLS;
  51231. unsigned char data[32];
  51232. unsigned int i, j, len;
  51233. /* Test case with 0 length */
  51234. ForceZero(data, 0);
  51235. /* Test ForceZero */
  51236. for (i = 0; i < sizeof(data); i++) {
  51237. for (len = 1; len < sizeof(data) - i; len++) {
  51238. for (j = 0; j < sizeof(data); j++)
  51239. data[j] = j + 1;
  51240. ForceZero(data + i, len);
  51241. for (j = 0; j < sizeof(data); j++) {
  51242. if (j < i || j >= i + len) {
  51243. ExpectIntNE(data[j], 0x00);
  51244. }
  51245. else {
  51246. ExpectIntEQ(data[j], 0x00);
  51247. }
  51248. }
  51249. }
  51250. }
  51251. return EXPECT_RESULT();
  51252. }
  51253. #ifndef NO_BIO
  51254. static int test_wolfSSL_X509_print(void)
  51255. {
  51256. EXPECT_DECLS;
  51257. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51258. !defined(NO_RSA) && defined(XSNPRINTF)
  51259. X509 *x509 = NULL;
  51260. BIO *bio = NULL;
  51261. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51262. const X509_ALGOR *cert_sig_alg = NULL;
  51263. #endif
  51264. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  51265. WOLFSSL_FILETYPE_PEM));
  51266. /* print to memory */
  51267. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51268. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51269. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  51270. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  51271. /* Will print IP address subject alt name. */
  51272. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  51273. #elif defined(NO_ASN_TIME)
  51274. /* Will print IP address subject alt name but not Validity. */
  51275. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  51276. #else
  51277. /* Will print IP address subject alt name. */
  51278. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  51279. #endif
  51280. #elif defined(NO_ASN_TIME)
  51281. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  51282. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  51283. #else
  51284. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  51285. #endif
  51286. BIO_free(bio);
  51287. bio = NULL;
  51288. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51289. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51290. /* Print signature */
  51291. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  51292. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  51293. #endif
  51294. /* print to stderr */
  51295. #if !defined(NO_WOLFSSL_DIR)
  51296. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51297. #endif
  51298. /* print again */
  51299. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  51300. X509_free(x509);
  51301. BIO_free(bio);
  51302. #endif
  51303. return EXPECT_RESULT();
  51304. }
  51305. static int test_wolfSSL_X509_CRL_print(void)
  51306. {
  51307. EXPECT_DECLS;
  51308. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  51309. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  51310. X509_CRL* crl = NULL;
  51311. BIO *bio = NULL;
  51312. XFILE fp = XBADFILE;
  51313. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  51314. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51315. NULL, NULL));
  51316. if (fp != XBADFILE)
  51317. XFCLOSE(fp);
  51318. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51319. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  51320. X509_CRL_free(crl);
  51321. BIO_free(bio);
  51322. #endif
  51323. return EXPECT_RESULT();
  51324. }
  51325. static int test_wolfSSL_BIO_get_len(void)
  51326. {
  51327. EXPECT_DECLS;
  51328. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51329. BIO *bio = NULL;
  51330. const char txt[] = "Some example text to push to the BIO.";
  51331. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  51332. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  51333. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  51334. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  51335. BIO_free(bio);
  51336. bio = NULL;
  51337. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51338. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  51339. BIO_free(bio);
  51340. #endif
  51341. return EXPECT_RESULT();
  51342. }
  51343. #endif /* !NO_BIO */
  51344. static int test_wolfSSL_RSA(void)
  51345. {
  51346. EXPECT_DECLS;
  51347. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  51348. RSA* rsa = NULL;
  51349. const BIGNUM *n = NULL;
  51350. const BIGNUM *e = NULL;
  51351. const BIGNUM *d = NULL;
  51352. const BIGNUM *p = NULL;
  51353. const BIGNUM *q = NULL;
  51354. const BIGNUM *dmp1 = NULL;
  51355. const BIGNUM *dmq1 = NULL;
  51356. const BIGNUM *iqmp = NULL;
  51357. ExpectNotNull(rsa = RSA_new());
  51358. ExpectIntEQ(RSA_size(NULL), 0);
  51359. ExpectIntEQ(RSA_size(rsa), 0);
  51360. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  51361. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  51362. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  51363. #ifdef WOLFSSL_RSA_KEY_CHECK
  51364. ExpectIntEQ(RSA_check_key(rsa), 0);
  51365. #endif
  51366. RSA_free(rsa);
  51367. rsa = NULL;
  51368. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51369. ExpectIntEQ(RSA_size(rsa), 256);
  51370. #ifdef WOLFSSL_RSA_KEY_CHECK
  51371. ExpectIntEQ(RSA_check_key(NULL), 0);
  51372. ExpectIntEQ(RSA_check_key(rsa), 1);
  51373. #endif
  51374. /* sanity check */
  51375. ExpectIntEQ(RSA_bits(NULL), 0);
  51376. /* key */
  51377. ExpectIntEQ(RSA_bits(rsa), 2048);
  51378. RSA_get0_key(rsa, &n, &e, &d);
  51379. ExpectPtrEq(rsa->n, n);
  51380. ExpectPtrEq(rsa->e, e);
  51381. ExpectPtrEq(rsa->d, d);
  51382. n = NULL;
  51383. e = NULL;
  51384. d = NULL;
  51385. ExpectNotNull(n = BN_new());
  51386. ExpectNotNull(e = BN_new());
  51387. ExpectNotNull(d = BN_new());
  51388. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  51389. if (EXPECT_FAIL()) {
  51390. BN_free((BIGNUM*)n);
  51391. BN_free((BIGNUM*)e);
  51392. BN_free((BIGNUM*)d);
  51393. }
  51394. ExpectPtrEq(rsa->n, n);
  51395. ExpectPtrEq(rsa->e, e);
  51396. ExpectPtrEq(rsa->d, d);
  51397. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  51398. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  51399. /* crt_params */
  51400. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  51401. ExpectPtrEq(rsa->dmp1, dmp1);
  51402. ExpectPtrEq(rsa->dmq1, dmq1);
  51403. ExpectPtrEq(rsa->iqmp, iqmp);
  51404. dmp1 = NULL;
  51405. dmq1 = NULL;
  51406. iqmp = NULL;
  51407. ExpectNotNull(dmp1 = BN_new());
  51408. ExpectNotNull(dmq1 = BN_new());
  51409. ExpectNotNull(iqmp = BN_new());
  51410. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51411. (BIGNUM*)iqmp), 1);
  51412. if (EXPECT_FAIL()) {
  51413. BN_free((BIGNUM*)dmp1);
  51414. BN_free((BIGNUM*)dmq1);
  51415. BN_free((BIGNUM*)iqmp);
  51416. }
  51417. ExpectPtrEq(rsa->dmp1, dmp1);
  51418. ExpectPtrEq(rsa->dmq1, dmq1);
  51419. ExpectPtrEq(rsa->iqmp, iqmp);
  51420. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  51421. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51422. (BIGNUM*)iqmp), 0);
  51423. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  51424. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  51425. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  51426. ExpectNull(dmp1);
  51427. ExpectNull(dmq1);
  51428. ExpectNull(iqmp);
  51429. /* factors */
  51430. RSA_get0_factors(rsa, NULL, NULL);
  51431. RSA_get0_factors(rsa, &p, &q);
  51432. ExpectPtrEq(rsa->p, p);
  51433. ExpectPtrEq(rsa->q, q);
  51434. p = NULL;
  51435. q = NULL;
  51436. ExpectNotNull(p = BN_new());
  51437. ExpectNotNull(q = BN_new());
  51438. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  51439. if (EXPECT_FAIL()) {
  51440. BN_free((BIGNUM*)p);
  51441. BN_free((BIGNUM*)q);
  51442. }
  51443. ExpectPtrEq(rsa->p, p);
  51444. ExpectPtrEq(rsa->q, q);
  51445. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  51446. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  51447. RSA_get0_factors(NULL, NULL, NULL);
  51448. RSA_get0_factors(NULL, &p, &q);
  51449. ExpectNull(p);
  51450. ExpectNull(q);
  51451. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  51452. ExpectIntEQ(RSA_bits(rsa), 21);
  51453. RSA_free(rsa);
  51454. rsa = NULL;
  51455. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  51456. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  51457. ExpectIntEQ(RSA_size(rsa), 384);
  51458. ExpectIntEQ(RSA_bits(rsa), 3072);
  51459. RSA_free(rsa);
  51460. rsa = NULL;
  51461. #endif
  51462. /* remove for now with odd key size until adjusting rsa key size check with
  51463. wc_MakeRsaKey()
  51464. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  51465. RSA_free(rsa);
  51466. rsa = NULL;
  51467. */
  51468. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  51469. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  51470. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  51471. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  51472. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  51473. {
  51474. byte buff[FOURK_BUF];
  51475. byte der[FOURK_BUF];
  51476. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  51477. XFILE f = XBADFILE;
  51478. int bytes = 0;
  51479. /* test loading encrypted RSA private pem w/o password */
  51480. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  51481. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  51482. if (f != XBADFILE)
  51483. XFCLOSE(f);
  51484. XMEMSET(der, 0, sizeof(der));
  51485. /* test that error value is returned with no password */
  51486. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  51487. 0);
  51488. }
  51489. #endif
  51490. #endif
  51491. return EXPECT_RESULT();
  51492. }
  51493. static int test_wolfSSL_RSA_DER(void)
  51494. {
  51495. EXPECT_DECLS;
  51496. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51497. RSA *rsa = NULL;
  51498. int i;
  51499. const unsigned char *buff = NULL;
  51500. unsigned char *newBuff = NULL;
  51501. struct tbl_s
  51502. {
  51503. const unsigned char *der;
  51504. int sz;
  51505. } tbl[] = {
  51506. #ifdef USE_CERT_BUFFERS_1024
  51507. {client_key_der_1024, sizeof_client_key_der_1024},
  51508. {server_key_der_1024, sizeof_server_key_der_1024},
  51509. #endif
  51510. #ifdef USE_CERT_BUFFERS_2048
  51511. {client_key_der_2048, sizeof_client_key_der_2048},
  51512. {server_key_der_2048, sizeof_server_key_der_2048},
  51513. #endif
  51514. {NULL, 0}
  51515. };
  51516. /* Public Key DER */
  51517. struct tbl_s pub[] = {
  51518. #ifdef USE_CERT_BUFFERS_1024
  51519. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  51520. #endif
  51521. #ifdef USE_CERT_BUFFERS_2048
  51522. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  51523. #endif
  51524. {NULL, 0}
  51525. };
  51526. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  51527. buff = pub[0].der;
  51528. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  51529. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  51530. buff = tbl[0].der;
  51531. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  51532. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  51533. rsa = RSA_new();
  51534. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  51535. RSA_free(rsa);
  51536. rsa = NULL;
  51537. for (i = 0; tbl[i].der != NULL; i++)
  51538. {
  51539. /* Passing in pointer results in pointer moving. */
  51540. buff = tbl[i].der;
  51541. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  51542. ExpectNotNull(rsa);
  51543. RSA_free(rsa);
  51544. rsa = NULL;
  51545. }
  51546. for (i = 0; tbl[i].der != NULL; i++)
  51547. {
  51548. /* Passing in pointer results in pointer moving. */
  51549. buff = tbl[i].der;
  51550. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  51551. ExpectNotNull(rsa);
  51552. RSA_free(rsa);
  51553. rsa = NULL;
  51554. }
  51555. for (i = 0; pub[i].der != NULL; i++)
  51556. {
  51557. buff = pub[i].der;
  51558. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  51559. ExpectNotNull(rsa);
  51560. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  51561. newBuff = NULL;
  51562. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  51563. ExpectNotNull(newBuff);
  51564. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  51565. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51566. RSA_free(rsa);
  51567. rsa = NULL;
  51568. }
  51569. #endif
  51570. return EXPECT_RESULT();
  51571. }
  51572. static int test_wolfSSL_RSA_print(void)
  51573. {
  51574. EXPECT_DECLS;
  51575. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51576. !defined(NO_STDIO_FILESYSTEM) && \
  51577. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  51578. !defined(NO_BIO) && defined(XFPRINTF)
  51579. BIO *bio = NULL;
  51580. WOLFSSL_RSA* rsa = NULL;
  51581. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51582. ExpectNotNull(rsa = RSA_new());
  51583. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  51584. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  51585. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  51586. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  51587. /* Some very large number of indent spaces. */
  51588. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  51589. /* RSA is empty. */
  51590. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  51591. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  51592. RSA_free(rsa);
  51593. rsa = NULL;
  51594. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51595. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  51596. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  51597. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  51598. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  51599. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  51600. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  51601. BIO_free(bio);
  51602. RSA_free(rsa);
  51603. #endif
  51604. return EXPECT_RESULT();
  51605. }
  51606. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  51607. {
  51608. EXPECT_DECLS;
  51609. #ifndef NO_RSA
  51610. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51611. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51612. RSA *rsa = NULL;
  51613. const unsigned char *derBuf = client_key_der_2048;
  51614. unsigned char em[256] = {0}; /* len = 2048/8 */
  51615. /* Random data simulating a hash */
  51616. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  51617. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51618. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51619. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51620. };
  51621. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51622. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  51623. RSA_PSS_SALTLEN_DIGEST), 0);
  51624. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  51625. RSA_PSS_SALTLEN_DIGEST), 0);
  51626. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  51627. RSA_PSS_SALTLEN_DIGEST), 0);
  51628. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  51629. RSA_PSS_SALTLEN_DIGEST), 0);
  51630. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  51631. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  51632. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51633. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  51634. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51635. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  51636. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51637. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  51638. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51639. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51640. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51641. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  51642. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51643. RSA_PSS_SALTLEN_DIGEST), 1);
  51644. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51645. RSA_PSS_SALTLEN_DIGEST), 1);
  51646. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51647. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51648. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51649. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51650. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51651. RSA_PSS_SALTLEN_MAX), 1);
  51652. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51653. RSA_PSS_SALTLEN_MAX), 1);
  51654. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  51655. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  51656. RSA_free(rsa);
  51657. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51658. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51659. #endif
  51660. return EXPECT_RESULT();
  51661. }
  51662. static int test_wolfSSL_RSA_sign_sha3(void)
  51663. {
  51664. EXPECT_DECLS;
  51665. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  51666. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51667. RSA* rsa = NULL;
  51668. const unsigned char *derBuf = client_key_der_2048;
  51669. unsigned char sigRet[256] = {0};
  51670. unsigned int sigLen = sizeof(sigRet);
  51671. /* Random data simulating a hash */
  51672. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  51673. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51674. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51675. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51676. };
  51677. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51678. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  51679. rsa), 1);
  51680. RSA_free(rsa);
  51681. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51682. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  51683. return EXPECT_RESULT();
  51684. }
  51685. static int test_wolfSSL_RSA_get0_key(void)
  51686. {
  51687. EXPECT_DECLS;
  51688. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51689. RSA *rsa = NULL;
  51690. const BIGNUM* n = NULL;
  51691. const BIGNUM* e = NULL;
  51692. const BIGNUM* d = NULL;
  51693. const unsigned char* der;
  51694. int derSz;
  51695. #ifdef USE_CERT_BUFFERS_1024
  51696. der = client_key_der_1024;
  51697. derSz = sizeof_client_key_der_1024;
  51698. #elif defined(USE_CERT_BUFFERS_2048)
  51699. der = client_key_der_2048;
  51700. derSz = sizeof_client_key_der_2048;
  51701. #else
  51702. der = NULL;
  51703. derSz = 0;
  51704. #endif
  51705. if (der != NULL) {
  51706. RSA_get0_key(NULL, NULL, NULL, NULL);
  51707. RSA_get0_key(rsa, NULL, NULL, NULL);
  51708. RSA_get0_key(NULL, &n, &e, &d);
  51709. ExpectNull(n);
  51710. ExpectNull(e);
  51711. ExpectNull(d);
  51712. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  51713. ExpectNotNull(rsa);
  51714. RSA_get0_key(rsa, NULL, NULL, NULL);
  51715. RSA_get0_key(rsa, &n, NULL, NULL);
  51716. ExpectNotNull(n);
  51717. RSA_get0_key(rsa, NULL, &e, NULL);
  51718. ExpectNotNull(e);
  51719. RSA_get0_key(rsa, NULL, NULL, &d);
  51720. ExpectNotNull(d);
  51721. RSA_get0_key(rsa, &n, &e, &d);
  51722. ExpectNotNull(n);
  51723. ExpectNotNull(e);
  51724. ExpectNotNull(d);
  51725. RSA_free(rsa);
  51726. }
  51727. #endif
  51728. return EXPECT_RESULT();
  51729. }
  51730. static int test_wolfSSL_RSA_meth(void)
  51731. {
  51732. EXPECT_DECLS;
  51733. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51734. RSA *rsa = NULL;
  51735. RSA_METHOD *rsa_meth = NULL;
  51736. #ifdef WOLFSSL_KEY_GEN
  51737. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51738. RSA_free(rsa);
  51739. rsa = NULL;
  51740. #else
  51741. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51742. #endif
  51743. ExpectNotNull(RSA_get_default_method());
  51744. wolfSSL_RSA_meth_free(NULL);
  51745. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  51746. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  51747. RSA_METHOD_FLAG_NO_CHECK));
  51748. #ifndef NO_WOLFSSL_STUB
  51749. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  51750. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  51751. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  51752. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  51753. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  51754. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  51755. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  51756. #endif
  51757. ExpectIntEQ(RSA_flags(NULL), 0);
  51758. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51759. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51760. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  51761. ExpectNotNull(rsa = RSA_new());
  51762. /* No method set. */
  51763. ExpectIntEQ(RSA_flags(rsa), 0);
  51764. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51765. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51766. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51767. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  51768. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  51769. if (EXPECT_FAIL()) {
  51770. wolfSSL_RSA_meth_free(rsa_meth);
  51771. }
  51772. ExpectNull(RSA_get_method(NULL));
  51773. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  51774. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  51775. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51776. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51777. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  51778. RSA_METHOD_FLAG_NO_CHECK);
  51779. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51780. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51781. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  51782. /* rsa_meth is freed here */
  51783. RSA_free(rsa);
  51784. #endif
  51785. return EXPECT_RESULT();
  51786. }
  51787. static int test_wolfSSL_RSA_verify(void)
  51788. {
  51789. EXPECT_DECLS;
  51790. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  51791. #ifndef NO_BIO
  51792. XFILE fp = XBADFILE;
  51793. RSA *pKey = NULL;
  51794. RSA *pubKey = NULL;
  51795. X509 *cert = NULL;
  51796. const char *text = "Hello wolfSSL !";
  51797. unsigned char hash[SHA256_DIGEST_LENGTH];
  51798. unsigned char signature[2048/8];
  51799. unsigned int signatureLength;
  51800. byte *buf = NULL;
  51801. BIO *bio = NULL;
  51802. SHA256_CTX c;
  51803. EVP_PKEY *evpPkey = NULL;
  51804. EVP_PKEY *evpPubkey = NULL;
  51805. size_t sz;
  51806. /* generate hash */
  51807. SHA256_Init(&c);
  51808. SHA256_Update(&c, text, strlen(text));
  51809. SHA256_Final(hash, &c);
  51810. #ifdef WOLFSSL_SMALL_STACK_CACHE
  51811. /* workaround for small stack cache case */
  51812. wc_Sha256Free((wc_Sha256*)&c);
  51813. #endif
  51814. /* read privete key file */
  51815. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  51816. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  51817. ExpectTrue((sz = XFTELL(fp)) > 0);
  51818. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  51819. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  51820. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  51821. if (fp != XBADFILE) {
  51822. XFCLOSE(fp);
  51823. fp = XBADFILE;
  51824. }
  51825. /* read private key and sign hash data */
  51826. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  51827. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  51828. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  51829. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  51830. signature, &signatureLength, pKey), SSL_SUCCESS);
  51831. /* read public key and verify signed data */
  51832. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  51833. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  51834. if (fp != XBADFILE)
  51835. XFCLOSE(fp);
  51836. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  51837. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  51838. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51839. signatureLength, pubKey), SSL_SUCCESS);
  51840. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  51841. signatureLength, NULL), SSL_FAILURE);
  51842. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  51843. signatureLength, pubKey), SSL_FAILURE);
  51844. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  51845. signatureLength, pubKey), SSL_FAILURE);
  51846. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51847. signatureLength, NULL), SSL_FAILURE);
  51848. RSA_free(pKey);
  51849. EVP_PKEY_free(evpPkey);
  51850. RSA_free(pubKey);
  51851. EVP_PKEY_free(evpPubkey);
  51852. X509_free(cert);
  51853. BIO_free(bio);
  51854. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  51855. #endif
  51856. #endif
  51857. return EXPECT_RESULT();
  51858. }
  51859. static int test_wolfSSL_RSA_sign(void)
  51860. {
  51861. EXPECT_DECLS;
  51862. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51863. RSA *rsa;
  51864. unsigned char hash[SHA256_DIGEST_LENGTH];
  51865. #ifdef USE_CERT_BUFFERS_1024
  51866. const unsigned char* privDer = client_key_der_1024;
  51867. size_t privDerSz = sizeof_client_key_der_1024;
  51868. const unsigned char* pubDer = client_keypub_der_1024;
  51869. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51870. unsigned char signature[1024/8];
  51871. #else
  51872. const unsigned char* privDer = client_key_der_2048;
  51873. size_t privDerSz = sizeof_client_key_der_2048;
  51874. const unsigned char* pubDer = client_keypub_der_2048;
  51875. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51876. unsigned char signature[2048/8];
  51877. #endif
  51878. unsigned int signatureLen;
  51879. const unsigned char* der;
  51880. XMEMSET(hash, 0, sizeof(hash));
  51881. der = privDer;
  51882. rsa = NULL;
  51883. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51884. /* Invalid parameters. */
  51885. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  51886. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  51887. &signatureLen, rsa), 0);
  51888. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  51889. &signatureLen, rsa), 0);
  51890. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  51891. &signatureLen, rsa), 0);
  51892. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51893. NULL, rsa), 0);
  51894. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51895. &signatureLen, NULL), 0);
  51896. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51897. &signatureLen, rsa), 1);
  51898. RSA_free(rsa);
  51899. der = pubDer;
  51900. rsa = NULL;
  51901. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51902. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51903. signatureLen, rsa), 1);
  51904. RSA_free(rsa);
  51905. #endif
  51906. return EXPECT_RESULT();
  51907. }
  51908. static int test_wolfSSL_RSA_sign_ex(void)
  51909. {
  51910. EXPECT_DECLS;
  51911. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51912. RSA *rsa = NULL;
  51913. unsigned char hash[SHA256_DIGEST_LENGTH];
  51914. #ifdef USE_CERT_BUFFERS_1024
  51915. const unsigned char* privDer = client_key_der_1024;
  51916. size_t privDerSz = sizeof_client_key_der_1024;
  51917. const unsigned char* pubDer = client_keypub_der_1024;
  51918. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51919. unsigned char signature[1024/8];
  51920. #else
  51921. const unsigned char* privDer = client_key_der_2048;
  51922. size_t privDerSz = sizeof_client_key_der_2048;
  51923. const unsigned char* pubDer = client_keypub_der_2048;
  51924. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51925. unsigned char signature[2048/8];
  51926. #endif
  51927. unsigned int signatureLen;
  51928. const unsigned char* der;
  51929. unsigned char encodedHash[51];
  51930. unsigned int encodedHashLen;
  51931. const unsigned char expEncHash[] = {
  51932. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  51933. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  51934. 0x00, 0x04, 0x20,
  51935. /* Hash data */
  51936. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51937. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51938. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51940. };
  51941. XMEMSET(hash, 0, sizeof(hash));
  51942. ExpectNotNull(rsa = wolfSSL_RSA_new());
  51943. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51944. &signatureLen, rsa, 1), 0);
  51945. wolfSSL_RSA_free(rsa);
  51946. der = privDer;
  51947. rsa = NULL;
  51948. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51949. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  51950. -1), 0);
  51951. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  51952. signature, &signatureLen, rsa, 1), 0);
  51953. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51954. &signatureLen, rsa, 1), 0);
  51955. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51956. &signatureLen, rsa, 1), 0);
  51957. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51958. NULL, rsa, 1), 0);
  51959. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51960. &signatureLen, NULL, 1), 0);
  51961. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51962. &signatureLen, rsa, -1), 0);
  51963. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51964. &signatureLen, rsa, 0), 0);
  51965. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51966. &signatureLen, rsa, 0), 0);
  51967. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51968. NULL, rsa, 0), 0);
  51969. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51970. &signatureLen, rsa, 1), 1);
  51971. /* Test returning encoded hash. */
  51972. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  51973. &encodedHashLen, rsa, 0), 1);
  51974. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  51975. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  51976. RSA_free(rsa);
  51977. der = pubDer;
  51978. rsa = NULL;
  51979. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51980. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51981. signatureLen, rsa), 1);
  51982. RSA_free(rsa);
  51983. #endif
  51984. return EXPECT_RESULT();
  51985. }
  51986. static int test_wolfSSL_RSA_public_decrypt(void)
  51987. {
  51988. EXPECT_DECLS;
  51989. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51990. RSA *rsa;
  51991. unsigned char msg[SHA256_DIGEST_LENGTH];
  51992. #ifdef USE_CERT_BUFFERS_1024
  51993. const unsigned char* pubDer = client_keypub_der_1024;
  51994. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51995. unsigned char decMsg[1024/8];
  51996. const unsigned char encMsg[] = {
  51997. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  51998. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  51999. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52000. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52001. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52002. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52003. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52004. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52005. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52006. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52007. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52008. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52009. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52010. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52011. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52012. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52013. };
  52014. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52015. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52016. defined(WC_RSA_NO_PADDING)
  52017. const unsigned char encMsgNoPad[] = {
  52018. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52019. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52020. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52021. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52022. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52023. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52024. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52025. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52026. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52027. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52028. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52029. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52030. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52031. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52032. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52033. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52034. };
  52035. #endif
  52036. #else
  52037. const unsigned char* pubDer = client_keypub_der_2048;
  52038. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52039. unsigned char decMsg[2048/8];
  52040. const unsigned char encMsg[] = {
  52041. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52042. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52043. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52044. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52045. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52046. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52047. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52048. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52049. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52050. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52051. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52052. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52053. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52054. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52055. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52056. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52057. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52058. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52059. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52060. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52061. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52062. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52063. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52064. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52065. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52066. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52067. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52068. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52069. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52070. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52071. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52072. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52073. };
  52074. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52075. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52076. defined(WC_RSA_NO_PADDING)
  52077. const unsigned char encMsgNoPad[] = {
  52078. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52079. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52080. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52081. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52082. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52083. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52084. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52085. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52086. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52087. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52088. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52089. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52090. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52091. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52092. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52093. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52094. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52095. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52096. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52097. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52098. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52099. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52100. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52101. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52102. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52103. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52104. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52105. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52106. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52107. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52108. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52109. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52110. };
  52111. #endif
  52112. #endif
  52113. const unsigned char* der;
  52114. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52115. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52116. defined(WC_RSA_NO_PADDING)
  52117. int i;
  52118. #endif
  52119. XMEMSET(msg, 0, sizeof(msg));
  52120. der = pubDer;
  52121. rsa = NULL;
  52122. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52123. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  52124. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  52125. RSA_PKCS1_PADDING), -1);
  52126. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  52127. RSA_PKCS1_PADDING), -1);
  52128. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52129. RSA_PKCS1_PADDING), -1);
  52130. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  52131. RSA_PKCS1_PADDING), -1);
  52132. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52133. RSA_PKCS1_PSS_PADDING), -1);
  52134. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52135. RSA_PKCS1_PADDING), 32);
  52136. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  52137. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52138. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52139. defined(WC_RSA_NO_PADDING)
  52140. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  52141. rsa, RSA_NO_PADDING), sizeof(decMsg));
  52142. /* Zeros before actual data. */
  52143. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  52144. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52145. }
  52146. /* Check actual data. */
  52147. XMEMSET(msg, 0x01, sizeof(msg));
  52148. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52149. #endif
  52150. RSA_free(rsa);
  52151. #endif
  52152. return EXPECT_RESULT();
  52153. }
  52154. static int test_wolfSSL_RSA_private_encrypt(void)
  52155. {
  52156. EXPECT_DECLS;
  52157. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52158. RSA *rsa;
  52159. unsigned char msg[SHA256_DIGEST_LENGTH];
  52160. #ifdef USE_CERT_BUFFERS_1024
  52161. const unsigned char* privDer = client_key_der_1024;
  52162. size_t privDerSz = sizeof_client_key_der_1024;
  52163. unsigned char encMsg[1024/8];
  52164. const unsigned char expEncMsg[] = {
  52165. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52166. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52167. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52168. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52169. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52170. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52171. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52172. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52173. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52174. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52175. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52176. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52177. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52178. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52179. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52180. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52181. };
  52182. #ifdef WC_RSA_NO_PADDING
  52183. const unsigned char expEncMsgNoPad[] = {
  52184. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52185. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52186. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52187. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52188. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52189. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52190. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52191. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52192. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52193. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52194. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52195. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52196. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52197. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52198. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52199. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52200. };
  52201. #endif
  52202. #else
  52203. const unsigned char* privDer = client_key_der_2048;
  52204. size_t privDerSz = sizeof_client_key_der_2048;
  52205. unsigned char encMsg[2048/8];
  52206. const unsigned char expEncMsg[] = {
  52207. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52208. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52209. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52210. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52211. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52212. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52213. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52214. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52215. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52216. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52217. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52218. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52219. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52220. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52221. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52222. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52223. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52224. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52225. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52226. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52227. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52228. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52229. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52230. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52231. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52232. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52233. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52234. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52235. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52236. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52237. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52238. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52239. };
  52240. #ifdef WC_RSA_NO_PADDING
  52241. const unsigned char expEncMsgNoPad[] = {
  52242. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52243. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52244. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52245. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52246. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52247. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52248. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52249. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52250. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52251. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52252. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52253. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52254. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52255. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52256. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52257. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52258. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52259. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52260. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52261. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52262. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52263. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52264. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52265. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52266. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52267. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52268. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52269. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52270. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52271. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52272. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52273. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52274. };
  52275. #endif
  52276. #endif
  52277. const unsigned char* der;
  52278. XMEMSET(msg, 0x00, sizeof(msg));
  52279. der = privDer;
  52280. rsa = NULL;
  52281. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52282. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  52283. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  52284. -1);
  52285. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52286. RSA_PKCS1_PADDING), -1);
  52287. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  52288. RSA_PKCS1_PADDING), -1);
  52289. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  52290. RSA_PKCS1_PADDING), -1);
  52291. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52292. RSA_PKCS1_PSS_PADDING), -1);
  52293. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52294. RSA_PKCS1_PADDING), sizeof(encMsg));
  52295. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  52296. #ifdef WC_RSA_NO_PADDING
  52297. /* Non-zero message. */
  52298. XMEMSET(msg, 0x01, sizeof(msg));
  52299. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52300. RSA_NO_PADDING), sizeof(encMsg));
  52301. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  52302. #endif
  52303. RSA_free(rsa);
  52304. #endif
  52305. return EXPECT_RESULT();
  52306. }
  52307. static int test_wolfSSL_RSA_public_encrypt(void)
  52308. {
  52309. EXPECT_DECLS;
  52310. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52311. RSA* rsa = NULL;
  52312. const unsigned char msg[2048/8] = { 0 };
  52313. unsigned char encMsg[2048/8];
  52314. ExpectNotNull(rsa = RSA_new());
  52315. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  52316. RSA_PKCS1_PADDING), -1);
  52317. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52318. RSA_PKCS1_PADDING), -1);
  52319. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  52320. RSA_PKCS1_PADDING), -1);
  52321. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  52322. RSA_PKCS1_PADDING), -1);
  52323. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52324. RSA_PKCS1_PSS_PADDING), -1);
  52325. /* Empty RSA key. */
  52326. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52327. RSA_PKCS1_PADDING), -1);
  52328. RSA_free(rsa);
  52329. #endif
  52330. return EXPECT_RESULT();
  52331. }
  52332. static int test_wolfSSL_RSA_private_decrypt(void)
  52333. {
  52334. EXPECT_DECLS;
  52335. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52336. RSA* rsa = NULL;
  52337. unsigned char msg[2048/8];
  52338. const unsigned char encMsg[2048/8] = { 0 };
  52339. ExpectNotNull(rsa = RSA_new());
  52340. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  52341. RSA_PKCS1_PADDING), -1);
  52342. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  52343. RSA_PKCS1_PADDING), -1);
  52344. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52345. RSA_PKCS1_PADDING), -1);
  52346. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  52347. RSA_PKCS1_PADDING), -1);
  52348. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52349. RSA_PKCS1_PSS_PADDING), -1);
  52350. /* Empty RSA key. */
  52351. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52352. RSA_PKCS1_PADDING), -1);
  52353. RSA_free(rsa);
  52354. #endif
  52355. return EXPECT_RESULT();
  52356. }
  52357. static int test_wolfSSL_RSA_GenAdd(void)
  52358. {
  52359. EXPECT_DECLS;
  52360. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52361. RSA *rsa;
  52362. #ifdef USE_CERT_BUFFERS_1024
  52363. const unsigned char* privDer = client_key_der_1024;
  52364. size_t privDerSz = sizeof_client_key_der_1024;
  52365. const unsigned char* pubDer = client_keypub_der_1024;
  52366. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52367. #else
  52368. const unsigned char* privDer = client_key_der_2048;
  52369. size_t privDerSz = sizeof_client_key_der_2048;
  52370. const unsigned char* pubDer = client_keypub_der_2048;
  52371. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52372. #endif
  52373. const unsigned char* der;
  52374. der = privDer;
  52375. rsa = NULL;
  52376. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52377. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  52378. #ifndef RSA_LOW_MEM
  52379. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  52380. #else
  52381. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  52382. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52383. #endif
  52384. RSA_free(rsa);
  52385. der = pubDer;
  52386. rsa = NULL;
  52387. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52388. /* Need private values. */
  52389. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52390. RSA_free(rsa);
  52391. #endif
  52392. return EXPECT_RESULT();
  52393. }
  52394. static int test_wolfSSL_RSA_blinding_on(void)
  52395. {
  52396. EXPECT_DECLS;
  52397. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  52398. RSA *rsa;
  52399. WOLFSSL_BN_CTX *bnCtx = NULL;
  52400. #ifdef USE_CERT_BUFFERS_1024
  52401. const unsigned char* privDer = client_key_der_1024;
  52402. size_t privDerSz = sizeof_client_key_der_1024;
  52403. #else
  52404. const unsigned char* privDer = client_key_der_2048;
  52405. size_t privDerSz = sizeof_client_key_der_2048;
  52406. #endif
  52407. const unsigned char* der;
  52408. der = privDer;
  52409. rsa = NULL;
  52410. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52411. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  52412. /* Does nothing so all parameters are valid. */
  52413. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  52414. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  52415. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  52416. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  52417. wolfSSL_BN_CTX_free(bnCtx);
  52418. RSA_free(rsa);
  52419. #endif
  52420. return EXPECT_RESULT();
  52421. }
  52422. static int test_wolfSSL_RSA_ex_data(void)
  52423. {
  52424. EXPECT_DECLS;
  52425. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52426. RSA* rsa = NULL;
  52427. unsigned char data[1];
  52428. ExpectNotNull(rsa = RSA_new());
  52429. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  52430. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52431. #ifdef MAX_EX_DATA
  52432. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  52433. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  52434. #endif
  52435. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  52436. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  52437. #ifdef HAVE_EX_DATA
  52438. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  52439. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  52440. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  52441. #else
  52442. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  52443. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  52444. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52445. #endif
  52446. RSA_free(rsa);
  52447. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52448. return EXPECT_RESULT();
  52449. }
  52450. static int test_wolfSSL_RSA_LoadDer(void)
  52451. {
  52452. EXPECT_DECLS;
  52453. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  52454. defined(OPENSSL_EXTRA_X509_SMALL))
  52455. RSA *rsa = NULL;
  52456. #ifdef USE_CERT_BUFFERS_1024
  52457. const unsigned char* privDer = client_key_der_1024;
  52458. size_t privDerSz = sizeof_client_key_der_1024;
  52459. #else
  52460. const unsigned char* privDer = client_key_der_2048;
  52461. size_t privDerSz = sizeof_client_key_der_2048;
  52462. #endif
  52463. ExpectNotNull(rsa = RSA_new());
  52464. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  52465. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  52466. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  52467. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  52468. RSA_free(rsa);
  52469. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52470. return EXPECT_RESULT();
  52471. }
  52472. /* Local API. */
  52473. static int test_wolfSSL_RSA_To_Der(void)
  52474. {
  52475. EXPECT_DECLS;
  52476. #ifdef WOLFSSL_TEST_STATIC_BUILD
  52477. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52478. RSA* rsa;
  52479. #ifdef USE_CERT_BUFFERS_1024
  52480. const unsigned char* privDer = client_key_der_1024;
  52481. size_t privDerSz = sizeof_client_key_der_1024;
  52482. const unsigned char* pubDer = client_keypub_der_1024;
  52483. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52484. unsigned char out[sizeof(client_key_der_1024)];
  52485. #else
  52486. const unsigned char* privDer = client_key_der_2048;
  52487. size_t privDerSz = sizeof_client_key_der_2048;
  52488. const unsigned char* pubDer = client_keypub_der_2048;
  52489. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52490. unsigned char out[sizeof(client_key_der_2048)];
  52491. #endif
  52492. const unsigned char* der;
  52493. unsigned char* outDer = NULL;
  52494. der = privDer;
  52495. rsa = NULL;
  52496. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52497. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52498. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  52499. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  52500. outDer = out;
  52501. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52502. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  52503. outDer = NULL;
  52504. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52505. ExpectNotNull(outDer);
  52506. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  52507. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  52508. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  52509. outDer = out;
  52510. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  52511. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  52512. RSA_free(rsa);
  52513. ExpectNotNull(rsa = RSA_new());
  52514. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52515. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  52516. RSA_free(rsa);
  52517. der = pubDer;
  52518. rsa = NULL;
  52519. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52520. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52521. RSA_free(rsa);
  52522. #endif
  52523. #endif
  52524. return EXPECT_RESULT();
  52525. }
  52526. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52527. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  52528. {
  52529. EXPECT_DECLS;
  52530. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  52531. XFILE file = XBADFILE;
  52532. const char* fname = "./certs/server-keyPub.pem";
  52533. RSA *rsa = NULL;
  52534. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  52535. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  52536. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  52537. ExpectIntEQ(RSA_size(rsa), 256);
  52538. RSA_free(rsa);
  52539. if (file != XBADFILE)
  52540. XFCLOSE(file);
  52541. #endif
  52542. return EXPECT_RESULT();
  52543. }
  52544. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52545. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  52546. {
  52547. EXPECT_DECLS;
  52548. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52549. defined(WOLFSSL_KEY_GEN)
  52550. RSA* rsa = NULL;
  52551. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  52552. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  52553. /* Valid but stub so returns 0. */
  52554. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  52555. #endif
  52556. return EXPECT_RESULT();
  52557. }
  52558. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  52559. {
  52560. EXPECT_DECLS;
  52561. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52562. (defined(WOLFSSL_PEM_TO_DER) || \
  52563. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  52564. RSA* rsa = NULL;
  52565. #ifdef USE_CERT_BUFFERS_1024
  52566. const unsigned char* privDer = client_key_der_1024;
  52567. size_t privDerSz = sizeof_client_key_der_1024;
  52568. #else
  52569. const unsigned char* privDer = client_key_der_2048;
  52570. size_t privDerSz = sizeof_client_key_der_2048;
  52571. #endif
  52572. const unsigned char* der;
  52573. #ifndef NO_AES
  52574. unsigned char passwd[] = "password";
  52575. #endif
  52576. ExpectNotNull(rsa = RSA_new());
  52577. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52578. NULL, NULL), 0);
  52579. RSA_free(rsa);
  52580. der = privDer;
  52581. rsa = NULL;
  52582. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52583. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  52584. NULL, NULL), 0);
  52585. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  52586. NULL, NULL), 0);
  52587. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52588. NULL, NULL), 1);
  52589. #ifndef NO_AES
  52590. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52591. NULL, 0, NULL, NULL), 1);
  52592. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52593. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  52594. #endif
  52595. RSA_free(rsa);
  52596. #endif
  52597. return EXPECT_RESULT();
  52598. }
  52599. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  52600. {
  52601. EXPECT_DECLS;
  52602. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52603. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  52604. RSA* rsa = NULL;
  52605. #ifdef USE_CERT_BUFFERS_1024
  52606. const unsigned char* privDer = client_key_der_1024;
  52607. size_t privDerSz = sizeof_client_key_der_1024;
  52608. #else
  52609. const unsigned char* privDer = client_key_der_2048;
  52610. size_t privDerSz = sizeof_client_key_der_2048;
  52611. #endif
  52612. const unsigned char* der;
  52613. #ifndef NO_AES
  52614. unsigned char passwd[] = "password";
  52615. #endif
  52616. unsigned char* pem = NULL;
  52617. int plen;
  52618. ExpectNotNull(rsa = RSA_new());
  52619. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52620. &plen), 0);
  52621. RSA_free(rsa);
  52622. der = privDer;
  52623. rsa = NULL;
  52624. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52625. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  52626. &plen), 0);
  52627. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  52628. &plen), 0);
  52629. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52630. NULL), 0);
  52631. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52632. &plen), 1);
  52633. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52634. pem = NULL;
  52635. #ifndef NO_AES
  52636. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52637. NULL, 0, &pem, &plen), 1);
  52638. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52639. pem = NULL;
  52640. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52641. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  52642. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52643. #endif
  52644. RSA_free(rsa);
  52645. #endif
  52646. return EXPECT_RESULT();
  52647. }
  52648. static int test_wolfSSL_DH(void)
  52649. {
  52650. EXPECT_DECLS;
  52651. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52652. DH *dh = NULL;
  52653. BIGNUM* p;
  52654. BIGNUM* q;
  52655. BIGNUM* g;
  52656. BIGNUM* pub = NULL;
  52657. BIGNUM* priv = NULL;
  52658. #if defined(OPENSSL_ALL)
  52659. #if !defined(HAVE_FIPS) || \
  52660. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  52661. FILE* f = NULL;
  52662. unsigned char buf[268];
  52663. const unsigned char* pt = buf;
  52664. long len = 0;
  52665. dh = NULL;
  52666. XMEMSET(buf, 0, sizeof(buf));
  52667. /* Test 2048 bit parameters */
  52668. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  52669. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52670. if (f != XBADFILE)
  52671. XFCLOSE(f);
  52672. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  52673. ExpectNotNull(dh->p);
  52674. ExpectNotNull(dh->g);
  52675. ExpectTrue(pt == buf);
  52676. ExpectIntEQ(DH_generate_key(dh), 1);
  52677. ExpectIntEQ(DH_generate_key(dh), 1);
  52678. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  52679. ExpectNotNull(pub = BN_new());
  52680. ExpectIntEQ(BN_set_word(pub, 1), 1);
  52681. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  52682. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  52683. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  52684. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  52685. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  52686. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  52687. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  52688. BN_free(pub);
  52689. pub = NULL;
  52690. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52691. (const BIGNUM**)&q,
  52692. (const BIGNUM**)&g);
  52693. ExpectPtrEq(p, dh->p);
  52694. ExpectPtrEq(q, dh->q);
  52695. ExpectPtrEq(g, dh->g);
  52696. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52697. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52698. ExpectPtrEq(pub, dh->pub_key);
  52699. ExpectPtrEq(priv, dh->priv_key);
  52700. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  52701. ExpectPtrEq(pub, dh->pub_key);
  52702. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  52703. ExpectPtrEq(priv, dh->priv_key);
  52704. pub = NULL;
  52705. priv = NULL;
  52706. ExpectNotNull(pub = BN_new());
  52707. ExpectNotNull(priv = BN_new());
  52708. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  52709. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52710. if (EXPECT_FAIL()) {
  52711. BN_free(pub);
  52712. BN_free(priv);
  52713. }
  52714. pub = NULL;
  52715. priv = NULL;
  52716. ExpectNotNull(pub = BN_new());
  52717. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  52718. if (EXPECT_FAIL()) {
  52719. BN_free(pub);
  52720. }
  52721. ExpectNotNull(priv = BN_new());
  52722. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  52723. if (EXPECT_FAIL()) {
  52724. BN_free(priv);
  52725. }
  52726. ExpectPtrEq(pub, dh->pub_key);
  52727. ExpectPtrEq(priv, dh->priv_key);
  52728. pub = NULL;
  52729. priv = NULL;
  52730. DH_free(dh);
  52731. dh = NULL;
  52732. ExpectNotNull(dh = DH_new());
  52733. p = NULL;
  52734. ExpectNotNull(p = BN_new());
  52735. ExpectIntEQ(BN_set_word(p, 1), 1);
  52736. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52737. ExpectNotNull(pub = BN_new());
  52738. ExpectNotNull(priv = BN_new());
  52739. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52740. if (EXPECT_FAIL()) {
  52741. BN_free(pub);
  52742. BN_free(priv);
  52743. }
  52744. pub = NULL;
  52745. priv = NULL;
  52746. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52747. BN_free(p);
  52748. p = NULL;
  52749. DH_free(dh);
  52750. dh = NULL;
  52751. #ifdef WOLFSSL_KEY_GEN
  52752. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  52753. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  52754. DH_free(dh);
  52755. dh = NULL;
  52756. #endif
  52757. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  52758. #endif /* OPENSSL_ALL */
  52759. (void)dh;
  52760. (void)p;
  52761. (void)q;
  52762. (void)g;
  52763. (void)pub;
  52764. (void)priv;
  52765. ExpectNotNull(dh = wolfSSL_DH_new());
  52766. /* invalid parameters test */
  52767. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  52768. (const BIGNUM**)&q,
  52769. (const BIGNUM**)&g);
  52770. DH_get0_pqg(dh, NULL,
  52771. (const BIGNUM**)&q,
  52772. (const BIGNUM**)&g);
  52773. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  52774. DH_get0_pqg(dh, NULL, NULL, NULL);
  52775. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52776. (const BIGNUM**)&q,
  52777. (const BIGNUM**)&g);
  52778. ExpectPtrEq(p, NULL);
  52779. ExpectPtrEq(q, NULL);
  52780. ExpectPtrEq(g, NULL);
  52781. DH_free(dh);
  52782. dh = NULL;
  52783. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  52784. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  52785. #if defined(OPENSSL_ALL) || \
  52786. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52787. dh = wolfSSL_DH_new();
  52788. ExpectNotNull(dh);
  52789. p = wolfSSL_BN_new();
  52790. ExpectNotNull(p);
  52791. ExpectIntEQ(BN_set_word(p, 11), 1);
  52792. g = wolfSSL_BN_new();
  52793. ExpectNotNull(g);
  52794. ExpectIntEQ(BN_set_word(g, 2), 1);
  52795. q = wolfSSL_BN_new();
  52796. ExpectNotNull(q);
  52797. ExpectIntEQ(BN_set_word(q, 5), 1);
  52798. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  52799. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  52800. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  52801. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  52802. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  52803. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  52804. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  52805. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  52806. /* Don't need q. */
  52807. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52808. if (EXPECT_FAIL()) {
  52809. BN_free(p);
  52810. BN_free(g);
  52811. }
  52812. p = NULL;
  52813. g = NULL;
  52814. /* Setting again will free the p and g. */
  52815. wolfSSL_BN_free(q);
  52816. q = NULL;
  52817. DH_free(dh);
  52818. dh = NULL;
  52819. dh = wolfSSL_DH_new();
  52820. ExpectNotNull(dh);
  52821. p = wolfSSL_BN_new();
  52822. ExpectNotNull(p);
  52823. ExpectIntEQ(BN_set_word(p, 11), 1);
  52824. g = wolfSSL_BN_new();
  52825. ExpectNotNull(g);
  52826. ExpectIntEQ(BN_set_word(g, 2), 1);
  52827. q = wolfSSL_BN_new();
  52828. ExpectNotNull(q);
  52829. ExpectIntEQ(BN_set_word(q, 5), 1);
  52830. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  52831. /* p, q and g are now owned by dh - don't free. */
  52832. if (EXPECT_FAIL()) {
  52833. BN_free(p);
  52834. BN_free(q);
  52835. BN_free(g);
  52836. }
  52837. p = NULL;
  52838. q = NULL;
  52839. g = NULL;
  52840. p = wolfSSL_BN_new();
  52841. ExpectNotNull(p);
  52842. ExpectIntEQ(BN_set_word(p, 11), 1);
  52843. g = wolfSSL_BN_new();
  52844. ExpectNotNull(g);
  52845. ExpectIntEQ(BN_set_word(g, 2), 1);
  52846. q = wolfSSL_BN_new();
  52847. ExpectNotNull(q);
  52848. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  52849. if (EXPECT_FAIL()) {
  52850. BN_free(p);
  52851. }
  52852. p = NULL;
  52853. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  52854. if (EXPECT_FAIL()) {
  52855. BN_free(q);
  52856. }
  52857. q = NULL;
  52858. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  52859. if (EXPECT_FAIL()) {
  52860. BN_free(g);
  52861. }
  52862. g = NULL;
  52863. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  52864. /* p, q and g are now owned by dh - don't free. */
  52865. DH_free(dh);
  52866. dh = NULL;
  52867. ExpectIntEQ(DH_generate_key(NULL), 0);
  52868. ExpectNotNull(dh = DH_new());
  52869. ExpectIntEQ(DH_generate_key(dh), 0);
  52870. p = wolfSSL_BN_new();
  52871. ExpectNotNull(p);
  52872. ExpectIntEQ(BN_set_word(p, 0), 1);
  52873. g = wolfSSL_BN_new();
  52874. ExpectNotNull(g);
  52875. ExpectIntEQ(BN_set_word(g, 2), 1);
  52876. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52877. if (EXPECT_FAIL()) {
  52878. BN_free(p);
  52879. BN_free(g);
  52880. }
  52881. p = NULL;
  52882. g = NULL;
  52883. ExpectIntEQ(DH_generate_key(dh), 0);
  52884. DH_free(dh);
  52885. dh = NULL;
  52886. #endif
  52887. #endif
  52888. /* Test DH_up_ref() */
  52889. dh = wolfSSL_DH_new();
  52890. ExpectNotNull(dh);
  52891. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  52892. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  52893. DH_free(dh); /* decrease ref count */
  52894. DH_free(dh); /* free WOLFSSL_DH */
  52895. dh = NULL;
  52896. q = NULL;
  52897. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  52898. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  52899. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  52900. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  52901. #ifdef HAVE_FFDHE_2048
  52902. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52903. DH_free(dh);
  52904. dh = NULL;
  52905. q = NULL;
  52906. #endif
  52907. #ifdef HAVE_FFDHE_3072
  52908. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  52909. DH_free(dh);
  52910. dh = NULL;
  52911. q = NULL;
  52912. #endif
  52913. #ifdef HAVE_FFDHE_4096
  52914. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  52915. DH_free(dh);
  52916. dh = NULL;
  52917. q = NULL;
  52918. #endif
  52919. #else
  52920. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52921. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  52922. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  52923. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  52924. #endif /* OPENSSL_EXTRA && !NO_DH */
  52925. return EXPECT_RESULT();
  52926. }
  52927. static int test_wolfSSL_DH_dup(void)
  52928. {
  52929. EXPECT_DECLS;
  52930. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  52931. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  52932. defined(OPENSSL_EXTRA)
  52933. DH *dh = NULL;
  52934. DH *dhDup = NULL;
  52935. ExpectNotNull(dh = wolfSSL_DH_new());
  52936. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  52937. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  52938. #if defined(OPENSSL_ALL) || \
  52939. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52940. {
  52941. WOLFSSL_BIGNUM* p = NULL;
  52942. WOLFSSL_BIGNUM* g = NULL;
  52943. ExpectNotNull(p = wolfSSL_BN_new());
  52944. ExpectNotNull(g = wolfSSL_BN_new());
  52945. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  52946. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  52947. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52948. if (EXPECT_FAIL()) {
  52949. wolfSSL_BN_free(p);
  52950. wolfSSL_BN_free(g);
  52951. }
  52952. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  52953. wolfSSL_DH_free(dhDup);
  52954. }
  52955. #endif
  52956. wolfSSL_DH_free(dh);
  52957. #endif
  52958. #endif
  52959. return EXPECT_RESULT();
  52960. }
  52961. static int test_wolfSSL_DH_check(void)
  52962. {
  52963. EXPECT_DECLS;
  52964. #ifdef OPENSSL_ALL
  52965. #ifndef NO_DH
  52966. #ifndef NO_BIO
  52967. #ifndef NO_DSA
  52968. byte buf[6000];
  52969. char file[] = "./certs/dsaparams.pem";
  52970. XFILE f = XBADFILE;
  52971. int bytes;
  52972. BIO* bio = NULL;
  52973. DSA* dsa = NULL;
  52974. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  52975. static const byte dh2048[] = {
  52976. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52977. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52978. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52979. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52980. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52981. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52982. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52983. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52984. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52985. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52986. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52987. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52988. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52989. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52990. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52991. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52992. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52993. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52994. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52995. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52996. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52997. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52998. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52999. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53000. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53001. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53002. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53003. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53004. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53005. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53006. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53007. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53008. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53009. 0x93, 0x02, 0x01, 0x02
  53010. };
  53011. const byte* params;
  53012. #endif
  53013. DH* dh = NULL;
  53014. WOLFSSL_BIGNUM* p = NULL;
  53015. WOLFSSL_BIGNUM* g = NULL;
  53016. WOLFSSL_BIGNUM* pTmp = NULL;
  53017. WOLFSSL_BIGNUM* gTmp = NULL;
  53018. int codes = -1;
  53019. #ifndef NO_DSA
  53020. /* Initialize DH */
  53021. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  53022. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  53023. if (f != XBADFILE)
  53024. XFCLOSE(f);
  53025. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  53026. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  53027. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  53028. ExpectNotNull(dh);
  53029. BIO_free(bio);
  53030. DSA_free(dsa);
  53031. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53032. params = dh2048;
  53033. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  53034. (long)sizeof(dh2048)));
  53035. #else
  53036. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  53037. #endif
  53038. /* Test assumed to be valid dh.
  53039. * Should return WOLFSSL_SUCCESS
  53040. * codes should be 0
  53041. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  53042. */
  53043. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53044. ExpectIntEQ(codes, 0);
  53045. /* Test NULL dh: expected BAD_FUNC_ARG */
  53046. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  53047. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  53048. if (dh != NULL) {
  53049. pTmp = dh->p;
  53050. dh->p = NULL;
  53051. }
  53052. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53053. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53054. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53055. /* set dh->p back to normal so it won't fail on next tests */
  53056. if (dh != NULL) {
  53057. dh->p = pTmp;
  53058. pTmp = NULL;
  53059. }
  53060. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  53061. if (dh != NULL) {
  53062. gTmp = dh->g;
  53063. dh->g = NULL;
  53064. }
  53065. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53066. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53067. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  53068. if (dh != NULL) {
  53069. dh->g = gTmp;
  53070. gTmp = NULL;
  53071. }
  53072. /* Cleanup */
  53073. DH_free(dh);
  53074. dh = NULL;
  53075. dh = DH_new();
  53076. ExpectNotNull(dh);
  53077. /* Check empty DH. */
  53078. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53079. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53080. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  53081. /* Check non-prime valued p. */
  53082. ExpectNotNull(p = BN_new());
  53083. ExpectIntEQ(BN_set_word(p, 4), 1);
  53084. ExpectNotNull(g = BN_new());
  53085. ExpectIntEQ(BN_set_word(g, 2), 1);
  53086. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  53087. if (EXPECT_FAIL()) {
  53088. wolfSSL_BN_free(p);
  53089. wolfSSL_BN_free(g);
  53090. }
  53091. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53092. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53093. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53094. DH_free(dh);
  53095. dh = NULL;
  53096. #endif
  53097. #endif /* !NO_DH && !NO_DSA */
  53098. #endif
  53099. return EXPECT_RESULT();
  53100. }
  53101. static int test_wolfSSL_DH_prime(void)
  53102. {
  53103. EXPECT_DECLS;
  53104. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53105. WOLFSSL_BIGNUM* bn = NULL;
  53106. #if WOLFSSL_MAX_BN_BITS >= 768
  53107. WOLFSSL_BIGNUM* bn2 = NULL;
  53108. #endif
  53109. bn = wolfSSL_DH_768_prime(NULL);
  53110. #if WOLFSSL_MAX_BN_BITS >= 768
  53111. ExpectNotNull(bn);
  53112. bn2 = wolfSSL_DH_768_prime(bn);
  53113. ExpectNotNull(bn2);
  53114. ExpectTrue(bn == bn2);
  53115. wolfSSL_BN_free(bn);
  53116. bn = NULL;
  53117. #else
  53118. ExpectNull(bn);
  53119. #endif
  53120. bn = wolfSSL_DH_1024_prime(NULL);
  53121. #if WOLFSSL_MAX_BN_BITS >= 1024
  53122. ExpectNotNull(bn);
  53123. wolfSSL_BN_free(bn);
  53124. bn = NULL;
  53125. #else
  53126. ExpectNull(bn);
  53127. #endif
  53128. bn = wolfSSL_DH_2048_prime(NULL);
  53129. #if WOLFSSL_MAX_BN_BITS >= 2048
  53130. ExpectNotNull(bn);
  53131. wolfSSL_BN_free(bn);
  53132. bn = NULL;
  53133. #else
  53134. ExpectNull(bn);
  53135. #endif
  53136. bn = wolfSSL_DH_3072_prime(NULL);
  53137. #if WOLFSSL_MAX_BN_BITS >= 3072
  53138. ExpectNotNull(bn);
  53139. wolfSSL_BN_free(bn);
  53140. bn = NULL;
  53141. #else
  53142. ExpectNull(bn);
  53143. #endif
  53144. bn = wolfSSL_DH_4096_prime(NULL);
  53145. #if WOLFSSL_MAX_BN_BITS >= 4096
  53146. ExpectNotNull(bn);
  53147. wolfSSL_BN_free(bn);
  53148. bn = NULL;
  53149. #else
  53150. ExpectNull(bn);
  53151. #endif
  53152. bn = wolfSSL_DH_6144_prime(NULL);
  53153. #if WOLFSSL_MAX_BN_BITS >= 6144
  53154. ExpectNotNull(bn);
  53155. wolfSSL_BN_free(bn);
  53156. bn = NULL;
  53157. #else
  53158. ExpectNull(bn);
  53159. #endif
  53160. bn = wolfSSL_DH_8192_prime(NULL);
  53161. #if WOLFSSL_MAX_BN_BITS >= 8192
  53162. ExpectNotNull(bn);
  53163. wolfSSL_BN_free(bn);
  53164. bn = NULL;
  53165. #else
  53166. ExpectNull(bn);
  53167. #endif
  53168. #endif
  53169. return EXPECT_RESULT();
  53170. }
  53171. static int test_wolfSSL_DH_1536_prime(void)
  53172. {
  53173. EXPECT_DECLS;
  53174. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53175. BIGNUM* bn = NULL;
  53176. unsigned char bits[200];
  53177. int sz = 192; /* known binary size */
  53178. const byte expected[] = {
  53179. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53180. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  53181. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  53182. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  53183. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  53184. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  53185. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  53186. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  53187. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  53188. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  53189. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  53190. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  53191. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  53192. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  53193. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  53194. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  53195. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  53196. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  53197. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  53198. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  53199. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  53200. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  53201. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  53202. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53203. };
  53204. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  53205. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  53206. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  53207. BN_free(bn);
  53208. #endif
  53209. return EXPECT_RESULT();
  53210. }
  53211. static int test_wolfSSL_DH_get_2048_256(void)
  53212. {
  53213. EXPECT_DECLS;
  53214. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53215. WOLFSSL_DH* dh = NULL;
  53216. const WOLFSSL_BIGNUM* pBn;
  53217. const WOLFSSL_BIGNUM* gBn;
  53218. const WOLFSSL_BIGNUM* qBn;
  53219. const byte pExpected[] = {
  53220. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  53221. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  53222. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  53223. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  53224. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  53225. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  53226. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  53227. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  53228. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  53229. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  53230. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  53231. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  53232. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  53233. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  53234. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  53235. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  53236. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  53237. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  53238. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  53239. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  53240. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  53241. 0x1E, 0x1A, 0x15, 0x97
  53242. };
  53243. const byte gExpected[] = {
  53244. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  53245. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  53246. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  53247. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  53248. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  53249. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  53250. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  53251. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  53252. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  53253. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  53254. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  53255. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  53256. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  53257. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  53258. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  53259. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  53260. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  53261. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  53262. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  53263. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  53264. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  53265. 0x6C, 0xC4, 0x16, 0x59
  53266. };
  53267. const byte qExpected[] = {
  53268. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  53269. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  53270. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  53271. };
  53272. int pSz = 0;
  53273. int qSz = 0;
  53274. int gSz = 0;
  53275. byte* pReturned = NULL;
  53276. byte* qReturned = NULL;
  53277. byte* gReturned = NULL;
  53278. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  53279. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  53280. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  53281. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  53282. DYNAMIC_TYPE_TMP_BUFFER));
  53283. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  53284. ExpectIntEQ(pSz, sizeof(pExpected));
  53285. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  53286. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  53287. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  53288. DYNAMIC_TYPE_TMP_BUFFER));
  53289. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  53290. ExpectIntEQ(qSz, sizeof(qExpected));
  53291. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  53292. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  53293. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  53294. DYNAMIC_TYPE_TMP_BUFFER));
  53295. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  53296. ExpectIntEQ(gSz, sizeof(gExpected));
  53297. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  53298. wolfSSL_DH_free(dh);
  53299. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53300. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53301. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53302. #endif
  53303. return EXPECT_RESULT();
  53304. }
  53305. static int test_wolfSSL_PEM_write_DHparams(void)
  53306. {
  53307. EXPECT_DECLS;
  53308. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  53309. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  53310. DH* dh = NULL;
  53311. BIO* bio = NULL;
  53312. XFILE fp = XBADFILE;
  53313. byte pem[2048];
  53314. int pemSz = 0;
  53315. const char expected[] =
  53316. "-----BEGIN DH PARAMETERS-----\n"
  53317. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  53318. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  53319. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  53320. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  53321. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  53322. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  53323. "-----END DH PARAMETERS-----\n";
  53324. const char badPem[] =
  53325. "-----BEGIN DH PARAMETERS-----\n"
  53326. "-----END DH PARAMETERS-----\n";
  53327. const char emptySeqPem[] =
  53328. "-----BEGIN DH PARAMETERS-----\n"
  53329. "MAA=\n"
  53330. "-----END DH PARAMETERS-----\n";
  53331. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  53332. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53333. if (fp != XBADFILE) {
  53334. XFCLOSE(fp);
  53335. fp = XBADFILE;
  53336. }
  53337. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  53338. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53339. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53340. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  53341. (int)sizeof(badPem));
  53342. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53343. BIO_free(bio);
  53344. bio = NULL;
  53345. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53346. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53347. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  53348. (int)sizeof(emptySeqPem));
  53349. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53350. BIO_free(bio);
  53351. bio = NULL;
  53352. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53353. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  53354. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53355. BIO_free(bio);
  53356. bio = NULL;
  53357. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  53358. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  53359. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  53360. DH_free(dh);
  53361. dh = NULL;
  53362. dh = wolfSSL_DH_new();
  53363. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  53364. if (fp != XBADFILE) {
  53365. XFCLOSE(fp);
  53366. fp = XBADFILE;
  53367. }
  53368. wolfSSL_DH_free(dh);
  53369. dh = NULL;
  53370. /* check results */
  53371. XMEMSET(pem, 0, sizeof(pem));
  53372. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  53373. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53374. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  53375. if (fp != XBADFILE)
  53376. XFCLOSE(fp);
  53377. #endif
  53378. return EXPECT_RESULT();
  53379. }
  53380. static int test_wolfSSL_d2i_DHparams(void)
  53381. {
  53382. EXPECT_DECLS;
  53383. #ifdef OPENSSL_ALL
  53384. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53385. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53386. XFILE f = XBADFILE;
  53387. unsigned char buf[4096];
  53388. const unsigned char* pt = buf;
  53389. #ifdef HAVE_FFDHE_2048
  53390. const char* params1 = "./certs/dh2048.der";
  53391. #endif
  53392. #ifdef HAVE_FFDHE_3072
  53393. const char* params2 = "./certs/dh3072.der";
  53394. #endif
  53395. long len = 0;
  53396. WOLFSSL_DH* dh = NULL;
  53397. XMEMSET(buf, 0, sizeof(buf));
  53398. /* Test 2048 bit parameters */
  53399. #ifdef HAVE_FFDHE_2048
  53400. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53401. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53402. if (f != XBADFILE) {
  53403. XFCLOSE(f);
  53404. f = XBADFILE;
  53405. }
  53406. /* Valid case */
  53407. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53408. ExpectNotNull(dh->p);
  53409. ExpectNotNull(dh->g);
  53410. ExpectTrue(pt == buf);
  53411. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  53412. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  53413. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  53414. /* Invalid cases */
  53415. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53416. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53417. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  53418. DH_free(dh);
  53419. dh = NULL;
  53420. *buf = 0;
  53421. pt = buf;
  53422. #endif /* HAVE_FFDHE_2048 */
  53423. /* Test 3072 bit parameters */
  53424. #ifdef HAVE_FFDHE_3072
  53425. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53426. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53427. if (f != XBADFILE) {
  53428. XFCLOSE(f);
  53429. f = XBADFILE;
  53430. }
  53431. /* Valid case */
  53432. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  53433. ExpectNotNull(dh->p);
  53434. ExpectNotNull(dh->g);
  53435. ExpectTrue(pt != buf);
  53436. ExpectIntEQ(DH_generate_key(dh), 1);
  53437. /* Invalid cases */
  53438. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53439. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53440. DH_free(dh);
  53441. dh = NULL;
  53442. #endif /* HAVE_FFDHE_3072 */
  53443. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53444. #endif /* !NO_DH */
  53445. #endif
  53446. return EXPECT_RESULT();
  53447. }
  53448. static int test_wolfSSL_DH_LoadDer(void)
  53449. {
  53450. EXPECT_DECLS;
  53451. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  53452. defined(OPENSSL_EXTRA)
  53453. static const byte dh2048[] = {
  53454. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53455. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53456. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53457. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53458. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53459. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53460. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53461. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53462. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53463. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53464. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53465. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53466. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53467. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53468. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53469. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53470. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53471. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53472. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53473. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53474. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53475. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53476. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53477. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53478. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53479. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53480. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53481. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53482. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53483. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53484. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53485. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53486. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53487. 0x93, 0x02, 0x01, 0x02
  53488. };
  53489. WOLFSSL_DH* dh = NULL;
  53490. ExpectNotNull(dh = wolfSSL_DH_new());
  53491. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  53492. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  53493. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  53494. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  53495. wolfSSL_DH_free(dh);
  53496. #endif
  53497. return EXPECT_RESULT();
  53498. }
  53499. static int test_wolfSSL_i2d_DHparams(void)
  53500. {
  53501. EXPECT_DECLS;
  53502. #ifdef OPENSSL_ALL
  53503. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53504. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53505. XFILE f = XBADFILE;
  53506. unsigned char buf[4096];
  53507. const unsigned char* pt;
  53508. unsigned char* pt2;
  53509. #ifdef HAVE_FFDHE_2048
  53510. const char* params1 = "./certs/dh2048.der";
  53511. #endif
  53512. #ifdef HAVE_FFDHE_3072
  53513. const char* params2 = "./certs/dh3072.der";
  53514. #endif
  53515. long len = 0;
  53516. WOLFSSL_DH* dh = NULL;
  53517. /* Test 2048 bit parameters */
  53518. #ifdef HAVE_FFDHE_2048
  53519. pt = buf;
  53520. pt2 = buf;
  53521. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53522. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53523. if (f != XBADFILE) {
  53524. XFCLOSE(f);
  53525. f = XBADFILE;
  53526. }
  53527. /* Valid case */
  53528. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53529. ExpectTrue(pt == buf);
  53530. ExpectIntEQ(DH_generate_key(dh), 1);
  53531. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  53532. /* Invalid case */
  53533. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53534. /* Return length only */
  53535. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  53536. DH_free(dh);
  53537. dh = NULL;
  53538. *buf = 0;
  53539. #endif
  53540. /* Test 3072 bit parameters */
  53541. #ifdef HAVE_FFDHE_3072
  53542. pt = buf;
  53543. pt2 = buf;
  53544. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53545. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53546. if (f != XBADFILE) {
  53547. XFCLOSE(f);
  53548. f = XBADFILE;
  53549. }
  53550. /* Valid case */
  53551. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53552. ExpectTrue(pt == buf);
  53553. ExpectIntEQ(DH_generate_key(dh), 1);
  53554. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  53555. /* Invalid case */
  53556. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53557. /* Return length only */
  53558. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  53559. DH_free(dh);
  53560. dh = NULL;
  53561. #endif
  53562. dh = DH_new();
  53563. ExpectNotNull(dh);
  53564. pt2 = buf;
  53565. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  53566. DH_free(dh);
  53567. dh = NULL;
  53568. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53569. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  53570. #endif
  53571. return EXPECT_RESULT();
  53572. }
  53573. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  53574. /*----------------------------------------------------------------------------*
  53575. | EC
  53576. *----------------------------------------------------------------------------*/
  53577. static int test_wolfSSL_EC_GROUP(void)
  53578. {
  53579. EXPECT_DECLS;
  53580. #ifdef OPENSSL_EXTRA
  53581. EC_GROUP *group = NULL;
  53582. EC_GROUP *group2 = NULL;
  53583. EC_GROUP *group3 = NULL;
  53584. #ifndef HAVE_ECC_BRAINPOOL
  53585. EC_GROUP *group4 = NULL;
  53586. #endif
  53587. WOLFSSL_BIGNUM* order = NULL;
  53588. int group_bits;
  53589. int i;
  53590. static const int knownEccNids[] = {
  53591. NID_X9_62_prime192v1,
  53592. NID_X9_62_prime192v2,
  53593. NID_X9_62_prime192v3,
  53594. NID_X9_62_prime239v1,
  53595. NID_X9_62_prime239v2,
  53596. NID_X9_62_prime239v3,
  53597. NID_X9_62_prime256v1,
  53598. NID_secp112r1,
  53599. NID_secp112r2,
  53600. NID_secp128r1,
  53601. NID_secp128r2,
  53602. NID_secp160r1,
  53603. NID_secp160r2,
  53604. NID_secp224r1,
  53605. NID_secp384r1,
  53606. NID_secp521r1,
  53607. NID_secp160k1,
  53608. NID_secp192k1,
  53609. NID_secp224k1,
  53610. NID_secp256k1,
  53611. NID_brainpoolP160r1,
  53612. NID_brainpoolP192r1,
  53613. NID_brainpoolP224r1,
  53614. NID_brainpoolP256r1,
  53615. NID_brainpoolP320r1,
  53616. NID_brainpoolP384r1,
  53617. NID_brainpoolP512r1,
  53618. };
  53619. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  53620. static const int knownEccEnums[] = {
  53621. ECC_SECP192R1,
  53622. ECC_PRIME192V2,
  53623. ECC_PRIME192V3,
  53624. ECC_PRIME239V1,
  53625. ECC_PRIME239V2,
  53626. ECC_PRIME239V3,
  53627. ECC_SECP256R1,
  53628. ECC_SECP112R1,
  53629. ECC_SECP112R2,
  53630. ECC_SECP128R1,
  53631. ECC_SECP128R2,
  53632. ECC_SECP160R1,
  53633. ECC_SECP160R2,
  53634. ECC_SECP224R1,
  53635. ECC_SECP384R1,
  53636. ECC_SECP521R1,
  53637. ECC_SECP160K1,
  53638. ECC_SECP192K1,
  53639. ECC_SECP224K1,
  53640. ECC_SECP256K1,
  53641. ECC_BRAINPOOLP160R1,
  53642. ECC_BRAINPOOLP192R1,
  53643. ECC_BRAINPOOLP224R1,
  53644. ECC_BRAINPOOLP256R1,
  53645. ECC_BRAINPOOLP320R1,
  53646. ECC_BRAINPOOLP384R1,
  53647. ECC_BRAINPOOLP512R1,
  53648. };
  53649. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  53650. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53651. ExpectNotNull(group2 = EC_GROUP_dup(group));
  53652. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  53653. #ifndef HAVE_ECC_BRAINPOOL
  53654. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  53655. NID_brainpoolP256r1));
  53656. #endif
  53657. ExpectNull(EC_GROUP_dup(NULL));
  53658. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  53659. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53660. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  53661. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  53662. #ifndef HAVE_ECC_BRAINPOOL
  53663. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  53664. #endif
  53665. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  53666. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  53667. ExpectNotNull(order = BN_new());
  53668. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  53669. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  53670. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  53671. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  53672. wolfSSL_BN_free(order);
  53673. ExpectNotNull(EC_GROUP_method_of(group));
  53674. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  53675. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  53676. NID_X9_62_prime_field);
  53677. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  53678. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  53679. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  53680. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  53681. #ifndef NO_WOLFSSL_STUB
  53682. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  53683. #endif
  53684. #ifndef HAVE_ECC_BRAINPOOL
  53685. EC_GROUP_free(group4);
  53686. #endif
  53687. EC_GROUP_free(group3);
  53688. EC_GROUP_free(group2);
  53689. EC_GROUP_free(group);
  53690. for (i = 0; i < knowEccNidsLen; i++) {
  53691. group = NULL;
  53692. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  53693. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  53694. EC_GROUP_free(group);
  53695. }
  53696. for (i = 0; i < knowEccEnumsLen; i++) {
  53697. group = NULL;
  53698. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  53699. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  53700. EC_GROUP_free(group);
  53701. }
  53702. #endif
  53703. return EXPECT_RESULT();
  53704. }
  53705. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  53706. {
  53707. EXPECT_DECLS;
  53708. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53709. EC_GROUP *group = NULL;
  53710. BIO* bio = NULL;
  53711. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53712. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53713. EC_GROUP *ret = NULL;
  53714. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  53715. "BgUrgQQAIg==\n"
  53716. "-----END EC PARAMETERS-----";
  53717. #endif
  53718. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  53719. "MAA=\n"
  53720. "-----END EC PARAMETERS-----";
  53721. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  53722. "BgA=\n"
  53723. "-----END EC PARAMETERS-----";
  53724. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  53725. "BgE=\n"
  53726. "-----END EC PARAMETERS-----";
  53727. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  53728. "BgE*\n"
  53729. "-----END EC PARAMETERS-----";
  53730. /* Test that first parameter, bio, being NULL fails. */
  53731. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  53732. /* Test that reading named parameters works. */
  53733. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53734. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  53735. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53736. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53737. BIO_free(bio);
  53738. bio = NULL;
  53739. EC_GROUP_free(group);
  53740. group = NULL;
  53741. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53742. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53743. /* Test that reusing group works. */
  53744. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53745. sizeof(ec_nc_p384)));
  53746. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53747. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53748. BIO_free(bio);
  53749. bio = NULL;
  53750. EC_GROUP_free(group);
  53751. group = NULL;
  53752. /* Test that returning through group works. */
  53753. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53754. sizeof(ec_nc_p384)));
  53755. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53756. ExpectIntEQ(group == ret, 1);
  53757. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53758. BIO_free(bio);
  53759. bio = NULL;
  53760. EC_GROUP_free(group);
  53761. group = NULL;
  53762. #endif
  53763. /* Test 0x30, 0x00 (not and object id) fails. */
  53764. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  53765. sizeof(ec_nc_bad_1)));
  53766. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53767. BIO_free(bio);
  53768. bio = NULL;
  53769. /* Test 0x06, 0x00 (empty object id) fails. */
  53770. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  53771. sizeof(ec_nc_bad_2)));
  53772. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53773. BIO_free(bio);
  53774. bio = NULL;
  53775. /* Test 0x06, 0x01 (badly formed object id) fails. */
  53776. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  53777. sizeof(ec_nc_bad_3)));
  53778. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53779. BIO_free(bio);
  53780. bio = NULL;
  53781. /* Test invalid PEM encoding - invalid character. */
  53782. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  53783. sizeof(ec_nc_bad_4)));
  53784. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53785. BIO_free(bio);
  53786. #endif
  53787. return EXPECT_RESULT();
  53788. }
  53789. static int test_wolfSSL_EC_POINT(void)
  53790. {
  53791. EXPECT_DECLS;
  53792. #if !defined(WOLFSSL_SP_MATH) && \
  53793. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  53794. #ifdef OPENSSL_EXTRA
  53795. BN_CTX* ctx = NULL;
  53796. EC_GROUP* group = NULL;
  53797. #ifndef HAVE_ECC_BRAINPOOL
  53798. EC_GROUP* group2 = NULL;
  53799. #endif
  53800. EC_POINT* Gxy = NULL;
  53801. EC_POINT* new_point = NULL;
  53802. EC_POINT* set_point = NULL;
  53803. EC_POINT* infinity = NULL;
  53804. BIGNUM* k = NULL;
  53805. BIGNUM* Gx = NULL;
  53806. BIGNUM* Gy = NULL;
  53807. BIGNUM* Gz = NULL;
  53808. BIGNUM* X = NULL;
  53809. BIGNUM* Y = NULL;
  53810. BIGNUM* set_point_bn = NULL;
  53811. char* hexStr = NULL;
  53812. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  53813. "17AF381913FF7A96314EA47055EA0FD0";
  53814. /* NISTP256R1 Gx/Gy */
  53815. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  53816. "77037D812DEB33A0F4A13945D898C296";
  53817. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53818. "2BCE33576B315ECECBB6406837BF51F5";
  53819. #ifndef HAVE_SELFTEST
  53820. EC_POINT *tmp = NULL;
  53821. size_t bin_len;
  53822. unsigned int blen = 0;
  53823. unsigned char* buf = NULL;
  53824. unsigned char bufInf[1] = { 0x00 };
  53825. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  53826. "77037D812DEB33A0F4A13945D898C296"
  53827. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53828. "2BCE33576B315ECECBB6406837BF51F5";
  53829. const unsigned char binUncompG[] = {
  53830. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53831. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53832. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53833. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53834. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53835. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53836. };
  53837. const unsigned char binUncompGBad[] = {
  53838. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53839. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53840. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53841. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53842. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53843. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53844. };
  53845. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  53846. "77037D812DEB33A0F4A13945D898C296";
  53847. #ifdef HAVE_COMP_KEY
  53848. const unsigned char binCompG[] = {
  53849. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53850. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53851. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53852. };
  53853. #endif
  53854. #endif
  53855. ExpectNotNull(ctx = BN_CTX_new());
  53856. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53857. #ifndef HAVE_ECC_BRAINPOOL
  53858. /* Used to make groups curve_idx == -1. */
  53859. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  53860. #endif
  53861. ExpectNull(EC_POINT_new(NULL));
  53862. ExpectNotNull(Gxy = EC_POINT_new(group));
  53863. ExpectNotNull(new_point = EC_POINT_new(group));
  53864. ExpectNotNull(set_point = EC_POINT_new(group));
  53865. ExpectNotNull(X = BN_new());
  53866. ExpectNotNull(Y = BN_new());
  53867. ExpectNotNull(set_point_bn = BN_new());
  53868. ExpectNotNull(infinity = EC_POINT_new(group));
  53869. /* load test values */
  53870. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  53871. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  53872. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  53873. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  53874. /* populate coordinates for input point */
  53875. if (Gxy != NULL) {
  53876. Gxy->X = Gx;
  53877. Gxy->Y = Gy;
  53878. Gxy->Z = Gz;
  53879. }
  53880. /* Test handling of NULL point. */
  53881. EC_POINT_clear_free(NULL);
  53882. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53883. NULL, NULL, ctx), 0);
  53884. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53885. NULL, NULL, ctx), 0);
  53886. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53887. NULL, NULL, ctx), 0);
  53888. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53889. X, NULL, ctx), 0);
  53890. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53891. NULL, Y, ctx), 0);
  53892. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53893. X, Y, ctx), 0);
  53894. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53895. X, Y, ctx), 0);
  53896. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53897. NULL, Y, ctx), 0);
  53898. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53899. X, NULL, ctx), 0);
  53900. /* Getting point at infinity returns an error. */
  53901. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  53902. X, Y, ctx), 0);
  53903. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53904. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53905. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53906. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  53907. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  53908. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  53909. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  53910. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  53911. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  53912. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  53913. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  53914. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  53915. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  53916. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  53917. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  53918. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  53919. /* perform point multiplication */
  53920. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  53921. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53922. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53923. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53924. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  53925. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53926. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53927. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53928. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  53929. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53930. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53931. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53932. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  53933. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  53934. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  53935. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  53936. /* Set point to something. */
  53937. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  53938. #else
  53939. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  53940. ctx), 1);
  53941. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53942. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53943. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53944. #endif
  53945. /* check if point X coordinate is zero */
  53946. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53947. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  53948. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  53949. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  53950. #endif
  53951. /* extract the coordinates from point */
  53952. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  53953. ctx), WOLFSSL_SUCCESS);
  53954. /* check if point X coordinate is zero */
  53955. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  53956. /* set the same X and Y points in another object */
  53957. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  53958. ctx), WOLFSSL_SUCCESS);
  53959. /* compare points as they should be the same */
  53960. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  53961. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  53962. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  53963. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  53964. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  53965. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  53966. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  53967. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  53968. /* Test copying */
  53969. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  53970. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  53971. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  53972. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  53973. /* Test inverting */
  53974. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  53975. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  53976. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  53977. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  53978. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53979. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53980. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53981. {
  53982. EC_POINT* orig_point = NULL;
  53983. ExpectNotNull(orig_point = EC_POINT_new(group));
  53984. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  53985. 1);
  53986. /* new_point should be set_point inverted so adding it will revert
  53987. * the point back to set_point */
  53988. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  53989. NULL), 1);
  53990. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  53991. EC_POINT_free(orig_point);
  53992. }
  53993. #endif
  53994. /* Test getting affine converts from projective. */
  53995. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  53996. /* Force non-affine coordinates */
  53997. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  53998. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  53999. if (new_point != NULL) {
  54000. new_point->inSet = 0;
  54001. }
  54002. /* extract the coordinates from point */
  54003. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54004. ctx), WOLFSSL_SUCCESS);
  54005. /* check if point ordinates have changed. */
  54006. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  54007. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  54008. /* Test check for infinity */
  54009. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  54010. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  54011. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  54012. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  54013. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  54014. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  54015. #else
  54016. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  54017. #endif
  54018. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  54019. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  54020. /* check bn2hex */
  54021. hexStr = BN_bn2hex(k);
  54022. ExpectStrEQ(hexStr, kTest);
  54023. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54024. defined(XFPRINTF)
  54025. BN_print_fp(stderr, k);
  54026. fprintf(stderr, "\n");
  54027. #endif
  54028. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54029. hexStr = BN_bn2hex(Gx);
  54030. ExpectStrEQ(hexStr, kGx);
  54031. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54032. defined(XFPRINTF)
  54033. BN_print_fp(stderr, Gx);
  54034. fprintf(stderr, "\n");
  54035. #endif
  54036. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54037. hexStr = BN_bn2hex(Gy);
  54038. ExpectStrEQ(hexStr, kGy);
  54039. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54040. defined(XFPRINTF)
  54041. BN_print_fp(stderr, Gy);
  54042. fprintf(stderr, "\n");
  54043. #endif
  54044. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54045. #ifndef HAVE_SELFTEST
  54046. /* Test point to hex */
  54047. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54048. ctx));
  54049. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54050. ctx));
  54051. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54052. ctx));
  54053. #ifndef HAVE_ECC_BRAINPOOL
  54054. /* Group not supported in wolfCrypt. */
  54055. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54056. ctx));
  54057. #endif
  54058. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  54059. ExpectNotNull(hexStr);
  54060. ExpectStrEQ(hexStr, uncompG);
  54061. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54062. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  54063. ExpectNotNull(hexStr);
  54064. ExpectStrEQ(hexStr, compG);
  54065. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54066. /* Test point to oct */
  54067. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54068. NULL, 0, ctx), 0);
  54069. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54070. NULL, 0, ctx), 0);
  54071. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54072. NULL, 0, ctx), 0);
  54073. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54074. NULL, 0, ctx);
  54075. ExpectIntEQ(bin_len, sizeof(binUncompG));
  54076. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54077. DYNAMIC_TYPE_ECC));
  54078. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54079. buf, bin_len, ctx), bin_len);
  54080. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54081. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54082. /* Infinity (x=0, y=0) encodes as '0x00'. */
  54083. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54084. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  54085. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54086. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  54087. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54088. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  54089. ExpectIntEQ(bufInf[0], 0);
  54090. wolfSSL_EC_POINT_dump(NULL, NULL);
  54091. /* Test point i2d */
  54092. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  54093. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  54094. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  54095. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  54096. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  54097. ExpectIntEQ(blen, sizeof(binUncompG));
  54098. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  54099. blen -= 1;
  54100. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  54101. blen += 1;
  54102. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  54103. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54104. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54105. #ifdef HAVE_COMP_KEY
  54106. /* Test point to oct compressed */
  54107. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  54108. 0, ctx);
  54109. ExpectIntEQ(bin_len, sizeof(binCompG));
  54110. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54111. DYNAMIC_TYPE_ECC));
  54112. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  54113. bin_len, ctx), bin_len);
  54114. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  54115. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54116. #endif
  54117. /* Test point BN */
  54118. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  54119. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54120. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  54121. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54122. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  54123. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54124. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  54125. /* Test oct to point */
  54126. ExpectNotNull(tmp = EC_POINT_new(group));
  54127. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  54128. ctx), 0);
  54129. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  54130. ctx), 0);
  54131. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  54132. ctx), 0);
  54133. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  54134. sizeof(binUncompGBad), ctx), 0);
  54135. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  54136. ctx), 1);
  54137. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54138. EC_POINT_free(tmp);
  54139. tmp = NULL;
  54140. /* Test setting BN ordinates. */
  54141. ExpectNotNull(tmp = EC_POINT_new(group));
  54142. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54143. NULL, ctx), 0);
  54144. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  54145. NULL, ctx), 0);
  54146. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  54147. NULL, ctx), 0);
  54148. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  54149. NULL, ctx), 0);
  54150. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54151. Gy, ctx), 0);
  54152. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  54153. ctx), 0);
  54154. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  54155. ctx), 0);
  54156. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  54157. Gy, ctx), 0);
  54158. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  54159. NULL, ctx), 0);
  54160. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  54161. ctx), 1);
  54162. EC_POINT_free(tmp);
  54163. tmp = NULL;
  54164. /* Test point d2i */
  54165. ExpectNotNull(tmp = EC_POINT_new(group));
  54166. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  54167. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  54168. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  54169. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  54170. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  54171. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  54172. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  54173. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  54174. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  54175. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54176. EC_POINT_free(tmp);
  54177. tmp = NULL;
  54178. #ifdef HAVE_COMP_KEY
  54179. /* Test oct compressed to point */
  54180. ExpectNotNull(tmp = EC_POINT_new(group));
  54181. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  54182. 1);
  54183. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54184. EC_POINT_free(tmp);
  54185. tmp = NULL;
  54186. /* Test point d2i - compressed */
  54187. ExpectNotNull(tmp = EC_POINT_new(group));
  54188. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  54189. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54190. EC_POINT_free(tmp);
  54191. tmp = NULL;
  54192. #endif
  54193. #endif
  54194. /* test BN_mod_add */
  54195. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54196. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  54197. 1);
  54198. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54199. /* cleanup */
  54200. BN_free(X);
  54201. BN_free(Y);
  54202. BN_free(k);
  54203. BN_free(set_point_bn);
  54204. EC_POINT_free(infinity);
  54205. EC_POINT_free(new_point);
  54206. EC_POINT_free(set_point);
  54207. EC_POINT_clear_free(Gxy);
  54208. #ifndef HAVE_ECC_BRAINPOOL
  54209. EC_GROUP_free(group2);
  54210. #endif
  54211. EC_GROUP_free(group);
  54212. BN_CTX_free(ctx);
  54213. #endif
  54214. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  54215. return EXPECT_RESULT();
  54216. }
  54217. static int test_wolfSSL_SPAKE(void)
  54218. {
  54219. EXPECT_DECLS;
  54220. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  54221. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  54222. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54223. BIGNUM* x = NULL; /* kdc priv */
  54224. BIGNUM* y = NULL; /* client priv */
  54225. BIGNUM* w = NULL; /* shared value */
  54226. byte M_bytes[] = {
  54227. /* uncompressed */
  54228. 0x04,
  54229. /* x */
  54230. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  54231. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  54232. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  54233. /* y */
  54234. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  54235. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  54236. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  54237. };
  54238. EC_POINT* M = NULL; /* shared value */
  54239. byte N_bytes[] = {
  54240. /* uncompressed */
  54241. 0x04,
  54242. /* x */
  54243. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  54244. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  54245. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  54246. /* y */
  54247. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  54248. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  54249. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  54250. };
  54251. EC_POINT* N = NULL; /* shared value */
  54252. EC_POINT* T = NULL; /* kdc pub */
  54253. EC_POINT* tmp1 = NULL; /* kdc pub */
  54254. EC_POINT* tmp2 = NULL; /* kdc pub */
  54255. EC_POINT* S = NULL; /* client pub */
  54256. EC_POINT* client_secret = NULL;
  54257. EC_POINT* kdc_secret = NULL;
  54258. EC_GROUP* group = NULL;
  54259. BN_CTX* bn_ctx = NULL;
  54260. /* Values taken from a test run of Kerberos 5 */
  54261. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54262. ExpectNotNull(bn_ctx = BN_CTX_new());
  54263. ExpectNotNull(M = EC_POINT_new(group));
  54264. ExpectNotNull(N = EC_POINT_new(group));
  54265. ExpectNotNull(T = EC_POINT_new(group));
  54266. ExpectNotNull(tmp1 = EC_POINT_new(group));
  54267. ExpectNotNull(tmp2 = EC_POINT_new(group));
  54268. ExpectNotNull(S = EC_POINT_new(group));
  54269. ExpectNotNull(client_secret = EC_POINT_new(group));
  54270. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  54271. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  54272. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  54273. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  54274. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  54275. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  54276. "506AB395478F0AAB647752CF117B36250"), 1);
  54277. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  54278. 1);
  54279. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  54280. 1);
  54281. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  54282. /* kdc */
  54283. /* T=x*P+w*M */
  54284. /* All in one function call */
  54285. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  54286. /* Spread into separate calls */
  54287. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  54288. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  54289. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54290. 1);
  54291. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  54292. /* client */
  54293. /* S=y*P+w*N */
  54294. /* All in one function call */
  54295. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  54296. /* Spread into separate calls */
  54297. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  54298. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  54299. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54300. 1);
  54301. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  54302. /* K=y*(T-w*M) */
  54303. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  54304. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  54305. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  54306. 1);
  54307. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  54308. bn_ctx), 1);
  54309. /* kdc */
  54310. /* K=x*(S-w*N) */
  54311. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  54312. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  54313. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  54314. 1);
  54315. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  54316. 1);
  54317. /* kdc_secret == client_secret */
  54318. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  54319. BN_free(x);
  54320. BN_free(y);
  54321. BN_free(w);
  54322. EC_POINT_free(M);
  54323. EC_POINT_free(N);
  54324. EC_POINT_free(T);
  54325. EC_POINT_free(tmp1);
  54326. EC_POINT_free(tmp2);
  54327. EC_POINT_free(S);
  54328. EC_POINT_free(client_secret);
  54329. EC_POINT_free(kdc_secret);
  54330. EC_GROUP_free(group);
  54331. BN_CTX_free(bn_ctx);
  54332. #endif
  54333. return EXPECT_RESULT();
  54334. }
  54335. static int test_wolfSSL_EC_KEY_generate(void)
  54336. {
  54337. EXPECT_DECLS;
  54338. #ifdef OPENSSL_EXTRA
  54339. WOLFSSL_EC_KEY* key = NULL;
  54340. #ifndef HAVE_ECC_BRAINPOOL
  54341. WOLFSSL_EC_GROUP* group = NULL;
  54342. #endif
  54343. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54344. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  54345. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54346. wolfSSL_EC_KEY_free(key);
  54347. key = NULL;
  54348. #ifndef HAVE_ECC_BRAINPOOL
  54349. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  54350. NID_brainpoolP256r1));
  54351. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  54352. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  54353. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  54354. wolfSSL_EC_KEY_free(key);
  54355. wolfSSL_EC_GROUP_free(group);
  54356. #endif
  54357. #endif
  54358. return EXPECT_RESULT();
  54359. }
  54360. static int test_EC_i2d(void)
  54361. {
  54362. EXPECT_DECLS;
  54363. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  54364. EC_KEY *key = NULL;
  54365. EC_KEY *copy = NULL;
  54366. int len = 0;
  54367. unsigned char *buf = NULL;
  54368. unsigned char *p = NULL;
  54369. const unsigned char *tmp = NULL;
  54370. const unsigned char octBad[] = {
  54371. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54372. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54373. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54374. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54375. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54376. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54377. };
  54378. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54379. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54380. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  54381. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54382. DYNAMIC_TYPE_TMP_BUFFER));
  54383. p = buf;
  54384. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  54385. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  54386. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  54387. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  54388. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54389. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  54390. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  54391. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  54392. tmp = buf;
  54393. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  54394. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  54395. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54396. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  54397. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  54398. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  54399. tmp = buf;
  54400. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  54401. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  54402. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  54403. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  54404. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  54405. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  54406. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  54407. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  54408. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  54409. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  54410. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  54411. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  54412. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  54413. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54414. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  54415. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54416. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  54417. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54418. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  54419. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54420. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  54421. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  54422. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54423. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54424. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54425. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54426. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54427. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54428. buf = NULL;
  54429. buf = NULL;
  54430. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  54431. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54432. DYNAMIC_TYPE_TMP_BUFFER));
  54433. p = buf;
  54434. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54435. p = NULL;
  54436. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54437. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54438. p = NULL;
  54439. /* Bad point is also an invalid private key. */
  54440. tmp = octBad;
  54441. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  54442. tmp = buf;
  54443. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  54444. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54445. buf = NULL;
  54446. buf = NULL;
  54447. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  54448. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54449. DYNAMIC_TYPE_TMP_BUFFER));
  54450. p = buf;
  54451. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54452. p = NULL;
  54453. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54454. tmp = buf;
  54455. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  54456. tmp = octBad;
  54457. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  54458. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  54459. ExpectIntEQ(EC_KEY_check_key(key), 1);
  54460. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  54461. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  54462. EC_KEY_free(key);
  54463. EC_KEY_free(copy);
  54464. #endif
  54465. return EXPECT_RESULT();
  54466. }
  54467. static int test_wolfSSL_EC_curve(void)
  54468. {
  54469. EXPECT_DECLS;
  54470. #if defined(OPENSSL_EXTRA)
  54471. int nid = NID_secp160k1;
  54472. const char* nid_name = NULL;
  54473. ExpectNull(EC_curve_nid2nist(NID_sha256));
  54474. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  54475. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  54476. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  54477. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  54478. #endif
  54479. return EXPECT_RESULT();
  54480. }
  54481. static int test_wolfSSL_EC_KEY_dup(void)
  54482. {
  54483. EXPECT_DECLS;
  54484. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  54485. WOLFSSL_EC_KEY* ecKey = NULL;
  54486. WOLFSSL_EC_KEY* dupKey = NULL;
  54487. ecc_key* srcKey = NULL;
  54488. ecc_key* destKey = NULL;
  54489. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54490. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54491. /* Valid cases */
  54492. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54493. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  54494. /* Compare pubkey */
  54495. if (ecKey != NULL) {
  54496. srcKey = (ecc_key*)ecKey->internal;
  54497. }
  54498. if (dupKey != NULL) {
  54499. destKey = (ecc_key*)dupKey->internal;
  54500. }
  54501. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  54502. /* compare EC_GROUP */
  54503. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  54504. /* compare EC_POINT */
  54505. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  54506. dupKey->pub_key, NULL), MP_EQ);
  54507. /* compare BIGNUM */
  54508. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  54509. wolfSSL_EC_KEY_free(dupKey);
  54510. dupKey = NULL;
  54511. /* Invalid cases */
  54512. /* NULL key */
  54513. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  54514. /* NULL ecc_key */
  54515. if (ecKey != NULL) {
  54516. wc_ecc_free((ecc_key*)ecKey->internal);
  54517. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  54518. ecKey->internal = NULL; /* Set ecc_key to NULL */
  54519. }
  54520. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54521. wolfSSL_EC_KEY_free(ecKey);
  54522. ecKey = NULL;
  54523. wolfSSL_EC_KEY_free(dupKey);
  54524. dupKey = NULL;
  54525. /* NULL Group */
  54526. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54527. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54528. if (ecKey != NULL) {
  54529. wolfSSL_EC_GROUP_free(ecKey->group);
  54530. ecKey->group = NULL; /* Set group to NULL */
  54531. }
  54532. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54533. wolfSSL_EC_KEY_free(ecKey);
  54534. ecKey = NULL;
  54535. wolfSSL_EC_KEY_free(dupKey);
  54536. dupKey = NULL;
  54537. /* NULL public key */
  54538. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54539. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54540. if (ecKey != NULL) {
  54541. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  54542. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  54543. }
  54544. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54545. if (ecKey != NULL) {
  54546. wolfSSL_EC_POINT_free(ecKey->pub_key);
  54547. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  54548. }
  54549. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54550. wolfSSL_EC_KEY_free(ecKey);
  54551. ecKey = NULL;
  54552. wolfSSL_EC_KEY_free(dupKey);
  54553. dupKey = NULL;
  54554. /* NULL private key */
  54555. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54556. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54557. if (ecKey != NULL) {
  54558. wolfSSL_BN_free(ecKey->priv_key);
  54559. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  54560. }
  54561. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54562. wolfSSL_EC_KEY_free(ecKey);
  54563. ecKey = NULL;
  54564. wolfSSL_EC_KEY_free(dupKey);
  54565. dupKey = NULL;
  54566. /* Test EC_KEY_up_ref */
  54567. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54568. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  54569. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  54570. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  54571. /* reference count doesn't follow duplicate */
  54572. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54573. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  54574. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  54575. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  54576. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  54577. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  54578. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  54579. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  54580. #endif
  54581. return EXPECT_RESULT();
  54582. }
  54583. static int test_wolfSSL_EC_KEY_set_group(void)
  54584. {
  54585. EXPECT_DECLS;
  54586. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54587. defined(OPENSSL_EXTRA)
  54588. EC_KEY *key = NULL;
  54589. EC_GROUP *group = NULL;
  54590. const EC_GROUP *group2 = NULL;
  54591. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54592. ExpectNotNull(key = EC_KEY_new());
  54593. ExpectNull(EC_KEY_get0_group(NULL));
  54594. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  54595. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  54596. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  54597. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  54598. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  54599. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  54600. EC_GROUP_free(group);
  54601. EC_KEY_free(key);
  54602. #endif
  54603. return EXPECT_RESULT();
  54604. }
  54605. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  54606. {
  54607. EXPECT_DECLS;
  54608. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54609. BIO* bio = NULL;
  54610. EC_KEY* key = NULL;
  54611. /* Error condition: NULL key. */
  54612. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  54613. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  54614. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  54615. /* Conversion form defaults to uncompressed. */
  54616. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54617. #ifdef HAVE_COMP_KEY
  54618. /* Explicitly set to compressed. */
  54619. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54620. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  54621. #else
  54622. /* Will still work just won't change anything. */
  54623. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54624. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54625. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  54626. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54627. #endif
  54628. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  54629. BIO_free(bio);
  54630. EC_KEY_free(key);
  54631. #endif
  54632. return EXPECT_RESULT();
  54633. }
  54634. static int test_wolfSSL_EC_KEY_private_key(void)
  54635. {
  54636. EXPECT_DECLS;
  54637. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54638. WOLFSSL_EC_KEY* key = NULL;
  54639. WOLFSSL_BIGNUM* priv = NULL;
  54640. WOLFSSL_BIGNUM* priv2 = NULL;
  54641. WOLFSSL_BIGNUM* bn;
  54642. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54643. ExpectNotNull(priv = wolfSSL_BN_new());
  54644. ExpectNotNull(priv2 = wolfSSL_BN_new());
  54645. ExpectIntNE(BN_set_word(priv, 2), 0);
  54646. ExpectIntNE(BN_set_word(priv2, 2), 0);
  54647. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  54648. /* No private key set. */
  54649. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  54650. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  54651. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  54652. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  54653. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  54654. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54655. ExpectPtrNE(bn, priv);
  54656. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  54657. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54658. ExpectPtrNE(bn, priv2);
  54659. wolfSSL_BN_free(priv2);
  54660. wolfSSL_BN_free(priv);
  54661. wolfSSL_EC_KEY_free(key);
  54662. #endif
  54663. return EXPECT_RESULT();
  54664. }
  54665. static int test_wolfSSL_EC_KEY_public_key(void)
  54666. {
  54667. EXPECT_DECLS;
  54668. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54669. WOLFSSL_EC_KEY* key = NULL;
  54670. WOLFSSL_EC_POINT* pub = NULL;
  54671. WOLFSSL_EC_POINT* point = NULL;
  54672. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54673. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  54674. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  54675. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54676. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  54677. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  54678. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  54679. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  54680. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  54681. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  54682. ExpectPtrEq(point, pub);
  54683. wolfSSL_EC_KEY_free(key);
  54684. #endif
  54685. return EXPECT_RESULT();
  54686. }
  54687. static int test_wolfSSL_EC_KEY_print_fp(void)
  54688. {
  54689. EXPECT_DECLS;
  54690. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  54691. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  54692. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  54693. !defined(NO_STDIO_FILESYSTEM)
  54694. EC_KEY* key = NULL;
  54695. /* Bad file pointer. */
  54696. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  54697. /* NULL key. */
  54698. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  54699. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  54700. /* Negative indent. */
  54701. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  54702. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54703. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54704. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54705. wolfSSL_EC_KEY_free(key);
  54706. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  54707. NID_X9_62_prime256v1)));
  54708. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54709. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54710. wolfSSL_EC_KEY_free(key);
  54711. #endif
  54712. return EXPECT_RESULT();
  54713. }
  54714. static int test_wolfSSL_EC_get_builtin_curves(void)
  54715. {
  54716. EXPECT_DECLS;
  54717. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54718. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54719. EC_builtin_curve* curves = NULL;
  54720. size_t crv_len = 0;
  54721. size_t i = 0;
  54722. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  54723. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  54724. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  54725. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  54726. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  54727. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  54728. if (curves[i].comment != NULL) {
  54729. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  54730. }
  54731. }
  54732. if (crv_len > 1) {
  54733. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  54734. }
  54735. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54736. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54737. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  54738. return EXPECT_RESULT();
  54739. }
  54740. static int test_wolfSSL_ECDSA_SIG(void)
  54741. {
  54742. EXPECT_DECLS;
  54743. #ifdef OPENSSL_EXTRA
  54744. WOLFSSL_ECDSA_SIG* sig = NULL;
  54745. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  54746. WOLFSSL_BIGNUM* r = NULL;
  54747. WOLFSSL_BIGNUM* s = NULL;
  54748. const WOLFSSL_BIGNUM* r2 = NULL;
  54749. const WOLFSSL_BIGNUM* s2 = NULL;
  54750. const unsigned char* cp = NULL;
  54751. unsigned char* p = NULL;
  54752. unsigned char outSig[8];
  54753. unsigned char sigData[8] =
  54754. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54755. unsigned char sigDataBad[8] =
  54756. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54757. wolfSSL_ECDSA_SIG_free(NULL);
  54758. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  54759. ExpectNotNull(r = wolfSSL_BN_new());
  54760. ExpectNotNull(s = wolfSSL_BN_new());
  54761. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  54762. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  54763. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  54764. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  54765. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  54766. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54767. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  54768. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  54769. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  54770. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  54771. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  54772. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  54773. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  54774. r2 = NULL;
  54775. s2 = NULL;
  54776. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54777. ExpectNull(r2);
  54778. ExpectNull(s2);
  54779. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  54780. if (EXPECT_FAIL()) {
  54781. wolfSSL_BN_free(r);
  54782. wolfSSL_BN_free(s);
  54783. }
  54784. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  54785. ExpectPtrEq(r2, r);
  54786. ExpectPtrEq(s2, s);
  54787. r2 = NULL;
  54788. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  54789. ExpectPtrEq(r2, r);
  54790. s2 = NULL;
  54791. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  54792. ExpectPtrEq(s2, s);
  54793. /* r and s are freed when sig is freed. */
  54794. wolfSSL_ECDSA_SIG_free(sig);
  54795. sig = NULL;
  54796. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  54797. cp = sigDataBad;
  54798. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  54799. cp = sigData;
  54800. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  54801. ExpectIntEQ((cp == sigData + 8), 1);
  54802. cp = sigData;
  54803. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  54804. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  54805. ExpectIntEQ((sig == sig2), 1);
  54806. cp = outSig;
  54807. p = outSig;
  54808. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  54809. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  54810. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  54811. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  54812. ExpectIntEQ((p == outSig + 8), 1);
  54813. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  54814. wolfSSL_ECDSA_SIG_free(sig);
  54815. #endif
  54816. return EXPECT_RESULT();
  54817. }
  54818. static int test_ECDSA_size_sign(void)
  54819. {
  54820. EXPECT_DECLS;
  54821. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  54822. EC_KEY* key = NULL;
  54823. ECDSA_SIG* ecdsaSig = NULL;
  54824. int id;
  54825. byte hash[WC_MAX_DIGEST_SIZE];
  54826. byte hash2[WC_MAX_DIGEST_SIZE];
  54827. byte sig[ECC_MAX_SIG_SIZE];
  54828. unsigned int sigSz = sizeof(sig);
  54829. XMEMSET(hash, 123, sizeof(hash));
  54830. XMEMSET(hash2, 234, sizeof(hash2));
  54831. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  54832. ExpectIntEQ(id, ECC_SECP256R1);
  54833. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54834. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54835. ExpectIntGE(ECDSA_size(NULL), 0);
  54836. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  54837. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  54838. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  54839. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  54840. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  54841. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  54842. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  54843. ExpectIntGE(ECDSA_size(key), sigSz);
  54844. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  54845. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  54846. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  54847. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  54848. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  54849. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  54850. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  54851. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  54852. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  54853. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  54854. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  54855. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  54856. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  54857. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  54858. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  54859. ECDSA_SIG_free(ecdsaSig);
  54860. EC_KEY_free(key);
  54861. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  54862. return EXPECT_RESULT();
  54863. }
  54864. static int test_ECDH_compute_key(void)
  54865. {
  54866. EXPECT_DECLS;
  54867. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54868. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54869. EC_KEY* key1 = NULL;
  54870. EC_KEY* key2 = NULL;
  54871. EC_POINT* pub1 = NULL;
  54872. EC_POINT* pub2 = NULL;
  54873. byte secret1[32];
  54874. byte secret2[32];
  54875. int i;
  54876. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54877. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  54878. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  54879. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54880. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  54881. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  54882. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  54883. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  54884. 0);
  54885. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  54886. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  54887. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  54888. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  54889. 0);
  54890. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  54891. 0);
  54892. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  54893. NULL), 0);
  54894. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  54895. sizeof(secret1));
  54896. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  54897. sizeof(secret2));
  54898. for (i = 0; i < (int)sizeof(secret1); i++) {
  54899. ExpectIntEQ(secret1[i], secret2[i]);
  54900. }
  54901. EC_KEY_free(key2);
  54902. EC_KEY_free(key1);
  54903. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  54904. * !WOLF_CRYPTO_CB_ONLY_ECC */
  54905. return EXPECT_RESULT();
  54906. }
  54907. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  54908. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  54909. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  54910. !defined(NO_ASN_TIME)
  54911. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  54912. int expectedDerSz)
  54913. {
  54914. EXPECT_DECLS;
  54915. X509* x509 = NULL;
  54916. BIGNUM* serial_number = NULL;
  54917. X509_NAME* name = NULL;
  54918. time_t epoch_off = 0;
  54919. ASN1_INTEGER* asn1_serial_number;
  54920. long not_before, not_after;
  54921. int derSz;
  54922. ExpectNotNull(x509 = X509_new());
  54923. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  54924. ExpectNotNull(serial_number = BN_new());
  54925. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  54926. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  54927. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  54928. /* version 3 */
  54929. ExpectIntNE(X509_set_version(x509, 2L), 0);
  54930. ExpectNotNull(name = X509_NAME_new());
  54931. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  54932. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  54933. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  54934. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  54935. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  54936. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  54937. not_before = (long)wc_Time(NULL);
  54938. not_after = not_before + (365 * 24 * 60 * 60);
  54939. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  54940. &epoch_off));
  54941. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  54942. &epoch_off));
  54943. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  54944. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  54945. ExpectIntGE(derSz, expectedDerSz);
  54946. BN_free(serial_number);
  54947. X509_NAME_free(name);
  54948. X509_free(x509);
  54949. return EXPECT_RESULT();
  54950. }
  54951. #endif
  54952. static int test_openssl_generate_key_and_cert(void)
  54953. {
  54954. EXPECT_DECLS;
  54955. #if defined(OPENSSL_EXTRA)
  54956. int expectedDerSz;
  54957. EVP_PKEY* pkey = NULL;
  54958. #ifdef HAVE_ECC
  54959. EC_KEY* ec_key = NULL;
  54960. #endif
  54961. #if !defined(NO_RSA)
  54962. int key_length = 2048;
  54963. BIGNUM* exponent = NULL;
  54964. RSA* rsa = NULL;
  54965. ExpectNotNull(pkey = EVP_PKEY_new());
  54966. ExpectNotNull(exponent = BN_new());
  54967. ExpectNotNull(rsa = RSA_new());
  54968. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  54969. #ifndef WOLFSSL_KEY_GEN
  54970. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54971. #if defined(USE_CERT_BUFFERS_1024)
  54972. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  54973. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54974. key_length = 1024;
  54975. #elif defined(USE_CERT_BUFFERS_2048)
  54976. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  54977. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54978. #else
  54979. RSA_free(rsa);
  54980. rsa = NULL;
  54981. #endif
  54982. #else
  54983. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  54984. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  54985. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  54986. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54987. #endif
  54988. if (rsa) {
  54989. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  54990. if (EXPECT_FAIL()) {
  54991. RSA_free(rsa);
  54992. }
  54993. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  54994. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  54995. expectedDerSz = 743;
  54996. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  54997. expectedDerSz), TEST_SUCCESS);
  54998. #endif
  54999. }
  55000. EVP_PKEY_free(pkey);
  55001. pkey = NULL;
  55002. BN_free(exponent);
  55003. #endif /* !NO_RSA */
  55004. #ifdef HAVE_ECC
  55005. ExpectNotNull(pkey = EVP_PKEY_new());
  55006. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55007. #ifndef NO_WOLFSSL_STUB
  55008. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  55009. #endif
  55010. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  55011. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  55012. if (EXPECT_FAIL()) {
  55013. EC_KEY_free(ec_key);
  55014. }
  55015. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55016. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55017. expectedDerSz = 344;
  55018. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  55019. TEST_SUCCESS);
  55020. #endif
  55021. EVP_PKEY_free(pkey);
  55022. #endif /* HAVE_ECC */
  55023. (void)pkey;
  55024. (void)expectedDerSz;
  55025. #endif /* OPENSSL_EXTRA */
  55026. return EXPECT_RESULT();
  55027. }
  55028. static int test_stubs_are_stubs(void)
  55029. {
  55030. EXPECT_DECLS;
  55031. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  55032. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55033. WOLFSSL_CTX* ctx = NULL;
  55034. WOLFSSL_CTX* ctxN = NULL;
  55035. #ifndef NO_WOLFSSL_CLIENT
  55036. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55037. #elif !defined(NO_WOLFSSL_SERVER)
  55038. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55039. #endif
  55040. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  55041. ExpectIntEQ((int) x(z), 0)
  55042. /* test logic, all stubs return same result regardless of ctx being NULL
  55043. * as there are no sanity checks, it's just a stub! If at some
  55044. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  55045. * if invalid inputs are supplied. Test calling both
  55046. * with and without valid inputs, if a stub functionality remains unchanged.
  55047. */
  55048. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  55049. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  55050. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  55051. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  55052. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  55053. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  55054. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  55055. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  55056. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  55057. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  55058. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  55059. wolfSSL_CTX_free(ctx);
  55060. ctx = NULL;
  55061. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  55062. * !NO_WOLFSSL_SERVER) */
  55063. return EXPECT_RESULT();
  55064. }
  55065. static int test_CONF_modules_xxx(void)
  55066. {
  55067. int res = TEST_SKIPPED;
  55068. #if defined(OPENSSL_EXTRA)
  55069. CONF_modules_free();
  55070. CONF_modules_unload(0);
  55071. CONF_modules_unload(1);
  55072. CONF_modules_unload(-1);
  55073. res = TEST_SUCCESS;
  55074. #endif /* OPENSSL_EXTRA */
  55075. return res;
  55076. }
  55077. static int test_CRYPTO_set_dynlock_xxx(void)
  55078. {
  55079. int res = TEST_SKIPPED;
  55080. #if defined(OPENSSL_EXTRA)
  55081. CRYPTO_set_dynlock_create_callback(
  55082. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  55083. CRYPTO_set_dynlock_create_callback(
  55084. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  55085. CRYPTO_set_dynlock_destroy_callback(
  55086. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  55087. CRYPTO_set_dynlock_destroy_callback(
  55088. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  55089. CRYPTO_set_dynlock_lock_callback(
  55090. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  55091. CRYPTO_set_dynlock_lock_callback(
  55092. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  55093. res = TEST_SUCCESS;
  55094. #endif /* OPENSSL_EXTRA */
  55095. return res;
  55096. }
  55097. static int test_CRYPTO_THREADID_xxx(void)
  55098. {
  55099. EXPECT_DECLS;
  55100. #if defined(OPENSSL_EXTRA)
  55101. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  55102. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  55103. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  55104. #endif /* OPENSSL_EXTRA */
  55105. return EXPECT_RESULT();
  55106. }
  55107. static int test_ENGINE_cleanup(void)
  55108. {
  55109. int res = TEST_SKIPPED;
  55110. #if defined(OPENSSL_EXTRA)
  55111. ENGINE_cleanup();
  55112. res = TEST_SUCCESS;
  55113. #endif /* OPENSSL_EXTRA */
  55114. return res;
  55115. }
  55116. static int test_wolfSSL_CTX_LoadCRL(void)
  55117. {
  55118. EXPECT_DECLS;
  55119. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  55120. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55121. WOLFSSL_CTX* ctx = NULL;
  55122. WOLFSSL* ssl = NULL;
  55123. const char* badPath = "dummypath";
  55124. const char* validPath = "./certs/crl";
  55125. const char* validFilePath = "./certs/crl/cliCrl.pem";
  55126. const char* issuerCert = "./certs/client-cert.pem";
  55127. int derType = WOLFSSL_FILETYPE_ASN1;
  55128. int pemType = WOLFSSL_FILETYPE_PEM;
  55129. #ifdef HAVE_CRL_MONITOR
  55130. int monitor = WOLFSSL_CRL_MONITOR;
  55131. #else
  55132. int monitor = 0;
  55133. #endif
  55134. WOLFSSL_CERT_MANAGER* cm = NULL;
  55135. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55136. BAD_FUNC_ARG)
  55137. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55138. NOT_COMPILED_IN)
  55139. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55140. WOLFSSL_SUCCESS)
  55141. #ifndef NO_WOLFSSL_CLIENT
  55142. #define NEW_CTX(ctx) ExpectNotNull( \
  55143. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  55144. #elif !defined(NO_WOLFSSL_SERVER)
  55145. #define NEW_CTX(ctx) ExpectNotNull( \
  55146. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  55147. #else
  55148. #define NEW_CTX(ctx) return
  55149. #endif
  55150. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55151. NEW_CTX(ctx);
  55152. #ifndef HAVE_CRL_MONITOR
  55153. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  55154. wolfSSL_CTX_free(ctx);
  55155. NEW_CTX(ctx);
  55156. #endif
  55157. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55158. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  55159. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  55160. wolfSSL_CTX_free(ctx);
  55161. ctx = NULL;
  55162. NEW_CTX(ctx);
  55163. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55164. WOLFSSL_SUCCESS);
  55165. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  55166. wolfSSL_CTX_free(ctx);
  55167. ctx = NULL;
  55168. NEW_CTX(ctx);
  55169. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55170. WOLFSSL_SUCCESS);
  55171. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55172. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  55173. wolfSSL_free(ssl);
  55174. ssl = NULL;
  55175. wolfSSL_CTX_free(ctx);
  55176. ctx = NULL;
  55177. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55178. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  55179. WOLFSSL_SUCCESS);
  55180. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  55181. WOLFSSL_SUCCESS);
  55182. wolfSSL_CertManagerFree(cm);
  55183. #endif
  55184. return EXPECT_RESULT();
  55185. }
  55186. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55187. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  55188. {
  55189. EXPECT_DECLS;
  55190. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  55191. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  55192. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55193. return EXPECT_RESULT();
  55194. }
  55195. #endif
  55196. static int test_multiple_crls_same_issuer(void)
  55197. {
  55198. EXPECT_DECLS;
  55199. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55200. test_ssl_cbf client_cbs, server_cbs;
  55201. struct {
  55202. const char* server_cert;
  55203. const char* server_key;
  55204. } test_params[] = {
  55205. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  55206. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  55207. };
  55208. size_t i;
  55209. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  55210. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55211. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55212. server_cbs.certPemFile = test_params[i].server_cert;
  55213. server_cbs.keyPemFile = test_params[i].server_key;
  55214. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  55215. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  55216. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  55217. &server_cbs, NULL), TEST_FAIL);
  55218. }
  55219. #endif
  55220. return EXPECT_RESULT();
  55221. }
  55222. static int test_SetTmpEC_DHE_Sz(void)
  55223. {
  55224. EXPECT_DECLS;
  55225. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  55226. WOLFSSL_CTX *ctx = NULL;
  55227. WOLFSSL *ssl = NULL;
  55228. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55229. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  55230. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55231. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  55232. wolfSSL_free(ssl);
  55233. wolfSSL_CTX_free(ctx);
  55234. #endif
  55235. return EXPECT_RESULT();
  55236. }
  55237. static int test_wolfSSL_CTX_get0_privatekey(void)
  55238. {
  55239. EXPECT_DECLS;
  55240. #ifdef OPENSSL_ALL
  55241. WOLFSSL_CTX* ctx = NULL;
  55242. (void)ctx;
  55243. #ifndef NO_RSA
  55244. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55245. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55246. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55247. WOLFSSL_FILETYPE_PEM));
  55248. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55249. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55250. WOLFSSL_FILETYPE_PEM));
  55251. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55252. wolfSSL_CTX_free(ctx);
  55253. ctx = NULL;
  55254. #endif
  55255. #ifdef HAVE_ECC
  55256. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55257. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55258. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  55259. WOLFSSL_FILETYPE_PEM));
  55260. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55261. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  55262. WOLFSSL_FILETYPE_PEM));
  55263. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55264. wolfSSL_CTX_free(ctx);
  55265. #endif
  55266. #endif
  55267. return EXPECT_RESULT();
  55268. }
  55269. static int test_wolfSSL_dtls_set_mtu(void)
  55270. {
  55271. EXPECT_DECLS;
  55272. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  55273. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  55274. !defined(WOLFSSL_NO_TLS12)
  55275. WOLFSSL_CTX* ctx = NULL;
  55276. WOLFSSL* ssl = NULL;
  55277. const char* testCertFile;
  55278. const char* testKeyFile;
  55279. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55280. #ifndef NO_RSA
  55281. testCertFile = svrCertFile;
  55282. testKeyFile = svrKeyFile;
  55283. #elif defined(HAVE_ECC)
  55284. testCertFile = eccCertFile;
  55285. testKeyFile = eccKeyFile;
  55286. #endif
  55287. if (testCertFile != NULL && testKeyFile != NULL) {
  55288. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  55289. WOLFSSL_FILETYPE_PEM));
  55290. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55291. WOLFSSL_FILETYPE_PEM));
  55292. }
  55293. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55294. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55295. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55296. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  55297. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  55298. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  55299. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  55300. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  55301. wolfSSL_free(ssl);
  55302. wolfSSL_CTX_free(ctx);
  55303. #endif
  55304. return EXPECT_RESULT();
  55305. }
  55306. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55307. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55308. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  55309. enum HandShakeType hsType, word16 length)
  55310. {
  55311. size_t idx = 0;
  55312. byte* l;
  55313. /* record layer */
  55314. /* handshake type */
  55315. out[idx++] = handshake;
  55316. /* protocol version */
  55317. out[idx++] = 0xfe;
  55318. out[idx++] = 0xfd; /* DTLS 1.2 */
  55319. /* epoch 0 */
  55320. XMEMSET(out + idx, 0, 2);
  55321. idx += 2;
  55322. /* sequence number */
  55323. XMEMSET(out + idx, 0, 6);
  55324. c32toa(seq, out + idx + 2);
  55325. idx += 6;
  55326. /* length in BE */
  55327. if (length)
  55328. c16toa(length, out + idx);
  55329. else
  55330. c16toa(outSz - idx - 2, out + idx);
  55331. idx += 2;
  55332. /* handshake layer */
  55333. /* handshake type */
  55334. out[idx++] = (byte)hsType;
  55335. /* length */
  55336. l = out + idx;
  55337. idx += 3;
  55338. /* message seq */
  55339. c16toa(0, out + idx);
  55340. idx += 2;
  55341. /* frag offset */
  55342. c32to24(0, out + idx);
  55343. idx += 3;
  55344. /* frag length */
  55345. c32to24((word32)outSz - (word32)idx - 3, l);
  55346. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  55347. idx += 3;
  55348. XMEMSET(out + idx, 0, outSz - idx);
  55349. }
  55350. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  55351. {
  55352. byte msg[] = "This is a msg for the client";
  55353. byte reply[40];
  55354. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55355. reply[sizeof(reply) - 1] = '\0';
  55356. fprintf(stderr, "Client message: %s\n", reply);
  55357. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55358. }
  55359. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  55360. {
  55361. byte ch[50];
  55362. int fd = wolfSSL_get_fd(ssl);
  55363. byte msg[] = "This is a msg for the server";
  55364. byte reply[40];
  55365. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  55366. /* Server should ignore this datagram */
  55367. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55368. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  55369. /* Server should ignore this datagram */
  55370. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55371. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55372. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55373. reply[sizeof(reply) - 1] = '\0';
  55374. fprintf(stderr, "Server response: %s\n", reply);
  55375. }
  55376. static int test_wolfSSL_dtls_plaintext(void)
  55377. {
  55378. callback_functions func_cb_client;
  55379. callback_functions func_cb_server;
  55380. size_t i;
  55381. struct test_params {
  55382. method_provider client_meth;
  55383. method_provider server_meth;
  55384. ssl_callback on_result_server;
  55385. ssl_callback on_result_client;
  55386. } params[] = {
  55387. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55388. test_wolfSSL_dtls_plaintext_server,
  55389. test_wolfSSL_dtls_plaintext_client},
  55390. };
  55391. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55392. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55393. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55394. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55395. func_cb_server.method = params[i].server_meth;
  55396. func_cb_client.method = params[i].client_meth;
  55397. func_cb_client.on_result = params[i].on_result_client;
  55398. func_cb_server.on_result = params[i].on_result_server;
  55399. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55400. if (!func_cb_client.return_code)
  55401. return TEST_FAIL;
  55402. if (!func_cb_server.return_code)
  55403. return TEST_FAIL;
  55404. }
  55405. return TEST_RES_CHECK(1);
  55406. }
  55407. #else
  55408. static int test_wolfSSL_dtls_plaintext(void) {
  55409. return TEST_SKIPPED;
  55410. }
  55411. #endif
  55412. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55413. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55414. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  55415. {
  55416. byte b[1100]; /* buffer for the messages to send */
  55417. size_t idx = 0;
  55418. size_t seq_offset = 0;
  55419. size_t msg_offset = 0;
  55420. int i;
  55421. int fd = wolfSSL_get_fd(ssl);
  55422. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55423. word32 seq_number = 100; /* start high so server definitely reads this */
  55424. word16 msg_number = 50; /* start high so server has to buffer this */
  55425. AssertIntEQ(ret, 1);
  55426. /* Now let's start spamming the peer with fragments it needs to store */
  55427. XMEMSET(b, -1, sizeof(b));
  55428. /* record layer */
  55429. /* handshake type */
  55430. b[idx++] = 22;
  55431. /* protocol version */
  55432. b[idx++] = 0xfe;
  55433. b[idx++] = 0xfd; /* DTLS 1.2 */
  55434. /* epoch 0 */
  55435. XMEMSET(b + idx, 0, 2);
  55436. idx += 2;
  55437. /* sequence number */
  55438. XMEMSET(b + idx, 0, 6);
  55439. seq_offset = idx + 2; /* increment only the low 32 bits */
  55440. idx += 6;
  55441. /* static length in BE */
  55442. c16toa(42, b + idx);
  55443. idx += 2;
  55444. /* handshake layer */
  55445. /* cert type */
  55446. b[idx++] = 11;
  55447. /* length */
  55448. c32to24(1000, b + idx);
  55449. idx += 3;
  55450. /* message seq */
  55451. c16toa(0, b + idx);
  55452. msg_offset = idx;
  55453. idx += 2;
  55454. /* frag offset */
  55455. c32to24(500, b + idx);
  55456. idx += 3;
  55457. /* frag length */
  55458. c32to24(30, b + idx);
  55459. idx += 3;
  55460. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  55461. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  55462. seq_number++, msg_number++, i++) {
  55463. struct timespec delay;
  55464. XMEMSET(&delay, 0, sizeof(delay));
  55465. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55466. c32toa(seq_number, b + seq_offset);
  55467. c16toa(msg_number, b + msg_offset);
  55468. ret = (int)send(fd, b, 55, 0);
  55469. nanosleep(&delay, NULL);
  55470. }
  55471. }
  55472. #ifdef WOLFSSL_DTLS13
  55473. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  55474. {
  55475. const word16 sendCountMax = 100;
  55476. byte b[150]; /* buffer for the messages to send */
  55477. size_t idx = 0;
  55478. size_t msg_offset = 0;
  55479. int fd = wolfSSL_get_fd(ssl);
  55480. word16 msg_number = 10; /* start high so server has to buffer this */
  55481. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55482. AssertIntEQ(ret, 1);
  55483. /* Now let's start spamming the peer with fragments it needs to store */
  55484. XMEMSET(b, -1, sizeof(b));
  55485. /* handshake type */
  55486. b[idx++] = 11;
  55487. /* length */
  55488. c32to24(10000, b + idx);
  55489. idx += 3;
  55490. /* message_seq */
  55491. msg_offset = idx;
  55492. idx += 2;
  55493. /* fragment_offset */
  55494. c32to24(5000, b + idx);
  55495. idx += 3;
  55496. /* fragment_length */
  55497. c32to24(100, b + idx);
  55498. idx += 3;
  55499. /* fragment contents */
  55500. idx += 100;
  55501. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  55502. byte sendBuf[150];
  55503. int sendSz = sizeof(sendBuf);
  55504. struct timespec delay;
  55505. XMEMSET(&delay, 0, sizeof(delay));
  55506. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55507. c16toa(msg_number, b + msg_offset);
  55508. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  55509. (int)idx, handshake, 0, 0, 0);
  55510. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  55511. nanosleep(&delay, NULL);
  55512. }
  55513. }
  55514. #endif
  55515. static int test_wolfSSL_dtls_fragments(void)
  55516. {
  55517. EXPECT_DECLS;
  55518. callback_functions func_cb_client;
  55519. callback_functions func_cb_server;
  55520. size_t i;
  55521. struct test_params {
  55522. method_provider client_meth;
  55523. method_provider server_meth;
  55524. ssl_callback spammer;
  55525. } params[] = {
  55526. #if !defined(WOLFSSL_NO_TLS12)
  55527. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55528. test_wolfSSL_dtls12_fragments_spammer},
  55529. #endif
  55530. #ifdef WOLFSSL_DTLS13
  55531. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55532. test_wolfSSL_dtls13_fragments_spammer},
  55533. #endif
  55534. };
  55535. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55536. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55537. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55538. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55539. func_cb_server.method = params[i].server_meth;
  55540. func_cb_client.method = params[i].client_meth;
  55541. func_cb_client.ssl_ready = params[i].spammer;
  55542. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55543. ExpectFalse(func_cb_client.return_code);
  55544. ExpectFalse(func_cb_server.return_code);
  55545. /* The socket should be closed by the server resulting in a
  55546. * socket error, fatal error or reading a close notify alert */
  55547. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  55548. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  55549. func_cb_client.last_err != FATAL_ERROR) {
  55550. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  55551. }
  55552. /* Check the server returned an error indicating the msg buffer
  55553. * was full */
  55554. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  55555. if (EXPECT_FAIL())
  55556. break;
  55557. }
  55558. return EXPECT_RESULT();
  55559. }
  55560. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  55561. {
  55562. int fd, ret;
  55563. byte alert_msg[] = {
  55564. 0x15, /* alert type */
  55565. 0xfe, 0xfd, /* version */
  55566. 0x00, 0x00, /* epoch */
  55567. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  55568. 0x00, 0x02, /* length */
  55569. 0x02, /* level: fatal */
  55570. 0x46 /* protocol version */
  55571. };
  55572. fd = wolfSSL_get_fd(ssl);
  55573. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  55574. AssertIntGT(ret, 0);
  55575. }
  55576. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  55577. {
  55578. callback_functions client_cbs, server_cbs;
  55579. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55580. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55581. client_cbs.doUdp = server_cbs.doUdp = 1;
  55582. if (version12) {
  55583. #if !defined(WOLFSSL_NO_TLS12)
  55584. client_cbs.method = wolfDTLSv1_2_client_method;
  55585. server_cbs.method = wolfDTLSv1_2_server_method;
  55586. #else
  55587. return TEST_SKIPPED;
  55588. #endif
  55589. }
  55590. else
  55591. {
  55592. #ifdef WOLFSSL_DTLS13
  55593. client_cbs.method = wolfDTLSv1_3_client_method;
  55594. server_cbs.method = wolfDTLSv1_3_server_method;
  55595. #else
  55596. return TEST_SKIPPED;
  55597. #endif /* WOLFSSL_DTLS13 */
  55598. }
  55599. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  55600. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55601. if (!client_cbs.return_code)
  55602. return TEST_FAIL;
  55603. if (!server_cbs.return_code)
  55604. return TEST_FAIL;
  55605. return TEST_SUCCESS;
  55606. }
  55607. static int test_wolfSSL_ignore_alert_before_cookie(void)
  55608. {
  55609. int ret;
  55610. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  55611. if (ret != 0)
  55612. return ret;
  55613. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  55614. if (ret != 0)
  55615. return ret;
  55616. return 0;
  55617. }
  55618. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  55619. {
  55620. int ret;
  55621. int fd;
  55622. byte bad_msg[] = {
  55623. 0x17, /* app data */
  55624. 0xaa, 0xfd, /* bad version */
  55625. 0x00, 0x01, /* epoch 1 */
  55626. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  55627. 0x00, 0x26, /* length: 38 bytes */
  55628. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  55629. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  55630. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  55631. 0x03, 0x18, 0x72
  55632. };
  55633. fd = wolfSSL_get_fd(ssl);
  55634. AssertIntGE(fd, 0);
  55635. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  55636. AssertIntEQ(ret, sizeof(bad_msg));
  55637. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  55638. AssertIntEQ(ret, sizeof("badrecordtest"));
  55639. }
  55640. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  55641. {
  55642. byte buf[100];
  55643. int ret;
  55644. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  55645. AssertIntGT(ret, 0);
  55646. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  55647. }
  55648. static int _test_wolfSSL_dtls_bad_record(
  55649. method_provider client_method, method_provider server_method)
  55650. {
  55651. callback_functions client_cbs, server_cbs;
  55652. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55653. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55654. client_cbs.doUdp = server_cbs.doUdp = 1;
  55655. client_cbs.method = client_method;
  55656. server_cbs.method = server_method;
  55657. client_cbs.on_result = test_wolfSSL_send_bad_record;
  55658. server_cbs.on_result = test_wolfSSL_read_string;
  55659. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55660. if (!client_cbs.return_code)
  55661. return TEST_FAIL;
  55662. if (!server_cbs.return_code)
  55663. return TEST_FAIL;
  55664. return TEST_SUCCESS;
  55665. }
  55666. static int test_wolfSSL_dtls_bad_record(void)
  55667. {
  55668. int ret = TEST_SUCCESS;
  55669. #if !defined(WOLFSSL_NO_TLS12)
  55670. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  55671. wolfDTLSv1_2_server_method);
  55672. #endif
  55673. #ifdef WOLFSSL_DTLS13
  55674. if (ret == TEST_SUCCESS) {
  55675. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  55676. wolfDTLSv1_3_server_method);
  55677. }
  55678. #endif /* WOLFSSL_DTLS13 */
  55679. return ret;
  55680. }
  55681. #else
  55682. static int test_wolfSSL_dtls_fragments(void) {
  55683. return TEST_SKIPPED;
  55684. }
  55685. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  55686. return TEST_SKIPPED;
  55687. }
  55688. static int test_wolfSSL_dtls_bad_record(void) {
  55689. return TEST_SKIPPED;
  55690. }
  55691. #endif
  55692. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  55693. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55694. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55695. static byte test_AEAD_fail_decryption = 0;
  55696. static byte test_AEAD_seq_num = 0;
  55697. static byte test_AEAD_done = 0;
  55698. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  55699. {
  55700. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  55701. if (ret > 0) {
  55702. if (test_AEAD_fail_decryption) {
  55703. /* Modify the packet to trigger a decryption failure */
  55704. buf[ret/2] ^= 0xFF;
  55705. if (test_AEAD_fail_decryption == 1)
  55706. test_AEAD_fail_decryption = 0;
  55707. }
  55708. }
  55709. (void)ctx;
  55710. return ret;
  55711. }
  55712. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  55713. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  55714. {
  55715. if (sendLimit)
  55716. w64Zero(sendLimit);
  55717. switch (ssl->specs.bulk_cipher_algorithm) {
  55718. case wolfssl_aes_gcm:
  55719. if (sendLimit)
  55720. *sendLimit = AEAD_AES_LIMIT;
  55721. FALL_THROUGH;
  55722. case wolfssl_chacha:
  55723. if (hardLimit)
  55724. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  55725. if (keyUpdateLimit)
  55726. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  55727. break;
  55728. case wolfssl_aes_ccm:
  55729. if (sendLimit)
  55730. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  55731. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  55732. if (hardLimit)
  55733. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  55734. if (keyUpdateLimit)
  55735. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  55736. }
  55737. else {
  55738. if (hardLimit)
  55739. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  55740. if (keyUpdateLimit)
  55741. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  55742. }
  55743. break;
  55744. default:
  55745. fprintf(stderr, "Unrecognized bulk cipher");
  55746. AssertFalse(1);
  55747. break;
  55748. }
  55749. }
  55750. static void test_AEAD_limit_client(WOLFSSL* ssl)
  55751. {
  55752. int ret;
  55753. int i;
  55754. int didReKey = 0;
  55755. char msgBuf[20];
  55756. w64wrapper hardLimit;
  55757. w64wrapper keyUpdateLimit;
  55758. w64wrapper counter;
  55759. w64wrapper sendLimit;
  55760. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  55761. w64Zero(&counter);
  55762. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  55763. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  55764. for (i = 0; i < 10; i++) {
  55765. /* Test some failed decryptions */
  55766. test_AEAD_fail_decryption = 1;
  55767. w64Increment(&counter);
  55768. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55769. /* Should succeed since decryption failures are dropped */
  55770. AssertIntGT(ret, 0);
  55771. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  55772. }
  55773. test_AEAD_fail_decryption = 1;
  55774. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  55775. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55776. /* 100 read calls should be enough to complete the key update */
  55777. w64Zero(&counter);
  55778. for (i = 0; i < 100; i++) {
  55779. /* Key update should be sent and negotiated */
  55780. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55781. AssertIntGT(ret, 0);
  55782. /* Epoch after one key update is 4 */
  55783. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  55784. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  55785. didReKey = 1;
  55786. break;
  55787. }
  55788. }
  55789. AssertTrue(didReKey);
  55790. if (!w64IsZero(sendLimit)) {
  55791. /* Test the sending limit for AEAD ciphers */
  55792. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  55793. test_AEAD_seq_num = 1;
  55794. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55795. AssertIntGT(ret, 0);
  55796. didReKey = 0;
  55797. w64Zero(&counter);
  55798. /* 100 read calls should be enough to complete the key update */
  55799. for (i = 0; i < 100; i++) {
  55800. /* Key update should be sent and negotiated */
  55801. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55802. AssertIntGT(ret, 0);
  55803. /* Epoch after another key update is 5 */
  55804. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  55805. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  55806. didReKey = 1;
  55807. break;
  55808. }
  55809. }
  55810. AssertTrue(didReKey);
  55811. }
  55812. test_AEAD_fail_decryption = 2;
  55813. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  55814. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55815. /* Connection should fail with a DECRYPT_ERROR */
  55816. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55817. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  55818. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  55819. test_AEAD_done = 1;
  55820. }
  55821. int counter = 0;
  55822. static void test_AEAD_limit_server(WOLFSSL* ssl)
  55823. {
  55824. char msgBuf[] = "Sending data";
  55825. int ret = WOLFSSL_SUCCESS;
  55826. w64wrapper sendLimit;
  55827. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55828. struct timespec delay;
  55829. XMEMSET(&delay, 0, sizeof(delay));
  55830. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  55831. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  55832. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55833. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  55834. while (!test_AEAD_done && ret > 0) {
  55835. counter++;
  55836. if (test_AEAD_seq_num) {
  55837. /* We need to update the seq number so that we can understand the
  55838. * peer. Otherwise we will incorrectly interpret the seq number. */
  55839. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  55840. AssertNotNull(e);
  55841. e->nextPeerSeqNumber = sendLimit;
  55842. test_AEAD_seq_num = 0;
  55843. }
  55844. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55845. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55846. nanosleep(&delay, NULL);
  55847. }
  55848. }
  55849. static int test_wolfSSL_dtls_AEAD_limit(void)
  55850. {
  55851. callback_functions func_cb_client;
  55852. callback_functions func_cb_server;
  55853. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55854. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55855. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55856. func_cb_server.method = wolfDTLSv1_3_server_method;
  55857. func_cb_client.method = wolfDTLSv1_3_client_method;
  55858. func_cb_server.on_result = test_AEAD_limit_server;
  55859. func_cb_client.on_result = test_AEAD_limit_client;
  55860. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55861. if (!func_cb_client.return_code)
  55862. return TEST_FAIL;
  55863. if (!func_cb_server.return_code)
  55864. return TEST_FAIL;
  55865. return TEST_SUCCESS;
  55866. }
  55867. #else
  55868. static int test_wolfSSL_dtls_AEAD_limit(void)
  55869. {
  55870. return TEST_SKIPPED;
  55871. }
  55872. #endif
  55873. #if defined(WOLFSSL_DTLS) && \
  55874. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  55875. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  55876. {
  55877. int fd, ret;
  55878. byte ch_msg[] = {
  55879. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55880. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55881. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  55882. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  55883. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  55884. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55885. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55886. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55887. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55888. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55889. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55890. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55891. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55892. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  55893. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  55894. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  55895. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  55896. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  55897. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  55898. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  55899. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  55900. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  55901. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  55902. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  55903. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  55904. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  55905. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  55906. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  55907. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  55908. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  55909. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  55910. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  55911. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  55912. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  55913. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  55914. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  55915. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  55916. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  55917. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  55918. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  55919. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  55920. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  55921. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  55922. 0x31, 0x68, 0x4c
  55923. };
  55924. fd = wolfSSL_get_fd(ssl);
  55925. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  55926. AssertIntGT(ret, 0);
  55927. /* consume the HRR otherwise handshake will fail */
  55928. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  55929. AssertIntGT(ret, 0);
  55930. }
  55931. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55932. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  55933. {
  55934. int fd, ret;
  55935. byte ch_msh_invalid_cookie[] = {
  55936. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  55937. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  55938. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  55939. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  55940. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  55941. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55942. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55943. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55944. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55945. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55946. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55947. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55948. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55949. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  55950. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55951. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55952. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55953. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55954. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55955. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  55956. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  55957. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  55958. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  55959. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  55960. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  55961. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  55962. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  55963. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  55964. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  55965. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  55966. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  55967. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  55968. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  55969. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  55970. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  55971. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  55972. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  55973. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  55974. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  55975. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  55976. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  55977. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  55978. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  55979. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  55980. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  55981. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  55982. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  55983. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  55984. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  55985. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  55986. 0x23, 0x00, 0x00
  55987. };
  55988. byte alert_reply[50];
  55989. byte expected_alert_reply[] = {
  55990. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  55991. 0x02, 0x02, 0x2f
  55992. };
  55993. fd = wolfSSL_get_fd(ssl);
  55994. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  55995. AssertIntGT(ret, 0);
  55996. /* should reply with an illegal_parameter reply */
  55997. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  55998. AssertIntEQ(ret, sizeof(expected_alert_reply));
  55999. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  56000. }
  56001. #endif
  56002. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  56003. {
  56004. #ifndef NO_MD5
  56005. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  56006. #elif !defined(NO_SHA)
  56007. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  56008. #elif !defined(NO_SHA256)
  56009. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  56010. #else
  56011. #error "We need a digest to hash the WOLFSSL object"
  56012. #endif
  56013. byte hashBuf[WC_MAX_DIGEST_SIZE];
  56014. wc_HashAlg hash;
  56015. const TLSX* exts = ssl->extensions;
  56016. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  56017. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  56018. * InitHandshakeHashes */
  56019. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  56020. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  56021. /* Following fields are not important to compare */
  56022. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56023. sslCopy.buffers.inputBuffer.buffer = NULL;
  56024. sslCopy.buffers.inputBuffer.bufferSize = 0;
  56025. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  56026. sslCopy.buffers.inputBuffer.offset = 0;
  56027. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56028. sslCopy.buffers.outputBuffer.buffer = NULL;
  56029. sslCopy.buffers.outputBuffer.bufferSize = 0;
  56030. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  56031. sslCopy.buffers.outputBuffer.offset = 0;
  56032. sslCopy.error = 0;
  56033. sslCopy.curSize = 0;
  56034. sslCopy.curStartIdx = 0;
  56035. sslCopy.keys.curSeq_lo = 0;
  56036. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  56037. #ifdef WOLFSSL_DTLS13
  56038. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  56039. sslCopy.dtls13FastTimeout = 0;
  56040. #endif
  56041. sslCopy.keys.dtls_peer_handshake_number = 0;
  56042. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  56043. sslCopy.hsHashes = NULL;
  56044. #ifdef WOLFSSL_ASYNC_IO
  56045. #ifdef WOLFSSL_ASYNC_CRYPT
  56046. sslCopy.asyncDev = NULL;
  56047. #endif
  56048. sslCopy.async = NULL;
  56049. #endif
  56050. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  56051. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  56052. /* hash extension list */
  56053. while (exts != NULL) {
  56054. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  56055. exts = exts->next;
  56056. }
  56057. /* Hash suites */
  56058. if (sslCopy.suites != NULL) {
  56059. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  56060. sizeof(struct Suites)), 0);
  56061. }
  56062. /* Hash hsHashes */
  56063. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  56064. sizeof(*hsHashes)), 0);
  56065. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  56066. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  56067. return MakeWordFromHash(hashBuf);
  56068. }
  56069. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  56070. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  56071. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  56072. char *buf, int sz, void *ctx)
  56073. {
  56074. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  56075. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  56076. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  56077. }
  56078. else {
  56079. return WOLFSSL_CBIO_ERR_WANT_READ;
  56080. }
  56081. }
  56082. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  56083. {
  56084. /* Compare the ssl object before and after one ClientHello msg */
  56085. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56086. int res;
  56087. int err;
  56088. word32 initHash;
  56089. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  56090. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  56091. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56092. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  56093. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  56094. (void)initHash;
  56095. res = tcp_select(fd, 5);
  56096. /* We are expecting a msg. A timeout indicates failure. */
  56097. AssertIntEQ(res, TEST_RECV_READY);
  56098. res = wolfSSL_accept(ssl);
  56099. err = wolfSSL_get_error(ssl, res);
  56100. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  56101. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56102. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  56103. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  56104. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  56105. }
  56106. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56107. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  56108. {
  56109. int ret;
  56110. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  56111. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56112. test_wolfSSL_dtls_compare_stateless(ssl);
  56113. }
  56114. #endif
  56115. static int test_wolfSSL_dtls_stateless(void)
  56116. {
  56117. callback_functions client_cbs, server_cbs;
  56118. size_t i;
  56119. struct {
  56120. method_provider client_meth;
  56121. method_provider server_meth;
  56122. ssl_callback client_ssl_ready;
  56123. ssl_callback server_ssl_ready;
  56124. } test_params[] = {
  56125. #if !defined(WOLFSSL_NO_TLS12)
  56126. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56127. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  56128. #endif
  56129. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56130. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56131. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  56132. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56133. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  56134. #endif
  56135. };
  56136. if (0 == sizeof(test_params)){
  56137. return TEST_SKIPPED;
  56138. }
  56139. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  56140. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56141. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56142. client_cbs.doUdp = server_cbs.doUdp = 1;
  56143. client_cbs.method = test_params[i].client_meth;
  56144. server_cbs.method = test_params[i].server_meth;
  56145. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  56146. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  56147. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56148. if (!client_cbs.return_code)
  56149. return TEST_FAIL;
  56150. if (!server_cbs.return_code)
  56151. return TEST_FAIL;
  56152. }
  56153. return TEST_SUCCESS;
  56154. }
  56155. #else
  56156. static int test_wolfSSL_dtls_stateless(void)
  56157. {
  56158. return TEST_SKIPPED;
  56159. }
  56160. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  56161. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  56162. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  56163. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56164. !defined(WOLFSSL_NO_CLIENT_AUTH))
  56165. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56166. {
  56167. int ret;
  56168. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  56169. fprintf(stderr, "loading cert %s failed\n", certA);
  56170. fprintf(stderr, "Error: (%d): %s\n", ret,
  56171. wolfSSL_ERR_reason_error_string(ret));
  56172. return -1;
  56173. }
  56174. return 0;
  56175. }
  56176. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56177. {
  56178. int ret;
  56179. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  56180. != WOLFSSL_SUCCESS) {
  56181. fprintf(stderr, "could not verify the cert: %s\n", certA);
  56182. fprintf(stderr, "Error: (%d): %s\n", ret,
  56183. wolfSSL_ERR_reason_error_string(ret));
  56184. return -1;
  56185. }
  56186. else {
  56187. fprintf(stderr, "successfully verified: %s\n", certA);
  56188. }
  56189. return 0;
  56190. }
  56191. #define LOAD_ONE_CA(a, b, c, d) \
  56192. do { \
  56193. (a) = load_ca_into_cm(c, d); \
  56194. if ((a) != 0) \
  56195. return (b); \
  56196. else \
  56197. (b)--; \
  56198. } while(0)
  56199. #define VERIFY_ONE_CERT(a, b, c, d) \
  56200. do { \
  56201. (a) = verify_cert_with_cm(c, d);\
  56202. if ((a) != 0) \
  56203. return (b); \
  56204. else \
  56205. (b)--; \
  56206. } while(0)
  56207. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  56208. {
  56209. int ret;
  56210. int i = -1;
  56211. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  56212. char chainGArr[9][50] = {"certs/ca-cert.pem",
  56213. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  56214. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  56215. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  56216. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  56217. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  56218. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  56219. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  56220. "certs/test-pathlen/chainG-entity.pem"};
  56221. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  56222. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  56223. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  56224. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  56225. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  56226. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  56227. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  56228. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  56229. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  56230. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  56231. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  56232. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  56233. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  56234. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  56235. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  56236. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  56237. /* test validating the entity twice, should have no effect on pathLen since
  56238. * entity/leaf cert */
  56239. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  56240. return ret;
  56241. }
  56242. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  56243. {
  56244. int ret;
  56245. int i = -1;
  56246. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  56247. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  56248. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  56249. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  56250. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  56251. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56252. */
  56253. char chainHArr[6][50] = {"certs/ca-cert.pem",
  56254. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  56255. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  56256. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  56257. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  56258. "certs/test-pathlen/chainH-entity.pem"};
  56259. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  56260. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  56261. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  56262. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  56263. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  56264. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  56265. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  56266. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  56267. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  56268. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  56269. return ret;
  56270. }
  56271. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  56272. {
  56273. int ret;
  56274. int i = -1;
  56275. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  56276. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  56277. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  56278. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  56279. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56280. */
  56281. char chainIArr[5][50] = {"certs/ca-cert.pem",
  56282. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  56283. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  56284. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  56285. "certs/test-pathlen/chainI-entity.pem"};
  56286. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  56287. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  56288. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  56289. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  56290. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  56291. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  56292. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  56293. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  56294. return ret;
  56295. }
  56296. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  56297. {
  56298. int ret;
  56299. int i = -1;
  56300. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  56301. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  56302. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  56303. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  56304. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  56305. */
  56306. char chainJArr[6][50] = {"certs/ca-cert.pem",
  56307. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  56308. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  56309. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  56310. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  56311. "certs/test-pathlen/chainJ-entity.pem"};
  56312. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  56313. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  56314. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  56315. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  56316. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  56317. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  56318. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  56319. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  56320. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  56321. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  56322. return ret;
  56323. }
  56324. static int test_various_pathlen_chains(void)
  56325. {
  56326. EXPECT_DECLS;
  56327. WOLFSSL_CERT_MANAGER* cm = NULL;
  56328. /* Test chain G (large chain with varying pathLens) */
  56329. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56330. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56331. ExpectIntEQ(test_chainG(cm), -1);
  56332. #else
  56333. ExpectIntEQ(test_chainG(cm), 0);
  56334. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56335. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56336. wolfSSL_CertManagerFree(cm);
  56337. /* end test chain G */
  56338. /* Test chain H (5 chain with same pathLens) */
  56339. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56340. ExpectIntLT(test_chainH(cm), 0);
  56341. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56342. wolfSSL_CertManagerFree(cm);
  56343. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56344. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56345. wolfSSL_CertManagerFree(cm);
  56346. /* end test chain H */
  56347. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  56348. * followed by 2 ICA's, should pass) */
  56349. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56350. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56351. ExpectIntEQ(test_chainI(cm), -1);
  56352. #else
  56353. ExpectIntEQ(test_chainI(cm), 0);
  56354. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56355. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56356. wolfSSL_CertManagerFree(cm);
  56357. cm = NULL;
  56358. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56359. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56360. wolfSSL_CertManagerFree(cm);
  56361. cm = NULL;
  56362. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  56363. * this time followed by 3 ICA's, should fail */
  56364. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56365. ExpectIntLT(test_chainJ(cm), 0);
  56366. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56367. wolfSSL_CertManagerFree(cm);
  56368. cm = NULL;
  56369. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56370. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56371. wolfSSL_CertManagerFree(cm);
  56372. return EXPECT_RESULT();
  56373. }
  56374. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  56375. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56376. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  56377. {
  56378. EXPECT_DECLS;
  56379. byte ekm[100] = {0};
  56380. (void)ctx;
  56381. /* Success Cases */
  56382. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56383. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  56384. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56385. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  56386. /* Use some random context */
  56387. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56388. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  56389. /* Failure cases */
  56390. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56391. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  56392. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56393. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  56394. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56395. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  56396. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56397. "extended master secret", XSTR_SIZEOF("extended master secret"),
  56398. NULL, 0, 0), 0);
  56399. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56400. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  56401. return EXPECT_RESULT();
  56402. }
  56403. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  56404. {
  56405. wolfSSL_KeepArrays(ssl);
  56406. return TEST_SUCCESS;
  56407. }
  56408. static int test_export_keying_material(void)
  56409. {
  56410. EXPECT_DECLS;
  56411. test_ssl_cbf serverCb;
  56412. test_ssl_cbf clientCb;
  56413. XMEMSET(&serverCb, 0, sizeof(serverCb));
  56414. XMEMSET(&clientCb, 0, sizeof(clientCb));
  56415. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  56416. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  56417. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  56418. return EXPECT_RESULT();
  56419. }
  56420. #endif /* HAVE_KEYING_MATERIAL */
  56421. static int test_wolfSSL_THREADID_hash(void)
  56422. {
  56423. EXPECT_DECLS;
  56424. #if defined(OPENSSL_EXTRA)
  56425. CRYPTO_THREADID id;
  56426. CRYPTO_THREADID_current(NULL);
  56427. /* Hash result is unsigned long. */
  56428. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  56429. XMEMSET(&id, 0, sizeof(id));
  56430. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  56431. #endif /* OPENSSL_EXTRA */
  56432. return EXPECT_RESULT();
  56433. }
  56434. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  56435. {
  56436. EXPECT_DECLS;
  56437. #if defined(OPENSSL_EXTRA)
  56438. WOLFSSL_CTX* ctx = NULL;
  56439. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  56440. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  56441. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  56442. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  56443. #endif /* OPENSSL_EXTRA */
  56444. return EXPECT_RESULT();
  56445. }
  56446. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56447. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56448. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  56449. {
  56450. EXPECT_DECLS;
  56451. callback_functions* callbacks = NULL;
  56452. WOLFSSL_CTX* ctx = NULL;
  56453. WOLFSSL* ssl = NULL;
  56454. SOCKET_T sfd = 0;
  56455. SOCKET_T cfd = 0;
  56456. word16 port;
  56457. char msg[] = "I hear you fa shizzle!";
  56458. int len = (int) XSTRLEN(msg);
  56459. char input[1024];
  56460. int ret = 0;
  56461. int err = 0;
  56462. if (!args)
  56463. WOLFSSL_RETURN_FROM_THREAD(0);
  56464. ((func_args*)args)->return_code = TEST_FAIL;
  56465. callbacks = ((func_args*)args)->callbacks;
  56466. ctx = wolfSSL_CTX_new(callbacks->method());
  56467. #if defined(USE_WINDOWS_API)
  56468. port = ((func_args*)args)->signal->port;
  56469. #else
  56470. /* Let tcp_listen assign port */
  56471. port = 0;
  56472. #endif
  56473. #ifdef WOLFSSL_TIRTOS
  56474. fdOpenSession(Task_self());
  56475. #endif
  56476. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56477. caCertFile, 0));
  56478. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56479. svrCertFile, WOLFSSL_FILETYPE_PEM));
  56480. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56481. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  56482. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  56483. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  56484. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56485. #elif !defined(NO_DH)
  56486. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  56487. #endif
  56488. if (callbacks->ctx_ready)
  56489. callbacks->ctx_ready(ctx);
  56490. ssl = wolfSSL_new(ctx);
  56491. ExpectNotNull(ssl);
  56492. /* listen and accept */
  56493. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  56494. CloseSocket(sfd);
  56495. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  56496. if (callbacks->ssl_ready)
  56497. callbacks->ssl_ready(ssl);
  56498. if (EXPECT_SUCCESS()) {
  56499. do {
  56500. err = 0; /* Reset error */
  56501. ret = wolfSSL_accept(ssl);
  56502. if (ret != WOLFSSL_SUCCESS) {
  56503. err = wolfSSL_get_error(ssl, 0);
  56504. }
  56505. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56506. }
  56507. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56508. /* read and write data */
  56509. XMEMSET(input, 0, sizeof(input));
  56510. while (EXPECT_SUCCESS()) {
  56511. ret = wolfSSL_read(ssl, input, sizeof(input));
  56512. if (ret > 0) {
  56513. break;
  56514. }
  56515. else {
  56516. err = wolfSSL_get_error(ssl,ret);
  56517. if (err == WOLFSSL_ERROR_WANT_READ) {
  56518. continue;
  56519. }
  56520. break;
  56521. }
  56522. }
  56523. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  56524. do {
  56525. ret = wolfSSL_write(ssl, msg, len);
  56526. if (ret > 0) {
  56527. break;
  56528. }
  56529. } while (ret < 0);
  56530. }
  56531. /* bidirectional shutdown */
  56532. while (EXPECT_SUCCESS()) {
  56533. ret = wolfSSL_shutdown(ssl);
  56534. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  56535. if (ret == WOLFSSL_SUCCESS) {
  56536. break;
  56537. }
  56538. }
  56539. if (EXPECT_SUCCESS()) {
  56540. /* wait for the peer to disconnect the tcp connection */
  56541. do {
  56542. ret = wolfSSL_read(ssl, input, sizeof(input));
  56543. err = wolfSSL_get_error(ssl, ret);
  56544. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  56545. }
  56546. /* detect TCP disconnect */
  56547. ExpectIntLE(ret,WOLFSSL_FAILURE);
  56548. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  56549. ((func_args*)args)->return_code = EXPECT_RESULT();
  56550. wolfSSL_free(ssl);
  56551. wolfSSL_CTX_free(ctx);
  56552. CloseSocket(cfd);
  56553. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56554. wc_ecc_fp_free(); /* free per thread cache */
  56555. #endif
  56556. WOLFSSL_RETURN_FROM_THREAD(0);
  56557. }
  56558. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  56559. {
  56560. EXPECT_DECLS;
  56561. callback_functions* callbacks = NULL;
  56562. WOLFSSL_CTX* ctx = NULL;
  56563. WOLFSSL* ssl = NULL;
  56564. SOCKET_T sfd = 0;
  56565. char msg[] = "hello wolfssl server!";
  56566. int len = (int) XSTRLEN(msg);
  56567. char input[1024];
  56568. int idx;
  56569. int ret, err;
  56570. if (!args)
  56571. WOLFSSL_RETURN_FROM_THREAD(0);
  56572. ((func_args*)args)->return_code = TEST_FAIL;
  56573. callbacks = ((func_args*)args)->callbacks;
  56574. ctx = wolfSSL_CTX_new(callbacks->method());
  56575. #ifdef WOLFSSL_TIRTOS
  56576. fdOpenSession(Task_self());
  56577. #endif
  56578. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56579. caCertFile, 0));
  56580. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56581. cliCertFile, WOLFSSL_FILETYPE_PEM));
  56582. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56583. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  56584. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  56585. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  56586. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  56587. if (EXPECT_SUCCESS()) {
  56588. do {
  56589. err = 0; /* Reset error */
  56590. ret = wolfSSL_connect(ssl);
  56591. if (ret != WOLFSSL_SUCCESS) {
  56592. err = wolfSSL_get_error(ssl, 0);
  56593. }
  56594. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56595. }
  56596. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  56597. if (EXPECT_SUCCESS()) {
  56598. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  56599. input[idx] = 0;
  56600. }
  56601. }
  56602. if (EXPECT_SUCCESS()) {
  56603. ret = wolfSSL_shutdown(ssl);
  56604. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  56605. ret = wolfSSL_shutdown(ssl);
  56606. }
  56607. }
  56608. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56609. ((func_args*)args)->return_code = EXPECT_RESULT();
  56610. wolfSSL_free(ssl);
  56611. wolfSSL_CTX_free(ctx);
  56612. CloseSocket(sfd);
  56613. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56614. wc_ecc_fp_free(); /* free per thread cache */
  56615. #endif
  56616. WOLFSSL_RETURN_FROM_THREAD(0);
  56617. }
  56618. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  56619. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  56620. /* This test is to check wolfSSL_read behaves as same as
  56621. * openSSL when it is called after SSL_shutdown completes.
  56622. */
  56623. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  56624. {
  56625. EXPECT_DECLS;
  56626. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56627. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56628. tcp_ready ready;
  56629. func_args client_args;
  56630. func_args server_args;
  56631. THREAD_TYPE serverThread;
  56632. THREAD_TYPE clientThread;
  56633. callback_functions server_cbf;
  56634. callback_functions client_cbf;
  56635. #ifdef WOLFSSL_TIRTOS
  56636. fdOpenSession(Task_self());
  56637. #endif
  56638. StartTCP();
  56639. InitTcpReady(&ready);
  56640. #if defined(USE_WINDOWS_API)
  56641. /* use RNG to get random port if using windows */
  56642. ready.port = GetRandomPort();
  56643. #endif
  56644. XMEMSET(&client_args, 0, sizeof(func_args));
  56645. XMEMSET(&server_args, 0, sizeof(func_args));
  56646. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  56647. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  56648. server_cbf.method = wolfTLSv1_2_server_method;
  56649. client_cbf.method = wolfTLSv1_2_client_method;
  56650. server_args.callbacks = &server_cbf;
  56651. client_args.callbacks = &client_cbf;
  56652. server_args.signal = &ready;
  56653. client_args.signal = &ready;
  56654. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  56655. wait_tcp_ready(&server_args);
  56656. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  56657. join_thread(clientThread);
  56658. join_thread(serverThread);
  56659. ExpectTrue(client_args.return_code);
  56660. ExpectTrue(server_args.return_code);
  56661. FreeTcpReady(&ready);
  56662. #endif
  56663. return EXPECT_RESULT();
  56664. }
  56665. static int test_wolfSSL_CTX_get_min_proto_version(void)
  56666. {
  56667. EXPECT_DECLS;
  56668. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  56669. WOLFSSL_CTX *ctx = NULL;
  56670. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56671. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  56672. WOLFSSL_SUCCESS);
  56673. #ifdef WOLFSSL_ALLOW_SSLV3
  56674. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56675. #else
  56676. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56677. #endif
  56678. wolfSSL_CTX_free(ctx);
  56679. ctx = NULL;
  56680. #ifndef NO_OLD_TLS
  56681. #ifdef WOLFSSL_ALLOW_TLSV10
  56682. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  56683. #else
  56684. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56685. #endif
  56686. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  56687. WOLFSSL_SUCCESS);
  56688. #ifdef WOLFSSL_ALLOW_TLSV10
  56689. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56690. #else
  56691. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56692. #endif
  56693. wolfSSL_CTX_free(ctx);
  56694. ctx = NULL;
  56695. #endif
  56696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56697. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  56698. WOLFSSL_SUCCESS);
  56699. #ifndef NO_OLD_TLS
  56700. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56701. #else
  56702. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56703. #endif
  56704. wolfSSL_CTX_free(ctx);
  56705. ctx = NULL;
  56706. #ifndef WOLFSSL_NO_TLS12
  56707. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  56708. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  56709. WOLFSSL_SUCCESS);
  56710. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  56711. wolfSSL_CTX_free(ctx);
  56712. ctx = NULL;
  56713. #endif
  56714. #ifdef WOLFSSL_TLS13
  56715. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  56716. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  56717. WOLFSSL_SUCCESS);
  56718. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  56719. wolfSSL_CTX_free(ctx);
  56720. ctx = NULL;
  56721. #endif
  56722. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56723. return EXPECT_RESULT();
  56724. }
  56725. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56726. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56727. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56728. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56729. static int test_wolfSSL_set_SSL_CTX(void)
  56730. {
  56731. EXPECT_DECLS;
  56732. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  56733. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  56734. !defined(NO_RSA)
  56735. WOLFSSL_CTX *ctx1 = NULL;
  56736. WOLFSSL_CTX *ctx2 = NULL;
  56737. WOLFSSL *ssl = NULL;
  56738. const byte *session_id1 = (const byte *)"CTX1";
  56739. const byte *session_id2 = (const byte *)"CTX2";
  56740. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56741. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  56742. WOLFSSL_FILETYPE_PEM));
  56743. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  56744. WOLFSSL_FILETYPE_PEM));
  56745. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  56746. WOLFSSL_SUCCESS);
  56747. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  56748. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  56749. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  56750. WOLFSSL_SUCCESS);
  56751. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56752. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  56753. WOLFSSL_FILETYPE_PEM));
  56754. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  56755. WOLFSSL_FILETYPE_PEM));
  56756. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  56757. WOLFSSL_SUCCESS);
  56758. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  56759. WOLFSSL_SUCCESS);
  56760. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  56761. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  56762. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  56763. WOLFSSL_SUCCESS);
  56764. #ifdef HAVE_SESSION_TICKET
  56765. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  56766. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  56767. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  56768. #endif
  56769. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  56770. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56771. #ifdef WOLFSSL_INT_H
  56772. #ifdef WOLFSSL_SESSION_ID_CTX
  56773. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  56774. #endif
  56775. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  56776. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  56777. #endif
  56778. #ifdef HAVE_SESSION_TICKET
  56779. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56780. #endif
  56781. /* Set the ctx1 that has TLSv1.3 as max proto version */
  56782. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  56783. /* MUST not change proto versions of ssl */
  56784. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56785. #ifdef HAVE_SESSION_TICKET
  56786. /* MUST not change */
  56787. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56788. #endif
  56789. /* MUST change */
  56790. #ifdef WOLFSSL_INT_H
  56791. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  56792. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  56793. #ifdef WOLFSSL_SESSION_ID_CTX
  56794. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  56795. #endif
  56796. #endif
  56797. wolfSSL_free(ssl);
  56798. wolfSSL_CTX_free(ctx1);
  56799. wolfSSL_CTX_free(ctx2);
  56800. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56801. return EXPECT_RESULT();
  56802. }
  56803. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56804. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56805. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56806. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  56807. static int test_wolfSSL_security_level(void)
  56808. {
  56809. EXPECT_DECLS;
  56810. #if defined(OPENSSL_EXTRA)
  56811. SSL_CTX *ctx = NULL;
  56812. #ifdef WOLFSSL_TLS13
  56813. #ifdef NO_WOLFSSL_SERVER
  56814. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56815. #else
  56816. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56817. #endif
  56818. SSL_CTX_set_security_level(NULL, 1);
  56819. SSL_CTX_set_security_level(ctx, 1);
  56820. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  56821. /* Stub so nothing happens. */
  56822. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  56823. SSL_CTX_free(ctx);
  56824. #else
  56825. (void)ctx;
  56826. #endif
  56827. #endif
  56828. return EXPECT_RESULT();
  56829. }
  56830. static int test_wolfSSL_SSL_in_init(void)
  56831. {
  56832. EXPECT_DECLS;
  56833. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  56834. SSL_CTX* ctx = NULL;
  56835. SSL* ssl = NULL;
  56836. const char* testCertFile;
  56837. const char* testKeyFile;
  56838. #ifdef WOLFSSL_TLS13
  56839. #ifdef NO_WOLFSSL_SERVER
  56840. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56841. #else
  56842. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56843. #endif
  56844. #else
  56845. #ifdef NO_WOLFSSL_SERVER
  56846. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56847. #else
  56848. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56849. #endif
  56850. #endif
  56851. #ifndef NO_RSA
  56852. testCertFile = svrCertFile;
  56853. testKeyFile = svrKeyFile;
  56854. #elif defined(HAVE_ECC)
  56855. testCertFile = eccCertFile;
  56856. testKeyFile = eccKeyFile;
  56857. #else
  56858. testCertFile = NULL;
  56859. testKeyFile = NULL;
  56860. #endif
  56861. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56862. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56863. SSL_FILETYPE_PEM));
  56864. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56865. SSL_FILETYPE_PEM));
  56866. }
  56867. ExpectNotNull(ssl = SSL_new(ctx));
  56868. ExpectIntEQ(SSL_in_init(ssl), 1);
  56869. SSL_CTX_free(ctx);
  56870. SSL_free(ssl);
  56871. #endif
  56872. return EXPECT_RESULT();
  56873. }
  56874. static int test_wolfSSL_CTX_set_timeout(void)
  56875. {
  56876. EXPECT_DECLS;
  56877. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  56878. int timeout;
  56879. WOLFSSL_CTX* ctx = NULL;
  56880. (void)timeout;
  56881. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56882. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  56883. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  56884. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  56885. */
  56886. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56887. /* giving 0 as timeout value sets default timeout */
  56888. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  56889. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  56890. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  56891. #else
  56892. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56893. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  56894. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  56895. #endif
  56896. wolfSSL_CTX_free(ctx);
  56897. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  56898. return EXPECT_RESULT();
  56899. }
  56900. static int test_wolfSSL_OpenSSL_version(void)
  56901. {
  56902. EXPECT_DECLS;
  56903. #if defined(OPENSSL_EXTRA)
  56904. const char* ver;
  56905. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  56906. ExpectNotNull(ver = OpenSSL_version(0));
  56907. #else
  56908. ExpectNotNull(ver = OpenSSL_version());
  56909. #endif
  56910. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  56911. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  56912. #endif
  56913. return EXPECT_RESULT();
  56914. }
  56915. static int test_CONF_CTX_CMDLINE(void)
  56916. {
  56917. EXPECT_DECLS;
  56918. #if defined(OPENSSL_ALL)
  56919. SSL_CTX* ctx = NULL;
  56920. SSL_CONF_CTX* cctx = NULL;
  56921. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56922. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56923. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56924. /* set flags */
  56925. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  56926. WOLFSSL_CONF_FLAG_CMDLINE);
  56927. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56928. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56929. /* cmd invalid command */
  56930. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56931. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56932. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56933. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56934. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56935. /* cmd Certificate and Private Key*/
  56936. {
  56937. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56938. const char* ourCert = svrCertFile;
  56939. const char* ourKey = svrKeyFile;
  56940. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  56941. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  56942. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  56943. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  56944. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56945. #endif
  56946. }
  56947. /* cmd curves */
  56948. {
  56949. #if defined(HAVE_ECC)
  56950. const char* curve = "secp256r1";
  56951. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  56952. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  56953. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56954. #endif
  56955. }
  56956. /* cmd CipherString */
  56957. {
  56958. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  56959. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  56960. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  56961. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56962. }
  56963. /* cmd DH parameter */
  56964. {
  56965. #if !defined(NO_DH) && !defined(NO_BIO)
  56966. const char* ourdhcert = "./certs/dh2048.pem";
  56967. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  56968. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  56969. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56970. #endif
  56971. }
  56972. SSL_CTX_free(ctx);
  56973. SSL_CONF_CTX_free(cctx);
  56974. #endif /* OPENSSL_EXTRA */
  56975. return EXPECT_RESULT();
  56976. }
  56977. static int test_CONF_CTX_FILE(void)
  56978. {
  56979. EXPECT_DECLS;
  56980. #if defined(OPENSSL_ALL)
  56981. SSL_CTX* ctx = NULL;
  56982. SSL_CONF_CTX* cctx = NULL;
  56983. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56984. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56985. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56986. /* set flags */
  56987. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  56988. WOLFSSL_CONF_FLAG_FILE);
  56989. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56990. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56991. /* sanity check */
  56992. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56993. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56994. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56995. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56996. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56997. /* cmd Certificate and Private Key*/
  56998. {
  56999. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57000. const char* ourCert = svrCertFile;
  57001. const char* ourKey = svrKeyFile;
  57002. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  57003. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  57004. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  57005. WOLFSSL_SUCCESS);
  57006. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  57007. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57008. #endif
  57009. }
  57010. /* cmd curves */
  57011. {
  57012. #if defined(HAVE_ECC)
  57013. const char* curve = "secp256r1";
  57014. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  57015. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  57016. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57017. #endif
  57018. }
  57019. /* cmd CipherString */
  57020. {
  57021. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57022. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  57023. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  57024. WOLFSSL_SUCCESS);
  57025. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57026. }
  57027. /* cmd DH parameter */
  57028. {
  57029. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  57030. const char* ourdhcert = "./certs/dh3072.pem";
  57031. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  57032. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  57033. WOLFSSL_SUCCESS);
  57034. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57035. #endif
  57036. }
  57037. SSL_CTX_free(ctx);
  57038. SSL_CONF_CTX_free(cctx);
  57039. #endif /* OPENSSL_EXTRA */
  57040. return EXPECT_RESULT();
  57041. }
  57042. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  57043. {
  57044. EXPECT_DECLS;
  57045. #ifdef HAVE_EX_DATA
  57046. int idx1, idx2;
  57047. /* test for unsupported class index */
  57048. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  57049. 0,NULL, NULL, NULL, NULL ), -1);
  57050. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  57051. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  57052. 0,NULL, NULL, NULL, NULL ), -1);
  57053. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  57054. 0,NULL, NULL, NULL, NULL ), -1);
  57055. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  57056. 0,NULL, NULL, NULL, NULL ), -1);
  57057. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  57058. 0,NULL, NULL, NULL, NULL ), -1);
  57059. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  57060. 0,NULL, NULL, NULL, NULL ), -1);
  57061. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  57062. 0,NULL, NULL, NULL, NULL ), -1);
  57063. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  57064. 0,NULL, NULL, NULL, NULL ), -1);
  57065. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  57066. 0,NULL, NULL, NULL, NULL ), -1);
  57067. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  57068. 0,NULL, NULL, NULL, NULL ), -1);
  57069. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  57070. 0,NULL, NULL, NULL, NULL ), -1);
  57071. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  57072. 0,NULL, NULL, NULL, NULL ), -1);
  57073. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  57074. 0,NULL, NULL, NULL, NULL ), -1);
  57075. /* test for supported class index */
  57076. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57077. 0,NULL, NULL, NULL, NULL );
  57078. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57079. 0,NULL, NULL, NULL, NULL );
  57080. ExpectIntNE(idx1, -1);
  57081. ExpectIntNE(idx2, -1);
  57082. ExpectIntNE(idx1, idx2);
  57083. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57084. 0,NULL, NULL, NULL, NULL );
  57085. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57086. 0,NULL, NULL, NULL, NULL );
  57087. ExpectIntNE(idx1, -1);
  57088. ExpectIntNE(idx2, -1);
  57089. ExpectIntNE(idx1, idx2);
  57090. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57091. 0,NULL, NULL, NULL, NULL );
  57092. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57093. 0,NULL, NULL, NULL, NULL );
  57094. ExpectIntNE(idx1, -1);
  57095. ExpectIntNE(idx2, -1);
  57096. ExpectIntNE(idx1, idx2);
  57097. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57098. 0,NULL, NULL, NULL, NULL );
  57099. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57100. 0,NULL, NULL, NULL, NULL );
  57101. ExpectIntNE(idx1, -1);
  57102. ExpectIntNE(idx2, -1);
  57103. ExpectIntNE(idx1, idx2);
  57104. #endif /* HAVE_EX_DATA */
  57105. return EXPECT_RESULT();
  57106. }
  57107. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  57108. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57109. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57110. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57111. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  57112. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  57113. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  57114. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  57115. #define SESSION_NEW_IDX_PTR "Testing"
  57116. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  57117. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57118. {
  57119. AssertNotNull(p);
  57120. AssertNull(ptr);
  57121. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  57122. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57123. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  57124. }
  57125. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  57126. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  57127. void* arg)
  57128. {
  57129. EXPECT_DECLS;
  57130. ExpectNotNull(out);
  57131. ExpectNotNull(in);
  57132. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  57133. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  57134. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  57135. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  57136. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57137. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  57138. if (EXPECT_SUCCESS()) {
  57139. return SSL_SUCCESS;
  57140. }
  57141. else {
  57142. return SSL_FAILURE;
  57143. }
  57144. }
  57145. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  57146. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  57147. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57148. {
  57149. EXPECT_DECLS;
  57150. ExpectNotNull(p);
  57151. ExpectNull(ptr);
  57152. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  57153. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57154. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57155. if (EXPECT_SUCCESS()) {
  57156. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  57157. }
  57158. }
  57159. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57160. {
  57161. EXPECT_DECLS;
  57162. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  57163. (void*)SESSION_NEW_IDX_PTR,
  57164. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  57165. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  57166. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  57167. SSL_SESSION* s = SSL_SESSION_new();
  57168. SSL_SESSION* d = NULL;
  57169. ExpectNotNull(s);
  57170. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  57171. ExpectNotNull(d = SSL_SESSION_dup(s));
  57172. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  57173. SSL_SESSION_free(s);
  57174. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  57175. SSL_SESSION_free(d);
  57176. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  57177. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  57178. crypto_ex_cb_ctx_session = NULL;
  57179. return EXPECT_RESULT();
  57180. }
  57181. #else
  57182. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57183. {
  57184. return TEST_SKIPPED;
  57185. }
  57186. #endif
  57187. static int test_wolfSSL_set_psk_use_session_callback(void)
  57188. {
  57189. EXPECT_DECLS;
  57190. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  57191. SSL_CTX* ctx = NULL;
  57192. SSL* ssl = NULL;
  57193. const char* testCertFile;
  57194. const char* testKeyFile;
  57195. #ifdef WOLFSSL_TLS13
  57196. #ifdef NO_WOLFSSL_SERVER
  57197. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57198. #else
  57199. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57200. #endif
  57201. #else
  57202. #ifdef NO_WOLFSSL_SERVER
  57203. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57204. #else
  57205. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57206. #endif
  57207. #endif
  57208. #ifndef NO_RSA
  57209. testCertFile = svrCertFile;
  57210. testKeyFile = svrKeyFile;
  57211. #elif defined(HAVE_ECC)
  57212. testCertFile = eccCertFile;
  57213. testKeyFile = eccKeyFile;
  57214. #else
  57215. testCertFile = NULL;
  57216. testKeyFile = NULL;
  57217. #endif
  57218. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57219. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57220. SSL_FILETYPE_PEM));
  57221. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57222. SSL_FILETYPE_PEM));
  57223. }
  57224. ExpectNotNull(ssl = SSL_new(ctx));
  57225. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  57226. SSL_CTX_free(ctx);
  57227. SSL_free(ssl);
  57228. #endif
  57229. return EXPECT_RESULT();
  57230. }
  57231. static int test_wolfSSL_ERR_strings(void)
  57232. {
  57233. EXPECT_DECLS;
  57234. #if !defined(NO_ERROR_STRINGS)
  57235. const char* err1 = "unsupported cipher suite";
  57236. const char* err2 = "wolfSSL PEM routines";
  57237. const char* err = NULL;
  57238. (void)err;
  57239. (void)err1;
  57240. (void)err2;
  57241. #if defined(OPENSSL_EXTRA)
  57242. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  57243. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57244. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  57245. ExpectIntEQ((*err == '\0'), 1);
  57246. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  57247. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  57248. #else
  57249. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  57250. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57251. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  57252. ExpectIntEQ((*err == '\0'), 1);
  57253. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  57254. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  57255. ExpectIntEQ((*err == '\0'), 1);
  57256. #endif
  57257. #endif
  57258. return EXPECT_RESULT();
  57259. }
  57260. static int test_wolfSSL_EVP_shake128(void)
  57261. {
  57262. EXPECT_DECLS;
  57263. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57264. defined(WOLFSSL_SHAKE128)
  57265. const EVP_MD* md = NULL;
  57266. ExpectNotNull(md = EVP_shake128());
  57267. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  57268. #endif
  57269. return EXPECT_RESULT();
  57270. }
  57271. static int test_wolfSSL_EVP_shake256(void)
  57272. {
  57273. EXPECT_DECLS;
  57274. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57275. defined(WOLFSSL_SHAKE256)
  57276. const EVP_MD* md = NULL;
  57277. ExpectNotNull(md = EVP_shake256());
  57278. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  57279. #endif
  57280. return EXPECT_RESULT();
  57281. }
  57282. /*
  57283. * Testing EVP digest API with SM3
  57284. */
  57285. static int test_wolfSSL_EVP_sm3(void)
  57286. {
  57287. int res = TEST_SKIPPED;
  57288. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  57289. EXPECT_DECLS;
  57290. const EVP_MD* md = NULL;
  57291. EVP_MD_CTX* mdCtx = NULL;
  57292. byte data[WC_SM3_BLOCK_SIZE * 4];
  57293. byte hash[WC_SM3_DIGEST_SIZE];
  57294. byte calcHash[WC_SM3_DIGEST_SIZE];
  57295. byte expHash[WC_SM3_DIGEST_SIZE] = {
  57296. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  57297. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  57298. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  57299. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  57300. };
  57301. word32 chunk;
  57302. word32 i;
  57303. unsigned int sz;
  57304. int ret;
  57305. XMEMSET(data, 0, sizeof(data));
  57306. md = EVP_sm3();
  57307. ExpectTrue(md != NULL);
  57308. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  57309. mdCtx = EVP_MD_CTX_new();
  57310. ExpectTrue(mdCtx != NULL);
  57311. /* Invalid Parameters */
  57312. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  57313. /* Valid Parameters */
  57314. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  57315. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  57316. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  57317. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  57318. /* Valid Parameters */
  57319. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  57320. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57321. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57322. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  57323. WOLFSSL_SUCCESS);
  57324. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  57325. WOLFSSL_SUCCESS);
  57326. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  57327. WOLFSSL_SUCCESS);
  57328. /* Ensure too many bytes for lengths. */
  57329. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  57330. WOLFSSL_SUCCESS);
  57331. /* Invalid Parameters */
  57332. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  57333. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57334. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57335. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57336. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57337. /* Valid Parameters */
  57338. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57339. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  57340. /* Chunk tests. */
  57341. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  57342. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  57343. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  57344. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  57345. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  57346. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  57347. WOLFSSL_SUCCESS);
  57348. }
  57349. if (i < (word32)sizeof(data)) {
  57350. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  57351. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  57352. }
  57353. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57354. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  57355. }
  57356. /* Not testing when the low 32-bit length overflows. */
  57357. ret = EVP_MD_CTX_cleanup(mdCtx);
  57358. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57359. wolfSSL_EVP_MD_CTX_free(mdCtx);
  57360. res = EXPECT_RESULT();
  57361. #endif
  57362. return res;
  57363. } /* END test_EVP_sm3 */
  57364. static int test_EVP_blake2(void)
  57365. {
  57366. EXPECT_DECLS;
  57367. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  57368. const EVP_MD* md = NULL;
  57369. (void)md;
  57370. #if defined(HAVE_BLAKE2)
  57371. ExpectNotNull(md = EVP_blake2b512());
  57372. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  57373. #endif
  57374. #if defined(HAVE_BLAKE2S)
  57375. ExpectNotNull(md = EVP_blake2s256());
  57376. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  57377. #endif
  57378. #endif
  57379. return EXPECT_RESULT();
  57380. }
  57381. #if defined(OPENSSL_EXTRA)
  57382. static void list_md_fn(const EVP_MD* m, const char* from,
  57383. const char* to, void* arg)
  57384. {
  57385. const char* mn;
  57386. BIO *bio;
  57387. (void) from;
  57388. (void) to;
  57389. (void) arg;
  57390. (void) mn;
  57391. (void) bio;
  57392. if (!m) {
  57393. /* alias */
  57394. AssertNull(m);
  57395. AssertNotNull(to);
  57396. }
  57397. else {
  57398. AssertNotNull(m);
  57399. AssertNull(to);
  57400. }
  57401. AssertNotNull(from);
  57402. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57403. mn = EVP_get_digestbyname(from);
  57404. /* print to stderr */
  57405. AssertNotNull(arg);
  57406. bio = BIO_new(BIO_s_file());
  57407. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57408. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  57409. BIO_free(bio);
  57410. #endif
  57411. }
  57412. #endif
  57413. static int test_EVP_MD_do_all(void)
  57414. {
  57415. int res = TEST_SKIPPED;
  57416. #if defined(OPENSSL_EXTRA)
  57417. EVP_MD_do_all(NULL, stderr);
  57418. EVP_MD_do_all(list_md_fn, stderr);
  57419. res = TEST_SUCCESS;
  57420. #endif
  57421. return res;
  57422. }
  57423. #if defined(OPENSSL_EXTRA)
  57424. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  57425. {
  57426. (void)arg;
  57427. (void)nm;
  57428. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  57429. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57430. /* print to stderr */
  57431. AssertNotNull(arg);
  57432. BIO *bio = BIO_new(BIO_s_file());
  57433. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57434. BIO_printf(bio, "%s\n", nm);
  57435. BIO_free(bio);
  57436. #endif
  57437. }
  57438. #endif
  57439. static int test_OBJ_NAME_do_all(void)
  57440. {
  57441. int res = TEST_SKIPPED;
  57442. #if defined(OPENSSL_EXTRA)
  57443. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  57444. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  57445. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  57446. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  57447. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  57448. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  57449. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  57450. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  57451. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  57452. res = TEST_SUCCESS;
  57453. #endif
  57454. return res;
  57455. }
  57456. static int test_SSL_CIPHER_get_xxx(void)
  57457. {
  57458. EXPECT_DECLS;
  57459. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  57460. !defined(NO_FILESYSTEM)
  57461. const SSL_CIPHER* cipher = NULL;
  57462. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  57463. int i, numCiphers = 0;
  57464. SSL_CTX* ctx = NULL;
  57465. SSL* ssl = NULL;
  57466. const char* testCertFile;
  57467. const char* testKeyFile;
  57468. char buf[256] = {0};
  57469. const char* cipher_id = NULL;
  57470. int expect_nid1 = NID_undef;
  57471. int expect_nid2 = NID_undef;
  57472. int expect_nid3 = NID_undef;
  57473. int expect_nid4 = NID_undef;
  57474. int expect_nid5 = 0;
  57475. const char* cipher_id2 = NULL;
  57476. int expect_nid21 = NID_undef;
  57477. int expect_nid22 = NID_undef;
  57478. int expect_nid23 = NID_undef;
  57479. int expect_nid24 = NID_undef;
  57480. int expect_nid25 = 0;
  57481. (void)cipher;
  57482. (void)supportedCiphers;
  57483. (void)i;
  57484. (void)numCiphers;
  57485. (void)ctx;
  57486. (void)ssl;
  57487. (void)testCertFile;
  57488. (void)testKeyFile;
  57489. #if defined(WOLFSSL_TLS13)
  57490. cipher_id = "TLS13-AES128-GCM-SHA256";
  57491. expect_nid1 = NID_auth_rsa;
  57492. expect_nid2 = NID_aes_128_gcm;
  57493. expect_nid3 = NID_sha256;
  57494. expect_nid4 = NID_kx_any;
  57495. expect_nid5 = 1;
  57496. #if !defined(WOLFSSL_NO_TLS12)
  57497. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  57498. expect_nid21 = NID_auth_rsa;
  57499. expect_nid22 = NID_aes_256_gcm;
  57500. expect_nid23 = NID_sha384;
  57501. expect_nid24 = NID_kx_ecdhe;
  57502. expect_nid25 = 1;
  57503. #endif
  57504. #endif
  57505. #ifdef NO_WOLFSSL_SERVER
  57506. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57507. #else
  57508. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57509. #endif
  57510. if (cipher_id) {
  57511. #ifndef NO_RSA
  57512. testCertFile = svrCertFile;
  57513. testKeyFile = svrKeyFile;
  57514. #elif defined(HAVE_ECC)
  57515. testCertFile = eccCertFile;
  57516. testKeyFile = eccKeyFile;
  57517. #else
  57518. testCertFile = NULL;
  57519. testKeyFile = NULL;
  57520. #endif
  57521. if (testCertFile != NULL && testKeyFile != NULL) {
  57522. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57523. SSL_FILETYPE_PEM));
  57524. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57525. SSL_FILETYPE_PEM));
  57526. }
  57527. ExpectNotNull(ssl = SSL_new(ctx));
  57528. ExpectIntEQ(SSL_in_init(ssl), 1);
  57529. supportedCiphers = SSL_get_ciphers(ssl);
  57530. numCiphers = sk_num(supportedCiphers);
  57531. for (i = 0; i < numCiphers; ++i) {
  57532. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57533. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57534. }
  57535. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  57536. break;
  57537. }
  57538. }
  57539. /* test case for */
  57540. if (i != numCiphers) {
  57541. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  57542. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  57543. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  57544. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  57545. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  57546. }
  57547. if (cipher_id2) {
  57548. for (i = 0; i < numCiphers; ++i) {
  57549. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57550. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57551. }
  57552. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  57553. break;
  57554. }
  57555. }
  57556. /* test case for */
  57557. if (i != numCiphers) {
  57558. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  57559. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  57560. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  57561. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  57562. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  57563. }
  57564. }
  57565. }
  57566. SSL_CTX_free(ctx);
  57567. SSL_free(ssl);
  57568. #endif
  57569. return EXPECT_RESULT();
  57570. }
  57571. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57572. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  57573. int* keyFormat)
  57574. {
  57575. int ret;
  57576. byte* key_buf = NULL;
  57577. size_t key_sz = 0;
  57578. EncryptedInfo encInfo;
  57579. XMEMSET(&encInfo, 0, sizeof(encInfo));
  57580. ret = load_file(privKeyFile, &key_buf, &key_sz);
  57581. if (ret == 0) {
  57582. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  57583. NULL, &encInfo, keyFormat);
  57584. }
  57585. if (key_buf != NULL) {
  57586. free(key_buf); key_buf = NULL;
  57587. }
  57588. (void)encInfo; /* not used in this test */
  57589. #ifdef DEBUG_WOLFSSL
  57590. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  57591. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  57592. (*pDer)->length);
  57593. #endif
  57594. return ret;
  57595. }
  57596. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  57597. {
  57598. int ret = CRYPTOCB_UNAVAILABLE;
  57599. const char* privKeyFile = (const char*)ctx;
  57600. DerBuffer* pDer = NULL;
  57601. int keyFormat = 0;
  57602. if (info->algo_type == WC_ALGO_TYPE_PK) {
  57603. #ifdef DEBUG_WOLFSSL
  57604. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  57605. #endif
  57606. #ifndef NO_RSA
  57607. if (info->pk.type == WC_PK_TYPE_RSA) {
  57608. switch (info->pk.rsa.type) {
  57609. case RSA_PUBLIC_ENCRYPT:
  57610. case RSA_PUBLIC_DECRYPT:
  57611. /* perform software based RSA public op */
  57612. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  57613. break;
  57614. case RSA_PRIVATE_ENCRYPT:
  57615. case RSA_PRIVATE_DECRYPT:
  57616. {
  57617. RsaKey key;
  57618. /* perform software based RSA private op */
  57619. #ifdef DEBUG_WOLFSSL
  57620. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  57621. #endif
  57622. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  57623. &keyFormat);
  57624. if (ret != 0) {
  57625. return ret;
  57626. }
  57627. ret = wc_InitRsaKey(&key, HEAP_HINT);
  57628. if (ret == 0) {
  57629. word32 keyIdx = 0;
  57630. /* load RSA private key and perform private transform */
  57631. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  57632. &key, pDer->length);
  57633. if (ret == 0) {
  57634. ret = wc_RsaFunction(
  57635. info->pk.rsa.in, info->pk.rsa.inLen,
  57636. info->pk.rsa.out, info->pk.rsa.outLen,
  57637. info->pk.rsa.type, &key, info->pk.rsa.rng);
  57638. }
  57639. else {
  57640. /* if decode fails, then fall-back to software based crypto */
  57641. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  57642. "key decode failed %d, falling back to "
  57643. "software\n", ret);
  57644. ret = CRYPTOCB_UNAVAILABLE;
  57645. }
  57646. wc_FreeRsaKey(&key);
  57647. }
  57648. wc_FreeDer(&pDer); pDer = NULL;
  57649. break;
  57650. }
  57651. }
  57652. #ifdef DEBUG_WOLFSSL
  57653. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  57654. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  57655. #endif
  57656. }
  57657. #endif /* !NO_RSA */
  57658. #ifdef HAVE_ECC
  57659. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  57660. /* mark this key as ephemeral */
  57661. if (info->pk.eckg.key != NULL) {
  57662. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  57663. sizeof(info->pk.eckg.key->label));
  57664. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  57665. }
  57666. }
  57667. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  57668. ecc_key key;
  57669. /* perform software based ECC sign */
  57670. #ifdef DEBUG_WOLFSSL
  57671. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  57672. #endif
  57673. if (info->pk.eccsign.key != NULL &&
  57674. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  57675. /* this is an empheral key */
  57676. #ifdef DEBUG_WOLFSSL
  57677. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  57678. "ephemeral key\n");
  57679. #endif
  57680. return CRYPTOCB_UNAVAILABLE;
  57681. }
  57682. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57683. if (ret != 0) {
  57684. return ret;
  57685. }
  57686. ret = wc_ecc_init(&key);
  57687. if (ret == 0) {
  57688. word32 keyIdx = 0;
  57689. /* load ECC private key and perform private transform */
  57690. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  57691. &key, pDer->length);
  57692. if (ret == 0) {
  57693. ret = wc_ecc_sign_hash(
  57694. info->pk.eccsign.in, info->pk.eccsign.inlen,
  57695. info->pk.eccsign.out, info->pk.eccsign.outlen,
  57696. info->pk.eccsign.rng, &key);
  57697. }
  57698. else {
  57699. /* if decode fails, then fall-back to software based crypto */
  57700. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  57701. "decode failed %d, falling back to software\n", ret);
  57702. ret = CRYPTOCB_UNAVAILABLE;
  57703. }
  57704. wc_ecc_free(&key);
  57705. }
  57706. wc_FreeDer(&pDer); pDer = NULL;
  57707. #ifdef DEBUG_WOLFSSL
  57708. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  57709. ret, *info->pk.eccsign.outlen);
  57710. #endif
  57711. }
  57712. #endif /* HAVE_ECC */
  57713. #ifdef HAVE_ED25519
  57714. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  57715. ed25519_key key;
  57716. /* perform software based ED25519 sign */
  57717. #ifdef DEBUG_WOLFSSL
  57718. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  57719. #endif
  57720. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57721. if (ret != 0) {
  57722. return ret;
  57723. }
  57724. ret = wc_ed25519_init(&key);
  57725. if (ret == 0) {
  57726. word32 keyIdx = 0;
  57727. /* load ED25519 private key and perform private transform */
  57728. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  57729. &key, pDer->length);
  57730. if (ret == 0) {
  57731. /* calculate public key */
  57732. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  57733. if (ret == 0) {
  57734. key.pubKeySet = 1;
  57735. ret = wc_ed25519_sign_msg_ex(
  57736. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  57737. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  57738. &key, info->pk.ed25519sign.type,
  57739. info->pk.ed25519sign.context,
  57740. info->pk.ed25519sign.contextLen);
  57741. }
  57742. }
  57743. else {
  57744. /* if decode fails, then fall-back to software based crypto */
  57745. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  57746. "decode failed %d, falling back to software\n", ret);
  57747. ret = CRYPTOCB_UNAVAILABLE;
  57748. }
  57749. wc_ed25519_free(&key);
  57750. }
  57751. wc_FreeDer(&pDer); pDer = NULL;
  57752. #ifdef DEBUG_WOLFSSL
  57753. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  57754. ret, *info->pk.ed25519sign.outLen);
  57755. #endif
  57756. }
  57757. #endif /* HAVE_ED25519 */
  57758. }
  57759. (void)thisDevId;
  57760. (void)keyFormat;
  57761. return ret;
  57762. }
  57763. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57764. static int test_wc_CryptoCb_TLS(int tlsVer,
  57765. const char* cliCaPemFile, const char* cliCertPemFile,
  57766. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  57767. const char* svrCaPemFile, const char* svrCertPemFile,
  57768. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  57769. {
  57770. EXPECT_DECLS;
  57771. callback_functions client_cbf;
  57772. callback_functions server_cbf;
  57773. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57774. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57775. if (tlsVer == WOLFSSL_TLSV1_3) {
  57776. #ifdef WOLFSSL_TLS13
  57777. server_cbf.method = wolfTLSv1_3_server_method;
  57778. client_cbf.method = wolfTLSv1_3_client_method;
  57779. #endif
  57780. }
  57781. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57782. #ifndef WOLFSSL_NO_TLS12
  57783. server_cbf.method = wolfTLSv1_2_server_method;
  57784. client_cbf.method = wolfTLSv1_2_client_method;
  57785. #endif
  57786. }
  57787. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57788. #ifndef NO_OLD_TLS
  57789. server_cbf.method = wolfTLSv1_1_server_method;
  57790. client_cbf.method = wolfTLSv1_1_client_method;
  57791. #endif
  57792. }
  57793. else if (tlsVer == WOLFSSL_TLSV1) {
  57794. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57795. server_cbf.method = wolfTLSv1_server_method;
  57796. client_cbf.method = wolfTLSv1_client_method;
  57797. #endif
  57798. }
  57799. else if (tlsVer == WOLFSSL_SSLV3) {
  57800. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57801. defined(WOLFSSL_STATIC_RSA)
  57802. server_cbf.method = wolfSSLv3_server_method;
  57803. client_cbf.method = wolfSSLv3_client_method;
  57804. #endif
  57805. }
  57806. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57807. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57808. server_cbf.method = wolfDTLSv1_2_server_method;
  57809. client_cbf.method = wolfDTLSv1_2_client_method;
  57810. #endif
  57811. }
  57812. else if (tlsVer == WOLFSSL_DTLSV1) {
  57813. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57814. server_cbf.method = wolfDTLSv1_server_method;
  57815. client_cbf.method = wolfDTLSv1_client_method;
  57816. #endif
  57817. }
  57818. if (server_cbf.method == NULL) {
  57819. /* not enabled */
  57820. return TEST_SUCCESS;
  57821. }
  57822. /* Setup the keys for the TLS test */
  57823. client_cbf.certPemFile = cliCertPemFile;
  57824. client_cbf.keyPemFile = cliPubKeyPemFile;
  57825. client_cbf.caPemFile = cliCaPemFile;
  57826. server_cbf.certPemFile = svrCertPemFile;
  57827. server_cbf.keyPemFile = svrPubKeyPemFile;
  57828. server_cbf.caPemFile = svrCaPemFile;
  57829. /* Setup a crypto callback with pointer to private key file for testing */
  57830. client_cbf.devId = 1;
  57831. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  57832. (void*)cliPrivKeyPemFile);
  57833. server_cbf.devId = 2;
  57834. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  57835. (void*)svrPrivKeyPemFile);
  57836. /* Perform TLS server and client test */
  57837. /* First test is at WOLFSSL_CTX level */
  57838. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57839. /* Check for success */
  57840. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57841. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57842. if (EXPECT_SUCCESS()) {
  57843. /* Second test is a WOLFSSL object level */
  57844. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57845. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57846. }
  57847. /* Check for success */
  57848. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57849. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57850. /* Un register the devId's */
  57851. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  57852. client_cbf.devId = INVALID_DEVID;
  57853. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  57854. server_cbf.devId = INVALID_DEVID;
  57855. return EXPECT_RESULT();
  57856. }
  57857. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  57858. static int test_wc_CryptoCb(void)
  57859. {
  57860. EXPECT_DECLS;
  57861. #ifdef WOLF_CRYPTO_CB
  57862. /* TODO: Add crypto callback API tests */
  57863. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57864. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57865. int tlsVer;
  57866. #endif
  57867. #ifndef NO_RSA
  57868. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57869. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57870. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  57871. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  57872. TEST_SUCCESS);
  57873. }
  57874. #endif
  57875. #ifdef HAVE_ECC
  57876. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57877. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57878. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  57879. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  57880. TEST_SUCCESS);
  57881. }
  57882. #endif
  57883. #ifdef HAVE_ED25519
  57884. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57885. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57886. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57887. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  57888. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  57889. TEST_SUCCESS);
  57890. }
  57891. #endif
  57892. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57893. #endif /* WOLF_CRYPTO_CB */
  57894. return EXPECT_RESULT();
  57895. }
  57896. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57897. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57898. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  57899. const char* cliCaPemFile, const char* cliCertPemFile,
  57900. const char* cliPrivKeyPemFile,
  57901. const char* svrCaPemFile, const char* svrCertPemFile,
  57902. const char* svrPrivKeyPemFile,
  57903. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  57904. {
  57905. EXPECT_DECLS;
  57906. callback_functions client_cbf;
  57907. callback_functions server_cbf;
  57908. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57909. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57910. if (tlsVer == WOLFSSL_TLSV1_3) {
  57911. #ifdef WOLFSSL_TLS13
  57912. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  57913. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  57914. #endif
  57915. }
  57916. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57917. #ifndef WOLFSSL_NO_TLS12
  57918. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  57919. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  57920. #endif
  57921. }
  57922. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57923. #ifndef NO_OLD_TLS
  57924. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  57925. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  57926. #endif
  57927. }
  57928. else if (tlsVer == WOLFSSL_TLSV1) {
  57929. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57930. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  57931. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  57932. #endif
  57933. }
  57934. else if (tlsVer == WOLFSSL_SSLV3) {
  57935. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57936. defined(WOLFSSL_STATIC_RSA)
  57937. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  57938. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  57939. #endif
  57940. }
  57941. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57942. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57943. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  57944. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  57945. #endif
  57946. }
  57947. else if (tlsVer == WOLFSSL_DTLSV1) {
  57948. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57949. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  57950. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  57951. #endif
  57952. }
  57953. if (server_cbf.method_ex == NULL) {
  57954. /* not enabled */
  57955. return TEST_SUCCESS;
  57956. }
  57957. /* Setup the keys for the TLS test */
  57958. client_cbf.certPemFile = cliCertPemFile;
  57959. client_cbf.keyPemFile = cliPrivKeyPemFile;
  57960. client_cbf.caPemFile = cliCaPemFile;
  57961. server_cbf.certPemFile = svrCertPemFile;
  57962. server_cbf.keyPemFile = svrPrivKeyPemFile;
  57963. server_cbf.caPemFile = svrCaPemFile;
  57964. client_cbf.mem = cliMem;
  57965. client_cbf.memSz = cliMemSz;
  57966. server_cbf.mem = svrMem;
  57967. server_cbf.memSz = svrMemSz;
  57968. client_cbf.devId = INVALID_DEVID;
  57969. server_cbf.devId = INVALID_DEVID;
  57970. /* Perform TLS server and client test */
  57971. /* First test is at WOLFSSL_CTX level */
  57972. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57973. /* Check for success */
  57974. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57975. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57976. if (EXPECT_SUCCESS()) {
  57977. /* Second test is a WOLFSSL object level */
  57978. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57979. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57980. }
  57981. /* Check for success */
  57982. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57983. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57984. return EXPECT_RESULT();
  57985. }
  57986. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  57987. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  57988. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  57989. {
  57990. EXPECT_DECLS;
  57991. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  57992. WOLFSSL_MEM_STATS mem_stats;
  57993. WOLFSSL_MEM_CONN_STATS ssl_stats;
  57994. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  57995. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  57996. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57997. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  57998. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57999. #endif
  58000. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  58001. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  58002. /* this should fail because kMaxCtxClients == 2 */
  58003. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  58004. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  58005. #ifdef DEBUG_WOLFSSL
  58006. wolfSSL_PrintStatsConn(&ssl_stats);
  58007. #endif
  58008. (void)ssl_stats;
  58009. }
  58010. /* display collected statistics */
  58011. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  58012. #ifdef DEBUG_WOLFSSL
  58013. wolfSSL_PrintStats(&mem_stats);
  58014. #endif
  58015. (void)mem_stats;
  58016. }
  58017. wolfSSL_free(ssl1);
  58018. wolfSSL_free(ssl2);
  58019. return EXPECT_RESULT();
  58020. }
  58021. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58022. static int test_wolfSSL_CTX_StaticMemory(void)
  58023. {
  58024. EXPECT_DECLS;
  58025. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58026. wolfSSL_method_func method_func;
  58027. WOLFSSL_CTX* ctx;
  58028. const int kMaxCtxClients = 2;
  58029. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58030. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58031. int tlsVer;
  58032. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  58033. #endif
  58034. #endif
  58035. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  58036. #ifndef NO_WOLFSSL_SERVER
  58037. #ifndef WOLFSSL_NO_TLS12
  58038. method_func = wolfTLSv1_2_server_method_ex;
  58039. #else
  58040. method_func = wolfTLSv1_3_server_method_ex;
  58041. #endif
  58042. #else
  58043. #ifndef WOLFSSL_NO_TLS12
  58044. method_func = wolfTLSv1_2_client_method_ex;
  58045. #else
  58046. method_func = wolfTLSv1_3_client_method_ex;
  58047. #endif
  58048. #endif
  58049. /* Test creating CTX directly from static memory pool */
  58050. ctx = NULL;
  58051. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  58052. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58053. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58054. wolfSSL_CTX_free(ctx);
  58055. ctx = NULL;
  58056. /* Test for heap allocated CTX, then assigning static pool to it */
  58057. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  58058. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  58059. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58060. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58061. wolfSSL_CTX_free(ctx);
  58062. /* TLS Level Tests using static memory */
  58063. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58064. #ifndef NO_RSA
  58065. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58066. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58067. svrCertFile, cliCertFile, cliKeyFile,
  58068. cliCertFile, svrCertFile, svrKeyFile,
  58069. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58070. TEST_SUCCESS);
  58071. }
  58072. #endif
  58073. #ifdef HAVE_ECC
  58074. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58075. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58076. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  58077. cliEccCertFile, eccCertFile, eccKeyFile,
  58078. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58079. TEST_SUCCESS);
  58080. }
  58081. #endif
  58082. #ifdef HAVE_ED25519
  58083. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58084. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58085. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58086. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  58087. cliEdCertFile, edCertFile, edKeyFile,
  58088. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58089. TEST_SUCCESS);
  58090. }
  58091. #endif
  58092. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58093. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58094. return EXPECT_RESULT();
  58095. }
  58096. static int test_openssl_FIPS_drbg(void)
  58097. {
  58098. EXPECT_DECLS;
  58099. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  58100. DRBG_CTX* dctx = NULL;
  58101. byte data1[32], data2[32], zeroData[32];
  58102. byte testSeed[16];
  58103. size_t dlen = sizeof(data1);
  58104. int i;
  58105. XMEMSET(data1, 0, dlen);
  58106. XMEMSET(data2, 0, dlen);
  58107. XMEMSET(zeroData, 0, sizeof(zeroData));
  58108. for (i = 0; i < (int)sizeof(testSeed); i++) {
  58109. testSeed[i] = (byte)i;
  58110. }
  58111. ExpectNotNull(dctx = FIPS_get_default_drbg());
  58112. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  58113. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  58114. WOLFSSL_SUCCESS);
  58115. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  58116. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  58117. WOLFSSL_SUCCESS);
  58118. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58119. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  58120. WOLFSSL_SUCCESS);
  58121. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  58122. WOLFSSL_SUCCESS);
  58123. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58124. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  58125. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  58126. #ifndef HAVE_GLOBAL_RNG
  58127. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  58128. wolfSSL_FIPS_drbg_free(dctx);
  58129. #endif
  58130. #endif
  58131. return EXPECT_RESULT();
  58132. }
  58133. static int test_wolfSSL_FIPS_mode(void)
  58134. {
  58135. EXPECT_DECLS;
  58136. #if defined(OPENSSL_ALL)
  58137. #ifdef HAVE_FIPS
  58138. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  58139. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  58140. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  58141. #else
  58142. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  58143. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  58144. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  58145. #endif
  58146. #endif
  58147. return EXPECT_RESULT();
  58148. }
  58149. #ifdef WOLFSSL_DTLS
  58150. /* Prints out the current window */
  58151. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  58152. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58153. int i;
  58154. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  58155. word32 b = w[i];
  58156. int j;
  58157. /* Prints out a 32 bit binary number in big endian order */
  58158. for (j = 0; j < 32; j++, b <<= 1) {
  58159. if (b & (((word32)1) << 31))
  58160. fprintf(stderr, "1");
  58161. else
  58162. fprintf(stderr, "0");
  58163. }
  58164. fprintf(stderr, " ");
  58165. }
  58166. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  58167. #else
  58168. (void)h;
  58169. (void)l;
  58170. (void)w;
  58171. #endif
  58172. }
  58173. /* a - cur_hi
  58174. * b - cur_lo
  58175. * c - next_hi
  58176. * d - next_lo
  58177. * e - window
  58178. * f - expected next_hi
  58179. * g - expected next_lo
  58180. * h - expected window[1]
  58181. * i - expected window[0]
  58182. */
  58183. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  58184. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  58185. DUW_TEST_print_window_binary((a), (b), (e)); \
  58186. ExpectIntEQ((c), (f)); \
  58187. ExpectIntEQ((d), (g)); \
  58188. ExpectIntEQ((e)[1], (h)); \
  58189. ExpectIntEQ((e)[0], (i)); \
  58190. } while (0)
  58191. static int test_wolfSSL_DtlsUpdateWindow(void)
  58192. {
  58193. EXPECT_DECLS;
  58194. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  58195. word32 next_lo = 0;
  58196. word16 next_hi = 0;
  58197. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58198. fprintf(stderr, "\n");
  58199. #endif
  58200. XMEMSET(window, 0, sizeof window);
  58201. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  58202. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  58203. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  58204. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  58205. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  58206. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  58207. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  58208. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  58209. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  58210. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  58211. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  58212. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  58213. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  58214. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  58215. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  58216. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  58217. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  58218. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  58219. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  58220. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  58221. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  58222. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58223. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58224. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  58225. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  58226. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58227. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58228. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58229. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58230. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58231. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  58232. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  58233. return EXPECT_RESULT();
  58234. }
  58235. #endif /* WOLFSSL_DTLS */
  58236. #ifdef WOLFSSL_DTLS
  58237. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  58238. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  58239. {
  58240. DtlsMsg* cur;
  58241. static byte msg[100];
  58242. static byte msgInit = 0;
  58243. if (!msgInit) {
  58244. int i;
  58245. for (i = 0; i < 100; i++)
  58246. msg[i] = i + 1;
  58247. msgInit = 1;
  58248. }
  58249. /* Sanitize test parameters */
  58250. if (len > sizeof(msg))
  58251. return -1;
  58252. if (f_offset + f_len > sizeof(msg))
  58253. return -1;
  58254. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  58255. if (ssl->dtls_rx_msg_list == NULL)
  58256. return -100;
  58257. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  58258. return -200;
  58259. if (cur->fragBucketListCount != f_count)
  58260. return -300;
  58261. if (cur->ready != ready)
  58262. return -400;
  58263. if (cur->bytesReceived != bytesReceived)
  58264. return -500;
  58265. if (ready) {
  58266. if (cur->fragBucketList != NULL)
  58267. return -600;
  58268. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  58269. return -700;
  58270. }
  58271. else {
  58272. DtlsFragBucket* fb;
  58273. if (cur->fragBucketList == NULL)
  58274. return -800;
  58275. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  58276. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  58277. return -900;
  58278. }
  58279. }
  58280. return 0;
  58281. }
  58282. static int test_wolfSSL_DTLS_fragment_buckets(void)
  58283. {
  58284. EXPECT_DECLS;
  58285. WOLFSSL ssl[1];
  58286. XMEMSET(ssl, 0, sizeof(*ssl));
  58287. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  58288. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58289. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  58290. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  58291. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  58292. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  58293. /* Test all permutations of 3 regions */
  58294. /* 1 2 3 */
  58295. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58296. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  58297. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58298. /* 1 3 2 */
  58299. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58300. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  58301. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58302. /* 2 1 3 */
  58303. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58304. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  58305. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58306. /* 2 3 1 */
  58307. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58308. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  58309. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58310. /* 3 1 2 */
  58311. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58312. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  58313. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58314. /* 3 2 1 */
  58315. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58316. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  58317. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58318. /* Test overlapping regions */
  58319. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58320. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  58321. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  58322. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  58323. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  58324. /* Test overlapping multiple regions */
  58325. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58326. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  58327. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  58328. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  58329. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  58330. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  58331. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  58332. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  58333. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  58334. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  58335. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58336. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  58337. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  58338. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  58339. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  58340. ssl->dtls_rx_msg_list = NULL;
  58341. ssl->dtls_rx_msg_list_sz = 0;
  58342. return EXPECT_RESULT();
  58343. }
  58344. #endif
  58345. #if !defined(NO_FILESYSTEM) && \
  58346. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58347. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58348. static int test_wolfSSL_dtls_stateless2(void)
  58349. {
  58350. EXPECT_DECLS;
  58351. WOLFSSL *ssl_c = NULL;
  58352. WOLFSSL *ssl_c2 = NULL;
  58353. WOLFSSL *ssl_s = NULL;
  58354. struct test_memio_ctx test_ctx;
  58355. WOLFSSL_CTX *ctx_c = NULL;
  58356. WOLFSSL_CTX *ctx_s = NULL;
  58357. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58358. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58359. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58360. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58361. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58362. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58363. /* send CH */
  58364. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58365. (ssl_c2->error == WANT_READ));
  58366. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58367. (ssl_s->error == WANT_READ));
  58368. ExpectIntNE(test_ctx.c_len, 0);
  58369. /* consume HRR */
  58370. test_ctx.c_len = 0;
  58371. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58372. wolfSSL_free(ssl_c2);
  58373. wolfSSL_free(ssl_c);
  58374. wolfSSL_free(ssl_s);
  58375. wolfSSL_CTX_free(ctx_c);
  58376. wolfSSL_CTX_free(ctx_s);
  58377. return EXPECT_RESULT();
  58378. }
  58379. #ifdef HAVE_MAX_FRAGMENT
  58380. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  58381. {
  58382. EXPECT_DECLS;
  58383. WOLFSSL *ssl_c = NULL;
  58384. WOLFSSL *ssl_c2 = NULL;
  58385. WOLFSSL *ssl_s = NULL;
  58386. struct test_memio_ctx test_ctx;
  58387. WOLFSSL_CTX *ctx_c = NULL;
  58388. WOLFSSL_CTX *ctx_s = NULL;
  58389. word16 max_fragment = 0;
  58390. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58391. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58392. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58393. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58394. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  58395. WOLFSSL_SUCCESS);
  58396. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58397. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58398. if (ssl_s != NULL) {
  58399. max_fragment = ssl_s->max_fragment;
  58400. }
  58401. /* send CH */
  58402. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58403. (ssl_c2->error == WANT_READ));
  58404. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58405. (ssl_s->error == WANT_READ));
  58406. /* CH without cookie shouldn't change state */
  58407. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  58408. ExpectIntNE(test_ctx.c_len, 0);
  58409. /* consume HRR from buffer */
  58410. test_ctx.c_len = 0;
  58411. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58412. wolfSSL_free(ssl_c2);
  58413. wolfSSL_free(ssl_c);
  58414. wolfSSL_free(ssl_s);
  58415. wolfSSL_CTX_free(ctx_c);
  58416. wolfSSL_CTX_free(ctx_s);
  58417. return EXPECT_RESULT();
  58418. }
  58419. #endif /* HAVE_MAX_FRAGMENT */
  58420. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  58421. #define ROUNDS_WITH_HVR 4
  58422. #define ROUNDS_WITHOUT_HVR 2
  58423. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  58424. static int buf_is_hvr(const byte *data, int len)
  58425. {
  58426. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  58427. return 0;
  58428. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  58429. }
  58430. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  58431. {
  58432. EXPECT_DECLS;
  58433. struct test_memio_ctx test_ctx;
  58434. WOLFSSL_CTX *ctx_c = NULL;
  58435. WOLFSSL_CTX *ctx_s = NULL;
  58436. WOLFSSL *ssl_c = NULL;
  58437. WOLFSSL *ssl_s = NULL;
  58438. WOLFSSL_SESSION *sess = NULL;
  58439. int round_trips;
  58440. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58441. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  58442. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58443. #ifdef HAVE_SESSION_TICKET
  58444. if (useticket) {
  58445. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  58446. }
  58447. #endif
  58448. round_trips = ROUNDS_WITH_HVR;
  58449. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  58450. &round_trips), 0);
  58451. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  58452. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58453. wolfSSL_shutdown(ssl_c);
  58454. wolfSSL_shutdown(ssl_s);
  58455. wolfSSL_free(ssl_c);
  58456. ssl_c = NULL;
  58457. wolfSSL_free(ssl_s);
  58458. ssl_s = NULL;
  58459. test_ctx.c_len = test_ctx.s_len = 0;
  58460. /* make resumption invalid */
  58461. if (bad && (sess != NULL)) {
  58462. if (useticket) {
  58463. #ifdef HAVE_SESSION_TICKET
  58464. if (sess->ticket != NULL) {
  58465. sess->ticket[0] = !sess->ticket[0];
  58466. }
  58467. #endif /* HAVE_SESSION_TICKET */
  58468. }
  58469. else {
  58470. sess->sessionID[0] = !sess->sessionID[0];
  58471. }
  58472. }
  58473. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58474. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58475. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58476. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58477. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58478. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58479. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58480. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58481. (ssl_c->error == WANT_READ));
  58482. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58483. (ssl_s->error == WANT_READ));
  58484. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58485. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58486. if (!useticket) {
  58487. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  58488. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  58489. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  58490. }
  58491. wolfSSL_SESSION_free(sess);
  58492. wolfSSL_free(ssl_c);
  58493. wolfSSL_free(ssl_s);
  58494. wolfSSL_CTX_free(ctx_c);
  58495. wolfSSL_CTX_free(ctx_s);
  58496. return EXPECT_RESULT();
  58497. }
  58498. static int test_wolfSSL_dtls_stateless_resume(void)
  58499. {
  58500. EXPECT_DECLS;
  58501. #ifdef HAVE_SESSION_TICKET
  58502. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  58503. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  58504. #endif /* HAVE_SESION_TICKET */
  58505. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  58506. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  58507. return EXPECT_RESULT();
  58508. }
  58509. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  58510. #if !defined(NO_OLD_TLS)
  58511. static int test_wolfSSL_dtls_stateless_downgrade(void)
  58512. {
  58513. EXPECT_DECLS;
  58514. WOLFSSL_CTX *ctx_c = NULL;
  58515. WOLFSSL_CTX *ctx_c2 = NULL;
  58516. WOLFSSL_CTX *ctx_s = NULL;
  58517. WOLFSSL *ssl_c = NULL;
  58518. WOLFSSL *ssl_c2 = NULL;
  58519. WOLFSSL *ssl_s = NULL;
  58520. struct test_memio_ctx test_ctx;
  58521. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58522. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58523. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58524. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  58525. WOLFSSL_SUCCESS);
  58526. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  58527. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  58528. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  58529. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  58530. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58531. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58532. /* send CH */
  58533. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58534. (ssl_c2->error == WANT_READ));
  58535. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58536. (ssl_s->error == WANT_READ));
  58537. ExpectIntNE(test_ctx.c_len, 0);
  58538. /* consume HRR */
  58539. test_ctx.c_len = 0;
  58540. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58541. wolfSSL_free(ssl_c2);
  58542. wolfSSL_free(ssl_c);
  58543. wolfSSL_free(ssl_s);
  58544. wolfSSL_CTX_free(ctx_c);
  58545. wolfSSL_CTX_free(ctx_c2);
  58546. wolfSSL_CTX_free(ctx_s);
  58547. return EXPECT_RESULT();
  58548. }
  58549. #endif /* !defined(NO_OLD_TLS) */
  58550. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58551. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  58552. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58553. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58554. static int test_WOLFSSL_dtls_version_alert(void)
  58555. {
  58556. EXPECT_DECLS;
  58557. struct test_memio_ctx test_ctx;
  58558. WOLFSSL_CTX *ctx_c = NULL;
  58559. WOLFSSL_CTX *ctx_s = NULL;
  58560. WOLFSSL *ssl_c = NULL;
  58561. WOLFSSL *ssl_s = NULL;
  58562. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58563. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58564. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  58565. /* client hello */
  58566. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58567. (ssl_c->error == WANT_READ));
  58568. /* hrr */
  58569. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58570. (ssl_s->error == WANT_READ));
  58571. /* client hello 1 */
  58572. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58573. (ssl_c->error == WANT_READ));
  58574. /* server hello */
  58575. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58576. (ssl_s->error == WANT_READ));
  58577. /* should fail */
  58578. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58579. (ssl_c->error == VERSION_ERROR));
  58580. /* shuould fail */
  58581. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58582. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  58583. wolfSSL_free(ssl_c);
  58584. wolfSSL_free(ssl_s);
  58585. wolfSSL_CTX_free(ctx_c);
  58586. wolfSSL_CTX_free(ctx_s);
  58587. return EXPECT_RESULT();
  58588. }
  58589. #else
  58590. static int test_WOLFSSL_dtls_version_alert(void)
  58591. {
  58592. return TEST_SKIPPED;
  58593. }
  58594. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  58595. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  58596. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  58597. */
  58598. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  58599. && defined(WOLFSSL_TLS13) && \
  58600. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  58601. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58602. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  58603. {
  58604. struct test_memio_ctx *test_ctx;
  58605. byte buf[2048];
  58606. int idx, sz;
  58607. word32 tmp;
  58608. int ret;
  58609. idx = 5; /* space for record header */
  58610. buf[idx] = session_ticket; /* type */
  58611. idx++;
  58612. tmp = OPAQUE32_LEN +
  58613. OPAQUE32_LEN +
  58614. OPAQUE8_LEN + nonceLength +
  58615. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  58616. c32to24(tmp, buf + idx);
  58617. idx += OPAQUE24_LEN;
  58618. c32toa((word32)12345, buf+idx); /* lifetime */
  58619. idx += OPAQUE32_LEN;
  58620. c32toa((word32)12345, buf+idx); /* add */
  58621. idx += OPAQUE32_LEN;
  58622. buf[idx] = nonceLength; /* nonce length */
  58623. idx++;
  58624. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  58625. idx += nonceLength;
  58626. tmp = 1; /* ticket len */
  58627. c16toa((word16)tmp, buf+idx);
  58628. idx += 2;
  58629. buf[idx] = 0xFF; /* ticket */
  58630. idx++;
  58631. tmp = 0; /* ext len */
  58632. c16toa((word16)tmp, buf+idx);
  58633. idx += 2;
  58634. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  58635. handshake, 0, 0, 0);
  58636. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  58637. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  58638. return !(ret == sz);
  58639. }
  58640. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  58641. {
  58642. int ret = 0;
  58643. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  58644. ret = -1;
  58645. }
  58646. else {
  58647. int i;
  58648. for (i = 0; i < len; i++) {
  58649. if (sess->ticketNonce.data[i] != len) {
  58650. ret = -1;
  58651. break;
  58652. }
  58653. }
  58654. }
  58655. return ret;
  58656. }
  58657. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58658. {
  58659. EXPECT_DECLS;
  58660. char *buf[1024];
  58661. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  58662. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  58663. (ssl_c->error == WANT_READ));
  58664. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  58665. return EXPECT_RESULT();
  58666. }
  58667. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58668. {
  58669. EXPECT_DECLS;
  58670. WOLFSSL_SESSION *sess = NULL;
  58671. WOLFSSL_SESSION *cached = NULL;
  58672. WOLFSSL_CTX *ctx = ssl_c->ctx;
  58673. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  58674. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58675. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  58676. NULL, ssl_c->options.side, 1,NULL), 0);
  58677. ExpectNotNull(cached = wolfSSL_SESSION_new());
  58678. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  58679. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  58680. wolfSSL_SESSION_free(cached);
  58681. wolfSSL_SESSION_free(sess);
  58682. return EXPECT_RESULT();
  58683. }
  58684. static int test_ticket_nonce_malloc(void)
  58685. {
  58686. EXPECT_DECLS;
  58687. struct test_memio_ctx test_ctx;
  58688. WOLFSSL_CTX *ctx_c = NULL;
  58689. WOLFSSL_CTX *ctx_s = NULL;
  58690. WOLFSSL *ssl_c = NULL;
  58691. WOLFSSL *ssl_s = NULL;
  58692. byte small;
  58693. byte medium;
  58694. byte big;
  58695. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58696. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58697. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58698. /* will send ticket manually */
  58699. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  58700. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58701. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58702. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  58703. (ssl_s->options.handShakeDone == 0)) {
  58704. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  58705. (ssl_c->error == WANT_READ));
  58706. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  58707. (ssl_s->error == WANT_READ));
  58708. }
  58709. small = TLS13_TICKET_NONCE_STATIC_SZ;
  58710. medium = small + 20 <= 255 ? small + 20 : 255;
  58711. big = medium + 20 <= 255 ? small + 20 : 255;
  58712. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58713. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  58714. ssl_c->session->ticketNonce.dataStatic);
  58715. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58716. TEST_SUCCESS);
  58717. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  58718. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58719. TEST_SUCCESS);
  58720. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58721. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58722. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58723. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  58724. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58725. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58726. wolfSSL_free(ssl_c);
  58727. wolfSSL_free(ssl_s);
  58728. wolfSSL_CTX_free(ctx_c);
  58729. wolfSSL_CTX_free(ctx_s);
  58730. return EXPECT_RESULT();
  58731. }
  58732. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  58733. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  58734. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  58735. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58736. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  58737. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58738. static int test_ticket_ret_create(void)
  58739. {
  58740. EXPECT_DECLS;
  58741. WOLFSSL_CTX *ctx_c = NULL;
  58742. WOLFSSL_CTX *ctx_s = NULL;
  58743. WOLFSSL *ssl_c = NULL;
  58744. WOLFSSL *ssl_s = NULL;
  58745. byte ticket[SESSION_TICKET_LEN];
  58746. struct test_memio_ctx test_ctx;
  58747. WOLFSSL_SESSION *sess = NULL;
  58748. word16 ticketLen = 0;
  58749. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58750. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58751. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58752. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58753. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58754. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  58755. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58756. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58757. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  58758. if (sess != NULL) {
  58759. ticketLen = sess->ticketLen;
  58760. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  58761. }
  58762. wolfSSL_free(ssl_c);
  58763. ssl_c = NULL;
  58764. wolfSSL_free(ssl_s);
  58765. ssl_s = NULL;
  58766. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58767. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58768. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58769. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58770. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58771. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58772. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58773. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58774. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  58775. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  58776. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  58777. wolfSSL_SESSION_free(sess);
  58778. wolfSSL_free(ssl_c);
  58779. wolfSSL_free(ssl_s);
  58780. wolfSSL_CTX_free(ctx_c);
  58781. wolfSSL_CTX_free(ctx_s);
  58782. return EXPECT_RESULT();
  58783. }
  58784. #else
  58785. static int test_ticket_ret_create(void)
  58786. {
  58787. return TEST_SKIPPED;
  58788. }
  58789. #endif
  58790. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  58791. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  58792. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58793. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58794. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58795. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  58796. {
  58797. int ret;
  58798. WOLFSSL_SESSION* session = NULL;
  58799. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58800. if (!wolfSSL_is_server(ssl)) {
  58801. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58802. AssertNotNull(session);
  58803. }
  58804. do {
  58805. ret = wolfSSL_shutdown(ssl);
  58806. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58807. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58808. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58809. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58810. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58811. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58812. #endif
  58813. if (!wolfSSL_is_server(ssl)) {
  58814. /* client */
  58815. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58816. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58817. wolfSSL_set_session(ssl, session);
  58818. wolfSSL_SESSION_free(session);
  58819. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58820. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58821. }
  58822. else {
  58823. /* server */
  58824. /* Different ciphersuite so that the ticket will be invalidated based on
  58825. * the ciphersuite */
  58826. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  58827. WOLFSSL_SUCCESS);
  58828. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  58829. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58830. }
  58831. }
  58832. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  58833. {
  58834. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  58835. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58836. WOLFSSL_SUCCESS);
  58837. }
  58838. static int test_ticket_and_psk_mixing(void)
  58839. {
  58840. EXPECT_DECLS;
  58841. /* Test mixing tickets and regular PSK */
  58842. callback_functions client_cbs, server_cbs;
  58843. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58844. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58845. client_cbs.method = wolfTLSv1_3_client_method;
  58846. server_cbs.method = wolfTLSv1_3_server_method;
  58847. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  58848. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58849. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58850. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58851. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58852. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58853. return EXPECT_RESULT();
  58854. }
  58855. #else
  58856. static int test_ticket_and_psk_mixing(void)
  58857. {
  58858. return TEST_SKIPPED;
  58859. }
  58860. #endif
  58861. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  58862. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  58863. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58864. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58865. static int test_prioritize_psk_cb_called = FALSE;
  58866. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  58867. const char* identity, unsigned char* key, unsigned int key_max_len,
  58868. const char** ciphersuite)
  58869. {
  58870. test_prioritize_psk_cb_called = TRUE;
  58871. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  58872. }
  58873. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  58874. {
  58875. int ret;
  58876. WOLFSSL_SESSION* session = NULL;
  58877. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58878. if (!wolfSSL_is_server(ssl)) {
  58879. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58880. AssertNotNull(session);
  58881. }
  58882. do {
  58883. ret = wolfSSL_shutdown(ssl);
  58884. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58885. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58886. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58887. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  58888. * important. */
  58889. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58890. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58891. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58892. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58893. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58894. #endif
  58895. if (!wolfSSL_is_server(ssl)) {
  58896. /* client */
  58897. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58898. wolfSSL_set_session(ssl, session);
  58899. wolfSSL_SESSION_free(session);
  58900. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58901. }
  58902. else {
  58903. /* server */
  58904. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  58905. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58906. #ifdef WOLFSSL_PRIORITIZE_PSK
  58907. /* The ticket should be first tried with all ciphersuites and chosen */
  58908. AssertFalse(test_prioritize_psk_cb_called);
  58909. #else
  58910. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  58911. * callback that sets this var. */
  58912. AssertTrue(test_prioritize_psk_cb_called);
  58913. #endif
  58914. }
  58915. }
  58916. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  58917. {
  58918. if (!wolfSSL_is_server(ssl))
  58919. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58920. WOLFSSL_SUCCESS);
  58921. else
  58922. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58923. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58924. }
  58925. static int test_prioritize_psk(void)
  58926. {
  58927. EXPECT_DECLS;
  58928. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  58929. * of the PSK's will be followed instead of the ciphersuite. */
  58930. callback_functions client_cbs, server_cbs;
  58931. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58932. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58933. client_cbs.method = wolfTLSv1_3_client_method;
  58934. server_cbs.method = wolfTLSv1_3_server_method;
  58935. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58936. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58937. client_cbs.on_result = test_prioritize_psk_on_result;
  58938. server_cbs.on_result = test_prioritize_psk_on_result;
  58939. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58940. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58941. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58942. return EXPECT_RESULT();
  58943. }
  58944. #else
  58945. static int test_prioritize_psk(void)
  58946. {
  58947. return TEST_SKIPPED;
  58948. }
  58949. #endif
  58950. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  58951. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58952. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58953. !defined(WOLFSSL_NO_TLS12)
  58954. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  58955. {
  58956. EXPECT_DECLS;
  58957. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  58958. /* Set TLS 1.3 specific suite */
  58959. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  58960. return EXPECT_RESULT();
  58961. }
  58962. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58963. {
  58964. EXPECT_DECLS;
  58965. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  58966. * do a 1.2 connection. */
  58967. test_ssl_cbf client_cbs, server_cbs;
  58968. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58969. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58970. client_cbs.method = wolfTLSv1_2_client_method;
  58971. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  58972. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  58973. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58974. &server_cbs, NULL), TEST_SUCCESS);
  58975. return EXPECT_RESULT();
  58976. }
  58977. #else
  58978. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58979. {
  58980. return TEST_SKIPPED;
  58981. }
  58982. #endif
  58983. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  58984. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58985. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  58986. {
  58987. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58988. return TEST_SUCCESS;
  58989. }
  58990. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  58991. {
  58992. EXPECT_DECLS;
  58993. WOLFSSL_ALERT_HISTORY h;
  58994. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58995. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58996. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  58997. return EXPECT_RESULT();
  58998. }
  58999. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59000. {
  59001. EXPECT_DECLS;
  59002. test_ssl_cbf client_cbs, server_cbs;
  59003. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59004. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59005. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  59006. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  59007. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  59008. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  59009. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  59010. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59011. &server_cbs, NULL), TEST_FAIL);
  59012. return EXPECT_RESULT();
  59013. }
  59014. #else
  59015. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59016. {
  59017. return TEST_SKIPPED;
  59018. }
  59019. #endif
  59020. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  59021. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59022. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59023. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59024. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59025. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59026. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  59027. static int test_TLS_13_ticket_different_ciphers_run = 0;
  59028. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  59029. {
  59030. EXPECT_DECLS;
  59031. switch (test_TLS_13_ticket_different_ciphers_run) {
  59032. case 0:
  59033. /* First run */
  59034. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59035. WOLFSSL_SUCCESS);
  59036. if (wolfSSL_is_server(ssl)) {
  59037. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  59038. wolfSSL_get_SSL_CTX(ssl));
  59039. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  59040. test_TLS_13_ticket_different_ciphers_ctx));
  59041. }
  59042. break;
  59043. case 1:
  59044. /* Second run */
  59045. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59046. "TLS13-AES128-GCM-SHA256"),
  59047. WOLFSSL_SUCCESS);
  59048. if (!wolfSSL_is_server(ssl)) {
  59049. ExpectIntEQ(wolfSSL_set_session(ssl,
  59050. test_TLS_13_ticket_different_ciphers_session),
  59051. WOLFSSL_SUCCESS);
  59052. }
  59053. break;
  59054. default:
  59055. /* Bad state? */
  59056. Fail(("Should not enter here"), ("Should not enter here"));
  59057. }
  59058. return EXPECT_RESULT();
  59059. }
  59060. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  59061. {
  59062. EXPECT_DECLS;
  59063. switch (test_TLS_13_ticket_different_ciphers_run) {
  59064. case 0:
  59065. /* First run */
  59066. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  59067. wolfSSL_get1_session(ssl));
  59068. break;
  59069. case 1:
  59070. /* Second run */
  59071. ExpectTrue(wolfSSL_session_reused(ssl));
  59072. break;
  59073. default:
  59074. /* Bad state? */
  59075. Fail(("Should not enter here"), ("Should not enter here"));
  59076. }
  59077. return EXPECT_RESULT();
  59078. }
  59079. static int test_TLS_13_ticket_different_ciphers(void)
  59080. {
  59081. EXPECT_DECLS;
  59082. /* Check that we handle the connection when the ticket doesn't match
  59083. * the first ciphersuite. */
  59084. test_ssl_cbf client_cbs, server_cbs;
  59085. struct test_params {
  59086. method_provider client_meth;
  59087. method_provider server_meth;
  59088. int doUdp;
  59089. } params[] = {
  59090. #ifdef WOLFSSL_DTLS13
  59091. /* Test that the stateless code handles sessions correctly */
  59092. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  59093. #endif
  59094. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  59095. };
  59096. size_t i;
  59097. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59098. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59099. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59100. test_TLS_13_ticket_different_ciphers_run = 0;
  59101. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  59102. client_cbs.method = params[i].client_meth;
  59103. server_cbs.method = params[i].server_meth;
  59104. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59105. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59106. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  59107. server_cbs.ticNoInit = 1;
  59108. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59109. &server_cbs, NULL), TEST_SUCCESS);
  59110. test_TLS_13_ticket_different_ciphers_run++;
  59111. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  59112. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59113. &server_cbs, NULL), TEST_SUCCESS);
  59114. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  59115. test_TLS_13_ticket_different_ciphers_session = NULL;
  59116. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  59117. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59118. }
  59119. return EXPECT_RESULT();
  59120. }
  59121. #else
  59122. static int test_TLS_13_ticket_different_ciphers(void)
  59123. {
  59124. return TEST_SKIPPED;
  59125. }
  59126. #endif
  59127. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  59128. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59129. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  59130. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  59131. byte test_extra_alerts_wrong_cs_sh[] = {
  59132. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  59133. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  59134. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  59135. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  59136. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  59137. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  59138. 0xbb, 0x07, 0x54, 0x1c,
  59139. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  59140. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  59141. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  59142. };
  59143. static int test_extra_alerts_wrong_cs(void)
  59144. {
  59145. EXPECT_DECLS;
  59146. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  59147. struct test_memio_ctx test_ctx;
  59148. WOLFSSL_CTX *ctx_c = NULL;
  59149. WOLFSSL_ALERT_HISTORY h;
  59150. WOLFSSL *ssl_c = NULL;
  59151. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59152. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59153. wolfTLSv1_2_client_method, NULL), 0);
  59154. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  59155. WOLFSSL_SUCCESS);
  59156. /* CH */
  59157. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59158. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59159. WOLFSSL_ERROR_WANT_READ);
  59160. /* consume CH */
  59161. test_ctx.s_len = 0;
  59162. /* inject SH */
  59163. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  59164. sizeof(test_extra_alerts_wrong_cs_sh));
  59165. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  59166. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59167. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59168. WOLFSSL_ERROR_WANT_READ);
  59169. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59170. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59171. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59172. wolfSSL_free(ssl_c);
  59173. wolfSSL_CTX_free(ctx_c);
  59174. #endif
  59175. return EXPECT_RESULT();
  59176. }
  59177. #else
  59178. static int test_extra_alerts_wrong_cs(void)
  59179. {
  59180. return TEST_SKIPPED;
  59181. }
  59182. #endif
  59183. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  59184. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  59185. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  59186. {
  59187. tail_len -= msg_length;
  59188. XMEMMOVE(msg, msg + msg_length, tail_len);
  59189. *len = *len - msg_length;
  59190. }
  59191. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  59192. byte *found)
  59193. {
  59194. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  59195. const unsigned int _RECORD_HEADER_SZ = 5;
  59196. const int _change_cipher_hs = 55;
  59197. const int _change_cipher = 20;
  59198. const int _handshake = 22;
  59199. unsigned int tail_len;
  59200. byte *idx, *curr;
  59201. word8 currType;
  59202. word16 rLength;
  59203. word32 hLength;
  59204. idx = buf;
  59205. tail_len = *len;
  59206. *found = 0;
  59207. while (tail_len > _RECORD_HEADER_SZ) {
  59208. curr = idx;
  59209. currType = *idx;
  59210. ato16(idx + 3, &rLength);
  59211. idx += _RECORD_HEADER_SZ;
  59212. tail_len -= _RECORD_HEADER_SZ;
  59213. if (tail_len < rLength)
  59214. return -1;
  59215. if (type == _change_cipher_hs && currType == _change_cipher) {
  59216. if (rLength != 1)
  59217. return -1;
  59218. /* match */
  59219. test_remove_msg(curr, *len - (int)(curr - buf),
  59220. len, _RECORD_HEADER_SZ + 1);
  59221. *found = 1;
  59222. return 0;
  59223. }
  59224. if (currType != _handshake) {
  59225. idx += rLength;
  59226. tail_len -= rLength;
  59227. continue;
  59228. }
  59229. if (rLength < _HANDSHAKE_HEADER_SZ)
  59230. return -1;
  59231. currType = *idx;
  59232. ato24(idx+1, &hLength);
  59233. hLength += _HANDSHAKE_HEADER_SZ;
  59234. if (tail_len < hLength)
  59235. return -1;
  59236. if (currType != type) {
  59237. idx += hLength;
  59238. tail_len -= hLength;
  59239. continue;
  59240. }
  59241. /* match */
  59242. test_remove_msg(curr, *len - (int)(curr - buf), len,
  59243. hLength + _RECORD_HEADER_SZ);
  59244. *found = 1;
  59245. return 0;
  59246. }
  59247. /* not found */
  59248. return 0;
  59249. }
  59250. static int test_remove_hs_message(byte hs_message_type,
  59251. int extra_round, byte alert_type)
  59252. {
  59253. EXPECT_DECLS;
  59254. WOLFSSL_CTX *ctx_c = NULL;
  59255. WOLFSSL_CTX *ctx_s = NULL;
  59256. WOLFSSL *ssl_c = NULL;
  59257. WOLFSSL *ssl_s = NULL;
  59258. struct test_memio_ctx test_ctx;
  59259. WOLFSSL_ALERT_HISTORY h;
  59260. byte found = 0;
  59261. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59262. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59263. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59264. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59265. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59266. WOLFSSL_ERROR_WANT_READ);
  59267. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59268. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59269. WOLFSSL_ERROR_WANT_READ);
  59270. if (extra_round) {
  59271. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59272. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59273. WOLFSSL_ERROR_WANT_READ);
  59274. /* this will complete handshake from server side */
  59275. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59276. }
  59277. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  59278. &test_ctx.c_len, hs_message_type, &found), 0);
  59279. if (!found) {
  59280. wolfSSL_free(ssl_c);
  59281. wolfSSL_CTX_free(ctx_c);
  59282. wolfSSL_free(ssl_s);
  59283. wolfSSL_CTX_free(ctx_s);
  59284. return TEST_SKIPPED;
  59285. }
  59286. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59287. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59288. WOLFSSL_ERROR_WANT_READ);
  59289. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59290. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  59291. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59292. wolfSSL_free(ssl_c);
  59293. wolfSSL_CTX_free(ctx_c);
  59294. wolfSSL_free(ssl_s);
  59295. wolfSSL_CTX_free(ctx_s);
  59296. return EXPECT_RESULT();
  59297. }
  59298. static int test_extra_alerts_skip_hs(void)
  59299. {
  59300. EXPECT_DECLS;
  59301. const byte _server_key_exchange = 12;
  59302. const byte _server_hello = 2;
  59303. const byte _certificate = 11;
  59304. /* server_hello */
  59305. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  59306. unexpected_message), TEST_FAIL);
  59307. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  59308. 0xff), TEST_FAIL);
  59309. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  59310. unexpected_message), TEST_FAIL);
  59311. return EXPECT_RESULT();
  59312. }
  59313. #else
  59314. static int test_extra_alerts_skip_hs(void)
  59315. {
  59316. return TEST_SKIPPED;
  59317. }
  59318. #endif
  59319. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59320. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  59321. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  59322. unsigned char* key, unsigned int key_max_len)
  59323. {
  59324. (void)ssl;
  59325. (void)id;
  59326. (void)key_max_len;
  59327. /* zero means error */
  59328. key[0] = 0x10;
  59329. return 1;
  59330. }
  59331. static int test_extra_alerts_bad_psk(void)
  59332. {
  59333. EXPECT_DECLS;
  59334. WOLFSSL_CTX *ctx_c = NULL;
  59335. WOLFSSL_CTX *ctx_s = NULL;
  59336. WOLFSSL *ssl_c = NULL;
  59337. WOLFSSL *ssl_s = NULL;
  59338. struct test_memio_ctx test_ctx;
  59339. WOLFSSL_ALERT_HISTORY h;
  59340. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59341. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59342. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59343. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  59344. WOLFSSL_SUCCESS);
  59345. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  59346. WOLFSSL_SUCCESS);
  59347. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  59348. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59349. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59350. WOLFSSL_ERROR_WANT_READ);
  59351. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59352. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59353. WOLFSSL_ERROR_WANT_READ);
  59354. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59355. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59356. WOLFSSL_ERROR_WANT_READ);
  59357. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59358. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59359. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59360. wolfSSL_free(ssl_c);
  59361. wolfSSL_CTX_free(ctx_c);
  59362. wolfSSL_free(ssl_s);
  59363. wolfSSL_CTX_free(ctx_s);
  59364. return EXPECT_RESULT();
  59365. }
  59366. #else
  59367. static int test_extra_alerts_bad_psk(void)
  59368. {
  59369. return TEST_SKIPPED;
  59370. }
  59371. #endif
  59372. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59373. && !defined(NO_PSK)
  59374. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  59375. const char* hint, char* identity, unsigned int id_max_len,
  59376. unsigned char* key, unsigned int key_max_len)
  59377. {
  59378. (void)ssl;
  59379. (void)hint;
  59380. (void)key_max_len;
  59381. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  59382. XSTRNCPY(identity, "Client_identity", id_max_len);
  59383. key[0] = 0x20;
  59384. return 1;
  59385. }
  59386. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  59387. const char* id, unsigned char* key, unsigned int key_max_len)
  59388. {
  59389. (void)ssl;
  59390. (void)id;
  59391. (void)key_max_len;
  59392. /* zero means error */
  59393. key[0] = 0x10;
  59394. return 1;
  59395. }
  59396. #endif
  59397. static int test_tls13_bad_psk_binder(void)
  59398. {
  59399. EXPECT_DECLS;
  59400. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59401. && !defined(NO_PSK)
  59402. WOLFSSL_CTX *ctx_c = NULL;
  59403. WOLFSSL_CTX *ctx_s = NULL;
  59404. WOLFSSL *ssl_c = NULL;
  59405. WOLFSSL *ssl_s = NULL;
  59406. struct test_memio_ctx test_ctx;
  59407. WOLFSSL_ALERT_HISTORY h;
  59408. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59409. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59410. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59411. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  59412. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  59413. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59414. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59415. WOLFSSL_ERROR_WANT_READ);
  59416. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59417. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59418. BAD_BINDER);
  59419. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59420. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59421. FATAL_ERROR);
  59422. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59423. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  59424. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59425. wolfSSL_free(ssl_c);
  59426. wolfSSL_CTX_free(ctx_c);
  59427. wolfSSL_free(ssl_s);
  59428. wolfSSL_CTX_free(ctx_s);
  59429. #endif
  59430. return EXPECT_RESULT();
  59431. }
  59432. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  59433. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59434. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  59435. int sz, void *ctx)
  59436. {
  59437. static int sentServerHello = FALSE;
  59438. if (!sentServerHello) {
  59439. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  59440. size_t i;
  59441. if (sz < (int)sizeof(renegExt))
  59442. return WOLFSSL_CBIO_ERR_GENERAL;
  59443. /* Remove SCR from ServerHello */
  59444. for (i = 0; i < sz - sizeof(renegExt); i++) {
  59445. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  59446. /* Found the extension. Change it to something unrecognized. */
  59447. buf[i+1] = 0x11;
  59448. break;
  59449. }
  59450. }
  59451. sentServerHello = TRUE;
  59452. }
  59453. return EmbedSend(ssl, buf, sz, ctx);
  59454. }
  59455. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  59456. {
  59457. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  59458. }
  59459. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  59460. {
  59461. WOLFSSL_ALERT_HISTORY h;
  59462. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59463. AssertIntEQ(h.last_rx.code, handshake_failure);
  59464. AssertIntEQ(h.last_rx.level, alert_fatal);
  59465. }
  59466. static int test_harden_no_secure_renegotiation(void)
  59467. {
  59468. EXPECT_DECLS;
  59469. callback_functions client_cbs, server_cbs;
  59470. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59471. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59472. client_cbs.method = wolfTLSv1_2_client_method;
  59473. server_cbs.method = wolfTLSv1_2_server_method;
  59474. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  59475. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  59476. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59477. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  59478. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  59479. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  59480. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  59481. server_cbs.last_err == FATAL_ERROR);
  59482. return EXPECT_RESULT();
  59483. }
  59484. #else
  59485. static int test_harden_no_secure_renegotiation(void)
  59486. {
  59487. return TEST_SKIPPED;
  59488. }
  59489. #endif
  59490. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59491. static int test_override_alt_cert_chain_cert_cb(int preverify,
  59492. WOLFSSL_X509_STORE_CTX* store)
  59493. {
  59494. fprintf(stderr, "preverify: %d\n", preverify);
  59495. fprintf(stderr, "store->error: %d\n", store->error);
  59496. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  59497. if (store->error == OCSP_INVALID_STATUS) {
  59498. fprintf(stderr, "Overriding OCSP error\n");
  59499. return 1;
  59500. }
  59501. #ifndef WOLFSSL_ALT_CERT_CHAINS
  59502. else if ((store->error == ASN_NO_SIGNER_E ||
  59503. store->error == ASN_SELF_SIGNED_E
  59504. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  59505. defined(HAVE_WEBSERVER)
  59506. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  59507. #endif
  59508. ) && store->error_depth == store->totalCerts - 1) {
  59509. fprintf(stderr, "Overriding no signer error only for root cert\n");
  59510. return 1;
  59511. }
  59512. #endif
  59513. else
  59514. return preverify;
  59515. }
  59516. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  59517. int urlSz, unsigned char* request, int requestSz,
  59518. unsigned char** response)
  59519. {
  59520. (void)ioCtx;
  59521. (void)url;
  59522. (void)urlSz;
  59523. (void)request;
  59524. (void)requestSz;
  59525. (void)response;
  59526. return -1;
  59527. }
  59528. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  59529. {
  59530. EXPECT_DECLS;
  59531. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  59532. test_override_alt_cert_chain_cert_cb);
  59533. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59534. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59535. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59536. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59537. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59538. WOLFSSL_SUCCESS);
  59539. return EXPECT_RESULT();
  59540. }
  59541. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  59542. {
  59543. EXPECT_DECLS;
  59544. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59545. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59546. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59547. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59548. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59549. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59550. WOLFSSL_SUCCESS);
  59551. return EXPECT_RESULT();
  59552. }
  59553. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  59554. {
  59555. EXPECT_DECLS;
  59556. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  59557. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  59558. return EXPECT_RESULT();
  59559. }
  59560. static int test_override_alt_cert_chain(void)
  59561. {
  59562. EXPECT_DECLS;
  59563. size_t i;
  59564. struct test_params {
  59565. ctx_cb client_ctx_cb;
  59566. ctx_cb server_ctx_cb;
  59567. int result;
  59568. } params[] = {
  59569. {test_override_alt_cert_chain_client_ctx_ready,
  59570. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  59571. {test_override_alt_cert_chain_client_ctx_ready2,
  59572. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  59573. };
  59574. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59575. test_ssl_cbf client_cbs, server_cbs;
  59576. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59577. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59578. fprintf(stderr, "test config: %d\n", (int)i);
  59579. client_cbs.ctx_ready = params[i].client_ctx_cb;
  59580. server_cbs.ctx_ready = params[i].server_ctx_cb;
  59581. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59582. &server_cbs, NULL), params[i].result);
  59583. ExpectIntEQ(client_cbs.return_code, params[i].result);
  59584. ExpectIntEQ(server_cbs.return_code, params[i].result);
  59585. }
  59586. return EXPECT_RESULT();
  59587. }
  59588. #else
  59589. static int test_override_alt_cert_chain(void)
  59590. {
  59591. return TEST_SKIPPED;
  59592. }
  59593. #endif
  59594. #if defined(HAVE_RPK)
  59595. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  59596. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  59597. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59598. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  59599. {
  59600. int ret = WOLFSSL_SUCCESS;
  59601. (void)mode;
  59602. (void)strctx;
  59603. WOLFSSL_ENTER("MyRpkVerifyCb");
  59604. return ret;
  59605. }
  59606. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59607. static WC_INLINE int test_rpk_memio_setup(
  59608. struct test_memio_ctx *ctx,
  59609. WOLFSSL_CTX **ctx_c,
  59610. WOLFSSL_CTX **ctx_s,
  59611. WOLFSSL **ssl_c,
  59612. WOLFSSL **ssl_s,
  59613. method_provider method_c,
  59614. method_provider method_s,
  59615. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  59616. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  59617. const char* pkey_c, int fmt_kc, /* client private key and format */
  59618. const char* pkey_s, int fmt_ks /* server private key and format */
  59619. )
  59620. {
  59621. int ret;
  59622. if (ctx_c != NULL && *ctx_c == NULL) {
  59623. *ctx_c = wolfSSL_CTX_new(method_c());
  59624. if (*ctx_c == NULL) {
  59625. return -1;
  59626. }
  59627. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  59628. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  59629. if (ret != WOLFSSL_SUCCESS) {
  59630. return -1;
  59631. }
  59632. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  59633. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  59634. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  59635. if (ret != WOLFSSL_SUCCESS) {
  59636. return -1;
  59637. }
  59638. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  59639. if (ret != WOLFSSL_SUCCESS) {
  59640. return -1;
  59641. }
  59642. }
  59643. if (ctx_s != NULL && *ctx_s == NULL) {
  59644. *ctx_s = wolfSSL_CTX_new(method_s());
  59645. if (*ctx_s == NULL) {
  59646. return -1;
  59647. }
  59648. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  59649. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  59650. if (ret != WOLFSSL_SUCCESS) {
  59651. return -1;
  59652. }
  59653. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  59654. if (ret != WOLFSSL_SUCCESS) {
  59655. return -1;
  59656. }
  59657. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  59658. if (ret != WOLFSSL_SUCCESS) {
  59659. return -1;
  59660. }
  59661. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  59662. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  59663. if (ctx->s_ciphers != NULL) {
  59664. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  59665. if (ret != WOLFSSL_SUCCESS) {
  59666. return -1;
  59667. }
  59668. }
  59669. }
  59670. if (ctx_c != NULL && ssl_c != NULL) {
  59671. *ssl_c = wolfSSL_new(*ctx_c);
  59672. if (*ssl_c == NULL) {
  59673. return -1;
  59674. }
  59675. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  59676. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  59677. }
  59678. if (ctx_s != NULL && ssl_s != NULL) {
  59679. *ssl_s = wolfSSL_new(*ctx_s);
  59680. if (*ssl_s == NULL) {
  59681. return -1;
  59682. }
  59683. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  59684. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  59685. #if !defined(NO_DH)
  59686. SetDH(*ssl_s);
  59687. #endif
  59688. }
  59689. return 0;
  59690. }
  59691. #endif /* HAVE_RPK */
  59692. static int test_rpk_set_xxx_cert_type(void)
  59693. {
  59694. EXPECT_DECLS;
  59695. #if defined(HAVE_RPK)
  59696. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  59697. WOLFSSL_CTX* ctx = NULL;
  59698. WOLFSSL* ssl = NULL;
  59699. int tp;
  59700. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  59701. ExpectNotNull(ctx);
  59702. ssl = wolfSSL_new(ctx);
  59703. ExpectNotNull(ssl);
  59704. /*--------------------------------------------*/
  59705. /* tests for wolfSSL_CTX_set_client_cert_type */
  59706. /*--------------------------------------------*/
  59707. /* illegal parameter test caces */
  59708. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  59709. MAX_CLIENT_CERT_TYPE_CNT),
  59710. BAD_FUNC_ARG);
  59711. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59712. sizeof(ctype)),
  59713. BAD_FUNC_ARG);
  59714. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59715. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59716. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59717. MAX_CLIENT_CERT_TYPE_CNT),
  59718. BAD_FUNC_ARG);
  59719. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59720. ctype[1] = 10; /* set unknown cert type */
  59721. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59722. MAX_CLIENT_CERT_TYPE_CNT),
  59723. BAD_FUNC_ARG);
  59724. /* pass larger type count */
  59725. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59726. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59727. ctype[2] = 1; /* pass unacceptable type count */
  59728. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59729. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59730. BAD_FUNC_ARG);
  59731. /* should accept NULL for type buffer */
  59732. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  59733. MAX_CLIENT_CERT_TYPE_CNT),
  59734. WOLFSSL_SUCCESS);
  59735. /* should accept zero for type count */
  59736. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59737. 0),
  59738. WOLFSSL_SUCCESS);
  59739. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59740. MAX_CLIENT_CERT_TYPE_CNT),
  59741. WOLFSSL_SUCCESS);
  59742. /*--------------------------------------------*/
  59743. /* tests for wolfSSL_CTX_set_server_cert_type */
  59744. /*--------------------------------------------*/
  59745. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  59746. MAX_SERVER_CERT_TYPE_CNT),
  59747. BAD_FUNC_ARG);
  59748. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59749. sizeof(ctype)),
  59750. BAD_FUNC_ARG);
  59751. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59752. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59753. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59754. MAX_SERVER_CERT_TYPE_CNT),
  59755. BAD_FUNC_ARG);
  59756. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59757. ctype[1] = 10; /* set unknown cert type */
  59758. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59759. MAX_SERVER_CERT_TYPE_CNT),
  59760. BAD_FUNC_ARG);
  59761. /* pass larger type count */
  59762. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59763. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59764. ctype[2] = 1; /* pass unacceptable type count */
  59765. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59766. MAX_SERVER_CERT_TYPE_CNT + 1),
  59767. BAD_FUNC_ARG);
  59768. /* should accept NULL for type buffer */
  59769. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  59770. MAX_SERVER_CERT_TYPE_CNT),
  59771. WOLFSSL_SUCCESS);
  59772. /* should accept zero for type count */
  59773. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59774. 0),
  59775. WOLFSSL_SUCCESS);
  59776. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59777. MAX_CLIENT_CERT_TYPE_CNT),
  59778. WOLFSSL_SUCCESS);
  59779. /*--------------------------------------------*/
  59780. /* tests for wolfSSL_set_client_cert_type */
  59781. /*--------------------------------------------*/
  59782. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  59783. MAX_CLIENT_CERT_TYPE_CNT),
  59784. BAD_FUNC_ARG);
  59785. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59786. sizeof(ctype)),
  59787. BAD_FUNC_ARG);
  59788. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59789. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59790. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59791. MAX_CLIENT_CERT_TYPE_CNT),
  59792. BAD_FUNC_ARG);
  59793. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59794. ctype[1] = 10; /* set unknown cert type */
  59795. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59796. MAX_CLIENT_CERT_TYPE_CNT),
  59797. BAD_FUNC_ARG);
  59798. /* pass larger type count */
  59799. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59800. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59801. ctype[2] = 1; /* pass unacceptable type count */
  59802. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59803. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59804. BAD_FUNC_ARG);
  59805. /* should accept NULL for type buffer */
  59806. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  59807. MAX_CLIENT_CERT_TYPE_CNT),
  59808. WOLFSSL_SUCCESS);
  59809. /* should accept zero for type count */
  59810. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59811. 0),
  59812. WOLFSSL_SUCCESS);
  59813. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59814. MAX_CLIENT_CERT_TYPE_CNT),
  59815. WOLFSSL_SUCCESS);
  59816. /*--------------------------------------------*/
  59817. /* tests for wolfSSL_CTX_set_server_cert_type */
  59818. /*--------------------------------------------*/
  59819. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  59820. MAX_SERVER_CERT_TYPE_CNT),
  59821. BAD_FUNC_ARG);
  59822. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59823. sizeof(ctype)),
  59824. BAD_FUNC_ARG);
  59825. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59826. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59827. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59828. MAX_SERVER_CERT_TYPE_CNT),
  59829. BAD_FUNC_ARG);
  59830. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59831. ctype[1] = 10; /* set unknown cert type */
  59832. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59833. MAX_SERVER_CERT_TYPE_CNT),
  59834. BAD_FUNC_ARG);
  59835. /* pass larger type count */
  59836. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59837. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59838. ctype[2] = 1; /* pass unacceptable type count */
  59839. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59840. MAX_SERVER_CERT_TYPE_CNT + 1),
  59841. BAD_FUNC_ARG);
  59842. /* should accept NULL for type buffer */
  59843. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  59844. MAX_SERVER_CERT_TYPE_CNT),
  59845. WOLFSSL_SUCCESS);
  59846. /* should accept zero for type count */
  59847. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59848. 0),
  59849. WOLFSSL_SUCCESS);
  59850. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59851. MAX_SERVER_CERT_TYPE_CNT),
  59852. WOLFSSL_SUCCESS);
  59853. /*------------------------------------------------*/
  59854. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  59855. /*------------------------------------------------*/
  59856. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  59857. BAD_FUNC_ARG);
  59858. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  59859. BAD_FUNC_ARG);
  59860. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  59861. BAD_FUNC_ARG);
  59862. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  59863. BAD_FUNC_ARG);
  59864. /* clean up */
  59865. wolfSSL_free(ssl);
  59866. wolfSSL_CTX_free(ctx);
  59867. #endif
  59868. return EXPECT_RESULT();
  59869. }
  59870. static int test_tls13_rpk_handshake(void)
  59871. {
  59872. EXPECT_DECLS;
  59873. #if defined(HAVE_RPK)
  59874. int ret = 0;
  59875. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59876. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59877. struct test_memio_ctx test_ctx;
  59878. int err;
  59879. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  59880. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  59881. int typeCnt_c;
  59882. int typeCnt_s;
  59883. int tp;
  59884. (void)err;
  59885. (void)typeCnt_c;
  59886. (void)typeCnt_s;
  59887. (void)certType_c;
  59888. (void)certType_s;
  59889. /* TLS1.2
  59890. * Both client and server load x509 cert and start handshaking.
  59891. * Check no negotiation occurred.
  59892. */
  59893. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59894. ExpectIntEQ(
  59895. test_rpk_memio_setup(
  59896. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59897. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59898. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59899. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59900. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59901. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  59902. , 0);
  59903. /* set client certificate type in client end */
  59904. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59905. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59906. typeCnt_c = 2;
  59907. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59908. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59909. typeCnt_s = 2;
  59910. /* both client and server do not call client/server_cert_type APIs,
  59911. * expecting default settings works and no negotiation performed.
  59912. */
  59913. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59914. return TEST_FAIL;
  59915. /* confirm no negotiation occurred */
  59916. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59917. WOLFSSL_SUCCESS);
  59918. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59919. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59920. WOLFSSL_SUCCESS);
  59921. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59922. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59923. WOLFSSL_SUCCESS);
  59924. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59925. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59926. WOLFSSL_SUCCESS);
  59927. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59928. (void)typeCnt_c;
  59929. (void)typeCnt_s;
  59930. wolfSSL_free(ssl_c);
  59931. wolfSSL_CTX_free(ctx_c);
  59932. wolfSSL_free(ssl_s);
  59933. wolfSSL_CTX_free(ctx_s);
  59934. ssl_c = ssl_s = NULL;
  59935. ctx_c = ctx_s = NULL;
  59936. /* Both client and server load x509 cert and start handshaking.
  59937. * Check no negotiation occurred.
  59938. */
  59939. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59940. ExpectIntEQ(
  59941. test_rpk_memio_setup(
  59942. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59943. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59944. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59945. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59946. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59947. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59948. , 0);
  59949. /* set client certificate type in client end */
  59950. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59951. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59952. typeCnt_c = 2;
  59953. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59954. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59955. typeCnt_s = 2;
  59956. /* both client and server do not call client/server_cert_type APIs,
  59957. * expecting default settings works and no negotiation performed.
  59958. */
  59959. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59960. return TEST_FAIL;
  59961. /* confirm no negotiation occurred */
  59962. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59963. WOLFSSL_SUCCESS);
  59964. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59965. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59966. WOLFSSL_SUCCESS);
  59967. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59968. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59969. WOLFSSL_SUCCESS);
  59970. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59971. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59972. WOLFSSL_SUCCESS);
  59973. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59974. (void)typeCnt_c;
  59975. (void)typeCnt_s;
  59976. wolfSSL_free(ssl_c);
  59977. wolfSSL_CTX_free(ctx_c);
  59978. wolfSSL_free(ssl_s);
  59979. wolfSSL_CTX_free(ctx_s);
  59980. ssl_c = ssl_s = NULL;
  59981. ctx_c = ctx_s = NULL;
  59982. /* Both client and server load RPK cert and start handshaking.
  59983. * Confirm negotiated cert types match as expected.
  59984. */
  59985. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59986. ExpectIntEQ(
  59987. test_rpk_memio_setup(
  59988. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59989. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59990. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59991. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59992. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59993. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59994. , 0);
  59995. /* set client certificate type in client end */
  59996. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59997. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59998. typeCnt_c = 2;
  59999. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60000. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60001. typeCnt_s = 2;
  60002. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60003. WOLFSSL_SUCCESS);
  60004. /* set server certificate type in client end */
  60005. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60006. WOLFSSL_SUCCESS);
  60007. /* set client certificate type in server end */
  60008. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60009. WOLFSSL_SUCCESS);
  60010. /* set server certificate type in server end */
  60011. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60012. WOLFSSL_SUCCESS);
  60013. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60014. return TEST_FAIL;
  60015. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60016. WOLFSSL_SUCCESS);
  60017. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60018. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60019. WOLFSSL_SUCCESS);
  60020. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60021. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60022. WOLFSSL_SUCCESS);
  60023. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60024. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60025. WOLFSSL_SUCCESS);
  60026. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60027. wolfSSL_free(ssl_c);
  60028. wolfSSL_CTX_free(ctx_c);
  60029. wolfSSL_free(ssl_s);
  60030. wolfSSL_CTX_free(ctx_s);
  60031. ssl_c = ssl_s = NULL;
  60032. ctx_c = ctx_s = NULL;
  60033. /* TLS1.2
  60034. * Both client and server load RPK cert and start handshaking.
  60035. * Confirm negotiated cert types match as expected.
  60036. */
  60037. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60038. ExpectIntEQ(
  60039. test_rpk_memio_setup(
  60040. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60041. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60042. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60043. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60044. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60045. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60046. , 0);
  60047. /* set client certificate type in client end */
  60048. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60049. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60050. typeCnt_c = 2;
  60051. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60052. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60053. typeCnt_s = 2;
  60054. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60055. WOLFSSL_SUCCESS);
  60056. /* set server certificate type in client end */
  60057. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60058. WOLFSSL_SUCCESS);
  60059. /* set client certificate type in server end */
  60060. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60061. WOLFSSL_SUCCESS);
  60062. /* set server certificate type in server end */
  60063. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60064. WOLFSSL_SUCCESS);
  60065. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60066. return TEST_FAIL;
  60067. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60068. WOLFSSL_SUCCESS);
  60069. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60070. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60071. WOLFSSL_SUCCESS);
  60072. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60073. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60074. WOLFSSL_SUCCESS);
  60075. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60076. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60077. WOLFSSL_SUCCESS);
  60078. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60079. wolfSSL_free(ssl_c);
  60080. wolfSSL_CTX_free(ctx_c);
  60081. wolfSSL_free(ssl_s);
  60082. wolfSSL_CTX_free(ctx_s);
  60083. ssl_c = ssl_s = NULL;
  60084. ctx_c = ctx_s = NULL;
  60085. /* Both client and server load x509 cert.
  60086. * Have client call set_client_cert_type with both RPK and x509.
  60087. * This doesn't makes client add client cert type extension to ClientHello,
  60088. * since it does not load RPK cert actually.
  60089. */
  60090. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60091. ExpectIntEQ(
  60092. test_rpk_memio_setup(
  60093. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60094. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60095. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60096. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60097. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60098. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60099. , 0);
  60100. /* set client certificate type in client end
  60101. *
  60102. * client indicates both RPK and x509 certs are available but loaded RPK
  60103. * cert only. It does not have client add client-cert-type extension in CH.
  60104. */
  60105. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60106. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60107. typeCnt_c = 2;
  60108. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60109. WOLFSSL_SUCCESS);
  60110. /* client indicates both RPK and x509 certs are acceptable */
  60111. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60112. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60113. typeCnt_s = 2;
  60114. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60115. WOLFSSL_SUCCESS);
  60116. /* server indicates both RPK and x509 certs are acceptable */
  60117. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60118. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60119. typeCnt_c = 2;
  60120. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60121. WOLFSSL_SUCCESS);
  60122. /* server should indicate only RPK cert is available */
  60123. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60124. certType_s[1] = -1;
  60125. typeCnt_s = 1;
  60126. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60127. WOLFSSL_SUCCESS);
  60128. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60129. return TEST_FAIL;
  60130. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  60131. * be returned as cert type.
  60132. */
  60133. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60134. WOLFSSL_SUCCESS);
  60135. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60136. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60137. WOLFSSL_SUCCESS);
  60138. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60139. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60140. WOLFSSL_SUCCESS);
  60141. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60142. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60143. WOLFSSL_SUCCESS);
  60144. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60145. wolfSSL_free(ssl_c);
  60146. wolfSSL_CTX_free(ctx_c);
  60147. wolfSSL_free(ssl_s);
  60148. wolfSSL_CTX_free(ctx_s);
  60149. ssl_c = ssl_s = NULL;
  60150. ctx_c = ctx_s = NULL;
  60151. /* Have client load RPK cert and have server load x509 cert.
  60152. * Check the negotiation result from both ends.
  60153. */
  60154. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60155. ExpectIntEQ(
  60156. test_rpk_memio_setup(
  60157. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60158. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60159. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60160. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60161. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60162. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60163. , 0);
  60164. /* have client tell to use RPK cert */
  60165. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60166. certType_c[1] = -1;
  60167. typeCnt_c = 1;
  60168. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60169. WOLFSSL_SUCCESS);
  60170. /* have client tell to accept both RPK and x509 cert */
  60171. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60172. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60173. typeCnt_s = 2;
  60174. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60175. WOLFSSL_SUCCESS);
  60176. /* have server accept to both RPK and x509 cert */
  60177. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60178. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60179. typeCnt_c = 2;
  60180. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60181. WOLFSSL_SUCCESS);
  60182. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  60183. * end, expecting the default setting works.
  60184. */
  60185. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60186. return TEST_FAIL;
  60187. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60188. WOLFSSL_SUCCESS);
  60189. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60190. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60191. WOLFSSL_SUCCESS);
  60192. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60193. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60194. WOLFSSL_SUCCESS);
  60195. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60196. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60197. WOLFSSL_SUCCESS);
  60198. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60199. wolfSSL_free(ssl_c);
  60200. wolfSSL_CTX_free(ctx_c);
  60201. wolfSSL_free(ssl_s);
  60202. wolfSSL_CTX_free(ctx_s);
  60203. ssl_c = ssl_s = NULL;
  60204. ctx_c = ctx_s = NULL;
  60205. /* Have both client and server load RPK cert, however, have server
  60206. * indicate its cert type x509.
  60207. * Client is expected to detect the cert type mismatch then to send alert
  60208. * with "unsupported_certificate".
  60209. */
  60210. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60211. ExpectIntEQ(
  60212. test_rpk_memio_setup(
  60213. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60214. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60215. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60216. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  60217. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60218. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60219. , 0);
  60220. /* have client tell to use RPK cert */
  60221. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60222. certType_c[1] = -1;
  60223. typeCnt_c = 1;
  60224. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60225. WOLFSSL_SUCCESS);
  60226. /* have client tell to accept both RPK and x509 cert */
  60227. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60228. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60229. typeCnt_s = 2;
  60230. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60231. WOLFSSL_SUCCESS);
  60232. /* have server accept to both RPK and x509 cert */
  60233. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60234. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60235. typeCnt_c = 2;
  60236. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60237. WOLFSSL_SUCCESS);
  60238. /* have server tell to use x509 cert intentionally. This will bring
  60239. * certificate type mismatch in client side.
  60240. */
  60241. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60242. certType_s[1] = -1;
  60243. typeCnt_s = 1;
  60244. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60245. WOLFSSL_SUCCESS);
  60246. /* expect client detect cert type mismatch then send Alert */
  60247. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60248. if (ret != -1)
  60249. return TEST_FAIL;
  60250. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  60251. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60252. WOLFSSL_SUCCESS);
  60253. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60254. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60255. WOLFSSL_SUCCESS);
  60256. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60257. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60258. WOLFSSL_SUCCESS);
  60259. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60260. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60261. WOLFSSL_SUCCESS);
  60262. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60263. wolfSSL_free(ssl_c);
  60264. wolfSSL_CTX_free(ctx_c);
  60265. wolfSSL_free(ssl_s);
  60266. wolfSSL_CTX_free(ctx_s);
  60267. ssl_c = ssl_s = NULL;
  60268. ctx_c = ctx_s = NULL;
  60269. /* Have client load x509 cert and server load RPK cert,
  60270. * however, have client indicate its cert type RPK.
  60271. * Server is expected to detect the cert type mismatch then to send alert
  60272. * with "unsupported_certificate".
  60273. */
  60274. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60275. ExpectIntEQ(
  60276. test_rpk_memio_setup(
  60277. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60278. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60279. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60280. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60281. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60282. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60283. , 0);
  60284. /* have client tell to use RPK cert intentionally */
  60285. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60286. certType_c[1] = -1;
  60287. typeCnt_c = 1;
  60288. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60289. WOLFSSL_SUCCESS);
  60290. /* have client tell to accept both RPK and x509 cert */
  60291. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60292. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60293. typeCnt_s = 2;
  60294. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60295. WOLFSSL_SUCCESS);
  60296. /* have server accept to both RPK and x509 cert */
  60297. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60298. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60299. typeCnt_c = 2;
  60300. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60301. WOLFSSL_SUCCESS);
  60302. /* have server tell to use x509 cert intentionally. This will bring
  60303. * certificate type mismatch in client side.
  60304. */
  60305. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60306. certType_s[1] = -1;
  60307. typeCnt_s = 1;
  60308. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60309. WOLFSSL_SUCCESS);
  60310. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60311. /* expect server detect cert type mismatch then send Alert */
  60312. ExpectIntNE(ret, 0);
  60313. err = wolfSSL_get_error(ssl_c, ret);
  60314. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  60315. /* client did not load RPK cert actually, so negotiation did not happen */
  60316. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60317. WOLFSSL_SUCCESS);
  60318. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60319. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60320. WOLFSSL_SUCCESS);
  60321. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60322. /* client did not load RPK cert actually, so negotiation did not happen */
  60323. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60324. WOLFSSL_SUCCESS);
  60325. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60326. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60327. WOLFSSL_SUCCESS);
  60328. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60329. wolfSSL_free(ssl_c);
  60330. wolfSSL_CTX_free(ctx_c);
  60331. wolfSSL_free(ssl_s);
  60332. wolfSSL_CTX_free(ctx_s);
  60333. ssl_c = ssl_s = NULL;
  60334. ctx_c = ctx_s = NULL;
  60335. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60336. /* Both client and server load RPK cert and set certificate verify
  60337. * callbacks then start handshaking.
  60338. * Confirm both side can refer the peer's cert.
  60339. */
  60340. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60341. ExpectIntEQ(
  60342. test_rpk_memio_setup(
  60343. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60344. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60345. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60346. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60347. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60348. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60349. , 0);
  60350. /* set client certificate type in client end */
  60351. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60352. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60353. typeCnt_c = 2;
  60354. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60355. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60356. typeCnt_s = 2;
  60357. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60358. WOLFSSL_SUCCESS);
  60359. /* set server certificate type in client end */
  60360. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60361. WOLFSSL_SUCCESS);
  60362. /* set client certificate type in server end */
  60363. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60364. WOLFSSL_SUCCESS);
  60365. /* set server certificate type in server end */
  60366. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60367. WOLFSSL_SUCCESS);
  60368. /* set certificate verify callback to both client and server */
  60369. int isServer = 0;
  60370. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60371. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60372. isServer = 1;
  60373. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60374. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60375. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60376. if (ret != 0)
  60377. return TEST_FAIL;
  60378. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60379. WOLFSSL_SUCCESS);
  60380. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60381. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60382. WOLFSSL_SUCCESS);
  60383. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60384. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60385. WOLFSSL_SUCCESS);
  60386. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60387. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60388. WOLFSSL_SUCCESS);
  60389. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60390. wolfSSL_free(ssl_c);
  60391. wolfSSL_CTX_free(ctx_c);
  60392. wolfSSL_free(ssl_s);
  60393. wolfSSL_CTX_free(ctx_s);
  60394. ssl_c = ssl_s = NULL;
  60395. ctx_c = ctx_s = NULL;
  60396. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60397. #endif /* HAVE_RPK */
  60398. return EXPECT_RESULT();
  60399. }
  60400. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60401. static int test_dtls13_bad_epoch_ch(void)
  60402. {
  60403. EXPECT_DECLS;
  60404. WOLFSSL_CTX *ctx_c = NULL;
  60405. WOLFSSL_CTX *ctx_s = NULL;
  60406. WOLFSSL *ssl_c = NULL;
  60407. WOLFSSL *ssl_s = NULL;
  60408. struct test_memio_ctx test_ctx;
  60409. const int EPOCH_OFF = 3;
  60410. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60411. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60412. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60413. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  60414. * with just one message */
  60415. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60416. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60417. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60418. WOLFSSL_ERROR_WANT_READ);
  60419. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  60420. /* first CH should use epoch 0x0 */
  60421. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  60422. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  60423. /* change epoch to 2 */
  60424. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  60425. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60426. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60427. WOLFSSL_ERROR_WANT_READ);
  60428. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  60429. /* resend the CH */
  60430. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60431. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60432. wolfSSL_free(ssl_c);
  60433. wolfSSL_CTX_free(ctx_c);
  60434. wolfSSL_free(ssl_s);
  60435. wolfSSL_CTX_free(ctx_s);
  60436. return EXPECT_RESULT();
  60437. }
  60438. #else
  60439. static int test_dtls13_bad_epoch_ch(void)
  60440. {
  60441. return TEST_SKIPPED;
  60442. }
  60443. #endif
  60444. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  60445. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  60446. {
  60447. EXPECT_DECLS;
  60448. WOLFSSL_SESSION *sess = NULL;
  60449. /* Setup the session to avoid errors */
  60450. ssl->session->timeout = -1;
  60451. ssl->session->side = WOLFSSL_CLIENT_END;
  60452. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  60453. defined(HAVE_SESSION_TICKET))
  60454. ssl->session->version = ssl->version;
  60455. #endif
  60456. /* Force a short session ID to be sent */
  60457. ssl->session->sessionIDSz = 4;
  60458. #ifndef NO_SESSION_CACHE_REF
  60459. /* Allow the client cache to be used */
  60460. ssl->session->idLen = 4;
  60461. #endif
  60462. ssl->session->isSetup = 1;
  60463. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  60464. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  60465. return EXPECT_RESULT();
  60466. }
  60467. static int test_short_session_id(void)
  60468. {
  60469. EXPECT_DECLS;
  60470. test_ssl_cbf client_cbf;
  60471. test_ssl_cbf server_cbf;
  60472. size_t i;
  60473. struct {
  60474. method_provider client_meth;
  60475. method_provider server_meth;
  60476. const char* tls_version;
  60477. } params[] = {
  60478. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  60479. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  60480. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  60481. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  60482. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  60483. #ifdef WOLFSSL_DTLS13
  60484. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  60485. #endif
  60486. #endif
  60487. #ifndef WOLFSSL_NO_TLS12
  60488. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  60489. #ifdef WOLFSSL_DTLS
  60490. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  60491. #endif
  60492. #endif
  60493. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  60494. !defined(NO_DES3))
  60495. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  60496. #ifdef WOLFSSL_DTLS
  60497. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  60498. #endif
  60499. #endif
  60500. };
  60501. fprintf(stderr, "\n");
  60502. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60503. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60504. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60505. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  60506. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  60507. client_cbf.method = params[i].client_meth;
  60508. server_cbf.method = params[i].server_meth;
  60509. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60510. &server_cbf, NULL), TEST_SUCCESS);
  60511. }
  60512. return EXPECT_RESULT();
  60513. }
  60514. #else
  60515. static int test_short_session_id(void)
  60516. {
  60517. return TEST_SKIPPED;
  60518. }
  60519. #endif
  60520. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  60521. && defined(WOLFSSL_DTLS13)
  60522. static byte* test_find_string(const char *string,
  60523. byte *buf, int buf_size)
  60524. {
  60525. int string_size, i;
  60526. string_size = (int)XSTRLEN(string);
  60527. for (i = 0; i < buf_size - string_size - 1; i++) {
  60528. if (XSTRCMP((char*)&buf[i], string) == 0)
  60529. return &buf[i];
  60530. }
  60531. return NULL;
  60532. }
  60533. static int test_wolfSSL_dtls13_null_cipher(void)
  60534. {
  60535. EXPECT_DECLS;
  60536. WOLFSSL_CTX *ctx_c = NULL;
  60537. WOLFSSL_CTX *ctx_s = NULL;
  60538. WOLFSSL *ssl_c = NULL;
  60539. WOLFSSL *ssl_s = NULL;
  60540. struct test_memio_ctx test_ctx;
  60541. const char *test_str = "test";
  60542. int test_str_size;
  60543. byte buf[255], *ptr = NULL;
  60544. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60545. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  60546. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60547. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60548. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60549. test_str_size = XSTRLEN("test") + 1;
  60550. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60551. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  60552. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  60553. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60554. /* check that the packet was sent cleartext */
  60555. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  60556. test_ctx.s_len));
  60557. if (ptr != NULL) {
  60558. /* modify the message */
  60559. *ptr = 'H';
  60560. /* bad messages should be ignored in DTLS */
  60561. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  60562. ExpectIntEQ(ssl_s->error, WANT_READ);
  60563. }
  60564. wolfSSL_free(ssl_c);
  60565. wolfSSL_free(ssl_s);
  60566. wolfSSL_CTX_free(ctx_c);
  60567. wolfSSL_CTX_free(ctx_s);
  60568. return TEST_SUCCESS;
  60569. }
  60570. #else
  60571. static int test_wolfSSL_dtls13_null_cipher(void)
  60572. {
  60573. return TEST_SKIPPED;
  60574. }
  60575. #endif
  60576. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60577. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60578. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  60579. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  60580. {
  60581. SOCKADDR_S peer;
  60582. XSOCKLENT len;
  60583. int ret;
  60584. XMEMSET((byte*)&peer, 0, sizeof(peer));
  60585. len = sizeof(peer);
  60586. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  60587. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  60588. return -1;
  60589. switch (peer.ss_family) {
  60590. #ifdef WOLFSSL_IPV6
  60591. case WOLFSSL_IP6: {
  60592. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  60593. break;
  60594. }
  60595. #endif /* WOLFSSL_IPV6 */
  60596. case WOLFSSL_IP4:
  60597. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  60598. break;
  60599. default:
  60600. return -1;
  60601. }
  60602. return 0;
  60603. }
  60604. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  60605. {
  60606. char buf[1] = {'t'};
  60607. SOCKADDR_IN_T addr;
  60608. int sock_fd;
  60609. word16 port;
  60610. int err;
  60611. (void)ssl;
  60612. (void)ctx;
  60613. if (ssl == NULL)
  60614. return -1;
  60615. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  60616. if (err != 0)
  60617. return -1;
  60618. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  60619. if (sock_fd == -1)
  60620. return -1;
  60621. build_addr(&addr, wolfSSLIP, port, 1, 0);
  60622. /* send a packet to the server. Being another socket, the kernel will ensure
  60623. * the source port will be different. */
  60624. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  60625. sizeof(addr));
  60626. close(sock_fd);
  60627. if (err == -1)
  60628. return -1;
  60629. return 0;
  60630. }
  60631. /* setup a SSL session but just after the handshake send a packet to the server
  60632. * with a source address different than the one of the connected client. The I/O
  60633. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  60634. * is done in test_dtls_msg_from_other_peer_cb */
  60635. static int test_dtls_msg_from_other_peer(void)
  60636. {
  60637. EXPECT_DECLS;
  60638. callback_functions client_cbs;
  60639. callback_functions server_cbs;
  60640. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  60641. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  60642. client_cbs.method = wolfDTLSv1_2_client_method;
  60643. server_cbs.method = wolfDTLSv1_2_server_method;
  60644. client_cbs.doUdp = 1;
  60645. server_cbs.doUdp = 1;
  60646. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  60647. test_dtls_msg_from_other_peer_cb);
  60648. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  60649. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  60650. return EXPECT_RESULT();
  60651. }
  60652. #else
  60653. static int test_dtls_msg_from_other_peer(void)
  60654. {
  60655. return TEST_SKIPPED;
  60656. }
  60657. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60658. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60659. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  60660. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  60661. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60662. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  60663. static int test_dtls_ipv6_check(void)
  60664. {
  60665. EXPECT_DECLS;
  60666. WOLFSSL_CTX *ctx_c = NULL;
  60667. WOLFSSL_CTX *ctx_s = NULL;
  60668. WOLFSSL *ssl_c = NULL;
  60669. WOLFSSL *ssl_s = NULL;
  60670. SOCKADDR_IN fake_addr6;
  60671. int sockfd = -1;
  60672. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  60673. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  60674. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  60675. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  60676. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60677. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  60678. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60679. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60680. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  60681. /* mimic a sockaddr_in6 struct, this way we can't test without
  60682. * WOLFSSL_IPV6 */
  60683. fake_addr6.sin_family = WOLFSSL_IP6;
  60684. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  60685. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60686. /* can't return error here, as the peer is opaque for wolfssl library at
  60687. * this point */
  60688. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  60689. WOLFSSL_SUCCESS);
  60690. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  60691. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  60692. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60693. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  60694. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  60695. WOLFSSL_SUCCESS);
  60696. /* reuse the socket */
  60697. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60698. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  60699. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60700. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  60701. if (sockfd != -1)
  60702. close(sockfd);
  60703. wolfSSL_free(ssl_c);
  60704. wolfSSL_CTX_free(ctx_c);
  60705. wolfSSL_free(ssl_s);
  60706. wolfSSL_CTX_free(ctx_s);
  60707. return EXPECT_RESULT();
  60708. }
  60709. #else
  60710. static int test_dtls_ipv6_check(void)
  60711. {
  60712. return TEST_SKIPPED;
  60713. }
  60714. #endif
  60715. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60716. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60717. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  60718. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  60719. {
  60720. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60721. }
  60722. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  60723. {
  60724. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  60725. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  60726. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  60727. }
  60728. else {
  60729. char testMsg[] = "Message after SCR";
  60730. char msgBuf[sizeof(testMsg)];
  60731. int ret;
  60732. if (!wolfSSL_is_server(ssl)) {
  60733. AssertIntEQ(WOLFSSL_SUCCESS,
  60734. wolfSSL_set_session(ssl,
  60735. test_wolfSSL_SCR_after_resumption_session));
  60736. }
  60737. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60738. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60739. sizeof(testMsg));
  60740. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60741. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  60742. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60743. AssertIntEQ(ret, sizeof(msgBuf));
  60744. }
  60745. }
  60746. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  60747. {
  60748. AssertIntEQ(WOLFSSL_SUCCESS,
  60749. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  60750. }
  60751. static int test_wolfSSL_SCR_after_resumption(void)
  60752. {
  60753. EXPECT_DECLS;
  60754. callback_functions func_cb_client;
  60755. callback_functions func_cb_server;
  60756. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60757. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60758. func_cb_client.method = wolfTLSv1_2_client_method;
  60759. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60760. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60761. func_cb_server.method = wolfTLSv1_2_server_method;
  60762. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60763. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60764. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60765. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60766. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  60767. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60768. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60769. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60770. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60771. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  60772. return EXPECT_RESULT();
  60773. }
  60774. #else
  60775. static int test_wolfSSL_SCR_after_resumption(void)
  60776. {
  60777. return TEST_SKIPPED;
  60778. }
  60779. #endif
  60780. static int test_wolfSSL_configure_args(void)
  60781. {
  60782. EXPECT_DECLS;
  60783. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  60784. ExpectNotNull(wolfSSL_configure_args());
  60785. #endif
  60786. return EXPECT_RESULT();
  60787. }
  60788. static int test_dtls_no_extensions(void)
  60789. {
  60790. EXPECT_DECLS;
  60791. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60792. !defined(WOLFSSL_NO_TLS12)
  60793. WOLFSSL *ssl_s = NULL;
  60794. WOLFSSL_CTX *ctx_s = NULL;
  60795. struct test_memio_ctx test_ctx;
  60796. const byte chNoExtensions[] = {
  60797. /* Handshake type */
  60798. 0x16,
  60799. /* Version */
  60800. 0xfe, 0xff,
  60801. /* Epoch */
  60802. 0x00, 0x00,
  60803. /* Seq number */
  60804. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60805. /* Length */
  60806. 0x00, 0x40,
  60807. /* CH type */
  60808. 0x01,
  60809. /* Length */
  60810. 0x00, 0x00, 0x34,
  60811. /* Msg Seq */
  60812. 0x00, 0x00,
  60813. /* Frag offset */
  60814. 0x00, 0x00, 0x00,
  60815. /* Frag length */
  60816. 0x00, 0x00, 0x34,
  60817. /* Version */
  60818. 0xfe, 0xff,
  60819. /* Random */
  60820. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  60821. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  60822. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  60823. /* Session Length */
  60824. 0x00,
  60825. /* Cookie Length */
  60826. 0x00,
  60827. /* CS Length */
  60828. 0x00, 0x0c,
  60829. /* CS */
  60830. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  60831. /* Comp Meths Length */
  60832. 0x01,
  60833. /* Comp Meths */
  60834. 0x00
  60835. /* And finally... no extensions */
  60836. };
  60837. int i;
  60838. #ifdef OPENSSL_EXTRA
  60839. int repeats = 2;
  60840. #else
  60841. int repeats = 1;
  60842. #endif
  60843. for (i = 0; i < repeats; i++) {
  60844. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60845. ssl_s = NULL;
  60846. ctx_s = NULL;
  60847. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60848. NULL, wolfDTLS_server_method), 0);
  60849. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  60850. test_ctx.s_len = sizeof(chNoExtensions);
  60851. #ifdef OPENSSL_EXTRA
  60852. if (i > 0) {
  60853. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  60854. WOLFSSL_SUCCESS);
  60855. }
  60856. #endif
  60857. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60858. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60859. /* Expecting a handshake msg. Either HVR or SH. */
  60860. ExpectIntGT(test_ctx.c_len, 0);
  60861. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  60862. wolfSSL_free(ssl_s);
  60863. wolfSSL_CTX_free(ctx_s);
  60864. }
  60865. #endif
  60866. return EXPECT_RESULT();
  60867. }
  60868. static int test_TLSX_CA_NAMES_bad_extension(void)
  60869. {
  60870. EXPECT_DECLS;
  60871. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60872. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  60873. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  60874. defined(HAVE_NULL_CIPHER)
  60875. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  60876. * parse the CA Names extension. Otherwise it will return other non-related
  60877. * errors. If CA Names will be parsed in more configurations, that should
  60878. * be reflected in the macro guard above. */
  60879. WOLFSSL *ssl_c = NULL;
  60880. WOLFSSL_CTX *ctx_c = NULL;
  60881. struct test_memio_ctx test_ctx;
  60882. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  60883. const byte shBadCaNamesExt[] = {
  60884. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60885. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60886. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60887. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60888. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60889. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60890. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  60891. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  60892. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  60893. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60894. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  60895. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  60896. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  60897. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  60898. };
  60899. const byte shBadCaNamesExt2[] = {
  60900. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60901. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60902. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60903. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60904. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60905. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60906. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  60907. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  60908. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  60909. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60910. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  60911. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  60912. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  60913. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  60914. };
  60915. int i = 0;
  60916. for (i = 0; i < 2; i++) {
  60917. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60918. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60919. wolfTLSv1_3_client_method, NULL), 0);
  60920. switch (i) {
  60921. case 0:
  60922. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  60923. sizeof(shBadCaNamesExt));
  60924. test_ctx.c_len = sizeof(shBadCaNamesExt);
  60925. break;
  60926. case 1:
  60927. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  60928. sizeof(shBadCaNamesExt2));
  60929. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  60930. break;
  60931. }
  60932. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60933. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  60934. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  60935. #else
  60936. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  60937. #endif
  60938. wolfSSL_free(ssl_c);
  60939. ssl_c = NULL;
  60940. wolfSSL_CTX_free(ctx_c);
  60941. ctx_c = NULL;
  60942. }
  60943. #endif
  60944. return EXPECT_RESULT();
  60945. }
  60946. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60947. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60948. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  60949. {
  60950. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60951. WOLFSSL_SUCCESS);
  60952. }
  60953. static int test_dtls_1_0_hvr_downgrade(void)
  60954. {
  60955. EXPECT_DECLS;
  60956. callback_functions func_cb_client;
  60957. callback_functions func_cb_server;
  60958. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60959. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60960. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60961. func_cb_client.method = wolfDTLS_client_method;
  60962. func_cb_server.method = wolfDTLSv1_2_server_method;
  60963. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  60964. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60965. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60966. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60967. return EXPECT_RESULT();
  60968. }
  60969. #else
  60970. static int test_dtls_1_0_hvr_downgrade(void)
  60971. {
  60972. EXPECT_DECLS;
  60973. return EXPECT_RESULT();
  60974. }
  60975. #endif
  60976. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  60977. defined(HAVE_SESSION_TICKET)
  60978. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  60979. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  60980. {
  60981. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  60982. AssertNotNull(test_session_ticket_no_id_session);
  60983. }
  60984. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  60985. {
  60986. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  60987. }
  60988. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  60989. {
  60990. test_session_ticket_no_id_session->sessionIDSz = 0;
  60991. AssertIntEQ(WOLFSSL_SUCCESS,
  60992. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  60993. }
  60994. static int test_session_ticket_no_id(void)
  60995. {
  60996. /* We are testing an expired (invalid crypto context in out case since the
  60997. * ctx changes) session ticket being sent with the session ID being 0
  60998. * length. */
  60999. EXPECT_DECLS;
  61000. callback_functions func_cb_client;
  61001. callback_functions func_cb_server;
  61002. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61003. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61004. func_cb_client.method = wolfTLSv1_2_client_method;
  61005. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61006. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  61007. func_cb_server.method = wolfTLSv1_2_server_method;
  61008. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61009. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61010. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61011. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61012. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61013. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61014. func_cb_client.method = wolfTLSv1_2_client_method;
  61015. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61016. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  61017. func_cb_server.method = wolfTLSv1_2_server_method;
  61018. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61019. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61020. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61021. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61022. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  61023. return EXPECT_RESULT();
  61024. }
  61025. #else
  61026. static int test_session_ticket_no_id(void)
  61027. {
  61028. EXPECT_DECLS;
  61029. return EXPECT_RESULT();
  61030. }
  61031. #endif
  61032. static int test_session_ticket_hs_update(void)
  61033. {
  61034. EXPECT_DECLS;
  61035. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61036. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61037. struct test_memio_ctx test_ctx;
  61038. struct test_memio_ctx test_ctx2;
  61039. struct test_memio_ctx test_ctx3;
  61040. WOLFSSL_CTX *ctx_c = NULL;
  61041. WOLFSSL_CTX *ctx_s = NULL;
  61042. WOLFSSL *ssl_c = NULL;
  61043. WOLFSSL *ssl_c2 = NULL;
  61044. WOLFSSL *ssl_c3 = NULL;
  61045. WOLFSSL *ssl_s = NULL;
  61046. WOLFSSL *ssl_s2 = NULL;
  61047. WOLFSSL *ssl_s3 = NULL;
  61048. WOLFSSL_SESSION *sess = NULL;
  61049. byte read_data[1];
  61050. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61051. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  61052. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  61053. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61054. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61055. /* Generate tickets */
  61056. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61057. wolfSSL_SetLoggingPrefix("client");
  61058. /* Read the ticket msg */
  61059. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  61060. WOLFSSL_FATAL_ERROR);
  61061. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61062. WOLFSSL_ERROR_WANT_READ);
  61063. wolfSSL_SetLoggingPrefix(NULL);
  61064. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  61065. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61066. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  61067. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61068. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61069. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  61070. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  61071. wolfSSL_SetLoggingPrefix("client");
  61072. /* Exchange initial flights for the second connection */
  61073. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  61074. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  61075. WOLFSSL_ERROR_WANT_READ);
  61076. wolfSSL_SetLoggingPrefix(NULL);
  61077. wolfSSL_SetLoggingPrefix("server");
  61078. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  61079. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  61080. WOLFSSL_ERROR_WANT_READ);
  61081. wolfSSL_SetLoggingPrefix(NULL);
  61082. /* Complete third connection so that new tickets are exchanged */
  61083. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  61084. /* Read the ticket msg */
  61085. wolfSSL_SetLoggingPrefix("client");
  61086. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  61087. WOLFSSL_FATAL_ERROR);
  61088. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  61089. WOLFSSL_ERROR_WANT_READ);
  61090. wolfSSL_SetLoggingPrefix(NULL);
  61091. /* Complete second connection */
  61092. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  61093. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  61094. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  61095. wolfSSL_free(ssl_c);
  61096. wolfSSL_free(ssl_c2);
  61097. wolfSSL_free(ssl_c3);
  61098. wolfSSL_free(ssl_s);
  61099. wolfSSL_free(ssl_s2);
  61100. wolfSSL_free(ssl_s3);
  61101. wolfSSL_CTX_free(ctx_c);
  61102. wolfSSL_CTX_free(ctx_s);
  61103. wolfSSL_SESSION_free(sess);
  61104. #endif
  61105. return EXPECT_RESULT();
  61106. }
  61107. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61108. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61109. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  61110. {
  61111. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61112. WOLFSSL_SUCCESS);
  61113. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61114. }
  61115. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  61116. {
  61117. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61118. }
  61119. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  61120. {
  61121. char testMsg[] = "Message after SCR";
  61122. char msgBuf[sizeof(testMsg)];
  61123. if (wolfSSL_is_server(ssl)) {
  61124. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61125. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61126. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61127. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61128. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61129. sizeof(testMsg));
  61130. }
  61131. else {
  61132. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61133. sizeof(testMsg));
  61134. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61135. }
  61136. }
  61137. static int test_dtls_downgrade_scr_server(void)
  61138. {
  61139. EXPECT_DECLS;
  61140. callback_functions func_cb_client;
  61141. callback_functions func_cb_server;
  61142. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61143. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61144. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61145. func_cb_client.method = wolfDTLSv1_2_client_method;
  61146. func_cb_server.method = wolfDTLS_server_method;
  61147. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  61148. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  61149. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  61150. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  61151. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61152. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61153. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61154. return EXPECT_RESULT();
  61155. }
  61156. #else
  61157. static int test_dtls_downgrade_scr_server(void)
  61158. {
  61159. EXPECT_DECLS;
  61160. return EXPECT_RESULT();
  61161. }
  61162. #endif
  61163. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61164. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61165. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  61166. {
  61167. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61168. WOLFSSL_SUCCESS);
  61169. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61170. }
  61171. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  61172. {
  61173. char testMsg[] = "Message after SCR";
  61174. char msgBuf[sizeof(testMsg)];
  61175. if (wolfSSL_is_server(ssl)) {
  61176. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61177. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61178. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61179. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61180. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61181. sizeof(testMsg));
  61182. }
  61183. else {
  61184. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61185. sizeof(testMsg));
  61186. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61187. }
  61188. }
  61189. static int test_dtls_downgrade_scr(void)
  61190. {
  61191. EXPECT_DECLS;
  61192. callback_functions func_cb_client;
  61193. callback_functions func_cb_server;
  61194. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61195. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61196. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61197. func_cb_client.method = wolfDTLS_client_method;
  61198. func_cb_server.method = wolfDTLSv1_2_server_method;
  61199. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  61200. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  61201. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  61202. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61203. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61204. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61205. return EXPECT_RESULT();
  61206. }
  61207. #else
  61208. static int test_dtls_downgrade_scr(void)
  61209. {
  61210. EXPECT_DECLS;
  61211. return EXPECT_RESULT();
  61212. }
  61213. #endif
  61214. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61215. && !defined(WOLFSSL_NO_TLS12)
  61216. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  61217. char *data, int sz, void *ctx)
  61218. {
  61219. static int call_counter = 0;
  61220. call_counter++;
  61221. (void)ssl;
  61222. (void)data;
  61223. (void)sz;
  61224. (void)ctx;
  61225. switch (call_counter) {
  61226. case 1:
  61227. case 2:
  61228. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61229. case 3:
  61230. return WOLFSSL_CBIO_ERR_WANT_READ;
  61231. default:
  61232. AssertIntLE(call_counter, 3);
  61233. return -1;
  61234. }
  61235. }
  61236. #endif
  61237. /* Make sure we don't send acks before getting a server hello */
  61238. static int test_dtls_client_hello_timeout_downgrade(void)
  61239. {
  61240. EXPECT_DECLS;
  61241. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61242. && !defined(WOLFSSL_NO_TLS12)
  61243. WOLFSSL_CTX *ctx_c = NULL;
  61244. WOLFSSL_CTX *ctx_s = NULL;
  61245. WOLFSSL *ssl_c = NULL;
  61246. WOLFSSL *ssl_s = NULL;
  61247. struct test_memio_ctx test_ctx;
  61248. DtlsRecordLayerHeader* dtlsRH;
  61249. size_t len;
  61250. byte sequence_number[8];
  61251. int i;
  61252. for (i = 0; i < 2; i++) {
  61253. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61254. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61255. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  61256. if (i == 0) {
  61257. /* First time simulate timeout in IO layer */
  61258. /* CH1 */
  61259. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61260. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61261. /* HVR */
  61262. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61263. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61264. /* CH2 */
  61265. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61266. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61267. /* SH flight */
  61268. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61269. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61270. /* Drop the SH */
  61271. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61272. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61273. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61274. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61275. (sizeof(DtlsRecordLayerHeader) + len));
  61276. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61277. /* Read the remainder of the flight */
  61278. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61279. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61280. wolfSSL_SSLSetIORecv(ssl_c,
  61281. test_dtls_client_hello_timeout_downgrade_read_cb);
  61282. /* CH3 */
  61283. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61284. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61285. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  61286. }
  61287. else {
  61288. /* Second time call wolfSSL_dtls_got_timeout */
  61289. /* CH1 */
  61290. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61291. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61292. /* HVR */
  61293. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61294. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61295. /* CH2 */
  61296. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61297. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61298. /* SH flight */
  61299. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61300. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61301. /* Drop the SH */
  61302. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61303. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61304. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61305. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61306. (sizeof(DtlsRecordLayerHeader) + len));
  61307. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61308. /* Read the remainder of the flight */
  61309. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61310. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61311. /* Quick timeout should be set as we received at least one msg */
  61312. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  61313. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61314. /* Quick timeout should be cleared after a quick timeout */
  61315. /* CH3 */
  61316. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  61317. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61318. }
  61319. /* Parse out to make sure we got exactly one ClientHello message */
  61320. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61321. /* Second ClientHello after HVR */
  61322. sequence_number[7] = 2;
  61323. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  61324. ExpectIntEQ(dtlsRH->type, handshake);
  61325. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61326. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61327. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61328. sizeof(sequence_number)), 0);
  61329. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61330. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61331. /* Connection should be able to continue */
  61332. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61333. wolfSSL_free(ssl_c);
  61334. wolfSSL_free(ssl_s);
  61335. wolfSSL_CTX_free(ctx_c);
  61336. wolfSSL_CTX_free(ctx_s);
  61337. ssl_c = NULL;
  61338. ssl_s = NULL;
  61339. ctx_c = NULL;
  61340. ctx_s = NULL;
  61341. if (!EXPECT_SUCCESS())
  61342. break;
  61343. }
  61344. #endif
  61345. return EXPECT_RESULT();
  61346. }
  61347. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61348. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  61349. int sz, void *ctx)
  61350. {
  61351. static int call_counter = 0;
  61352. call_counter++;
  61353. (void)ssl;
  61354. (void)data;
  61355. (void)sz;
  61356. (void)ctx;
  61357. switch (call_counter) {
  61358. case 1:
  61359. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61360. case 2:
  61361. return WOLFSSL_CBIO_ERR_WANT_READ;
  61362. default:
  61363. AssertIntLE(call_counter, 2);
  61364. return -1;
  61365. }
  61366. }
  61367. #endif
  61368. /* Make sure we don't send acks before getting a server hello */
  61369. static int test_dtls_client_hello_timeout(void)
  61370. {
  61371. EXPECT_DECLS;
  61372. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61373. WOLFSSL *ssl_c = NULL;
  61374. WOLFSSL_CTX *ctx_c = NULL;
  61375. struct test_memio_ctx test_ctx;
  61376. DtlsRecordLayerHeader* dtlsRH;
  61377. size_t idx;
  61378. size_t len;
  61379. byte sequence_number[8];
  61380. int i;
  61381. for (i = 0; i < 2; i++) {
  61382. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61383. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61384. wolfDTLSv1_3_client_method, NULL), 0);
  61385. if (i == 0) {
  61386. /* First time simulate timeout in IO layer */
  61387. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  61388. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61389. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61390. }
  61391. else {
  61392. /* Second time call wolfSSL_dtls_got_timeout */
  61393. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61394. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61395. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61396. }
  61397. /* Parse out to make sure we got exactly two ClientHello messages */
  61398. idx = 0;
  61399. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61400. /* First ClientHello */
  61401. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61402. ExpectIntEQ(dtlsRH->type, handshake);
  61403. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61404. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61405. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61406. sizeof(sequence_number)), 0);
  61407. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61408. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61409. idx += sizeof(DtlsRecordLayerHeader) + len;
  61410. /* Second ClientHello */
  61411. sequence_number[7] = 1;
  61412. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61413. ExpectIntEQ(dtlsRH->type, handshake);
  61414. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61415. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61416. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61417. sizeof(sequence_number)), 0);
  61418. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61419. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61420. wolfSSL_free(ssl_c);
  61421. wolfSSL_CTX_free(ctx_c);
  61422. ssl_c = NULL;
  61423. ctx_c = NULL;
  61424. if (!EXPECT_SUCCESS())
  61425. break;
  61426. }
  61427. #endif
  61428. return EXPECT_RESULT();
  61429. }
  61430. /* DTLS test when dropping the changed cipher spec message */
  61431. static int test_dtls_dropped_ccs(void)
  61432. {
  61433. EXPECT_DECLS;
  61434. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61435. && !defined(WOLFSSL_NO_TLS12)
  61436. WOLFSSL_CTX *ctx_c = NULL;
  61437. WOLFSSL_CTX *ctx_s = NULL;
  61438. WOLFSSL *ssl_c = NULL;
  61439. WOLFSSL *ssl_s = NULL;
  61440. struct test_memio_ctx test_ctx;
  61441. DtlsRecordLayerHeader* dtlsRH;
  61442. size_t len;
  61443. byte data[1];
  61444. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61445. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61446. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  61447. /* CH1 */
  61448. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61449. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61450. /* HVR */
  61451. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61452. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61453. /* CH2 */
  61454. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61455. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61456. /* Server first flight */
  61457. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61458. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61459. /* Client flight */
  61460. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61461. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61462. /* Server ccs + finished */
  61463. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  61464. /* Drop the ccs */
  61465. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61466. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61467. ExpectIntEQ(len, 1);
  61468. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  61469. if (EXPECT_SUCCESS()) {
  61470. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61471. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61472. (sizeof(DtlsRecordLayerHeader) + len));
  61473. }
  61474. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61475. /* Client rtx flight */
  61476. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61477. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61478. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61479. /* Server ccs + finished rtx */
  61480. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  61481. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61482. /* Client processes finished */
  61483. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  61484. wolfSSL_free(ssl_c);
  61485. wolfSSL_free(ssl_s);
  61486. wolfSSL_CTX_free(ctx_c);
  61487. wolfSSL_CTX_free(ctx_s);
  61488. #endif
  61489. return EXPECT_RESULT();
  61490. }
  61491. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61492. && !defined(WOLFSSL_NO_TLS12)
  61493. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  61494. byte seq_num)
  61495. {
  61496. EXPECT_DECLS;
  61497. DtlsRecordLayerHeader* dtlsRH;
  61498. byte sequence_number[8];
  61499. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61500. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  61501. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  61502. ExpectIntEQ(dtlsRH->type, handshake);
  61503. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61504. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61505. sequence_number[7] = seq_num;
  61506. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61507. sizeof(sequence_number)), 0);
  61508. return EXPECT_RESULT();
  61509. }
  61510. #endif
  61511. /*
  61512. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  61513. * and after a HelloRetryRequest. This is testing the server side as it is
  61514. * operating statelessly and should copy the sequence number of the ClientHello.
  61515. */
  61516. static int test_dtls_seq_num_downgrade(void)
  61517. {
  61518. EXPECT_DECLS;
  61519. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61520. && !defined(WOLFSSL_NO_TLS12)
  61521. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61522. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61523. struct test_memio_ctx test_ctx;
  61524. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61525. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61526. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  61527. /* CH1 */
  61528. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61529. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61530. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61531. test_ctx.s_len, 0), TEST_SUCCESS);
  61532. /* HVR */
  61533. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61534. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61535. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61536. test_ctx.c_len, 0), TEST_SUCCESS);
  61537. /* CH2 */
  61538. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61539. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61540. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61541. test_ctx.s_len, 1), TEST_SUCCESS);
  61542. /* Server first flight */
  61543. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61544. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61545. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61546. test_ctx.c_len, 1), TEST_SUCCESS);
  61547. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61548. wolfSSL_free(ssl_c);
  61549. wolfSSL_CTX_free(ctx_c);
  61550. wolfSSL_free(ssl_s);
  61551. wolfSSL_CTX_free(ctx_s);
  61552. #endif
  61553. return EXPECT_RESULT();
  61554. }
  61555. /**
  61556. * Make sure we don't send RSA Signature Hash Algorithms in the
  61557. * CertificateRequest when we don't have any such ciphers set.
  61558. * @return EXPECT_RESULT()
  61559. */
  61560. static int test_certreq_sighash_algos(void)
  61561. {
  61562. EXPECT_DECLS;
  61563. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61564. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  61565. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  61566. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  61567. WOLFSSL_CTX *ctx_c = NULL;
  61568. WOLFSSL_CTX *ctx_s = NULL;
  61569. WOLFSSL *ssl_c = NULL;
  61570. WOLFSSL *ssl_s = NULL;
  61571. struct test_memio_ctx test_ctx;
  61572. int idx = 0;
  61573. int maxIdx = 0;
  61574. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61575. test_ctx.c_ciphers = test_ctx.s_ciphers =
  61576. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  61577. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61578. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61579. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  61580. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  61581. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  61582. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  61583. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61584. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  61585. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61586. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  61587. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61588. WOLFSSL_ERROR_WANT_READ);
  61589. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  61590. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61591. WOLFSSL_ERROR_WANT_READ);
  61592. /* Find the CertificateRequest message */
  61593. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  61594. word16 len;
  61595. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  61596. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  61597. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  61598. ato16(test_ctx.c_buff + idx, &len);
  61599. idx += OPAQUE16_LEN;
  61600. if (test_ctx.c_buff[idx] == certificate_request) {
  61601. idx++;
  61602. /* length */
  61603. idx += OPAQUE24_LEN;
  61604. /* cert types */
  61605. idx += 1 + test_ctx.c_buff[idx];
  61606. /* Sig algos */
  61607. ato16(test_ctx.c_buff + idx, &len);
  61608. idx += OPAQUE16_LEN;
  61609. maxIdx = idx + (int)len;
  61610. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  61611. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  61612. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  61613. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  61614. else
  61615. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  61616. }
  61617. break;
  61618. }
  61619. else {
  61620. idx += (int)len;
  61621. }
  61622. }
  61623. ExpectIntLT(idx, test_ctx.c_len);
  61624. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61625. wolfSSL_free(ssl_c);
  61626. wolfSSL_free(ssl_s);
  61627. wolfSSL_CTX_free(ctx_c);
  61628. wolfSSL_CTX_free(ctx_s);
  61629. #endif
  61630. return EXPECT_RESULT();
  61631. }
  61632. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61633. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  61634. {
  61635. EXPECT_DECLS;
  61636. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61637. myVerifyAction = VERIFY_USE_PREVERFIY;
  61638. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61639. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61640. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61641. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61642. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61643. WOLFSSL_SUCCESS);
  61644. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61645. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61646. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61647. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61648. "./certs/crl/ca-int.pem",
  61649. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61650. return EXPECT_RESULT();
  61651. }
  61652. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  61653. {
  61654. EXPECT_DECLS;
  61655. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61656. myVerifyAction = VERIFY_USE_PREVERFIY;
  61657. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61658. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61659. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61660. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61661. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61662. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61663. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61664. WOLFSSL_SUCCESS);
  61665. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61666. "./certs/crl/ca-int2.pem",
  61667. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61668. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61669. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61670. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61671. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61672. "./certs/crl/ca-int.pem",
  61673. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61674. return EXPECT_RESULT();
  61675. }
  61676. #endif
  61677. static int test_revoked_loaded_int_cert(void)
  61678. {
  61679. EXPECT_DECLS;
  61680. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61681. test_ssl_cbf client_cbf;
  61682. test_ssl_cbf server_cbf;
  61683. struct {
  61684. const char* certPemFile;
  61685. const char* keyPemFile;
  61686. ctx_cb client_ctx_ready;
  61687. } test_params[] = {
  61688. {"./certs/intermediate/ca-int2-cert.pem",
  61689. "./certs/intermediate/ca-int2-key.pem",
  61690. test_revoked_loaded_int_cert_ctx_ready1},
  61691. {"./certs/intermediate/server-chain.pem",
  61692. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61693. {"./certs/intermediate/server-chain-short.pem",
  61694. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61695. };
  61696. size_t i;
  61697. printf("\n");
  61698. for (i = 0; i < XELEM_CNT(test_params); i++) {
  61699. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61700. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61701. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  61702. server_cbf.certPemFile = test_params[i].certPemFile;
  61703. server_cbf.keyPemFile = test_params[i].keyPemFile;
  61704. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  61705. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61706. &server_cbf, NULL), TEST_FAIL);
  61707. #ifndef WOLFSSL_HAPROXY
  61708. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  61709. #else
  61710. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  61711. #endif
  61712. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  61713. if (!EXPECT_SUCCESS())
  61714. break;
  61715. printf("\t%s passed\n", test_params[i].certPemFile);
  61716. }
  61717. #endif
  61718. return EXPECT_RESULT();
  61719. }
  61720. static int test_dtls13_frag_ch_pq(void)
  61721. {
  61722. EXPECT_DECLS;
  61723. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61724. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  61725. WOLFSSL_CTX *ctx_c = NULL;
  61726. WOLFSSL_CTX *ctx_s = NULL;
  61727. WOLFSSL *ssl_c = NULL;
  61728. WOLFSSL *ssl_s = NULL;
  61729. struct test_memio_ctx test_ctx;
  61730. const char *test_str = "test";
  61731. int test_str_size;
  61732. byte buf[255];
  61733. int group = WOLFSSL_KYBER_LEVEL5;
  61734. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61735. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61736. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61737. /* Add in a large post-quantum key share to make the CH long. */
  61738. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  61739. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  61740. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61741. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61742. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  61743. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  61744. test_str_size = XSTRLEN("test") + 1;
  61745. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61746. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61747. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61748. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61749. wolfSSL_free(ssl_c);
  61750. wolfSSL_free(ssl_s);
  61751. wolfSSL_CTX_free(ctx_c);
  61752. wolfSSL_CTX_free(ctx_s);
  61753. #endif
  61754. return EXPECT_RESULT();
  61755. }
  61756. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61757. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61758. static int test_dtls_frag_ch_count_records(byte* b, int len)
  61759. {
  61760. DtlsRecordLayerHeader* dtlsRH;
  61761. int records = 0;
  61762. size_t recordLen;
  61763. while (len > 0) {
  61764. records++;
  61765. dtlsRH = (DtlsRecordLayerHeader*)b;
  61766. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  61767. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  61768. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  61769. }
  61770. return records;
  61771. }
  61772. #endif
  61773. static int test_dtls_frag_ch(void)
  61774. {
  61775. EXPECT_DECLS;
  61776. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61777. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61778. WOLFSSL_CTX *ctx_c = NULL;
  61779. WOLFSSL_CTX *ctx_s = NULL;
  61780. WOLFSSL *ssl_c = NULL;
  61781. WOLFSSL *ssl_s = NULL;
  61782. struct test_memio_ctx test_ctx;
  61783. static unsigned int DUMMY_MTU = 256;
  61784. unsigned char four_frag_CH[] = {
  61785. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61786. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61787. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  61788. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  61789. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  61790. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61791. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61792. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61793. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61794. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  61795. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  61796. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  61797. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  61798. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  61799. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  61800. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  61801. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  61802. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  61803. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  61804. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61805. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  61806. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  61807. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  61808. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  61809. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  61810. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  61811. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  61812. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  61813. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  61814. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  61815. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  61816. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  61817. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  61818. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  61819. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  61820. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  61821. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  61822. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  61823. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  61824. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  61825. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  61826. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  61827. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  61828. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  61829. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  61830. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  61831. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  61832. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  61833. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  61834. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  61835. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  61836. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  61837. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  61838. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  61839. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  61840. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  61841. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  61842. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  61843. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  61844. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  61845. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  61846. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  61847. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  61848. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  61849. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  61850. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  61851. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  61852. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  61853. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  61854. 0xa5, 0xcb, 0x68, 0xf3
  61855. };
  61856. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61857. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61858. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61859. /* Fragment msgs */
  61860. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  61861. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  61862. /* Add in some key shares to make the CH long */
  61863. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  61864. WOLFSSL_SUCCESS);
  61865. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  61866. WOLFSSL_SUCCESS);
  61867. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  61868. WOLFSSL_SUCCESS);
  61869. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  61870. WOLFSSL_SUCCESS);
  61871. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61872. /* Reject fragmented first CH */
  61873. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  61874. sizeof(four_frag_CH)), 4);
  61875. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  61876. test_ctx.s_len = sizeof(four_frag_CH);
  61877. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  61878. int s_len = test_ctx.s_len;
  61879. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61880. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61881. /* Fail if we didn't advance the buffer to avoid infinite loops */
  61882. ExpectIntLT(test_ctx.s_len, s_len);
  61883. }
  61884. /* Expect all fragments read */
  61885. ExpectIntEQ(test_ctx.s_len, 0);
  61886. /* Expect quietly dropping fragmented first CH */
  61887. ExpectIntEQ(test_ctx.c_len, 0);
  61888. /* CH1 */
  61889. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61890. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61891. /* Count records. Expect 1 unfragmented CH */
  61892. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61893. test_ctx.s_len), 1);
  61894. /* HRR */
  61895. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61896. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61897. /* CH2 */
  61898. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61899. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61900. /* Count records. Expect fragmented CH */
  61901. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61902. test_ctx.s_len), 1);
  61903. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61904. wolfSSL_free(ssl_c);
  61905. wolfSSL_free(ssl_s);
  61906. wolfSSL_CTX_free(ctx_c);
  61907. wolfSSL_CTX_free(ctx_s);
  61908. ssl_c = ssl_s = NULL;
  61909. ctx_c = ctx_s = NULL;
  61910. #endif
  61911. return EXPECT_RESULT();
  61912. }
  61913. static int test_dtls_empty_keyshare_with_cookie(void)
  61914. {
  61915. EXPECT_DECLS;
  61916. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61917. WOLFSSL_CTX *ctx_s = NULL;
  61918. WOLFSSL *ssl_s = NULL;
  61919. struct test_memio_ctx test_ctx;
  61920. unsigned char ch_empty_keyshare_with_cookie[] = {
  61921. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  61922. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  61923. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  61924. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  61925. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  61926. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61927. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61928. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61929. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61930. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  61931. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  61932. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  61933. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  61934. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  61935. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  61936. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  61937. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  61938. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  61939. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  61940. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  61941. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  61942. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  61943. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  61944. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  61945. };
  61946. DtlsRecordLayerHeader* dtlsRH;
  61947. byte sequence_number[8];
  61948. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61949. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61950. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  61951. sizeof(ch_empty_keyshare_with_cookie));
  61952. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  61953. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61954. NULL, wolfDTLSv1_3_server_method), 0);
  61955. /* CH1 */
  61956. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61957. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61958. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  61959. ExpectIntEQ(test_ctx.c_len, 15);
  61960. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61961. ExpectIntEQ(dtlsRH->type, alert);
  61962. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61963. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61964. sequence_number[7] = 1;
  61965. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61966. sizeof(sequence_number)), 0);
  61967. ExpectIntEQ(dtlsRH->length[0], 0);
  61968. ExpectIntEQ(dtlsRH->length[1], 2);
  61969. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  61970. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  61971. wolfSSL_free(ssl_s);
  61972. wolfSSL_CTX_free(ctx_s);
  61973. #endif
  61974. return EXPECT_RESULT();
  61975. }
  61976. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61977. defined(HAVE_LIBOQS)
  61978. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  61979. {
  61980. int group = WOLFSSL_KYBER_LEVEL5;
  61981. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  61982. }
  61983. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  61984. {
  61985. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  61986. }
  61987. #endif
  61988. static int test_tls13_pq_groups(void)
  61989. {
  61990. EXPECT_DECLS;
  61991. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61992. defined(HAVE_LIBOQS)
  61993. callback_functions func_cb_client;
  61994. callback_functions func_cb_server;
  61995. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61996. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61997. func_cb_client.method = wolfTLSv1_3_client_method;
  61998. func_cb_server.method = wolfTLSv1_3_server_method;
  61999. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  62000. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  62001. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  62002. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62003. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62004. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62005. #endif
  62006. return EXPECT_RESULT();
  62007. }
  62008. static int test_tls13_early_data(void)
  62009. {
  62010. EXPECT_DECLS;
  62011. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62012. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  62013. int written = 0;
  62014. int read = 0;
  62015. size_t i;
  62016. int splitEarlyData;
  62017. char msg[] = "This is early data";
  62018. char msg2[] = "This is client data";
  62019. char msg3[] = "This is server data";
  62020. char msg4[] = "This is server immediate data";
  62021. char msgBuf[50];
  62022. struct {
  62023. method_provider client_meth;
  62024. method_provider server_meth;
  62025. const char* tls_version;
  62026. int isUdp;
  62027. } params[] = {
  62028. #ifdef WOLFSSL_TLS13
  62029. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62030. "TLS 1.3", 0 },
  62031. #endif
  62032. #ifdef WOLFSSL_DTLS13
  62033. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  62034. "DTLS 1.3", 1 },
  62035. #endif
  62036. };
  62037. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62038. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  62039. struct test_memio_ctx test_ctx;
  62040. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62041. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62042. WOLFSSL_SESSION *sess = NULL;
  62043. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62044. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  62045. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  62046. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  62047. /* Get a ticket so that we can do 0-RTT on the next connection */
  62048. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62049. /* Make sure we read the ticket */
  62050. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  62051. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62052. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62053. wolfSSL_free(ssl_c);
  62054. ssl_c = NULL;
  62055. wolfSSL_free(ssl_s);
  62056. ssl_s = NULL;
  62057. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62058. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62059. params[i].client_meth, params[i].server_meth), 0);
  62060. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  62061. #ifdef WOLFSSL_DTLS13
  62062. if (params[i].isUdp) {
  62063. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  62064. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  62065. #else
  62066. /* Let's test this but we generally don't recommend turning off the
  62067. * cookie exchange */
  62068. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62069. #endif
  62070. }
  62071. #endif
  62072. /* Test 0-RTT data */
  62073. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62074. &written), sizeof(msg));
  62075. ExpectIntEQ(written, sizeof(msg));
  62076. if (splitEarlyData) {
  62077. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62078. &written), sizeof(msg));
  62079. ExpectIntEQ(written, sizeof(msg));
  62080. }
  62081. /* Read first 0-RTT data (if split otherwise entire data) */
  62082. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62083. &read), sizeof(msg));
  62084. ExpectIntEQ(read, sizeof(msg));
  62085. ExpectStrEQ(msg, msgBuf);
  62086. /* Test 0.5-RTT data */
  62087. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  62088. if (splitEarlyData) {
  62089. /* Read second 0-RTT data */
  62090. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62091. &read), sizeof(msg));
  62092. ExpectIntEQ(read, sizeof(msg));
  62093. ExpectStrEQ(msg, msgBuf);
  62094. }
  62095. if (params[i].isUdp) {
  62096. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62097. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  62098. /* Read server 0.5-RTT data */
  62099. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62100. ExpectStrEQ(msg4, msgBuf);
  62101. /* Complete handshake */
  62102. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62103. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62104. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  62105. * a user would loop until it is true but here we control both sides so we
  62106. * just assert the expected value. wolfSSL_read_early_data does not provide
  62107. * handshake status to us with non-blocking IO and we can't use
  62108. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  62109. * parsing logic. */
  62110. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62111. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62112. &read), 0);
  62113. ExpectIntEQ(read, 0);
  62114. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62115. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62116. }
  62117. else {
  62118. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62119. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62120. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62121. &read), 0);
  62122. ExpectIntEQ(read, 0);
  62123. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62124. /* Read server 0.5-RTT data */
  62125. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62126. ExpectStrEQ(msg4, msgBuf);
  62127. }
  62128. /* Test bi-directional write */
  62129. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  62130. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  62131. ExpectStrEQ(msg2, msgBuf);
  62132. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  62133. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  62134. ExpectStrEQ(msg3, msgBuf);
  62135. ExpectTrue(wolfSSL_session_reused(ssl_c));
  62136. ExpectTrue(wolfSSL_session_reused(ssl_s));
  62137. wolfSSL_SESSION_free(sess);
  62138. wolfSSL_free(ssl_c);
  62139. wolfSSL_free(ssl_s);
  62140. wolfSSL_CTX_free(ctx_c);
  62141. wolfSSL_CTX_free(ctx_s);
  62142. }
  62143. }
  62144. #endif
  62145. return EXPECT_RESULT();
  62146. }
  62147. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62148. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  62149. {
  62150. EXPECT_DECLS;
  62151. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62152. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  62153. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  62154. return EXPECT_RESULT();
  62155. }
  62156. #endif
  62157. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62158. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  62159. {
  62160. EXPECT_DECLS;
  62161. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62162. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  62163. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  62164. return EXPECT_RESULT();
  62165. }
  62166. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  62167. {
  62168. EXPECT_DECLS;
  62169. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62170. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  62171. 0), 1);
  62172. return EXPECT_RESULT();
  62173. }
  62174. #endif
  62175. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62176. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62177. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  62178. {
  62179. EXPECT_DECLS;
  62180. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62181. return EXPECT_RESULT();
  62182. }
  62183. #endif
  62184. static int test_self_signed_stapling(void)
  62185. {
  62186. EXPECT_DECLS;
  62187. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62188. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62189. test_ssl_cbf client_cbf;
  62190. test_ssl_cbf server_cbf;
  62191. size_t i;
  62192. struct {
  62193. method_provider client_meth;
  62194. method_provider server_meth;
  62195. ctx_cb client_ctx;
  62196. const char* tls_version;
  62197. } params[] = {
  62198. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  62199. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62200. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  62201. #endif
  62202. #ifndef WOLFSSL_NO_TLS12
  62203. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62204. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62205. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  62206. #endif
  62207. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62208. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62209. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  62210. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62211. test_self_signed_stapling_client_v2_multi_ctx_ready,
  62212. "TLSv1_2 v2 multi" },
  62213. #endif
  62214. #endif
  62215. };
  62216. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62217. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62218. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62219. printf("\nTesting self-signed cert with status request: %s\n",
  62220. params[i].tls_version);
  62221. client_cbf.method = params[i].client_meth;
  62222. client_cbf.ctx_ready = params[i].client_ctx;
  62223. server_cbf.method = params[i].server_meth;
  62224. server_cbf.certPemFile = "certs/ca-cert.pem";
  62225. server_cbf.keyPemFile = "certs/ca-key.pem";
  62226. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  62227. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62228. &server_cbf, NULL), TEST_SUCCESS);
  62229. }
  62230. #endif
  62231. return EXPECT_RESULT();
  62232. }
  62233. static int test_tls_multi_handshakes_one_record(void)
  62234. {
  62235. EXPECT_DECLS;
  62236. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62237. struct test_memio_ctx test_ctx;
  62238. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62239. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62240. RecordLayerHeader* rh = NULL;
  62241. byte *len ;
  62242. int newRecIdx = RECORD_HEADER_SZ;
  62243. int idx = 0;
  62244. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62245. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62246. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  62247. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62248. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62249. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  62250. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62251. /* Combine server handshake msgs into one record */
  62252. while (idx < test_ctx.c_len) {
  62253. word16 recLen;
  62254. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  62255. len = &rh->length[0];
  62256. ato16((const byte*)len, &recLen);
  62257. idx += RECORD_HEADER_SZ;
  62258. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  62259. (size_t)recLen);
  62260. newRecIdx += recLen;
  62261. idx += recLen;
  62262. }
  62263. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  62264. len = &rh->length[0];
  62265. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  62266. test_ctx.c_len = newRecIdx;
  62267. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62268. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62269. wolfSSL_free(ssl_c);
  62270. wolfSSL_free(ssl_s);
  62271. wolfSSL_CTX_free(ctx_c);
  62272. wolfSSL_CTX_free(ctx_s);
  62273. #endif
  62274. return EXPECT_RESULT();
  62275. }
  62276. static int test_write_dup(void)
  62277. {
  62278. EXPECT_DECLS;
  62279. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  62280. size_t i, j;
  62281. char hiWorld[] = "dup message";
  62282. char readData[sizeof(hiWorld) + 5];
  62283. struct {
  62284. method_provider client_meth;
  62285. method_provider server_meth;
  62286. const char* version_name;
  62287. int version;
  62288. } methods[] = {
  62289. #ifndef WOLFSSL_NO_TLS12
  62290. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  62291. #endif
  62292. #ifdef WOLFSSL_TLS13
  62293. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  62294. #endif
  62295. };
  62296. struct {
  62297. const char* cipher;
  62298. int version;
  62299. } ciphers[] = {
  62300. /* For simplicity the macros are copied from internal.h */
  62301. /* TLS 1.2 */
  62302. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  62303. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  62304. #ifndef NO_RSA
  62305. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62306. #endif
  62307. #endif
  62308. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  62309. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62310. #endif
  62311. #endif
  62312. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  62313. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  62314. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62315. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62316. #endif
  62317. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62318. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62319. #endif
  62320. #endif
  62321. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  62322. && !defined(NO_TLS) && !defined(NO_AES)
  62323. #ifdef HAVE_AESGCM
  62324. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62325. #ifndef NO_RSA
  62326. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62327. #endif
  62328. #endif
  62329. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62330. #ifndef NO_RSA
  62331. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62332. #endif
  62333. #endif
  62334. #endif
  62335. #endif
  62336. /* TLS 1.3 */
  62337. #ifdef WOLFSSL_TLS13
  62338. #ifdef HAVE_AESGCM
  62339. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62340. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  62341. #endif
  62342. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62343. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  62344. #endif
  62345. #endif
  62346. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  62347. #ifndef NO_SHA256
  62348. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  62349. #endif
  62350. #endif
  62351. #ifdef HAVE_AESCCM
  62352. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62353. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  62354. #endif
  62355. #endif
  62356. #endif
  62357. };
  62358. for (i = 0; i < XELEM_CNT(methods); i++) {
  62359. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  62360. struct test_memio_ctx test_ctx;
  62361. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62362. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62363. WOLFSSL *ssl_c2 = NULL;
  62364. if (methods[i].version != ciphers[j].version)
  62365. continue;
  62366. if (i == 0 && j == 0)
  62367. printf("\n");
  62368. printf("Testing %s with %s... ", methods[i].version_name,
  62369. ciphers[j].cipher);
  62370. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62371. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  62372. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62373. methods[i].client_meth, methods[i].server_meth), 0);
  62374. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62375. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  62376. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  62377. WRITE_DUP_WRITE_E);
  62378. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  62379. sizeof(hiWorld));
  62380. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  62381. sizeof(hiWorld));
  62382. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  62383. sizeof(hiWorld));
  62384. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  62385. WRITE_DUP_READ_E);
  62386. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  62387. sizeof(hiWorld));
  62388. if (EXPECT_SUCCESS())
  62389. printf("ok\n");
  62390. else
  62391. printf("failed\n");
  62392. wolfSSL_free(ssl_c);
  62393. wolfSSL_free(ssl_c2);
  62394. wolfSSL_free(ssl_s);
  62395. wolfSSL_CTX_free(ctx_c);
  62396. wolfSSL_CTX_free(ctx_s);
  62397. }
  62398. }
  62399. #endif
  62400. return EXPECT_RESULT();
  62401. }
  62402. static int test_read_write_hs(void)
  62403. {
  62404. EXPECT_DECLS;
  62405. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62406. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  62407. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  62408. struct test_memio_ctx test_ctx;
  62409. uint8_t test_buffer[16];
  62410. unsigned int test;
  62411. /* test == 0 : client writes, server reads */
  62412. /* test == 1 : server writes, client reads */
  62413. for (test = 0; test < 2; test++) {
  62414. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62415. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62416. wolfTLSv1_2_client_method,
  62417. wolfTLSv1_2_server_method), 0);
  62418. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  62419. /* CH -> */
  62420. if (test == 0) {
  62421. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62422. } else {
  62423. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62424. sizeof(test_buffer)), -1);
  62425. }
  62426. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62427. /* <- SH + SKE + SHD */
  62428. if (test == 0) {
  62429. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62430. sizeof(test_buffer)), -1);
  62431. } else {
  62432. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62433. }
  62434. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62435. /* -> CKE + CLIENT FINISHED */
  62436. if (test == 0) {
  62437. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62438. } else {
  62439. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62440. sizeof(test_buffer)), -1);
  62441. }
  62442. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62443. /* abide clang static analyzer */
  62444. if (ssl_s != NULL) {
  62445. /* disable group message to separate sending of ChangeCipherspec
  62446. * from Finished */
  62447. ssl_s->options.groupMessages = 0;
  62448. }
  62449. /* allow writing of CS, but not FINISHED */
  62450. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  62451. /* <- CS */
  62452. if (test == 0) {
  62453. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62454. sizeof(test_buffer)), -1);
  62455. } else {
  62456. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62457. }
  62458. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  62459. /* move CS message where the client can read it */
  62460. memmove(test_ctx.c_buff,
  62461. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  62462. test_ctx.c_len = 6;
  62463. /* read CS */
  62464. if (test == 0) {
  62465. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62466. } else {
  62467. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62468. sizeof(test_buffer)), -1);
  62469. }
  62470. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62471. ExpectIntEQ(test_ctx.c_len, 0);
  62472. if (test == 0) {
  62473. /* send SERVER FINISHED */
  62474. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62475. sizeof(test_buffer)), -1);
  62476. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  62477. WOLFSSL_ERROR_WANT_READ);
  62478. } else {
  62479. /* send SERVER FINISHED + App Data */
  62480. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  62481. }
  62482. ExpectIntGT(test_ctx.c_len, 0);
  62483. /* Send and receive the data */
  62484. if (test == 0) {
  62485. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  62486. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62487. sizeof(test_buffer)), 5);
  62488. } else {
  62489. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62490. sizeof(test_buffer)), 5);
  62491. }
  62492. ExpectBufEQ(test_buffer, "hello", 5);
  62493. wolfSSL_free(ssl_c);
  62494. wolfSSL_free(ssl_s);
  62495. wolfSSL_CTX_free(ctx_c);
  62496. wolfSSL_CTX_free(ctx_s);
  62497. ssl_c = ssl_s = NULL;
  62498. ctx_c = ctx_s = NULL;
  62499. }
  62500. #endif
  62501. return EXPECT_RESULT();
  62502. }
  62503. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62504. static const char* test_get_signature_nid_siglag;
  62505. static int test_get_signature_nid_sig;
  62506. static int test_get_signature_nid_hash;
  62507. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  62508. {
  62509. EXPECT_DECLS;
  62510. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  62511. if (!wolfSSL_is_server(ssl)) {
  62512. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  62513. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  62514. }
  62515. return EXPECT_RESULT();
  62516. }
  62517. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62518. {
  62519. EXPECT_DECLS;
  62520. int nid = 0;
  62521. (void)ctx;
  62522. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  62523. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62524. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62525. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62526. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62527. }
  62528. else /* No sigalg info on static ciphersuite */
  62529. return TEST_SUCCESS;
  62530. return EXPECT_RESULT();
  62531. }
  62532. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62533. {
  62534. EXPECT_DECLS;
  62535. int nid = 0;
  62536. (void)ctx;
  62537. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62538. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62539. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62540. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62541. return EXPECT_RESULT();
  62542. }
  62543. #endif
  62544. static int test_get_signature_nid(void)
  62545. {
  62546. EXPECT_DECLS;
  62547. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62548. test_ssl_cbf client_cbf;
  62549. test_ssl_cbf server_cbf;
  62550. size_t i;
  62551. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  62552. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  62553. caCertFile }
  62554. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  62555. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  62556. caEccCertFile }
  62557. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  62558. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  62559. caCertFile }
  62560. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  62561. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  62562. caEccCertFile }
  62563. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  62564. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  62565. caEdCertFile }
  62566. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  62567. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  62568. caEd448CertFile }
  62569. struct {
  62570. const char* siglag;
  62571. int sig_nid;
  62572. int hash_nid;
  62573. int tls_ver;
  62574. const char* server_cert;
  62575. const char* server_key;
  62576. const char* client_ca;
  62577. } params[] = {
  62578. #ifndef NO_RSA
  62579. #ifndef NO_SHA256
  62580. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  62581. #ifdef WC_RSA_PSS
  62582. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62583. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62584. #endif
  62585. #endif
  62586. #ifdef WOLFSSL_SHA512
  62587. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  62588. #ifdef WC_RSA_PSS
  62589. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62590. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62591. #endif
  62592. #endif
  62593. #endif
  62594. #ifdef HAVE_ECC
  62595. #ifndef NO_SHA256
  62596. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62597. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62598. #endif
  62599. #endif
  62600. #ifdef HAVE_ED25519
  62601. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  62602. #endif
  62603. #ifdef HAVE_ED448
  62604. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  62605. #endif
  62606. };
  62607. printf("\n");
  62608. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  62609. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62610. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62611. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  62612. params[i].siglag);
  62613. switch (params[i].tls_ver) {
  62614. #ifndef WOLFSSL_NO_TLS12
  62615. case WOLFSSL_TLSV1_2:
  62616. client_cbf.method = wolfTLSv1_2_client_method;
  62617. server_cbf.method = wolfTLSv1_2_server_method;
  62618. break;
  62619. #endif
  62620. #ifdef WOLFSSL_TLS13
  62621. case WOLFSSL_TLSV1_3:
  62622. client_cbf.method = wolfTLSv1_3_client_method;
  62623. server_cbf.method = wolfTLSv1_3_server_method;
  62624. break;
  62625. #endif
  62626. default:
  62627. printf("skipping\n");
  62628. continue;
  62629. }
  62630. test_get_signature_nid_siglag = params[i].siglag;
  62631. test_get_signature_nid_sig = params[i].sig_nid;
  62632. test_get_signature_nid_hash = params[i].hash_nid;
  62633. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62634. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62635. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  62636. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  62637. server_cbf.certPemFile = params[i].server_cert;
  62638. server_cbf.keyPemFile = params[i].server_key;
  62639. client_cbf.caPemFile = params[i].client_ca;
  62640. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62641. &server_cbf, NULL), TEST_SUCCESS);
  62642. if (EXPECT_SUCCESS())
  62643. printf("passed\n");
  62644. }
  62645. #endif
  62646. return EXPECT_RESULT();
  62647. }
  62648. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62649. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  62650. {
  62651. #ifndef NO_MD5
  62652. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  62653. #elif !defined(NO_SHA)
  62654. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  62655. #elif !defined(NO_SHA256)
  62656. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  62657. #else
  62658. #error "We need a digest to hash the Signer object"
  62659. #endif
  62660. byte hashBuf[WC_MAX_DIGEST_SIZE];
  62661. wc_HashAlg hash;
  62662. size_t i;
  62663. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  62664. for (i = 0; i < CA_TABLE_SIZE; i++) {
  62665. Signer* cur;
  62666. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  62667. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  62668. sizeof(*cur)), 0);
  62669. }
  62670. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  62671. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  62672. return MakeWordFromHash(hashBuf);
  62673. }
  62674. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  62675. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  62676. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  62677. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  62678. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  62679. {
  62680. EXPECT_DECLS;
  62681. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  62682. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  62683. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62684. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  62685. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  62686. return EXPECT_RESULT();
  62687. }
  62688. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  62689. {
  62690. EXPECT_DECLS;
  62691. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  62692. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62693. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62694. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62695. return EXPECT_RESULT();
  62696. }
  62697. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62698. {
  62699. EXPECT_DECLS;
  62700. WOLFSSL_CERT_MANAGER* cm;
  62701. (void)ssl;
  62702. /* WARNING: this approach bypasses the reference counter check in
  62703. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  62704. * cause unexpected behaviour when other active connections try accessing
  62705. * the caTable. */
  62706. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  62707. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  62708. WOLFSSL_SUCCESS);
  62709. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62710. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62711. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  62712. return EXPECT_RESULT();
  62713. }
  62714. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  62715. {
  62716. EXPECT_DECLS;
  62717. WOLFSSL_CTX* ctx;
  62718. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  62719. return EXPECT_RESULT();
  62720. }
  62721. #endif
  62722. static int test_tls_cert_store_unchanged(void)
  62723. {
  62724. EXPECT_DECLS;
  62725. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62726. test_ssl_cbf client_cbf;
  62727. test_ssl_cbf server_cbf;
  62728. int i;
  62729. for (i = 0; i < 2; i++) {
  62730. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62731. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62732. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  62733. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  62734. sizeof(test_tls_cert_store_unchanged_before_hashes));
  62735. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  62736. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  62737. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62738. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62739. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62740. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62741. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62742. switch (i) {
  62743. case 0:
  62744. client_cbf.on_ctx_cleanup =
  62745. test_tls_cert_store_unchanged_ctx_cleanup;
  62746. server_cbf.on_ctx_cleanup =
  62747. test_tls_cert_store_unchanged_ctx_cleanup;
  62748. break;
  62749. case 1:
  62750. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62751. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62752. break;
  62753. default:
  62754. Fail(("Should not enter here"), ("Entered here"));
  62755. }
  62756. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  62757. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  62758. server_cbf.caPemFile = caCertFile;
  62759. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62760. &server_cbf, NULL), TEST_SUCCESS);
  62761. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  62762. test_tls_cert_store_unchanged_after_hashes,
  62763. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62764. }
  62765. #endif
  62766. return EXPECT_RESULT();
  62767. }
  62768. /*----------------------------------------------------------------------------*
  62769. | Main
  62770. *----------------------------------------------------------------------------*/
  62771. typedef int (*TEST_FUNC)(void);
  62772. typedef struct {
  62773. const char *name;
  62774. TEST_FUNC func;
  62775. byte run:1;
  62776. byte fail:1;
  62777. } TEST_CASE;
  62778. #define TEST_DECL(func) { #func, func, 0, 0 }
  62779. int testAll = 1;
  62780. TEST_CASE testCases[] = {
  62781. TEST_DECL(test_fileAccess),
  62782. /*********************************
  62783. * wolfcrypt
  62784. *********************************/
  62785. TEST_DECL(test_ForceZero),
  62786. TEST_DECL(test_wolfCrypt_Init),
  62787. /* Locking with Compat Mutex */
  62788. TEST_DECL(test_wc_SetMutexCb),
  62789. TEST_DECL(test_wc_LockMutex_ex),
  62790. /* Digests */
  62791. TEST_DECL(test_wc_InitMd5),
  62792. TEST_DECL(test_wc_Md5Update),
  62793. TEST_DECL(test_wc_Md5Final),
  62794. TEST_DECL(test_wc_InitSha),
  62795. TEST_DECL(test_wc_ShaUpdate),
  62796. TEST_DECL(test_wc_ShaFinal),
  62797. TEST_DECL(test_wc_InitSha256),
  62798. TEST_DECL(test_wc_Sha256Update),
  62799. TEST_DECL(test_wc_Sha256Final),
  62800. TEST_DECL(test_wc_Sha256FinalRaw),
  62801. TEST_DECL(test_wc_Sha256GetFlags),
  62802. TEST_DECL(test_wc_Sha256Free),
  62803. TEST_DECL(test_wc_Sha256GetHash),
  62804. TEST_DECL(test_wc_Sha256Copy),
  62805. TEST_DECL(test_wc_InitSha224),
  62806. TEST_DECL(test_wc_Sha224Update),
  62807. TEST_DECL(test_wc_Sha224Final),
  62808. TEST_DECL(test_wc_Sha224SetFlags),
  62809. TEST_DECL(test_wc_Sha224GetFlags),
  62810. TEST_DECL(test_wc_Sha224Free),
  62811. TEST_DECL(test_wc_Sha224GetHash),
  62812. TEST_DECL(test_wc_Sha224Copy),
  62813. TEST_DECL(test_wc_InitSha512),
  62814. TEST_DECL(test_wc_Sha512Update),
  62815. TEST_DECL(test_wc_Sha512Final),
  62816. TEST_DECL(test_wc_Sha512GetFlags),
  62817. TEST_DECL(test_wc_Sha512FinalRaw),
  62818. TEST_DECL(test_wc_Sha512Free),
  62819. TEST_DECL(test_wc_Sha512GetHash),
  62820. TEST_DECL(test_wc_Sha512Copy),
  62821. TEST_DECL(test_wc_InitSha512_224),
  62822. TEST_DECL(test_wc_Sha512_224Update),
  62823. TEST_DECL(test_wc_Sha512_224Final),
  62824. TEST_DECL(test_wc_Sha512_224GetFlags),
  62825. TEST_DECL(test_wc_Sha512_224FinalRaw),
  62826. TEST_DECL(test_wc_Sha512_224Free),
  62827. TEST_DECL(test_wc_Sha512_224GetHash),
  62828. TEST_DECL(test_wc_Sha512_224Copy),
  62829. TEST_DECL(test_wc_InitSha512_256),
  62830. TEST_DECL(test_wc_Sha512_256Update),
  62831. TEST_DECL(test_wc_Sha512_256Final),
  62832. TEST_DECL(test_wc_Sha512_256GetFlags),
  62833. TEST_DECL(test_wc_Sha512_256FinalRaw),
  62834. TEST_DECL(test_wc_Sha512_256Free),
  62835. TEST_DECL(test_wc_Sha512_256GetHash),
  62836. TEST_DECL(test_wc_Sha512_256Copy),
  62837. TEST_DECL(test_wc_InitSha384),
  62838. TEST_DECL(test_wc_Sha384Update),
  62839. TEST_DECL(test_wc_Sha384Final),
  62840. TEST_DECL(test_wc_Sha384GetFlags),
  62841. TEST_DECL(test_wc_Sha384FinalRaw),
  62842. TEST_DECL(test_wc_Sha384Free),
  62843. TEST_DECL(test_wc_Sha384GetHash),
  62844. TEST_DECL(test_wc_Sha384Copy),
  62845. TEST_DECL(test_wc_InitBlake2b),
  62846. TEST_DECL(test_wc_InitBlake2b_WithKey),
  62847. TEST_DECL(test_wc_InitBlake2s_WithKey),
  62848. TEST_DECL(test_wc_InitRipeMd),
  62849. TEST_DECL(test_wc_RipeMdUpdate),
  62850. TEST_DECL(test_wc_RipeMdFinal),
  62851. TEST_DECL(test_wc_InitSha3),
  62852. TEST_DECL(testing_wc_Sha3_Update),
  62853. TEST_DECL(test_wc_Sha3_224_Final),
  62854. TEST_DECL(test_wc_Sha3_256_Final),
  62855. TEST_DECL(test_wc_Sha3_384_Final),
  62856. TEST_DECL(test_wc_Sha3_512_Final),
  62857. TEST_DECL(test_wc_Sha3_224_Copy),
  62858. TEST_DECL(test_wc_Sha3_256_Copy),
  62859. TEST_DECL(test_wc_Sha3_384_Copy),
  62860. TEST_DECL(test_wc_Sha3_512_Copy),
  62861. TEST_DECL(test_wc_Sha3_GetFlags),
  62862. TEST_DECL(test_wc_InitShake256),
  62863. TEST_DECL(testing_wc_Shake256_Update),
  62864. TEST_DECL(test_wc_Shake256_Final),
  62865. TEST_DECL(test_wc_Shake256_Copy),
  62866. TEST_DECL(test_wc_Shake256Hash),
  62867. /* SM3 Digest */
  62868. TEST_DECL(test_wc_InitSm3Free),
  62869. TEST_DECL(test_wc_Sm3UpdateFinal),
  62870. TEST_DECL(test_wc_Sm3GetHash),
  62871. TEST_DECL(test_wc_Sm3Copy),
  62872. TEST_DECL(test_wc_Sm3FinalRaw),
  62873. TEST_DECL(test_wc_Sm3GetSetFlags),
  62874. TEST_DECL(test_wc_Sm3Hash),
  62875. TEST_DECL(test_wc_HashInit),
  62876. TEST_DECL(test_wc_HashSetFlags),
  62877. TEST_DECL(test_wc_HashGetFlags),
  62878. /* HMAC */
  62879. TEST_DECL(test_wc_Md5HmacSetKey),
  62880. TEST_DECL(test_wc_Md5HmacUpdate),
  62881. TEST_DECL(test_wc_Md5HmacFinal),
  62882. TEST_DECL(test_wc_ShaHmacSetKey),
  62883. TEST_DECL(test_wc_ShaHmacUpdate),
  62884. TEST_DECL(test_wc_ShaHmacFinal),
  62885. TEST_DECL(test_wc_Sha224HmacSetKey),
  62886. TEST_DECL(test_wc_Sha224HmacUpdate),
  62887. TEST_DECL(test_wc_Sha224HmacFinal),
  62888. TEST_DECL(test_wc_Sha256HmacSetKey),
  62889. TEST_DECL(test_wc_Sha256HmacUpdate),
  62890. TEST_DECL(test_wc_Sha256HmacFinal),
  62891. TEST_DECL(test_wc_Sha384HmacSetKey),
  62892. TEST_DECL(test_wc_Sha384HmacUpdate),
  62893. TEST_DECL(test_wc_Sha384HmacFinal),
  62894. /* CMAC */
  62895. TEST_DECL(test_wc_InitCmac),
  62896. TEST_DECL(test_wc_CmacUpdate),
  62897. TEST_DECL(test_wc_CmacFinal),
  62898. TEST_DECL(test_wc_AesCmacGenerate),
  62899. /* Cipher */
  62900. TEST_DECL(test_wc_AesGcmStream),
  62901. TEST_DECL(test_wc_Des3_SetIV),
  62902. TEST_DECL(test_wc_Des3_SetKey),
  62903. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  62904. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  62905. TEST_DECL(test_wc_Des3_EcbEncrypt),
  62906. TEST_DECL(test_wc_Chacha_SetKey),
  62907. TEST_DECL(test_wc_Chacha_Process),
  62908. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  62909. TEST_DECL(test_wc_Poly1305SetKey),
  62910. TEST_DECL(test_wc_CamelliaSetKey),
  62911. TEST_DECL(test_wc_CamelliaSetIV),
  62912. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  62913. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  62914. TEST_DECL(test_wc_Arc4SetKey),
  62915. TEST_DECL(test_wc_Arc4Process),
  62916. TEST_DECL(test_wc_Rc2SetKey),
  62917. TEST_DECL(test_wc_Rc2SetIV),
  62918. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  62919. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  62920. /* AES cipher and GMAC. */
  62921. TEST_DECL(test_wc_AesSetKey),
  62922. TEST_DECL(test_wc_AesSetIV),
  62923. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  62924. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  62925. TEST_DECL(test_wc_AesGcmSetKey),
  62926. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  62927. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  62928. TEST_DECL(test_wc_GmacSetKey),
  62929. TEST_DECL(test_wc_GmacUpdate),
  62930. TEST_DECL(test_wc_AesCcmSetKey),
  62931. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  62932. #if defined(WOLFSSL_AES_EAX) && \
  62933. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  62934. TEST_DECL(test_wc_AesEaxVectors),
  62935. TEST_DECL(test_wc_AesEaxEncryptAuth),
  62936. TEST_DECL(test_wc_AesEaxDecryptAuth),
  62937. #endif /* WOLFSSL_AES_EAX */
  62938. /* SM4 cipher */
  62939. TEST_DECL(test_wc_Sm4),
  62940. TEST_DECL(test_wc_Sm4Ecb),
  62941. TEST_DECL(test_wc_Sm4Cbc),
  62942. TEST_DECL(test_wc_Sm4Ctr),
  62943. TEST_DECL(test_wc_Sm4Gcm),
  62944. TEST_DECL(test_wc_Sm4Ccm),
  62945. /* RNG tests */
  62946. #ifdef HAVE_HASHDRBG
  62947. #ifdef TEST_RESEED_INTERVAL
  62948. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  62949. #endif
  62950. TEST_DECL(test_wc_RNG_GenerateBlock),
  62951. #endif
  62952. TEST_DECL(test_get_rand_digit),
  62953. TEST_DECL(test_wc_InitRngNonce),
  62954. TEST_DECL(test_wc_InitRngNonce_ex),
  62955. /* MP API tests */
  62956. TEST_DECL(test_get_digit_count),
  62957. TEST_DECL(test_mp_cond_copy),
  62958. TEST_DECL(test_mp_rand),
  62959. TEST_DECL(test_get_digit),
  62960. TEST_DECL(test_wc_export_int),
  62961. /* RSA */
  62962. TEST_DECL(test_wc_InitRsaKey),
  62963. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  62964. TEST_DECL(test_wc_RsaPublicKeyDecode),
  62965. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  62966. TEST_DECL(test_wc_MakeRsaKey),
  62967. TEST_DECL(test_wc_CheckProbablePrime),
  62968. TEST_DECL(test_wc_RsaPSS_Verify),
  62969. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  62970. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  62971. TEST_DECL(test_wc_RsaKeyToDer),
  62972. TEST_DECL(test_wc_RsaKeyToPublicDer),
  62973. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  62974. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  62975. TEST_DECL(test_wc_RsaEncryptSize),
  62976. TEST_DECL(test_wc_RsaSSL_SignVerify),
  62977. TEST_DECL(test_wc_RsaFlattenPublicKey),
  62978. TEST_DECL(test_RsaDecryptBoundsCheck),
  62979. /* DSA */
  62980. TEST_DECL(test_wc_InitDsaKey),
  62981. TEST_DECL(test_wc_DsaSignVerify),
  62982. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  62983. TEST_DECL(test_wc_MakeDsaKey),
  62984. TEST_DECL(test_wc_DsaKeyToDer),
  62985. TEST_DECL(test_wc_DsaKeyToPublicDer),
  62986. TEST_DECL(test_wc_DsaImportParamsRaw),
  62987. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  62988. TEST_DECL(test_wc_DsaExportParamsRaw),
  62989. TEST_DECL(test_wc_DsaExportKeyRaw),
  62990. /* DH */
  62991. TEST_DECL(test_wc_DhPublicKeyDecode),
  62992. /* wolfCrypt ECC tests */
  62993. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  62994. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  62995. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  62996. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  62997. !defined(HAVE_SELFTEST) && \
  62998. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  62999. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  63000. #endif
  63001. TEST_DECL(test_wc_ecc_make_key),
  63002. TEST_DECL(test_wc_ecc_init),
  63003. TEST_DECL(test_wc_ecc_check_key),
  63004. TEST_DECL(test_wc_ecc_get_generator),
  63005. TEST_DECL(test_wc_ecc_size),
  63006. TEST_DECL(test_wc_ecc_params),
  63007. TEST_DECL(test_wc_ecc_signVerify_hash),
  63008. TEST_DECL(test_wc_ecc_shared_secret),
  63009. TEST_DECL(test_wc_ecc_export_x963),
  63010. TEST_DECL(test_wc_ecc_export_x963_ex),
  63011. TEST_DECL(test_wc_ecc_import_x963),
  63012. TEST_DECL(test_wc_ecc_import_private_key),
  63013. TEST_DECL(test_wc_ecc_export_private_only),
  63014. TEST_DECL(test_wc_ecc_rs_to_sig),
  63015. TEST_DECL(test_wc_ecc_import_raw),
  63016. TEST_DECL(test_wc_ecc_import_unsigned),
  63017. TEST_DECL(test_wc_ecc_sig_size),
  63018. TEST_DECL(test_wc_ecc_ctx_new),
  63019. TEST_DECL(test_wc_ecc_ctx_reset),
  63020. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  63021. TEST_DECL(test_wc_ecc_ctx_set_info),
  63022. TEST_DECL(test_wc_ecc_encryptDecrypt),
  63023. TEST_DECL(test_wc_ecc_del_point),
  63024. TEST_DECL(test_wc_ecc_pointFns),
  63025. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  63026. TEST_DECL(test_wc_ecc_verify_hash_ex),
  63027. TEST_DECL(test_wc_ecc_mulmod),
  63028. TEST_DECL(test_wc_ecc_is_valid_idx),
  63029. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  63030. TEST_DECL(test_wc_ecc_sig_size_calc),
  63031. TEST_DECL(test_wc_EccPrivateKeyToDer),
  63032. /* SM2 elliptic curve */
  63033. TEST_DECL(test_wc_ecc_sm2_make_key),
  63034. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  63035. TEST_DECL(test_wc_ecc_sm2_create_digest),
  63036. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  63037. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  63038. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  63039. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  63040. /* Curve25519 */
  63041. TEST_DECL(test_wc_curve25519_init),
  63042. TEST_DECL(test_wc_curve25519_size),
  63043. TEST_DECL(test_wc_curve25519_export_key_raw),
  63044. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  63045. TEST_DECL(test_wc_curve25519_make_key),
  63046. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  63047. TEST_DECL(test_wc_curve25519_make_pub),
  63048. TEST_DECL(test_wc_curve25519_export_public_ex),
  63049. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  63050. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  63051. TEST_DECL(test_wc_curve25519_import_private),
  63052. /* ED25519 */
  63053. TEST_DECL(test_wc_ed25519_make_key),
  63054. TEST_DECL(test_wc_ed25519_init),
  63055. TEST_DECL(test_wc_ed25519_sign_msg),
  63056. TEST_DECL(test_wc_ed25519_import_public),
  63057. TEST_DECL(test_wc_ed25519_import_private_key),
  63058. TEST_DECL(test_wc_ed25519_export),
  63059. TEST_DECL(test_wc_ed25519_size),
  63060. TEST_DECL(test_wc_ed25519_exportKey),
  63061. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  63062. TEST_DECL(test_wc_Ed25519KeyToDer),
  63063. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  63064. /* Curve448 */
  63065. TEST_DECL(test_wc_curve448_make_key),
  63066. TEST_DECL(test_wc_curve448_shared_secret_ex),
  63067. TEST_DECL(test_wc_curve448_export_public_ex),
  63068. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  63069. TEST_DECL(test_wc_curve448_export_key_raw),
  63070. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  63071. TEST_DECL(test_wc_curve448_import_private),
  63072. TEST_DECL(test_wc_curve448_init),
  63073. TEST_DECL(test_wc_curve448_size),
  63074. /* Ed448 */
  63075. TEST_DECL(test_wc_ed448_make_key),
  63076. TEST_DECL(test_wc_ed448_init),
  63077. TEST_DECL(test_wc_ed448_sign_msg),
  63078. TEST_DECL(test_wc_ed448_import_public),
  63079. TEST_DECL(test_wc_ed448_import_private_key),
  63080. TEST_DECL(test_wc_ed448_export),
  63081. TEST_DECL(test_wc_ed448_size),
  63082. TEST_DECL(test_wc_ed448_exportKey),
  63083. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  63084. TEST_DECL(test_wc_Ed448KeyToDer),
  63085. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  63086. /* Signature API */
  63087. TEST_DECL(test_wc_SignatureGetSize_ecc),
  63088. TEST_DECL(test_wc_SignatureGetSize_rsa),
  63089. /* PEM and DER APIs. */
  63090. TEST_DECL(test_wc_PemToDer),
  63091. TEST_DECL(test_wc_AllocDer),
  63092. TEST_DECL(test_wc_CertPemToDer),
  63093. TEST_DECL(test_wc_KeyPemToDer),
  63094. TEST_DECL(test_wc_PubKeyPemToDer),
  63095. TEST_DECL(test_wc_PemPubKeyToDer),
  63096. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  63097. TEST_DECL(test_wc_CheckCertSigPubKey),
  63098. /* wolfCrypt ASN tests */
  63099. TEST_DECL(test_ToTraditional),
  63100. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  63101. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  63102. /* Certificate */
  63103. TEST_DECL(test_wc_SetSubjectRaw),
  63104. TEST_DECL(test_wc_GetSubjectRaw),
  63105. TEST_DECL(test_wc_SetIssuerRaw),
  63106. TEST_DECL(test_wc_SetIssueBuffer),
  63107. TEST_DECL(test_wc_SetSubjectKeyId),
  63108. TEST_DECL(test_wc_SetSubject),
  63109. TEST_DECL(test_CheckCertSignature),
  63110. TEST_DECL(test_wc_ParseCert),
  63111. TEST_DECL(test_wc_ParseCert_Error),
  63112. TEST_DECL(test_MakeCertWithPathLen),
  63113. TEST_DECL(test_MakeCertWithCaFalse),
  63114. TEST_DECL(test_wc_SetKeyUsage),
  63115. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  63116. TEST_DECL(test_wc_SetSubjectBuffer),
  63117. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  63118. /* wolfcrypt PKCS#7 */
  63119. TEST_DECL(test_wc_PKCS7_New),
  63120. TEST_DECL(test_wc_PKCS7_Init),
  63121. TEST_DECL(test_wc_PKCS7_InitWithCert),
  63122. TEST_DECL(test_wc_PKCS7_EncodeData),
  63123. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  63124. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  63125. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  63126. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  63127. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  63128. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  63129. TEST_DECL(test_wc_PKCS7_Degenerate),
  63130. TEST_DECL(test_wc_PKCS7_BER),
  63131. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  63132. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  63133. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  63134. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  63135. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  63136. /* wolfCrypt PKCS#12 */
  63137. TEST_DECL(test_wc_i2d_PKCS12),
  63138. /*
  63139. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  63140. * avoid memory leaks.
  63141. */
  63142. TEST_DECL(test_wolfCrypt_Cleanup),
  63143. TEST_DECL(test_wolfSSL_Init),
  63144. TEST_DECL(test_dual_alg_support),
  63145. /*********************************
  63146. * OpenSSL compatibility API tests
  63147. *********************************/
  63148. /* If at some point a stub get implemented this test should fail indicating
  63149. * a need to implement a new test case
  63150. */
  63151. TEST_DECL(test_stubs_are_stubs),
  63152. /* ASN.1 compatibility API tests */
  63153. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  63154. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  63155. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  63156. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  63157. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  63158. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  63159. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  63160. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  63161. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  63162. TEST_DECL(test_wolfSSL_ASN1_get_object),
  63163. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  63164. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  63165. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  63166. TEST_DECL(test_wolfSSL_ASN1_STRING),
  63167. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  63168. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  63169. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  63170. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  63171. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  63172. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  63173. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  63174. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  63175. TEST_DECL(test_wolfSSL_ASN1_TIME),
  63176. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  63177. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  63178. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  63179. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  63180. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  63181. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  63182. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  63183. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  63184. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  63185. TEST_DECL(test_wolfSSL_lhash),
  63186. TEST_DECL(test_wolfSSL_certs),
  63187. TEST_DECL(test_wolfSSL_private_keys),
  63188. TEST_DECL(test_wolfSSL_PEM_def_callback),
  63189. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  63190. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  63191. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  63192. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  63193. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  63194. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  63195. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  63196. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  63197. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  63198. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  63199. #ifndef NO_BIO
  63200. TEST_DECL(test_wolfSSL_BIO),
  63201. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  63202. TEST_DECL(test_wolfSSL_PEM_read_bio),
  63203. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  63204. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  63205. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  63206. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  63207. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  63208. #endif
  63209. /* EVP API testing */
  63210. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  63211. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  63212. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  63213. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  63214. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  63215. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  63216. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  63217. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  63218. TEST_DECL(test_wolfSSL_EVP_shake128),
  63219. TEST_DECL(test_wolfSSL_EVP_shake256),
  63220. TEST_DECL(test_wolfSSL_EVP_sm3),
  63221. TEST_DECL(test_EVP_blake2),
  63222. #ifdef OPENSSL_ALL
  63223. TEST_DECL(test_wolfSSL_EVP_md4),
  63224. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  63225. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  63226. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  63227. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  63228. #endif
  63229. TEST_DECL(test_EVP_MD_do_all),
  63230. TEST_DECL(test_wolfSSL_EVP_MD_size),
  63231. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  63232. TEST_DECL(test_wolfSSL_EVP_Digest),
  63233. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  63234. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  63235. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  63236. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  63237. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  63238. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  63239. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  63240. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  63241. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  63242. TEST_DECL(test_wolfssl_EVP_chacha20),
  63243. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  63244. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  63245. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  63246. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  63247. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  63248. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  63249. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  63250. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  63251. #ifdef OPENSSL_ALL
  63252. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  63253. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  63254. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  63255. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  63256. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  63257. TEST_DECL(test_wolfSSL_EVP_rc4),
  63258. TEST_DECL(test_wolfSSL_EVP_enc_null),
  63259. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  63260. TEST_DECL(test_wolfSSL_EVP_mdc2),
  63261. TEST_DECL(test_evp_cipher_aes_gcm),
  63262. #endif
  63263. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  63264. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  63265. #ifdef OPENSSL_EXTRA
  63266. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  63267. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  63268. #endif
  63269. #ifdef OPENSSL_ALL
  63270. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  63271. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  63272. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  63273. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  63274. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  63275. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  63276. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  63277. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  63278. #endif
  63279. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  63280. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  63281. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  63282. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  63283. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  63284. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  63285. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  63286. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  63287. #ifndef NO_BIO
  63288. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  63289. #endif
  63290. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  63291. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  63292. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63293. #ifndef NO_BIO
  63294. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  63295. #endif /* !NO_BIO */
  63296. #endif
  63297. #ifdef OPENSSL_ALL
  63298. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  63299. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  63300. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  63301. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  63302. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  63303. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  63304. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  63305. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  63306. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  63307. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  63308. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  63309. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  63310. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  63311. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  63312. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  63313. #endif
  63314. TEST_DECL(test_EVP_PKEY_rsa),
  63315. TEST_DECL(test_EVP_PKEY_ec),
  63316. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  63317. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  63318. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  63319. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  63320. TEST_DECL(test_EVP_PKEY_cmp),
  63321. #ifdef OPENSSL_ALL
  63322. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  63323. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  63324. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  63325. #endif
  63326. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  63327. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  63328. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63329. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  63330. #endif
  63331. #ifndef NO_BIO
  63332. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  63333. TEST_DECL(test_wolfSSL_GetLoggingCb),
  63334. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  63335. TEST_DECL(test_wc_ERR_remove_state),
  63336. TEST_DECL(test_wc_ERR_print_errors_fp),
  63337. #endif
  63338. TEST_DECL(test_wolfSSL_configure_args),
  63339. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  63340. TEST_DECL(test_wolfSSL_set1_curves_list),
  63341. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  63342. TEST_DECL(test_wolfSSL_OtherName),
  63343. TEST_DECL(test_wolfSSL_FPKI),
  63344. TEST_DECL(test_wolfSSL_URI),
  63345. TEST_DECL(test_wolfSSL_TBS),
  63346. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  63347. TEST_DECL(test_X509_STORE_untrusted),
  63348. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  63349. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  63350. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  63351. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  63352. TEST_DECL(test_wolfSSL_X509_Name_canon),
  63353. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  63354. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  63355. TEST_DECL(test_wolfSSL_X509_NID),
  63356. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  63357. TEST_DECL(test_wolfSSL_get0_param),
  63358. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  63359. TEST_DECL(test_wolfSSL_set1_host),
  63360. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  63361. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  63362. TEST_DECL(test_wolfSSL_X509_STORE),
  63363. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  63364. TEST_DECL(test_X509_STORE_get0_objects),
  63365. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  63366. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  63367. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  63368. TEST_DECL(test_wolfSSL_X509_cmp_time),
  63369. TEST_DECL(test_wolfSSL_X509_time_adj),
  63370. /* X509 tests */
  63371. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  63372. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  63373. TEST_DECL(test_wolfSSL_X509_check_host),
  63374. TEST_DECL(test_wolfSSL_X509_check_email),
  63375. TEST_DECL(test_wolfSSL_X509_check_private_key),
  63376. TEST_DECL(test_wolfSSL_X509),
  63377. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  63378. TEST_DECL(test_wolfSSL_X509_sign),
  63379. TEST_DECL(test_wolfSSL_X509_sign2),
  63380. TEST_DECL(test_wolfSSL_X509_verify),
  63381. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  63382. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  63383. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  63384. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  63385. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  63386. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  63387. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  63388. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  63389. TEST_DECL(test_wolfSSL_X509_NAME),
  63390. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  63391. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  63392. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  63393. TEST_DECL(test_wolfSSL_X509_set_name),
  63394. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  63395. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  63396. TEST_DECL(test_wolfSSL_X509_set_version),
  63397. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  63398. TEST_DECL(test_wolfSSL_X509_CRL),
  63399. TEST_DECL(test_wolfSSL_i2d_X509),
  63400. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  63401. TEST_DECL(test_wolfSSL_PEM_read_X509),
  63402. TEST_DECL(test_wolfSSL_X509_check_ca),
  63403. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  63404. TEST_DECL(test_wolfSSL_make_cert),
  63405. #ifndef NO_BIO
  63406. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  63407. TEST_DECL(test_wolfSSL_X509_INFO),
  63408. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  63409. #endif
  63410. #ifdef OPENSSL_ALL
  63411. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  63412. #endif
  63413. TEST_DECL(test_wolfSSL_X509_CA_num),
  63414. TEST_DECL(test_wolfSSL_X509_get_version),
  63415. #ifndef NO_BIO
  63416. TEST_DECL(test_wolfSSL_X509_print),
  63417. TEST_DECL(test_wolfSSL_X509_CRL_print),
  63418. #endif
  63419. TEST_DECL(test_X509_get_signature_nid),
  63420. /* X509 extension testing. */
  63421. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  63422. TEST_DECL(test_wolfSSL_X509_get_ext),
  63423. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  63424. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  63425. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  63426. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  63427. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  63428. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  63429. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  63430. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  63431. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  63432. TEST_DECL(test_wolfSSL_X509V3_EXT),
  63433. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  63434. TEST_DECL(test_wolfSSL_X509_cmp),
  63435. TEST_DECL(test_GENERAL_NAME_set0_othername),
  63436. TEST_DECL(test_othername_and_SID_ext),
  63437. TEST_DECL(test_wolfSSL_dup_CA_list),
  63438. /* OpenSSL sk_X509 API test */
  63439. TEST_DECL(test_sk_X509),
  63440. /* OpenSSL sk_X509_CRL API test */
  63441. TEST_DECL(test_sk_X509_CRL),
  63442. /* OpenSSL X509 REQ API test */
  63443. TEST_DECL(test_X509_REQ),
  63444. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  63445. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  63446. TEST_DECL(test_X509_LOOKUP_add_dir),
  63447. /* RAND compatibility API */
  63448. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  63449. TEST_DECL(test_wolfSSL_RAND_bytes),
  63450. TEST_DECL(test_wolfSSL_RAND),
  63451. /* BN compatibility API */
  63452. TEST_DECL(test_wolfSSL_BN_CTX),
  63453. TEST_DECL(test_wolfSSL_BN),
  63454. TEST_DECL(test_wolfSSL_BN_init),
  63455. TEST_DECL(test_wolfSSL_BN_enc_dec),
  63456. TEST_DECL(test_wolfSSL_BN_word),
  63457. TEST_DECL(test_wolfSSL_BN_bits),
  63458. TEST_DECL(test_wolfSSL_BN_shift),
  63459. TEST_DECL(test_wolfSSL_BN_math),
  63460. TEST_DECL(test_wolfSSL_BN_math_mod),
  63461. TEST_DECL(test_wolfSSL_BN_math_other),
  63462. TEST_DECL(test_wolfSSL_BN_rand),
  63463. TEST_DECL(test_wolfSSL_BN_prime),
  63464. /* OpenSSL PKCS5 API test */
  63465. TEST_DECL(test_wolfSSL_PKCS5),
  63466. /* OpenSSL PKCS8 API test */
  63467. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  63468. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  63469. /* OpenSSL PKCS7 API test */
  63470. TEST_DECL(test_wolfssl_PKCS7),
  63471. TEST_DECL(test_wolfSSL_PKCS7_certs),
  63472. TEST_DECL(test_wolfSSL_PKCS7_sign),
  63473. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  63474. #ifndef NO_BIO
  63475. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  63476. #ifdef HAVE_SMIME
  63477. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  63478. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  63479. #endif /* HAVE_SMIME */
  63480. #endif /* !NO_BIO */
  63481. /* OpenSSL PKCS12 API test */
  63482. TEST_DECL(test_wolfSSL_PKCS12),
  63483. /* Can't memory test as callbacks use Assert. */
  63484. TEST_DECL(test_error_queue_per_thread),
  63485. TEST_DECL(test_wolfSSL_ERR_put_error),
  63486. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  63487. #ifndef NO_BIO
  63488. TEST_DECL(test_wolfSSL_ERR_print_errors),
  63489. #endif
  63490. TEST_DECL(test_OBJ_NAME_do_all),
  63491. TEST_DECL(test_wolfSSL_OBJ),
  63492. TEST_DECL(test_wolfSSL_OBJ_cmp),
  63493. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  63494. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  63495. #ifdef OPENSSL_ALL
  63496. TEST_DECL(test_wolfSSL_OBJ_ln),
  63497. TEST_DECL(test_wolfSSL_OBJ_sn),
  63498. #endif
  63499. #ifndef NO_BIO
  63500. TEST_DECL(test_wolfSSL_BIO_gets),
  63501. TEST_DECL(test_wolfSSL_BIO_puts),
  63502. TEST_DECL(test_wolfSSL_BIO_dump),
  63503. /* Can't memory test as server hangs. */
  63504. TEST_DECL(test_wolfSSL_BIO_should_retry),
  63505. TEST_DECL(test_wolfSSL_BIO_write),
  63506. TEST_DECL(test_wolfSSL_BIO_printf),
  63507. TEST_DECL(test_wolfSSL_BIO_f_md),
  63508. TEST_DECL(test_wolfSSL_BIO_up_ref),
  63509. TEST_DECL(test_wolfSSL_BIO_reset),
  63510. TEST_DECL(test_wolfSSL_BIO_get_len),
  63511. #endif
  63512. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63513. TEST_DECL(test_wolfSSL_check_domain),
  63514. #endif
  63515. TEST_DECL(test_wolfSSL_cert_cb),
  63516. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  63517. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  63518. TEST_DECL(test_wolfSSL_sigalg_info),
  63519. /* Can't memory test as tcp_connect aborts. */
  63520. TEST_DECL(test_wolfSSL_SESSION),
  63521. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  63522. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  63523. TEST_DECL(test_wolfSSL_ticket_keys),
  63524. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  63525. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  63526. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  63527. TEST_DECL(test_wolfSSL_verify_mode),
  63528. TEST_DECL(test_wolfSSL_verify_depth),
  63529. TEST_DECL(test_wolfSSL_verify_result),
  63530. TEST_DECL(test_wolfSSL_msg_callback),
  63531. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  63532. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  63533. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  63534. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  63535. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  63536. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  63537. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  63538. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  63539. TEST_DECL(test_wolfSSL_PEM_read),
  63540. TEST_DECL(test_wolfSSL_OpenSSL_version),
  63541. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  63542. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  63543. TEST_DECL(test_CONF_modules_xxx),
  63544. #ifdef OPENSSL_ALL
  63545. TEST_DECL(test_wolfSSL_TXT_DB),
  63546. TEST_DECL(test_wolfSSL_NCONF),
  63547. #endif
  63548. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  63549. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  63550. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  63551. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  63552. TEST_DECL(test_CRYPTO_THREADID_xxx),
  63553. TEST_DECL(test_ENGINE_cleanup),
  63554. /* test the no op functions for compatibility */
  63555. TEST_DECL(test_no_op_functions),
  63556. /* OpenSSL error API tests */
  63557. TEST_DECL(test_ERR_load_crypto_strings),
  63558. #ifdef OPENSSL_ALL
  63559. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  63560. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  63561. TEST_DECL(test_wolfSSL_CTX_ctrl),
  63562. #endif /* OPENSSL_ALL */
  63563. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63564. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  63565. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  63566. /*********************************
  63567. * Crypto API tests
  63568. *********************************/
  63569. TEST_DECL(test_wolfSSL_MD4),
  63570. TEST_DECL(test_wolfSSL_MD5),
  63571. TEST_DECL(test_wolfSSL_MD5_Transform),
  63572. TEST_DECL(test_wolfSSL_SHA),
  63573. TEST_DECL(test_wolfSSL_SHA_Transform),
  63574. TEST_DECL(test_wolfSSL_SHA224),
  63575. TEST_DECL(test_wolfSSL_SHA256),
  63576. TEST_DECL(test_wolfSSL_SHA256_Transform),
  63577. TEST_DECL(test_wolfSSL_SHA512_Transform),
  63578. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  63579. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  63580. TEST_DECL(test_wolfSSL_HMAC_CTX),
  63581. TEST_DECL(test_wolfSSL_HMAC),
  63582. TEST_DECL(test_wolfSSL_CMAC),
  63583. TEST_DECL(test_wolfSSL_DES),
  63584. TEST_DECL(test_wolfSSL_DES_ncbc),
  63585. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  63586. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  63587. TEST_DECL(test_wolfSSL_AES_encrypt),
  63588. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  63589. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  63590. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  63591. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  63592. TEST_DECL(test_wolfSSL_RC4),
  63593. TEST_DECL(test_wolfSSL_RSA),
  63594. TEST_DECL(test_wolfSSL_RSA_DER),
  63595. TEST_DECL(test_wolfSSL_RSA_print),
  63596. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  63597. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  63598. TEST_DECL(test_wolfSSL_RSA_get0_key),
  63599. TEST_DECL(test_wolfSSL_RSA_meth),
  63600. TEST_DECL(test_wolfSSL_RSA_verify),
  63601. TEST_DECL(test_wolfSSL_RSA_sign),
  63602. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  63603. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  63604. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  63605. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  63606. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  63607. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  63608. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  63609. TEST_DECL(test_wolfSSL_RSA_ex_data),
  63610. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  63611. TEST_DECL(test_wolfSSL_RSA_To_Der),
  63612. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  63613. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  63614. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  63615. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  63616. TEST_DECL(test_wolfSSL_DH),
  63617. TEST_DECL(test_wolfSSL_DH_dup),
  63618. TEST_DECL(test_wolfSSL_DH_check),
  63619. TEST_DECL(test_wolfSSL_DH_prime),
  63620. TEST_DECL(test_wolfSSL_DH_1536_prime),
  63621. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  63622. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  63623. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  63624. TEST_DECL(test_wolfSSL_d2i_DHparams),
  63625. TEST_DECL(test_wolfSSL_DH_LoadDer),
  63626. TEST_DECL(test_wolfSSL_i2d_DHparams),
  63627. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  63628. TEST_DECL(test_wolfSSL_EC_GROUP),
  63629. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  63630. TEST_DECL(test_wolfSSL_EC_POINT),
  63631. TEST_DECL(test_wolfSSL_SPAKE),
  63632. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  63633. TEST_DECL(test_EC_i2d),
  63634. TEST_DECL(test_wolfSSL_EC_curve),
  63635. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  63636. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  63637. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  63638. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  63639. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  63640. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  63641. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  63642. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  63643. TEST_DECL(test_ECDSA_size_sign),
  63644. TEST_DECL(test_ECDH_compute_key),
  63645. #endif
  63646. #ifdef OPENSSL_EXTRA
  63647. TEST_DECL(test_EC25519),
  63648. TEST_DECL(test_ED25519),
  63649. TEST_DECL(test_EC448),
  63650. TEST_DECL(test_ED448),
  63651. #endif
  63652. TEST_DECL(test_DSA_do_sign_verify),
  63653. #ifdef OPENSSL_ALL
  63654. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  63655. TEST_DECL(test_wolfSSL_DSA_SIG),
  63656. #endif
  63657. TEST_DECL(test_openssl_generate_key_and_cert),
  63658. TEST_DECL(test_wolfSSL_FIPS_mode),
  63659. TEST_DECL(test_openssl_FIPS_drbg),
  63660. /*********************************
  63661. * CertManager API tests
  63662. *********************************/
  63663. TEST_DECL(test_wolfSSL_CertManagerAPI),
  63664. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  63665. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  63666. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  63667. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  63668. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  63669. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  63670. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  63671. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  63672. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  63673. TEST_DECL(test_wolfSSL_CertManagerCRL),
  63674. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  63675. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  63676. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  63677. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63678. !defined(WOLFSSL_NO_CLIENT_AUTH))
  63679. TEST_DECL(test_various_pathlen_chains),
  63680. #endif
  63681. /*********************************
  63682. * SSL/TLS API tests
  63683. *********************************/
  63684. TEST_DECL(test_wolfSSL_Method_Allocators),
  63685. #ifndef NO_WOLFSSL_SERVER
  63686. TEST_DECL(test_wolfSSL_CTX_new),
  63687. #endif
  63688. TEST_DECL(test_server_wolfSSL_new),
  63689. TEST_DECL(test_client_wolfSSL_new),
  63690. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  63691. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  63692. TEST_DECL(test_for_double_Free),
  63693. #endif
  63694. TEST_DECL(test_wolfSSL_set_options),
  63695. #ifdef WOLFSSL_TLS13
  63696. /* TLS v1.3 API tests */
  63697. TEST_DECL(test_tls13_apis),
  63698. TEST_DECL(test_tls13_cipher_suites),
  63699. #endif
  63700. TEST_DECL(test_wolfSSL_tmp_dh),
  63701. TEST_DECL(test_wolfSSL_ctrl),
  63702. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  63703. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  63704. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  63705. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  63706. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  63707. #endif
  63708. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  63709. TEST_DECL(test_wolfSSL_security_level),
  63710. TEST_DECL(test_wolfSSL_SSL_in_init),
  63711. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  63712. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  63713. TEST_DECL(test_CONF_CTX_FILE),
  63714. TEST_DECL(test_CONF_CTX_CMDLINE),
  63715. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63716. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  63717. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  63718. /* Bad certificate signature tests */
  63719. TEST_DECL(test_EccSigFailure_cm),
  63720. TEST_DECL(test_RsaSigFailure_cm),
  63721. #endif /* NO_CERTS */
  63722. /* PKCS8 testing */
  63723. TEST_DECL(test_wolfSSL_no_password_cb),
  63724. TEST_DECL(test_wolfSSL_PKCS8),
  63725. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  63726. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  63727. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63728. TEST_DECL(test_wolfSSL_get_finished),
  63729. /* Uses Assert in handshake callback. */
  63730. TEST_DECL(test_wolfSSL_CTX_add_session),
  63731. /* Large number of memory allocations. */
  63732. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  63733. /* Large number of memory allocations. */
  63734. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  63735. /* Large number of memory allocations. */
  63736. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  63737. /* Large number of memory allocations. */
  63738. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  63739. /* Large number of memory allocations. */
  63740. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  63741. /* Large number of memory allocations. */
  63742. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  63743. #endif
  63744. TEST_DECL(test_SSL_CIPHER_get_xxx),
  63745. TEST_DECL(test_wolfSSL_ERR_strings),
  63746. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  63747. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  63748. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  63749. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  63750. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  63751. /* Large number of memory allocations. */
  63752. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  63753. TEST_DECL(test_wolfSSL_CertRsaPss),
  63754. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  63755. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  63756. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  63757. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  63758. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  63759. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  63760. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  63761. TEST_DECL(test_multiple_crls_same_issuer),
  63762. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  63763. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  63764. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  63765. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  63766. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  63767. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  63768. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  63769. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  63770. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  63771. TEST_DECL(test_SetTmpEC_DHE_Sz),
  63772. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  63773. #ifdef WOLFSSL_DTLS
  63774. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  63775. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  63776. #endif
  63777. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  63778. /* Uses Assert in handshake callback. */
  63779. TEST_DECL(test_wolfSSL_dtls_plaintext),
  63780. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63781. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63782. TEST_DECL(test_wolfSSL_read_write),
  63783. /* Can't memory test as server hangs if client fails before second connect.
  63784. */
  63785. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  63786. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  63787. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  63788. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  63789. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  63790. /* Can't memory test as server hangs. */
  63791. TEST_DECL(test_wolfSSL_dtls_export),
  63792. /* Uses Assert in handshake callback. */
  63793. TEST_DECL(test_wolfSSL_tls_export),
  63794. #endif
  63795. TEST_DECL(test_wolfSSL_dtls_export_peers),
  63796. TEST_DECL(test_wolfSSL_SetMinVersion),
  63797. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  63798. /* wolfSSL handshake APIs. */
  63799. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  63800. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  63801. TEST_DECL(test_wolfSSL_BUF),
  63802. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  63803. /* Can't memory test as server hangs. */
  63804. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  63805. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  63806. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  63807. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  63808. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  63809. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  63810. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  63811. /* Can't memory test as server hangs. */
  63812. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  63813. TEST_DECL(test_wolfSSL_Tls13_postauth),
  63814. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  63815. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  63816. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  63817. TEST_DECL(test_wolfSSL_THREADID_hash),
  63818. /* TLS extensions tests */
  63819. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63820. #ifdef HAVE_SNI
  63821. TEST_DECL(test_wolfSSL_UseSNI_params),
  63822. /* Uses Assert in handshake callback. */
  63823. TEST_DECL(test_wolfSSL_UseSNI_connection),
  63824. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  63825. #endif /* HAVE_SNI */
  63826. #endif
  63827. TEST_DECL(test_wolfSSL_UseTrustedCA),
  63828. TEST_DECL(test_wolfSSL_UseMaxFragment),
  63829. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  63830. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  63831. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  63832. /* Uses Assert in handshake callback. */
  63833. TEST_DECL(test_wolfSSL_UseALPN_connection),
  63834. TEST_DECL(test_wolfSSL_UseALPN_params),
  63835. #endif
  63836. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  63837. /* Uses Assert in handshake callback. */
  63838. TEST_DECL(test_wolfSSL_set_alpn_protos),
  63839. #endif
  63840. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  63841. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  63842. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  63843. TEST_DECL(test_tls_ext_duplicate),
  63844. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  63845. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63846. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  63847. /* Uses Assert in handshake callback. */
  63848. TEST_DECL(test_wolfSSL_Tls13_ECH),
  63849. #endif
  63850. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  63851. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  63852. /* OCSP Stapling */
  63853. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  63854. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  63855. TEST_DECL(test_self_signed_stapling),
  63856. /* Multicast */
  63857. TEST_DECL(test_wolfSSL_mcast),
  63858. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  63859. TEST_DECL(test_wolfSSL_msgCb),
  63860. TEST_DECL(test_wolfSSL_either_side),
  63861. TEST_DECL(test_wolfSSL_DTLS_either_side),
  63862. /* Uses Assert in handshake callback. */
  63863. TEST_DECL(test_wolfSSL_dtls_fragments),
  63864. /* Uses Assert in handshake callback. */
  63865. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  63866. /* Uses Assert in handshake callback. */
  63867. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  63868. /* Uses Assert in handshake callback. */
  63869. TEST_DECL(test_wolfSSL_dtls_bad_record),
  63870. /* Uses Assert in handshake callback. */
  63871. TEST_DECL(test_wolfSSL_dtls_stateless),
  63872. TEST_DECL(test_generate_cookie),
  63873. #ifndef NO_BIO
  63874. /* Can't memory test as server hangs. */
  63875. TEST_DECL(test_wolfSSL_BIO_connect),
  63876. /* Can't memory test as server Asserts in thread. */
  63877. TEST_DECL(test_wolfSSL_BIO_accept),
  63878. TEST_DECL(test_wolfSSL_BIO_tls),
  63879. #endif
  63880. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  63881. TEST_DECL(test_DhCallbacks),
  63882. #endif
  63883. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63884. TEST_DECL(test_export_keying_material),
  63885. #endif
  63886. /* Can't memory test as client/server Asserts in thread. */
  63887. TEST_DECL(test_ticket_and_psk_mixing),
  63888. /* Can't memory test as client/server Asserts in thread. */
  63889. TEST_DECL(test_prioritize_psk),
  63890. /* Can't memory test as client/server hangs. */
  63891. TEST_DECL(test_wc_CryptoCb),
  63892. /* Can't memory test as client/server hangs. */
  63893. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  63894. #if !defined(NO_FILESYSTEM) && \
  63895. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63896. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63897. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  63898. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  63899. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  63900. #ifdef HAVE_MAX_FRAGMENT
  63901. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  63902. #endif /* HAVE_MAX_FRAGMENT */
  63903. #ifndef NO_RSA
  63904. TEST_DECL(test_wolfSSL_dtls_stateless2),
  63905. #if !defined(NO_OLD_TLS)
  63906. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  63907. #endif /* !defined(NO_OLD_TLS) */
  63908. #endif /* ! NO_RSA */
  63909. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63910. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  63911. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  63912. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  63913. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  63914. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  63915. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  63916. && defined(WOLFSSL_TLS13) && \
  63917. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  63918. TEST_DECL(test_ticket_nonce_malloc),
  63919. #endif
  63920. TEST_DECL(test_ticket_ret_create),
  63921. TEST_DECL(test_extra_alerts_wrong_cs),
  63922. TEST_DECL(test_extra_alerts_skip_hs),
  63923. TEST_DECL(test_extra_alerts_bad_psk),
  63924. TEST_DECL(test_tls13_bad_psk_binder),
  63925. /* Can't memory test as client/server Asserts. */
  63926. TEST_DECL(test_harden_no_secure_renegotiation),
  63927. TEST_DECL(test_override_alt_cert_chain),
  63928. TEST_DECL(test_rpk_set_xxx_cert_type),
  63929. TEST_DECL(test_tls13_rpk_handshake),
  63930. TEST_DECL(test_dtls13_bad_epoch_ch),
  63931. TEST_DECL(test_short_session_id),
  63932. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  63933. /* Can't memory test as client/server hangs. */
  63934. TEST_DECL(test_dtls_msg_from_other_peer),
  63935. TEST_DECL(test_dtls_ipv6_check),
  63936. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  63937. TEST_DECL(test_dtls_no_extensions),
  63938. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  63939. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  63940. TEST_DECL(test_session_ticket_no_id),
  63941. TEST_DECL(test_session_ticket_hs_update),
  63942. TEST_DECL(test_dtls_downgrade_scr_server),
  63943. TEST_DECL(test_dtls_downgrade_scr),
  63944. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  63945. TEST_DECL(test_dtls_client_hello_timeout),
  63946. TEST_DECL(test_dtls_dropped_ccs),
  63947. TEST_DECL(test_dtls_seq_num_downgrade),
  63948. TEST_DECL(test_certreq_sighash_algos),
  63949. TEST_DECL(test_revoked_loaded_int_cert),
  63950. TEST_DECL(test_dtls_frag_ch),
  63951. TEST_DECL(test_dtls13_frag_ch_pq),
  63952. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  63953. TEST_DECL(test_tls13_pq_groups),
  63954. TEST_DECL(test_tls13_early_data),
  63955. TEST_DECL(test_tls_multi_handshakes_one_record),
  63956. TEST_DECL(test_write_dup),
  63957. TEST_DECL(test_read_write_hs),
  63958. TEST_DECL(test_get_signature_nid),
  63959. TEST_DECL(test_tls_cert_store_unchanged),
  63960. /* This test needs to stay at the end to clean up any caches allocated. */
  63961. TEST_DECL(test_wolfSSL_Cleanup)
  63962. };
  63963. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  63964. static void TestSetup(void)
  63965. {
  63966. /* Stub, for now. Add common test setup code here. */
  63967. }
  63968. static void TestCleanup(void)
  63969. {
  63970. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  63971. /* Clear any errors added to the error queue during the test run. */
  63972. wolfSSL_ERR_clear_error();
  63973. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  63974. }
  63975. /* Print out all API test cases with numeric identifier.
  63976. */
  63977. void ApiTest_PrintTestCases(void)
  63978. {
  63979. int i;
  63980. printf("All Test Cases:\n");
  63981. for (i = 0; i < TEST_CASE_CNT; i++) {
  63982. printf("%3d: %s\n", i + 1, testCases[i].name);
  63983. }
  63984. }
  63985. /* Add test case with index to the list to run.
  63986. *
  63987. * @param [in] idx Index of test case to run starting at 1.
  63988. * @return 0 on success.
  63989. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  63990. */
  63991. int ApiTest_RunIdx(int idx)
  63992. {
  63993. if (idx < 1 || idx > TEST_CASE_CNT) {
  63994. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  63995. return BAD_FUNC_ARG;
  63996. }
  63997. testAll = 0;
  63998. testCases[idx-1].run = 1;
  63999. return 0;
  64000. }
  64001. /* Add test case with name to the list to run.
  64002. *
  64003. * @param [in] name Name of test case to run.
  64004. * @return 0 on success.
  64005. * @return BAD_FUNC_ARG when name is not a known test case name.
  64006. */
  64007. int ApiTest_RunName(char* name)
  64008. {
  64009. int i;
  64010. for (i = 0; i < TEST_CASE_CNT; i++) {
  64011. if (XSTRCMP(testCases[i].name, name) == 0) {
  64012. testAll = 0;
  64013. testCases[i].run = 1;
  64014. return 0;
  64015. }
  64016. }
  64017. printf("Test case name not found: %s\n", name);
  64018. printf("Use --list to see all test case names.\n");
  64019. return BAD_FUNC_ARG;
  64020. }
  64021. /* Converts the result code to a string.
  64022. *
  64023. * @param [in] res Test result code.
  64024. * @return String describing test result.
  64025. */
  64026. static const char* apitest_res_string(int res)
  64027. {
  64028. const char* str = "invalid result";
  64029. switch (res) {
  64030. case TEST_SUCCESS:
  64031. str = "passed";
  64032. break;
  64033. case TEST_FAIL:
  64034. str = "failed";
  64035. break;
  64036. case TEST_SKIPPED:
  64037. str = "skipped";
  64038. break;
  64039. }
  64040. return str;
  64041. }
  64042. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64043. static double gettime_secs(void)
  64044. #if defined(_MSC_VER) && defined(_WIN32)
  64045. {
  64046. /* there's no gettimeofday for Windows, so we'll use system time */
  64047. #define EPOCH_DIFF 11644473600LL
  64048. FILETIME currentFileTime;
  64049. GetSystemTimePreciseAsFileTime(&currentFileTime);
  64050. ULARGE_INTEGER uli = { 0, 0 };
  64051. uli.LowPart = currentFileTime.dwLowDateTime;
  64052. uli.HighPart = currentFileTime.dwHighDateTime;
  64053. /* Convert to seconds since Unix epoch */
  64054. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  64055. }
  64056. #else
  64057. {
  64058. struct timeval tv;
  64059. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  64060. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  64061. }
  64062. #endif
  64063. #endif
  64064. int ApiTest(void)
  64065. {
  64066. int i;
  64067. int ret;
  64068. int res = 0;
  64069. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64070. double timeDiff;
  64071. #endif
  64072. printf(" Begin API Tests\n");
  64073. fflush(stdout);
  64074. /* we must perform init and cleanup if not all tests are running */
  64075. if (!testAll) {
  64076. #ifdef WOLFCRYPT_ONLY
  64077. if (wolfCrypt_Init() != 0) {
  64078. printf("wolfCrypt Initialization failed\n");
  64079. res = 1;
  64080. }
  64081. #else
  64082. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  64083. printf("wolfSSL Initialization failed\n");
  64084. res = 1;
  64085. }
  64086. #endif
  64087. }
  64088. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64089. if (res == 0) {
  64090. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  64091. printf("failed to create tmp dir\n");
  64092. res = 1;
  64093. }
  64094. else {
  64095. tmpDirNameSet = 1;
  64096. }
  64097. }
  64098. #endif
  64099. if (res == 0) {
  64100. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64101. EXPECT_DECLS;
  64102. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64103. currentTestName = testCases[i].name;
  64104. #endif
  64105. /* When not testing all cases then skip if not marked for running.
  64106. */
  64107. if (!testAll && !testCases[i].run) {
  64108. continue;
  64109. }
  64110. TestSetup();
  64111. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  64112. fflush(stdout);
  64113. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64114. timeDiff = gettime_secs();
  64115. #endif
  64116. ret = testCases[i].func();
  64117. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64118. timeDiff = gettime_secs() - timeDiff;
  64119. #endif
  64120. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64121. if (ret != TEST_SKIPPED) {
  64122. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  64123. }
  64124. else
  64125. #endif
  64126. {
  64127. printf(" %s\n", apitest_res_string(ret));
  64128. }
  64129. fflush(stdout);
  64130. /* if return code is < 0 and not skipped then assert error */
  64131. Expect((ret > 0 || ret == TEST_SKIPPED),
  64132. ("Test failed\n"),
  64133. ("ret %d", ret));
  64134. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  64135. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  64136. TestCleanup();
  64137. }
  64138. }
  64139. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  64140. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  64141. wc_ecc_fp_free(); /* free per thread cache */
  64142. #endif
  64143. if (!testAll) {
  64144. #ifdef WOLFCRYPT_ONLY
  64145. wolfCrypt_Cleanup();
  64146. #else
  64147. wolfSSL_Cleanup();
  64148. #endif
  64149. }
  64150. (void)testDevId;
  64151. if (res != 0) {
  64152. printf("\nFAILURES:\n");
  64153. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64154. if (testCases[i].fail) {
  64155. printf(" %3d: %s\n", i + 1, testCases[i].name);
  64156. }
  64157. }
  64158. printf("\n");
  64159. fflush(stdout);
  64160. }
  64161. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64162. if (tmpDirNameSet) {
  64163. printf("\nBinary dumps of the memio streams can be found in the\n"
  64164. "%s directory. This can be imported into\n"
  64165. "Wireshark by transforming the file with\n"
  64166. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  64167. "And then loading test_output.dump.hex into Wireshark using\n"
  64168. "the \"Import from Hex Dump...\" option and selecting the\n"
  64169. "TCP encapsulation option.\n", tmpDirName);
  64170. }
  64171. #endif
  64172. printf(" End API Tests\n");
  64173. fflush(stdout);
  64174. return res;
  64175. }